From bb03cfb13344414a7e8a7c0bc4c29e0b6b24a81e Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 31 May 2013 12:42:54 +0200 Subject: [PATCH] [PageScreen] Add option to choose in which format to save. Also makes the dialog less confusing whether the image was saved or not. Closes #902 --- src/lib/other/pagescreen.cpp | 91 +++++++++++++++++++++-------------- src/lib/other/pagescreen.h | 10 ++-- src/lib/other/pagescreen.ui | 92 +++++++++++++++++++++++++++++++----- translations/empty.ts | 22 ++++++++- 4 files changed, 160 insertions(+), 55 deletions(-) diff --git a/src/lib/other/pagescreen.cpp b/src/lib/other/pagescreen.cpp index cb6d80014..2fd890551 100644 --- a/src/lib/other/pagescreen.cpp +++ b/src/lib/other/pagescreen.cpp @@ -39,76 +39,97 @@ PageScreen::PageScreen(WebView* view, QWidget* parent) : QDialog(parent) , ui(new Ui::PageScreen) , m_view(view) - , m_blockClose(false) - , m_fileSaving(0) , m_imageScaling(0) { setAttribute(Qt::WA_DeleteOnClose); ui->setupUi(this); + m_formats[0] = QLatin1String("PNG"); + m_formats[1] = QLatin1String("BMP"); + m_formats[2] = QLatin1String("JPG"); + m_formats[3] = QLatin1String("PPM"); + m_formats[4] = QLatin1String("TIFF"); + + QHashIterator i(m_formats); + while (i.hasNext()) { + i.next(); + ui->formats->addItem(tr("Save as %1").arg(i.value())); + } + + // Set png as a default format + m_pageTitle = m_view->title(); + ui->location->setText(QString("%1/%2.png").arg(QDir::homePath(), QzTools::filterCharsFromFilename(m_pageTitle))); + QMovie* mov = new QMovie(":html/loading.gif"); ui->label->setMovie(mov); mov->start(); - m_pageTitle = m_view->title(); - + connect(ui->changeLocation, SIGNAL(clicked()), this, SLOT(changeLocation())); + connect(ui->formats, SIGNAL(currentIndexChanged(int)), this, SLOT(formatChanged())); connect(ui->buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()), this, SLOT(dialogAccepted())); connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(close())); QTimer::singleShot(200, this, SLOT(createThumbnail())); } +void PageScreen::formatChanged() +{ + QString text = ui->location->text(); + int pos = text.lastIndexOf(QLatin1Char('.')); + + if (pos > -1) { + text = text.left(pos + 1) + m_formats[ui->formats->currentIndex()].toLower(); + } + else { + text.append(QLatin1Char('.') + m_formats[ui->formats->currentIndex()].toLower()); + } + + ui->location->setText(text); +} + +void PageScreen::changeLocation() +{ + const QString &suggestedPath = QString("%1/%2.%3").arg(QDir::homePath(), QzTools::filterCharsFromFilename(m_pageTitle), + m_formats[ui->formats->currentIndex()].toLower()); + const QString &path = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), suggestedPath); + + if (!path.isEmpty()) { + ui->location->setText(path); + } +} + void PageScreen::dialogAccepted() { - const QString &suggestedPath = QString("%1/%2.png").arg(QDir::homePath(), - QzTools::filterCharsFromFilename(m_pageTitle)); - m_filePath = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), suggestedPath); - - if (!m_filePath.isEmpty()) { + if (!ui->location->text().isEmpty()) { QApplication::setOverrideCursor(Qt::WaitCursor); - m_blockClose = true; + saveScreen(); + QApplication::restoreOverrideCursor(); - m_fileSaving = new QFutureWatcher(this); - m_fileSaving->setFuture(QtConcurrent::run(this, &PageScreen::saveScreen)); - connect(m_fileSaving, SIGNAL(finished()), SLOT(screenSaved())); + close(); } } void PageScreen::saveScreen() { - QString pathWithoutSuffix = m_filePath; - if (pathWithoutSuffix.endsWith(QLatin1String(".png"), Qt::CaseInsensitive)) { - pathWithoutSuffix = pathWithoutSuffix.mid(0, pathWithoutSuffix.length() - 4); + const QString &format = m_formats[ui->formats->currentIndex()]; + const QString &suffix = QLatin1Char('.') + m_formats[ui->formats->currentIndex()].toLower(); + + QString pathWithoutSuffix = ui->location->text(); + if (pathWithoutSuffix.endsWith(suffix, Qt::CaseInsensitive)) { + pathWithoutSuffix = pathWithoutSuffix.mid(0, pathWithoutSuffix.length() - suffix.length()); } if (m_pageImages.count() == 1) { - m_pageImages.first().save(pathWithoutSuffix + ".png", "PNG"); + m_pageImages.first().save(pathWithoutSuffix + suffix, format.toUtf8()); } else { int part = 1; foreach (const QImage &image, m_pageImages) { const QString &fileName = pathWithoutSuffix + ".part" + QString::number(part); - image.save(fileName + ".png", "PNG"); + image.save(fileName + suffix, format.toUtf8()); part++; } } - - m_blockClose = false; -} - -void PageScreen::screenSaved() -{ - QApplication::restoreOverrideCursor(); -} - -void PageScreen::closeEvent(QCloseEvent* event) -{ - if (m_blockClose) { - event->ignore(); - return; - } - - QDialog::closeEvent(event); } void PageScreen::createThumbnail() diff --git a/src/lib/other/pagescreen.h b/src/lib/other/pagescreen.h index 2b6fbf32e..7edcd8856 100644 --- a/src/lib/other/pagescreen.h +++ b/src/lib/other/pagescreen.h @@ -44,23 +44,21 @@ private slots: void createThumbnail(); void showImage(); + void formatChanged(); + void changeLocation(); void dialogAccepted(); - void saveScreen(); - void screenSaved(); private: - void closeEvent(QCloseEvent* event); + void saveScreen(); void createPixmap(); Ui::PageScreen* ui; WebView* m_view; QString m_pageTitle; - bool m_blockClose; - QFutureWatcher* m_fileSaving; QFutureWatcher* m_imageScaling; - QString m_filePath; QVector m_pageImages; + QHash m_formats; }; #endif // PAGESCREEN_H diff --git a/src/lib/other/pagescreen.ui b/src/lib/other/pagescreen.ui index 9b091c5ed..f7d749e2f 100644 --- a/src/lib/other/pagescreen.ui +++ b/src/lib/other/pagescreen.ui @@ -6,23 +6,14 @@ 0 0 - 562 - 332 + 539 + 368 Page Screen - - 0 - - - 6 - - - 0 - @@ -36,8 +27,8 @@ 0 0 - 562 - 277 + 531 + 271 @@ -58,6 +49,81 @@ + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Format: + + + + + + + + + + Location: + + + + + + + + + + 250 + 0 + + + + + + + + ... + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/translations/empty.ts b/translations/empty.ts index 18518d2a9..38f53056f 100644 --- a/translations/empty.ts +++ b/translations/empty.ts @@ -2398,7 +2398,27 @@ - + + Format: + + + + + Location: + + + + + ... + + + + + Save as %1 + + + + Save Page Screen...