Chris@303: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@303: Chris@303: /* Chris@303: Sonic Visualiser Chris@303: An audio file viewer and annotation editor. Chris@303: Centre for Digital Music, Queen Mary, University of London. Chris@303: This file copyright 2007 QMUL. Chris@303: Chris@303: This program is free software; you can redistribute it and/or Chris@303: modify it under the terms of the GNU General Public License as Chris@303: published by the Free Software Foundation; either version 2 of the Chris@303: License, or (at your option) any later version. See the file Chris@303: COPYING included with this distribution for more information. Chris@303: */ Chris@303: Chris@303: #include "ImageDialog.h" Chris@303: Chris@303: #include Chris@303: #include Chris@303: #include Chris@303: #include Chris@303: #include Chris@303: #include Chris@304: #include Chris@304: #include Chris@305: #include Chris@305: #include Chris@303: Chris@318: #include "data/fileio/FileSource.h" Chris@303: #include "data/fileio/FileFinder.h" Chris@303: Chris@305: #include Chris@305: Chris@303: ImageDialog::ImageDialog(QString title, Chris@303: QString image, Chris@303: QString label, Chris@303: QWidget *parent) : Chris@303: QDialog(parent), Chris@305: m_imagePreview(0), Chris@305: m_remoteFile(0) Chris@303: { Chris@303: setWindowTitle(title); Chris@303: Chris@303: QGridLayout *grid = new QGridLayout; Chris@303: setLayout(grid); Chris@303: Chris@303: QGroupBox *databox = new QGroupBox(tr("Image")); Chris@303: Chris@303: QGridLayout *subgrid = new QGridLayout; Chris@303: databox->setLayout(subgrid); Chris@303: Chris@303: int row = 0; Chris@303: Chris@303: subgrid->addWidget(new QLabel(tr("Label:")), row, 0); Chris@303: Chris@303: m_labelEdit = new QLineEdit; Chris@303: subgrid->addWidget(m_labelEdit, row, 1, 1, 2); Chris@303: Chris@303: ++row; Chris@303: Chris@305: subgrid->addWidget(new QLabel(tr("File or URL:")), row, 0); Chris@303: Chris@303: m_imageEdit = new QLineEdit; Chris@303: subgrid->addWidget(m_imageEdit, row, 1, 1, 1); Chris@305: Chris@304: connect(m_imageEdit, SIGNAL(textEdited(const QString &)), Chris@304: this, SLOT(imageEditEdited(const QString &))); Chris@305: connect(m_imageEdit, SIGNAL(editingFinished()), Chris@305: this, SLOT(imageEditEdited())); Chris@303: Chris@303: QPushButton *browse = new QPushButton(tr("Browse...")); Chris@303: connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked())); Chris@303: subgrid->addWidget(browse, row, 2, 1, 1); Chris@303: Chris@303: ++row; Chris@303: Chris@303: QGroupBox *previewbox = new QGroupBox(tr("Preview")); Chris@303: Chris@303: subgrid = new QGridLayout; Chris@303: previewbox->setLayout(subgrid); Chris@303: Chris@303: m_imagePreview = new QLabel; Chris@303: m_imagePreview->setAlignment(Qt::AlignCenter); Chris@303: subgrid->addWidget(m_imagePreview, 0, 0); Chris@303: Chris@303: m_imagePreview->setMinimumSize(QSize(100, 100)); Chris@303: Chris@304: QDesktopWidget *desktop = QApplication::desktop(); Chris@304: m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3, Chris@304: (desktop->height() * 2) / 3)); Chris@304: Chris@303: grid->addWidget(databox, 0, 0); Chris@303: grid->addWidget(previewbox, 1, 0); Chris@303: Chris@303: grid->setRowStretch(1, 10); Chris@303: Chris@303: QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | Chris@303: QDialogButtonBox::Cancel); Chris@303: grid->addWidget(bb, 2, 0, 1, 1); Chris@303: connect(bb, SIGNAL(accepted()), this, SLOT(accept())); Chris@303: connect(bb, SIGNAL(rejected()), this, SLOT(reject())); Chris@303: Chris@303: m_okButton = bb->button(QDialogButtonBox::Ok); Chris@303: m_okButton->setEnabled(false); Chris@303: Chris@303: if (image != "") setImage(image); Chris@303: if (label != "") setLabel(label); Chris@303: } Chris@303: Chris@303: ImageDialog::~ImageDialog() Chris@303: { Chris@305: delete m_remoteFile; Chris@303: } Chris@303: Chris@303: QString Chris@303: ImageDialog::getImage() Chris@303: { Chris@303: return m_loadedImageFile; Chris@303: } Chris@303: Chris@303: QPixmap Chris@303: ImageDialog::getPixmap() Chris@303: { Chris@303: return m_loadedImage; Chris@303: } Chris@303: Chris@303: QString Chris@303: ImageDialog::getLabel() Chris@303: { Chris@303: return m_labelEdit->text(); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageDialog::setImage(QString image) Chris@303: { Chris@303: m_imageEdit->setText(image); Chris@303: updatePreview(); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageDialog::setLabel(QString label) Chris@303: { Chris@303: m_labelEdit->setText(label); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageDialog::resizeEvent(QResizeEvent *) Chris@303: { Chris@303: updatePreview(); Chris@303: } Chris@303: Chris@303: void Chris@305: ImageDialog::imageEditEdited(const QString &s) Chris@305: { Chris@305: if (s.startsWith("http:") || s.startsWith("ftp:")) { Chris@305: return; Chris@305: } Chris@305: updatePreview(); Chris@305: } Chris@305: Chris@305: void Chris@305: ImageDialog::imageEditEdited() Chris@304: { Chris@304: updatePreview(); Chris@304: } Chris@304: Chris@304: void Chris@303: ImageDialog::updatePreview() Chris@303: { Chris@303: if (!m_imagePreview) return; Chris@303: Chris@303: QString img = m_imageEdit->text(); Chris@303: Chris@304: m_okButton->setEnabled(img != ""); Chris@304: Chris@303: if (img != m_loadedImageFile) { Chris@305: Chris@305: QString fileName = img; Chris@305: delete m_remoteFile; Chris@305: m_remoteFile = 0; Chris@305: Chris@318: if (FileSource::isRemote(fileName)) { Chris@305: QUrl url(fileName); Chris@318: if (!FileSource::canHandleScheme(url)) { Chris@305: QMessageBox::critical(this, tr("Unsupported scheme in URL"), Chris@305: tr("The URL scheme \"%1\" is not supported") Chris@305: .arg(url.scheme())); Chris@305: } else { Chris@344: m_remoteFile = new FileSource(url, FileSource::ProgressDialog); Chris@317: m_remoteFile->waitForData(); Chris@305: if (!m_remoteFile->isOK()) { Chris@305: QMessageBox::critical(this, tr("File download failed"), Chris@305: tr("Failed to download URL \"%1\": %2") Chris@305: .arg(url.toString()).arg(m_remoteFile->getErrorString())); Chris@305: delete m_remoteFile; Chris@305: m_remoteFile = 0; Chris@305: } else { Chris@305: fileName = m_remoteFile->getLocalFilename(); Chris@305: } Chris@305: } Chris@305: } Chris@305: Chris@305: // std::cerr << "image filename: \"" << fileName.toStdString() << "\"" << std::endl; Chris@305: Chris@305: m_loadedImage = QPixmap(fileName); Chris@303: m_loadedImageFile = img; Chris@303: } Chris@303: Chris@303: QSize sz(m_imagePreview->size()); Chris@303: int m = m_imagePreview->margin() * 2; Chris@303: sz -= QSize(m, m); Chris@303: Chris@303: if (m_loadedImage.isNull()) { Chris@303: m_imagePreview->setPixmap(QPixmap()); Chris@303: } else { Chris@303: m_imagePreview->setPixmap(m_loadedImage.scaled Chris@303: (sz, Chris@303: Qt::KeepAspectRatio, Chris@303: Qt::SmoothTransformation)); Chris@303: } Chris@303: } Chris@303: Chris@303: void Chris@303: ImageDialog::browseClicked() Chris@303: { Chris@303: QString file = Chris@303: FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile); Chris@303: Chris@303: if (file != "") { Chris@303: setImage(file); Chris@303: emit imageChanged(file); Chris@303: } Chris@303: } Chris@303: Chris@303: Chris@303: