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