From 8d27dc6660b9a0591b5e5e2180cf36256f7aa951 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Mon, 12 Jun 2017 03:31:13 +0200 Subject: [PATCH] Fix crash and lag using the File Dialog --- src/capture/capturewidget.cpp | 6 ++-- src/capture/screenshot.cpp | 52 ++++++++++++++++++++--------------- src/capture/screenshot.h | 8 ++++-- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/capture/capturewidget.cpp b/src/capture/capturewidget.cpp index 3cfd8e0c..18cb7b33 100644 --- a/src/capture/capturewidget.cpp +++ b/src/capture/capturewidget.cpp @@ -394,11 +394,13 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) { } void CaptureWidget::saveScreenshot() { + hide(); if (m_selection.isNull()) { - m_screenshot->graphicalSave(); + m_screenshot->graphicalSave(QRect(), this); } else { // save full screen when no selection - m_screenshot->graphicalSave(getExtendedSelection()); + m_screenshot->graphicalSave(getExtendedSelection(), this); } + close(); } void CaptureWidget::copyScreenshot() { diff --git a/src/capture/screenshot.cpp b/src/capture/screenshot.cpp index 5aab84ce..340e2f0f 100644 --- a/src/capture/screenshot.cpp +++ b/src/capture/screenshot.cpp @@ -35,7 +35,8 @@ // Screenshot is an extension of QPixmap which lets you manage specific tasks -Screenshot::Screenshot(const QPixmap &p) : m_baseScreenshot(p), +Screenshot::Screenshot(const QPixmap &p, QObject *parent) : QObject(parent), + m_baseScreenshot(p), m_modifiedScreenshot(p) {} Screenshot::~Screenshot() { @@ -58,7 +59,7 @@ QPixmap Screenshot::getScreenshot() const { // graphicalSave generates a graphical window to ask about the save path and // saves the screenshot with all the modifications in such directory -QString Screenshot::graphicalSave(const QRect &selection) const { +QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const { const QString format = "png"; QSettings settings; @@ -87,7 +88,7 @@ QString Screenshot::graphicalSave(const QRect &selection) const { } savePath += tempName + "." + format; - QFileDialog fileDialog(nullptr, QObject::tr("Save As"), savePath); + QFileDialog fileDialog(parent, QObject::tr("Save As"), savePath); fileDialog.setAcceptMode(QFileDialog::AcceptSave); fileDialog.setFileMode(QFileDialog::AnyFile); fileDialog.setDirectory(savePath); @@ -97,29 +98,36 @@ QString Screenshot::graphicalSave(const QRect &selection) const { fileDialog.setMimeTypeFilters(mimeTypes); fileDialog.selectMimeTypeFilter("image/" + format); fileDialog.setDefaultSuffix(format); - fileDialog.setWindowIcon(QIcon(":img/flameshot.svg")); - if (fileDialog.exec() != QDialog::Accepted) { return ""; } - const QString fileName = fileDialog.selectedFiles().first(); + fileDialog.setWindowIcon(QIcon(":img/flameshot.png")); - const QString pathNoFile = fileName.left(fileName.lastIndexOf("/")); - settings.setValue("savePath", pathNoFile); + bool saved = false; + QString fileName; + do { + if (fileDialog.exec() != QDialog::Accepted) { return fileName; } + fileName = fileDialog.selectedFiles().first(); - QPixmap pixToSave; - if (selection.isEmpty()) { - pixToSave = m_modifiedScreenshot; - } else { // save full screen when no selection - pixToSave = m_modifiedScreenshot.copy(selection); - } + const QString pathNoFile = fileName.left(fileName.lastIndexOf("/")); + settings.setValue("savePath", pathNoFile); -// if (settings.value("mouseVisible").toBool()) { -// // TO DO -// } + QPixmap pixToSave; + if (selection.isEmpty()) { + pixToSave = m_modifiedScreenshot; + } else { // save full screen when no selection + pixToSave = m_modifiedScreenshot.copy(selection); + } +// if (settings.value("mouseVisible").toBool()) { +// // TO DO +// } - if (!pixToSave.save(fileName)) { - QMessageBox::warning(nullptr, QObject::tr("Save Error"), - QObject::tr("The image could not be saved to \"%1\".") - .arg(QDir::toNativeSeparators(fileName))); - } + saved = pixToSave.save(fileName); + if (!saved) { + QMessageBox saveErrBox(QMessageBox::Warning, QObject::tr("Save Error"), + QObject::tr("The image could not be saved to \"%1\".") + .arg(QDir::toNativeSeparators(fileName))); + saveErrBox.setWindowIcon(QIcon(":img/flameshot.png")); + saveErrBox.exec(); + } + } while(!saved); return fileName; } diff --git a/src/capture/screenshot.h b/src/capture/screenshot.h index 9bea7d2b..1fbfb888 100644 --- a/src/capture/screenshot.h +++ b/src/capture/screenshot.h @@ -21,21 +21,23 @@ #include #include #include +#include class QString; class CaptureModification; class QNetworkAccessManager; -class Screenshot { +class Screenshot : public QObject { + Q_OBJECT public: - Screenshot(const QPixmap &); + Screenshot(const QPixmap &, QObject *parent = 0); ~Screenshot(); void setScreenshot(const QPixmap &); QPixmap getBaseScreenshot() const; QPixmap getScreenshot() const; - QString graphicalSave(const QRect &selection = QRect()) const; + QString graphicalSave(const QRect &selection = QRect(), QWidget *parent = 0) const; void uploadToImgur(QNetworkAccessManager *, const QRect &selection = QRect()); QPixmap paintModification(const CaptureModification &);