From 74db6805056aa7a7818d1d348d30ab8499174d07 Mon Sep 17 00:00:00 2001 From: borgmanJeremy <46930769+borgmanJeremy@users.noreply.github.com> Date: Sun, 6 Feb 2022 17:51:51 -0600 Subject: [PATCH] fix bug on macos with save dialog (#2379) * fix bug on macos with save dialog * refactored Saver to not be a class since there are no member variables * removed useless comment --- src/core/controller.cpp | 4 +- src/core/flameshotdaemon.cpp | 2 +- .../imgupload/storages/imguploaderbase.cpp | 2 +- src/utils/screenshotsaver.cpp | 151 +++++++++--------- src/utils/screenshotsaver.h | 25 +-- src/widgets/capturelauncher.cpp | 2 +- 6 files changed, 84 insertions(+), 102 deletions(-) diff --git a/src/core/controller.cpp b/src/core/controller.cpp index 95fc2268..6a5a9829 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -639,9 +639,9 @@ void Controller::exportCapture(QPixmap capture, if (tasks & CR::SAVE) { if (req.path().isEmpty()) { - ScreenshotSaver().saveToFilesystemGUI(capture); + saveToFilesystemGUI(capture); } else { - ScreenshotSaver().saveToFilesystem(capture, path); + saveToFilesystem(capture, path); } } diff --git a/src/core/flameshotdaemon.cpp b/src/core/flameshotdaemon.cpp index 9db914ff..1445d94e 100644 --- a/src/core/flameshotdaemon.cpp +++ b/src/core/flameshotdaemon.cpp @@ -203,7 +203,7 @@ void FlameshotDaemon::attachScreenshotToClipboard(QPixmap pixmap) // This variable is necessary because the signal doesn't get blocked on // windows for some reason m_clipboardSignalBlocked = true; - ScreenshotSaver().saveToClipboard(pixmap); + saveToClipboard(pixmap); clipboard->blockSignals(false); } diff --git a/src/tools/imgupload/storages/imguploaderbase.cpp b/src/tools/imgupload/storages/imguploaderbase.cpp index 5bea7dac..26b08be6 100644 --- a/src/tools/imgupload/storages/imguploaderbase.cpp +++ b/src/tools/imgupload/storages/imguploaderbase.cpp @@ -186,7 +186,7 @@ void ImgUploaderBase::deleteCurrentImage() void ImgUploaderBase::saveScreenshotToFilesystem() { - if (!ScreenshotSaver().saveToFilesystemGUI(m_pixmap)) { + if (!saveToFilesystemGUI(m_pixmap)) { m_notification->showMessage( tr("Unable to save the screenshot to disk.")); return; diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index d5cc0d47..53094072 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -28,10 +27,70 @@ #include "src/widgets/capture/capturewidget.h" #endif -ScreenshotSaver::ScreenshotSaver() {} +bool saveToFilesystem(const QPixmap& capture, + const QString& path, + const QString& messagePrefix) +{ + QString completePath = FileNameHandler().properScreenshotPath( + path, ConfigHandler().saveAsFileExtension()); + QFile file{ completePath }; + file.open(QIODevice::WriteOnly); + bool okay = capture.save(&file); + QString saveMessage = messagePrefix; + QString notificationPath = completePath; + if (!saveMessage.isEmpty()) { + saveMessage += " "; + } -void ScreenshotSaver::saveToClipboardMime(const QPixmap& capture, - const QString& imageType) + if (okay) { + saveMessage += QObject::tr("Capture saved as ") + completePath; + AbstractLogger::info().attachNotificationPath(notificationPath) + << saveMessage; + } else { + saveMessage += QObject::tr("Error trying to save as ") + completePath; + if (file.error() != QFile::NoError) { + saveMessage += ": " + file.errorString(); + } + notificationPath = ""; + AbstractLogger::error().attachNotificationPath(notificationPath) + << saveMessage; + } + + return okay; +} + +QString ShowSaveFileDialog(const QString& title, const QString& directory) +{ + QFileDialog dialog(nullptr, title, directory); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + // Build string list of supported image formats + QStringList mimeTypeList; + foreach (auto mimeType, QImageWriter::supportedMimeTypes()) { + // HEIF is meant for videos and it causes a glitch on MacOS + // because the native dialog lumps together heic and heif + if (mimeType != "image/heif") { + mimeTypeList.append(mimeType); + } + } + dialog.setMimeTypeFilters(mimeTypeList); + + QString suffix = ConfigHandler().saveAsFileExtension(); + if (suffix.isEmpty()) { + suffix = "png"; + } + QString defaultMimeType = + QMimeDatabase().mimeTypeForFile("image." + suffix).name(); + dialog.selectMimeTypeFilter(defaultMimeType); + dialog.setDefaultSuffix(suffix); + if (dialog.exec() == QDialog::Accepted) { + return dialog.selectedFiles().constFirst(); + } else { + return {}; + } +} + +void saveToClipboardMime(const QPixmap& capture, const QString& imageType) { QByteArray array; QBuffer buffer{ &array }; @@ -64,9 +123,9 @@ void ScreenshotSaver::saveToClipboardMime(const QPixmap& capture, } } -// TODO: If data is saved to the clipboard before the notification is sent via +// If data is saved to the clipboard before the notification is sent via // dbus, the application freezes. -void ScreenshotSaver::saveToClipboard(const QPixmap& capture) +void saveToClipboard(const QPixmap& capture) { // If we are able to properly save the file, save the file and copy to // clipboard. @@ -95,73 +154,9 @@ void ScreenshotSaver::saveToClipboard(const QPixmap& capture) } } -bool ScreenshotSaver::saveToFilesystem(const QPixmap& capture, - const QString& path, - const QString& messagePrefix) +bool saveToFilesystemGUI(const QPixmap& capture) { - QString completePath = FileNameHandler().properScreenshotPath( - path, ConfigHandler().saveAsFileExtension()); - QFile file{ completePath }; - file.open(QIODevice::WriteOnly); - bool ok = capture.save(&file); - QString saveMessage = messagePrefix; - QString notificationPath = completePath; - if (!saveMessage.isEmpty()) { - saveMessage += " "; - } - - if (ok) { - saveMessage += QObject::tr("Capture saved as ") + completePath; - AbstractLogger::info().attachNotificationPath(notificationPath) - << saveMessage; - } else { - saveMessage += QObject::tr("Error trying to save as ") + completePath; - if (file.error() != QFile::NoError) { - saveMessage += ": " + file.errorString(); - } - notificationPath = ""; - AbstractLogger::error().attachNotificationPath(notificationPath) - << saveMessage; - } - - return ok; -} - -QString ScreenshotSaver::ShowSaveFileDialog(QWidget* parent, - const QString& title, - const QString& directory) -{ - QFileDialog dialog(parent, title, directory); - if (parent) { - dialog.setWindowModality(Qt::WindowModal); - } - - dialog.setAcceptMode(QFileDialog::AcceptSave); - - // Build string list of supported image formats - QStringList mimeTypeList; - foreach (auto mimeType, QImageWriter::supportedMimeTypes()) - mimeTypeList.append(mimeType); - dialog.setMimeTypeFilters(mimeTypeList); - - QString suffix = ConfigHandler().saveAsFileExtension(); - if (suffix.isEmpty()) { - suffix = "png"; - } - QString defaultMimeType = - QMimeDatabase().mimeTypeForFile("image." + suffix).name(); - dialog.selectMimeTypeFilter(defaultMimeType); - dialog.setDefaultSuffix(suffix); - if (dialog.exec() == QDialog::Accepted) { - return dialog.selectedFiles().first(); - } else { - return QString(); - } -} - -bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture) -{ - bool ok = false; + bool okay = false; ConfigHandler config; QString defaultSavePath = ConfigHandler().savePath(); if (defaultSavePath.isEmpty() || !QDir(defaultSavePath).exists() || @@ -183,20 +178,18 @@ bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture) } #endif if (!config.savePathFixed()) { - // auto imageFormats = QImageWriter::supportedImageFormats(); - savePath = - ShowSaveFileDialog(nullptr, QObject::tr("Save screenshot"), savePath); + savePath = ShowSaveFileDialog(QObject::tr("Save screenshot"), savePath); } if (savePath == "") { - return ok; + return okay; } QFile file{ savePath }; file.open(QIODevice::WriteOnly); - ok = capture.save(&file); + okay = capture.save(&file); - if (ok) { + if (okay) { QString pathNoFile = savePath.left(savePath.lastIndexOf(QLatin1String("/"))); @@ -223,5 +216,5 @@ bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture) saveErrBox.exec(); } - return ok; + return okay; } diff --git a/src/utils/screenshotsaver.h b/src/utils/screenshotsaver.h index 42e6edfe..9face346 100644 --- a/src/utils/screenshotsaver.h +++ b/src/utils/screenshotsaver.h @@ -6,22 +6,11 @@ #include class QPixmap; -class QWidget; -class ScreenshotSaver -{ -public: - ScreenshotSaver(); - - void saveToClipboard(const QPixmap& capture); - void saveToClipboardMime(const QPixmap& capture, const QString& imageType); - bool saveToFilesystem(const QPixmap& capture, - const QString& path, - const QString& messagePrefix = ""); - bool saveToFilesystemGUI(const QPixmap& capture); - -private: - QString ShowSaveFileDialog(QWidget* parent, - const QString& title, - const QString& directory); -}; +bool saveToFilesystem(const QPixmap& capture, + const QString& path, + const QString& messagePrefix = ""); +QString ShowSaveFileDialog(const QString& title, const QString& directory); +void saveToClipboardMime(const QPixmap& capture, const QString& imageType); +void saveToClipboard(const QPixmap& capture); +bool saveToFilesystemGUI(const QPixmap& capture); diff --git a/src/widgets/capturelauncher.cpp b/src/widgets/capturelauncher.cpp index 311c40de..f352a4fc 100644 --- a/src/widgets/capturelauncher.cpp +++ b/src/widgets/capturelauncher.cpp @@ -119,7 +119,7 @@ void CaptureLauncher::captureTaken(QPixmap screenshot) ui->captureType->currentData().toInt()); if (mode == CaptureRequest::FULLSCREEN_MODE) { - ScreenshotSaver().saveToFilesystemGUI(screenshot); + saveToFilesystemGUI(screenshot); } ui->launchButton->setEnabled(true); }