ImageDialog.cpp

Go to the documentation of this file.
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 "data/fileio/FileSource.h"
00030 #include "data/fileio/FileFinder.h"
00031 
00032 #include <iostream>
00033 
00034 ImageDialog::ImageDialog(QString title,
00035                          QString image,
00036                          QString label,
00037                          QWidget *parent) :
00038     QDialog(parent),
00039     m_imagePreview(0),
00040     m_remoteFile(0)
00041 {
00042     setWindowTitle(title);
00043     
00044     QGridLayout *grid = new QGridLayout;
00045     setLayout(grid);
00046 
00047     QGroupBox *databox = new QGroupBox(tr("Image"));
00048 
00049     QGridLayout *subgrid = new QGridLayout;
00050     databox->setLayout(subgrid);
00051 
00052     int row = 0;
00053 
00054     subgrid->addWidget(new QLabel(tr("Label:")), row, 0);
00055 
00056     m_labelEdit = new QLineEdit;
00057     subgrid->addWidget(m_labelEdit, row, 1, 1, 2);
00058 
00059     ++row;
00060 
00061     subgrid->addWidget(new QLabel(tr("File or URL:")), row, 0);
00062 
00063     m_imageEdit = new QLineEdit;
00064     subgrid->addWidget(m_imageEdit, row, 1, 1, 1);
00065 
00066     connect(m_imageEdit, SIGNAL(textEdited(const QString &)),
00067             this, SLOT(imageEditEdited(const QString &)));
00068     connect(m_imageEdit, SIGNAL(editingFinished()),
00069             this, SLOT(imageEditEdited()));
00070 
00071     QPushButton *browse = new QPushButton(tr("Browse..."));
00072     connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked()));
00073     subgrid->addWidget(browse, row, 2, 1, 1);
00074 
00075     ++row;
00076 
00077     QGroupBox *previewbox = new QGroupBox(tr("Preview"));
00078     
00079     subgrid = new QGridLayout;
00080     previewbox->setLayout(subgrid);
00081 
00082     m_imagePreview = new QLabel;
00083     m_imagePreview->setAlignment(Qt::AlignCenter);
00084     subgrid->addWidget(m_imagePreview, 0, 0);
00085 
00086     m_imagePreview->setMinimumSize(QSize(100, 100));
00087 
00088     QDesktopWidget *desktop = QApplication::desktop();
00089     m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3,
00090                                          (desktop->height() * 2) / 3));
00091 
00092     grid->addWidget(databox, 0, 0);
00093     grid->addWidget(previewbox, 1, 0);
00094 
00095     grid->setRowStretch(1, 10);
00096 
00097     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
00098                                                 QDialogButtonBox::Cancel);
00099     grid->addWidget(bb, 2, 0, 1, 1);
00100     connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
00101     connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
00102 
00103     m_okButton = bb->button(QDialogButtonBox::Ok);
00104     m_okButton->setEnabled(false);
00105 
00106     if (image != "") setImage(image);
00107     if (label != "") setLabel(label);
00108 }
00109 
00110 ImageDialog::~ImageDialog()
00111 {
00112     delete m_remoteFile;
00113 }
00114 
00115 QString
00116 ImageDialog::getImage()
00117 {
00118     return m_loadedImageFile;
00119 }
00120 
00121 QPixmap
00122 ImageDialog::getPixmap()
00123 {
00124     return m_loadedImage;
00125 }
00126 
00127 QString
00128 ImageDialog::getLabel()
00129 {
00130     return m_labelEdit->text();
00131 }
00132 
00133 void
00134 ImageDialog::setImage(QString image)
00135 {
00136     m_imageEdit->setText(image);
00137     updatePreview();
00138 }
00139 
00140 void
00141 ImageDialog::setLabel(QString label)
00142 {
00143     m_labelEdit->setText(label);
00144 }
00145 
00146 void
00147 ImageDialog::resizeEvent(QResizeEvent *)
00148 {
00149     updatePreview();
00150 }
00151 
00152 void
00153 ImageDialog::imageEditEdited(const QString &s)
00154 {
00155     if (s.startsWith("http:") || s.startsWith("ftp:")) {
00156         return;
00157     }
00158     updatePreview();
00159 }
00160 
00161 void
00162 ImageDialog::imageEditEdited()
00163 {
00164     updatePreview();
00165 }
00166 
00167 void
00168 ImageDialog::updatePreview()
00169 {
00170     if (!m_imagePreview) return;
00171 
00172     QString img = m_imageEdit->text();
00173 
00174     m_okButton->setEnabled(img != "");
00175 
00176     if (img != m_loadedImageFile) {
00177 
00178         QString fileName = img;
00179         delete m_remoteFile;
00180         m_remoteFile = 0;
00181 
00182         if (FileSource::isRemote(fileName)) {
00183             QUrl url(fileName);
00184             if (!FileSource::canHandleScheme(url)) {
00185                 QMessageBox::critical(this, tr("Unsupported scheme in URL"),
00186                                       tr("The URL scheme \"%1\" is not supported")
00187                                       .arg(url.scheme()));
00188             } else {
00189                 m_remoteFile = new FileSource(url, FileSource::ProgressDialog);
00190                 m_remoteFile->waitForData();
00191                 if (!m_remoteFile->isOK()) {
00192                     QMessageBox::critical(this, tr("File download failed"),
00193                                           tr("Failed to download URL \"%1\": %2")
00194                                           .arg(url.toString()).arg(m_remoteFile->getErrorString()));
00195                     delete m_remoteFile;
00196                     m_remoteFile = 0;
00197                 } else {
00198                     fileName = m_remoteFile->getLocalFilename();
00199                 }
00200             }
00201         }
00202         
00203 //        std::cerr << "image filename: \"" << fileName.toStdString() << "\"" << std::endl;
00204 
00205         m_loadedImage = QPixmap(fileName);
00206         m_loadedImageFile = img;
00207     }
00208 
00209     QSize sz(m_imagePreview->size());
00210     int m = m_imagePreview->margin() * 2;
00211     sz -= QSize(m, m);
00212 
00213     if (m_loadedImage.isNull()) {
00214         m_imagePreview->setPixmap(QPixmap());
00215     } else {
00216         m_imagePreview->setPixmap(m_loadedImage.scaled
00217                                   (sz,
00218                                    Qt::KeepAspectRatio,
00219                                    Qt::SmoothTransformation));
00220     }
00221 }
00222 
00223 void
00224 ImageDialog::browseClicked()
00225 {
00226     QString file =
00227         FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile);
00228 
00229     if (file != "") {
00230         setImage(file);
00231         emit imageChanged(file);
00232     }
00233 }
00234 
00235 
00236 

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1