annotate widgets/ImageDialog.cpp @ 1163:ab169938832a 3.0-integration

Use peak cache at one zoom level lower (since it exists at that level already -- this is a >= vs > question) + make naming more uniform
author Chris Cannam
date Tue, 08 Nov 2016 14:55:53 +0000
parents 1a0dfcbffaf1
children c8a6fd3f9dff
rev   line source
Chris@303 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@303 2
Chris@303 3 /*
Chris@303 4 Sonic Visualiser
Chris@303 5 An audio file viewer and annotation editor.
Chris@303 6 Centre for Digital Music, Queen Mary, University of London.
Chris@303 7 This file copyright 2007 QMUL.
Chris@303 8
Chris@303 9 This program is free software; you can redistribute it and/or
Chris@303 10 modify it under the terms of the GNU General Public License as
Chris@303 11 published by the Free Software Foundation; either version 2 of the
Chris@303 12 License, or (at your option) any later version. See the file
Chris@303 13 COPYING included with this distribution for more information.
Chris@303 14 */
Chris@303 15
Chris@303 16 #include "ImageDialog.h"
Chris@303 17
Chris@303 18 #include <QLineEdit>
Chris@303 19 #include <QGridLayout>
Chris@303 20 #include <QLabel>
Chris@303 21 #include <QDialogButtonBox>
Chris@303 22 #include <QPushButton>
Chris@303 23 #include <QGroupBox>
Chris@304 24 #include <QDesktopWidget>
Chris@304 25 #include <QApplication>
Chris@305 26 #include <QUrl>
Chris@305 27 #include <QMessageBox>
Chris@303 28
Chris@378 29 #include "ProgressDialog.h"
Chris@378 30
Chris@318 31 #include "data/fileio/FileSource.h"
Chris@529 32 #include "InteractiveFileFinder.h"
Chris@303 33
Chris@305 34 #include <iostream>
Chris@305 35
Chris@303 36 ImageDialog::ImageDialog(QString title,
Chris@303 37 QString image,
Chris@303 38 QString label,
Chris@303 39 QWidget *parent) :
Chris@303 40 QDialog(parent),
Chris@305 41 m_imagePreview(0),
Chris@305 42 m_remoteFile(0)
Chris@303 43 {
Chris@303 44 setWindowTitle(title);
Chris@303 45
Chris@303 46 QGridLayout *grid = new QGridLayout;
Chris@303 47 setLayout(grid);
Chris@303 48
Chris@303 49 QGroupBox *databox = new QGroupBox(tr("Image"));
Chris@303 50
Chris@303 51 QGridLayout *subgrid = new QGridLayout;
Chris@303 52 databox->setLayout(subgrid);
Chris@303 53
Chris@303 54 int row = 0;
Chris@303 55
Chris@303 56 subgrid->addWidget(new QLabel(tr("Label:")), row, 0);
Chris@303 57
Chris@303 58 m_labelEdit = new QLineEdit;
Chris@303 59 subgrid->addWidget(m_labelEdit, row, 1, 1, 2);
Chris@303 60
Chris@303 61 ++row;
Chris@303 62
Chris@305 63 subgrid->addWidget(new QLabel(tr("File or URL:")), row, 0);
Chris@303 64
Chris@303 65 m_imageEdit = new QLineEdit;
Chris@303 66 subgrid->addWidget(m_imageEdit, row, 1, 1, 1);
Chris@305 67
Chris@304 68 connect(m_imageEdit, SIGNAL(textEdited(const QString &)),
Chris@304 69 this, SLOT(imageEditEdited(const QString &)));
Chris@305 70 connect(m_imageEdit, SIGNAL(editingFinished()),
Chris@305 71 this, SLOT(imageEditEdited()));
Chris@303 72
Chris@303 73 QPushButton *browse = new QPushButton(tr("Browse..."));
Chris@303 74 connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked()));
Chris@303 75 subgrid->addWidget(browse, row, 2, 1, 1);
Chris@303 76
Chris@303 77 ++row;
Chris@303 78
Chris@303 79 QGroupBox *previewbox = new QGroupBox(tr("Preview"));
Chris@303 80
Chris@303 81 subgrid = new QGridLayout;
Chris@303 82 previewbox->setLayout(subgrid);
Chris@303 83
Chris@303 84 m_imagePreview = new QLabel;
Chris@303 85 m_imagePreview->setAlignment(Qt::AlignCenter);
Chris@303 86 subgrid->addWidget(m_imagePreview, 0, 0);
Chris@303 87
Chris@303 88 m_imagePreview->setMinimumSize(QSize(100, 100));
Chris@303 89
Chris@304 90 QDesktopWidget *desktop = QApplication::desktop();
Chris@304 91 m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3,
Chris@304 92 (desktop->height() * 2) / 3));
Chris@304 93
Chris@303 94 grid->addWidget(databox, 0, 0);
Chris@303 95 grid->addWidget(previewbox, 1, 0);
Chris@303 96
Chris@303 97 grid->setRowStretch(1, 10);
Chris@303 98
Chris@303 99 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@303 100 QDialogButtonBox::Cancel);
Chris@303 101 grid->addWidget(bb, 2, 0, 1, 1);
Chris@303 102 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@303 103 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@303 104
Chris@303 105 m_okButton = bb->button(QDialogButtonBox::Ok);
Chris@303 106 m_okButton->setEnabled(false);
Chris@303 107
Chris@303 108 if (image != "") setImage(image);
Chris@303 109 if (label != "") setLabel(label);
Chris@303 110 }
Chris@303 111
Chris@303 112 ImageDialog::~ImageDialog()
Chris@303 113 {
Chris@305 114 delete m_remoteFile;
Chris@303 115 }
Chris@303 116
Chris@303 117 QString
Chris@303 118 ImageDialog::getImage()
Chris@303 119 {
Chris@303 120 return m_loadedImageFile;
Chris@303 121 }
Chris@303 122
Chris@303 123 QPixmap
Chris@303 124 ImageDialog::getPixmap()
Chris@303 125 {
Chris@303 126 return m_loadedImage;
Chris@303 127 }
Chris@303 128
Chris@303 129 QString
Chris@303 130 ImageDialog::getLabel()
Chris@303 131 {
Chris@303 132 return m_labelEdit->text();
Chris@303 133 }
Chris@303 134
Chris@303 135 void
Chris@303 136 ImageDialog::setImage(QString image)
Chris@303 137 {
Chris@303 138 m_imageEdit->setText(image);
Chris@303 139 updatePreview();
Chris@303 140 }
Chris@303 141
Chris@303 142 void
Chris@303 143 ImageDialog::setLabel(QString label)
Chris@303 144 {
Chris@303 145 m_labelEdit->setText(label);
Chris@303 146 }
Chris@303 147
Chris@303 148 void
Chris@303 149 ImageDialog::resizeEvent(QResizeEvent *)
Chris@303 150 {
Chris@303 151 updatePreview();
Chris@303 152 }
Chris@303 153
Chris@303 154 void
Chris@305 155 ImageDialog::imageEditEdited(const QString &s)
Chris@305 156 {
Chris@305 157 if (s.startsWith("http:") || s.startsWith("ftp:")) {
Chris@305 158 return;
Chris@305 159 }
Chris@305 160 updatePreview();
Chris@305 161 }
Chris@305 162
Chris@305 163 void
Chris@305 164 ImageDialog::imageEditEdited()
Chris@304 165 {
Chris@304 166 updatePreview();
Chris@304 167 }
Chris@304 168
Chris@304 169 void
Chris@303 170 ImageDialog::updatePreview()
Chris@303 171 {
Chris@303 172 if (!m_imagePreview) return;
Chris@303 173
Chris@303 174 QString img = m_imageEdit->text();
Chris@303 175
Chris@304 176 m_okButton->setEnabled(img != "");
Chris@304 177
Chris@303 178 if (img != m_loadedImageFile) {
Chris@305 179
Chris@305 180 QString fileName = img;
Chris@305 181 delete m_remoteFile;
Chris@305 182 m_remoteFile = 0;
Chris@305 183
Chris@318 184 if (FileSource::isRemote(fileName)) {
Chris@305 185 QUrl url(fileName);
Chris@318 186 if (!FileSource::canHandleScheme(url)) {
Chris@305 187 QMessageBox::critical(this, tr("Unsupported scheme in URL"),
Chris@305 188 tr("The URL scheme \"%1\" is not supported")
Chris@305 189 .arg(url.scheme()));
Chris@305 190 } else {
Chris@378 191
Chris@378 192 ProgressDialog dialog(tr("Opening image URL..."), true, 2000);
Chris@378 193 m_remoteFile = new FileSource(url, &dialog);
Chris@317 194 m_remoteFile->waitForData();
Chris@305 195 if (!m_remoteFile->isOK()) {
Chris@305 196 QMessageBox::critical(this, tr("File download failed"),
Chris@305 197 tr("Failed to download URL \"%1\": %2")
Chris@305 198 .arg(url.toString()).arg(m_remoteFile->getErrorString()));
Chris@305 199 delete m_remoteFile;
Chris@305 200 m_remoteFile = 0;
Chris@305 201 } else {
Chris@305 202 fileName = m_remoteFile->getLocalFilename();
Chris@305 203 }
Chris@305 204 }
Chris@305 205 }
Chris@305 206
Chris@682 207 // cerr << "image filename: \"" << fileName << "\"" << endl;
Chris@305 208
Chris@305 209 m_loadedImage = QPixmap(fileName);
Chris@303 210 m_loadedImageFile = img;
Chris@303 211 }
Chris@303 212
Chris@303 213 QSize sz(m_imagePreview->size());
Chris@303 214 int m = m_imagePreview->margin() * 2;
Chris@303 215 sz -= QSize(m, m);
Chris@303 216
Chris@303 217 if (m_loadedImage.isNull()) {
Chris@303 218 m_imagePreview->setPixmap(QPixmap());
Chris@303 219 } else {
Chris@303 220 m_imagePreview->setPixmap(m_loadedImage.scaled
Chris@303 221 (sz,
Chris@303 222 Qt::KeepAspectRatio,
Chris@303 223 Qt::SmoothTransformation));
Chris@303 224 }
Chris@303 225 }
Chris@303 226
Chris@303 227 void
Chris@303 228 ImageDialog::browseClicked()
Chris@303 229 {
Chris@303 230 QString file =
Chris@529 231 InteractiveFileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile);
Chris@303 232
Chris@303 233 if (file != "") {
Chris@303 234 setImage(file);
Chris@303 235 emit imageChanged(file);
Chris@303 236 }
Chris@303 237 }
Chris@303 238
Chris@303 239
Chris@303 240