annotate widgets/ImageDialog.cpp @ 371:098c568dc76b

* Fix #1894369 1.2pre5 crashes on copying data
author Chris Cannam
date Fri, 15 Feb 2008 15:15:29 +0000
parents 832d246b327b
children 22b72f0f6a4e
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@318 29 #include "data/fileio/FileSource.h"
Chris@303 30 #include "data/fileio/FileFinder.h"
Chris@303 31
Chris@305 32 #include <iostream>
Chris@305 33
Chris@303 34 ImageDialog::ImageDialog(QString title,
Chris@303 35 QString image,
Chris@303 36 QString label,
Chris@303 37 QWidget *parent) :
Chris@303 38 QDialog(parent),
Chris@305 39 m_imagePreview(0),
Chris@305 40 m_remoteFile(0)
Chris@303 41 {
Chris@303 42 setWindowTitle(title);
Chris@303 43
Chris@303 44 QGridLayout *grid = new QGridLayout;
Chris@303 45 setLayout(grid);
Chris@303 46
Chris@303 47 QGroupBox *databox = new QGroupBox(tr("Image"));
Chris@303 48
Chris@303 49 QGridLayout *subgrid = new QGridLayout;
Chris@303 50 databox->setLayout(subgrid);
Chris@303 51
Chris@303 52 int row = 0;
Chris@303 53
Chris@303 54 subgrid->addWidget(new QLabel(tr("Label:")), row, 0);
Chris@303 55
Chris@303 56 m_labelEdit = new QLineEdit;
Chris@303 57 subgrid->addWidget(m_labelEdit, row, 1, 1, 2);
Chris@303 58
Chris@303 59 ++row;
Chris@303 60
Chris@305 61 subgrid->addWidget(new QLabel(tr("File or URL:")), row, 0);
Chris@303 62
Chris@303 63 m_imageEdit = new QLineEdit;
Chris@303 64 subgrid->addWidget(m_imageEdit, row, 1, 1, 1);
Chris@305 65
Chris@304 66 connect(m_imageEdit, SIGNAL(textEdited(const QString &)),
Chris@304 67 this, SLOT(imageEditEdited(const QString &)));
Chris@305 68 connect(m_imageEdit, SIGNAL(editingFinished()),
Chris@305 69 this, SLOT(imageEditEdited()));
Chris@303 70
Chris@303 71 QPushButton *browse = new QPushButton(tr("Browse..."));
Chris@303 72 connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked()));
Chris@303 73 subgrid->addWidget(browse, row, 2, 1, 1);
Chris@303 74
Chris@303 75 ++row;
Chris@303 76
Chris@303 77 QGroupBox *previewbox = new QGroupBox(tr("Preview"));
Chris@303 78
Chris@303 79 subgrid = new QGridLayout;
Chris@303 80 previewbox->setLayout(subgrid);
Chris@303 81
Chris@303 82 m_imagePreview = new QLabel;
Chris@303 83 m_imagePreview->setAlignment(Qt::AlignCenter);
Chris@303 84 subgrid->addWidget(m_imagePreview, 0, 0);
Chris@303 85
Chris@303 86 m_imagePreview->setMinimumSize(QSize(100, 100));
Chris@303 87
Chris@304 88 QDesktopWidget *desktop = QApplication::desktop();
Chris@304 89 m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3,
Chris@304 90 (desktop->height() * 2) / 3));
Chris@304 91
Chris@303 92 grid->addWidget(databox, 0, 0);
Chris@303 93 grid->addWidget(previewbox, 1, 0);
Chris@303 94
Chris@303 95 grid->setRowStretch(1, 10);
Chris@303 96
Chris@303 97 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@303 98 QDialogButtonBox::Cancel);
Chris@303 99 grid->addWidget(bb, 2, 0, 1, 1);
Chris@303 100 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@303 101 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@303 102
Chris@303 103 m_okButton = bb->button(QDialogButtonBox::Ok);
Chris@303 104 m_okButton->setEnabled(false);
Chris@303 105
Chris@303 106 if (image != "") setImage(image);
Chris@303 107 if (label != "") setLabel(label);
Chris@303 108 }
Chris@303 109
Chris@303 110 ImageDialog::~ImageDialog()
Chris@303 111 {
Chris@305 112 delete m_remoteFile;
Chris@303 113 }
Chris@303 114
Chris@303 115 QString
Chris@303 116 ImageDialog::getImage()
Chris@303 117 {
Chris@303 118 return m_loadedImageFile;
Chris@303 119 }
Chris@303 120
Chris@303 121 QPixmap
Chris@303 122 ImageDialog::getPixmap()
Chris@303 123 {
Chris@303 124 return m_loadedImage;
Chris@303 125 }
Chris@303 126
Chris@303 127 QString
Chris@303 128 ImageDialog::getLabel()
Chris@303 129 {
Chris@303 130 return m_labelEdit->text();
Chris@303 131 }
Chris@303 132
Chris@303 133 void
Chris@303 134 ImageDialog::setImage(QString image)
Chris@303 135 {
Chris@303 136 m_imageEdit->setText(image);
Chris@303 137 updatePreview();
Chris@303 138 }
Chris@303 139
Chris@303 140 void
Chris@303 141 ImageDialog::setLabel(QString label)
Chris@303 142 {
Chris@303 143 m_labelEdit->setText(label);
Chris@303 144 }
Chris@303 145
Chris@303 146 void
Chris@303 147 ImageDialog::resizeEvent(QResizeEvent *)
Chris@303 148 {
Chris@303 149 updatePreview();
Chris@303 150 }
Chris@303 151
Chris@303 152 void
Chris@305 153 ImageDialog::imageEditEdited(const QString &s)
Chris@305 154 {
Chris@305 155 if (s.startsWith("http:") || s.startsWith("ftp:")) {
Chris@305 156 return;
Chris@305 157 }
Chris@305 158 updatePreview();
Chris@305 159 }
Chris@305 160
Chris@305 161 void
Chris@305 162 ImageDialog::imageEditEdited()
Chris@304 163 {
Chris@304 164 updatePreview();
Chris@304 165 }
Chris@304 166
Chris@304 167 void
Chris@303 168 ImageDialog::updatePreview()
Chris@303 169 {
Chris@303 170 if (!m_imagePreview) return;
Chris@303 171
Chris@303 172 QString img = m_imageEdit->text();
Chris@303 173
Chris@304 174 m_okButton->setEnabled(img != "");
Chris@304 175
Chris@303 176 if (img != m_loadedImageFile) {
Chris@305 177
Chris@305 178 QString fileName = img;
Chris@305 179 delete m_remoteFile;
Chris@305 180 m_remoteFile = 0;
Chris@305 181
Chris@318 182 if (FileSource::isRemote(fileName)) {
Chris@305 183 QUrl url(fileName);
Chris@318 184 if (!FileSource::canHandleScheme(url)) {
Chris@305 185 QMessageBox::critical(this, tr("Unsupported scheme in URL"),
Chris@305 186 tr("The URL scheme \"%1\" is not supported")
Chris@305 187 .arg(url.scheme()));
Chris@305 188 } else {
Chris@344 189 m_remoteFile = new FileSource(url, FileSource::ProgressDialog);
Chris@317 190 m_remoteFile->waitForData();
Chris@305 191 if (!m_remoteFile->isOK()) {
Chris@305 192 QMessageBox::critical(this, tr("File download failed"),
Chris@305 193 tr("Failed to download URL \"%1\": %2")
Chris@305 194 .arg(url.toString()).arg(m_remoteFile->getErrorString()));
Chris@305 195 delete m_remoteFile;
Chris@305 196 m_remoteFile = 0;
Chris@305 197 } else {
Chris@305 198 fileName = m_remoteFile->getLocalFilename();
Chris@305 199 }
Chris@305 200 }
Chris@305 201 }
Chris@305 202
Chris@305 203 // std::cerr << "image filename: \"" << fileName.toStdString() << "\"" << std::endl;
Chris@305 204
Chris@305 205 m_loadedImage = QPixmap(fileName);
Chris@303 206 m_loadedImageFile = img;
Chris@303 207 }
Chris@303 208
Chris@303 209 QSize sz(m_imagePreview->size());
Chris@303 210 int m = m_imagePreview->margin() * 2;
Chris@303 211 sz -= QSize(m, m);
Chris@303 212
Chris@303 213 if (m_loadedImage.isNull()) {
Chris@303 214 m_imagePreview->setPixmap(QPixmap());
Chris@303 215 } else {
Chris@303 216 m_imagePreview->setPixmap(m_loadedImage.scaled
Chris@303 217 (sz,
Chris@303 218 Qt::KeepAspectRatio,
Chris@303 219 Qt::SmoothTransformation));
Chris@303 220 }
Chris@303 221 }
Chris@303 222
Chris@303 223 void
Chris@303 224 ImageDialog::browseClicked()
Chris@303 225 {
Chris@303 226 QString file =
Chris@303 227 FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile);
Chris@303 228
Chris@303 229 if (file != "") {
Chris@303 230 setImage(file);
Chris@303 231 emit imageChanged(file);
Chris@303 232 }
Chris@303 233 }
Chris@303 234
Chris@303 235
Chris@303 236