svgui
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2007 QMUL. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "ImageDialog.h" 00017 00018 #include <QLineEdit> 00019 #include <QGridLayout> 00020 #include <QLabel> 00021 #include <QDialogButtonBox> 00022 #include <QPushButton> 00023 #include <QGroupBox> 00024 #include <QDesktopWidget> 00025 #include <QApplication> 00026 #include <QUrl> 00027 #include <QMessageBox> 00028 00029 #include "ProgressDialog.h" 00030 00031 #include "data/fileio/FileSource.h" 00032 #include "InteractiveFileFinder.h" 00033 00034 #include <iostream> 00035 00036 ImageDialog::ImageDialog(QString title, 00037 QString image, 00038 QString label, 00039 QWidget *parent) : 00040 QDialog(parent), 00041 m_imagePreview(0), 00042 m_remoteFile(0) 00043 { 00044 setWindowTitle(title); 00045 00046 QGridLayout *grid = new QGridLayout; 00047 setLayout(grid); 00048 00049 QGroupBox *databox = new QGroupBox(tr("Image")); 00050 00051 QGridLayout *subgrid = new QGridLayout; 00052 databox->setLayout(subgrid); 00053 00054 int row = 0; 00055 00056 subgrid->addWidget(new QLabel(tr("Label:")), row, 0); 00057 00058 m_labelEdit = new QLineEdit; 00059 subgrid->addWidget(m_labelEdit, row, 1, 1, 2); 00060 00061 ++row; 00062 00063 subgrid->addWidget(new QLabel(tr("File or URL:")), row, 0); 00064 00065 m_imageEdit = new QLineEdit; 00066 subgrid->addWidget(m_imageEdit, row, 1, 1, 1); 00067 00068 connect(m_imageEdit, SIGNAL(textEdited(const QString &)), 00069 this, SLOT(imageEditEdited(const QString &))); 00070 connect(m_imageEdit, SIGNAL(editingFinished()), 00071 this, SLOT(imageEditEdited())); 00072 00073 QPushButton *browse = new QPushButton(tr("Browse...")); 00074 connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked())); 00075 subgrid->addWidget(browse, row, 2, 1, 1); 00076 00077 ++row; 00078 00079 QGroupBox *previewbox = new QGroupBox(tr("Preview")); 00080 00081 subgrid = new QGridLayout; 00082 previewbox->setLayout(subgrid); 00083 00084 m_imagePreview = new QLabel; 00085 m_imagePreview->setAlignment(Qt::AlignCenter); 00086 subgrid->addWidget(m_imagePreview, 0, 0); 00087 00088 m_imagePreview->setMinimumSize(QSize(100, 100)); 00089 00090 QDesktopWidget *desktop = QApplication::desktop(); 00091 m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3, 00092 (desktop->height() * 2) / 3)); 00093 00094 grid->addWidget(databox, 0, 0); 00095 grid->addWidget(previewbox, 1, 0); 00096 00097 grid->setRowStretch(1, 10); 00098 00099 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | 00100 QDialogButtonBox::Cancel); 00101 grid->addWidget(bb, 2, 0, 1, 1); 00102 connect(bb, SIGNAL(accepted()), this, SLOT(accept())); 00103 connect(bb, SIGNAL(rejected()), this, SLOT(reject())); 00104 00105 m_okButton = bb->button(QDialogButtonBox::Ok); 00106 m_okButton->setEnabled(false); 00107 00108 if (image != "") setImage(image); 00109 if (label != "") setLabel(label); 00110 } 00111 00112 ImageDialog::~ImageDialog() 00113 { 00114 delete m_remoteFile; 00115 } 00116 00117 QString 00118 ImageDialog::getImage() 00119 { 00120 return m_loadedImageFile; 00121 } 00122 00123 QPixmap 00124 ImageDialog::getPixmap() 00125 { 00126 return m_loadedImage; 00127 } 00128 00129 QString 00130 ImageDialog::getLabel() 00131 { 00132 return m_labelEdit->text(); 00133 } 00134 00135 void 00136 ImageDialog::setImage(QString image) 00137 { 00138 m_imageEdit->setText(image); 00139 updatePreview(); 00140 } 00141 00142 void 00143 ImageDialog::setLabel(QString label) 00144 { 00145 m_labelEdit->setText(label); 00146 } 00147 00148 void 00149 ImageDialog::resizeEvent(QResizeEvent *) 00150 { 00151 updatePreview(); 00152 } 00153 00154 void 00155 ImageDialog::imageEditEdited(const QString &s) 00156 { 00157 if (s.startsWith("http:") || s.startsWith("ftp:")) { 00158 return; 00159 } 00160 updatePreview(); 00161 } 00162 00163 void 00164 ImageDialog::imageEditEdited() 00165 { 00166 updatePreview(); 00167 } 00168 00169 void 00170 ImageDialog::updatePreview() 00171 { 00172 if (!m_imagePreview) return; 00173 00174 QString img = m_imageEdit->text(); 00175 00176 m_okButton->setEnabled(img != ""); 00177 00178 if (img != m_loadedImageFile) { 00179 00180 QString fileName = img; 00181 delete m_remoteFile; 00182 m_remoteFile = 0; 00183 00184 if (FileSource::isRemote(fileName)) { 00185 QUrl url(fileName); 00186 if (!FileSource::canHandleScheme(url)) { 00187 QMessageBox::critical(this, tr("Unsupported scheme in URL"), 00188 tr("The URL scheme \"%1\" is not supported") 00189 .arg(url.scheme())); 00190 } else { 00191 00192 ProgressDialog dialog(tr("Opening image URL..."), true, 2000); 00193 m_remoteFile = new FileSource(url, &dialog); 00194 m_remoteFile->waitForData(); 00195 if (!m_remoteFile->isOK()) { 00196 QMessageBox::critical(this, tr("File download failed"), 00197 tr("Failed to download URL \"%1\": %2") 00198 .arg(url.toString()).arg(m_remoteFile->getErrorString())); 00199 delete m_remoteFile; 00200 m_remoteFile = 0; 00201 } else { 00202 fileName = m_remoteFile->getLocalFilename(); 00203 } 00204 } 00205 } 00206 00207 // cerr << "image filename: \"" << fileName << "\"" << endl; 00208 00209 m_loadedImage = QPixmap(fileName); 00210 m_loadedImageFile = img; 00211 } 00212 00213 QSize sz(m_imagePreview->size()); 00214 int m = m_imagePreview->margin() * 2; 00215 sz -= QSize(m, m); 00216 00217 if (m_loadedImage.isNull()) { 00218 m_imagePreview->setPixmap(QPixmap()); 00219 } else { 00220 m_imagePreview->setPixmap(m_loadedImage.scaled 00221 (sz, 00222 Qt::KeepAspectRatio, 00223 Qt::SmoothTransformation)); 00224 } 00225 } 00226 00227 void 00228 ImageDialog::browseClicked() 00229 { 00230 QString file = 00231 InteractiveFileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile); 00232 00233 if (file != "") { 00234 setImage(file); 00235 emit imageChanged(file); 00236 } 00237 } 00238 00239 00240