From 067f4a1914d93928940db40530ac2fff23e23088 Mon Sep 17 00:00:00 2001 From: Benja Appel <78376862+Lyqst@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:24:46 -0300 Subject: [PATCH] Add default file extension configuration (#1995) * Add default file extension configuration * Format fix * Implement ValueHandler for setSaveAsFileExtension * Fix formatting issues * Add format to properScreenshotPath call * Fix SaveFileExtension::check * Fix format * Move extension layout to Save Path group --- src/config/generalconf.cpp | 33 ++++++++++++++++++++++++++++++ src/config/generalconf.h | 3 +++ src/utils/confighandler.cpp | 9 +-------- src/utils/confighandler.h | 5 +++-- src/utils/screenshotsaver.cpp | 10 +++++---- src/utils/valuehandler.cpp | 38 +++++++++++++++++++++++++++++++++++ src/utils/valuehandler.h | 7 +++++++ 7 files changed, 91 insertions(+), 14 deletions(-) diff --git a/src/config/generalconf.cpp b/src/config/generalconf.cpp index 93c54df2..28d9eda7 100644 --- a/src/config/generalconf.cpp +++ b/src/config/generalconf.cpp @@ -5,9 +5,12 @@ #include "src/core/controller.h" #include "src/utils/confighandler.h" #include +#include #include #include #include +#include +#include #include #include #include @@ -388,6 +391,31 @@ void GeneralConf::initSaveAfterCopy() vboxLayout->addLayout(pathLayout); vboxLayout->addWidget(m_screenshotPathFixedCheck); + + QHBoxLayout* extensionLayout = new QHBoxLayout(); + + extensionLayout->addWidget( + new QLabel(tr("Preferred save file extension:"))); + m_setSaveAsFileExtension = new QComboBox(this); + m_setSaveAsFileExtension->addItem(""); + + QStringList imageFormatList; + foreach (auto mimeType, QImageWriter::supportedImageFormats()) + imageFormatList.append(mimeType); + + m_setSaveAsFileExtension->addItems(imageFormatList); + + int currentIndex = m_setSaveAsFileExtension->findText( + ConfigHandler().setSaveAsFileExtension()); + m_setSaveAsFileExtension->setCurrentIndex(currentIndex); + + connect(m_setSaveAsFileExtension, + SIGNAL(currentTextChanged(QString)), + this, + SLOT(setSaveAsFileExtension(QString))); + + extensionLayout->addWidget(m_setSaveAsFileExtension); + vboxLayout->addLayout(extensionLayout); } void GeneralConf::historyConfirmationToDelete(bool checked) @@ -533,6 +561,11 @@ void GeneralConf::togglePathFixed() ConfigHandler().setSavePathFixed(m_screenshotPathFixedCheck->isChecked()); } +void GeneralConf::setSaveAsFileExtension(QString extension) +{ + ConfigHandler().setSaveAsFileExtension(extension); +} + void GeneralConf::useJpgForClipboardChanged(bool checked) { ConfigHandler().setUseJpgForClipboard(checked); diff --git a/src/config/generalconf.h b/src/config/generalconf.h index 9b48e42e..50cc70d4 100644 --- a/src/config/generalconf.h +++ b/src/config/generalconf.h @@ -12,6 +12,7 @@ class QPushButton; class QLabel; class QLineEdit; class QSpinBox; +class QComboBox; class GeneralConf : public QWidget { @@ -39,6 +40,7 @@ private slots: void resetConfiguration(); void togglePathFixed(); void useJpgForClipboardChanged(bool checked); + void setSaveAsFileExtension(QString extension); private: const QString chooseFolder(const QString currentPath = ""); @@ -88,4 +90,5 @@ private: QCheckBox* m_useJpgForClipboard; QSpinBox* m_uploadHistoryMax; QSpinBox* m_undoLimit; + QComboBox* m_setSaveAsFileExtension; }; diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index 36edd41b..7728310c 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -94,6 +94,7 @@ static QMap> OPTION("saveAfterCopy" ,Bool ( false )), OPTION("savePath" ,ExistingDir ( )), OPTION("savePathFixed" ,Bool ( false )), + OPTION("setSaveAsFileExtension" ,SaveFileExtension ( )), OPTION("uploadHistoryMax" ,LowerBoundedInt(0 , 25 )), OPTION("undoLimit" ,BoundedInt(0, 999 , 100 )), // Interface tab @@ -111,7 +112,6 @@ static QMap> OPTION("ignoreUpdateToVersion" ,String ( "" )), OPTION("keepOpenAppLauncher" ,Bool ( false )), OPTION("fontFamily" ,String ( "" )), - OPTION("setSaveAsFileExtension" ,String ( ".png" )), // NOTE: If another tool size is added besides drawThickness and // drawFontSize, remember to update ConfigHandler::toolSize }; @@ -309,13 +309,6 @@ void ConfigHandler::setStartupLaunch(const bool start) #endif } -QString ConfigHandler::saveAsFileExtension() -{ - // TODO If the name of the option changes in the future, remove this - // function and use the macro CONFIG_GETTER_SETTER instead. - return value("setSaveAsFileExtension").toString(); -} - void ConfigHandler::setAllTheButtons() { QList buttons = diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 80459ee9..597a6815 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -88,6 +88,9 @@ public: CONFIG_GETTER_SETTER(uploadHistoryMax, setUploadHistoryMax, int) CONFIG_GETTER_SETTER(saveAfterCopy, setSaveAfterCopy, bool) CONFIG_GETTER_SETTER(copyPathAfterSave, setCopyPathAfterSave, bool) + CONFIG_GETTER_SETTER(setSaveAsFileExtension, + setSaveAsFileExtension, + QString) CONFIG_GETTER_SETTER(antialiasingPinZoom, setAntialiasingPinZoom, bool) CONFIG_GETTER_SETTER(useJpgForClipboard, setUseJpgForClipboard, bool) CONFIG_GETTER_SETTER(ignoreUpdateToVersion, @@ -99,8 +102,6 @@ public: // SPECIAL CASES bool startupLaunch(); void setStartupLaunch(const bool); - QString saveAsFileExtension(); - CONFIG_SETTER(setSaveAsFileExtension, setSaveAsFileExtension, QString) void setAllTheButtons(); void setToolSize(CaptureTool::Type toolType, int size); int toolSize(CaptureTool::Type toolType); diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index c16e5d42..75048075 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -88,7 +88,8 @@ bool ScreenshotSaver::saveToFilesystem(const QPixmap& capture, const QString& path, const QString& messagePrefix) { - QString completePath = FileNameHandler().properScreenshotPath(path); + QString completePath = FileNameHandler().properScreenshotPath( + path, ConfigHandler().setSaveAsFileExtension()); bool ok = capture.save(completePath); QString saveMessage = messagePrefix; QString notificationPath = completePath; @@ -126,9 +127,9 @@ QString ScreenshotSaver::ShowSaveFileDialog(QWidget* parent, mimeTypeList.append(mimeType); dialog.setMimeTypeFilters(mimeTypeList); - QString suffix = ConfigHandler().saveAsFileExtension(); + QString suffix = ConfigHandler().setSaveAsFileExtension(); QString defaultMimeType = - QMimeDatabase().mimeTypeForFile("image" + suffix).name(); + QMimeDatabase().mimeTypeForFile("image." + suffix).name(); dialog.selectMimeTypeFilter(defaultMimeType); if (dialog.exec() == QDialog::Accepted) { @@ -148,7 +149,8 @@ bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture) defaultSavePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); } - QString savePath = FileNameHandler().properScreenshotPath(defaultSavePath); + QString savePath = FileNameHandler().properScreenshotPath( + defaultSavePath, ConfigHandler().setSaveAsFileExtension()); #if defined(Q_OS_MACOS) for (QWidget* widget : qApp->topLevelWidgets()) { QString className(widget->metaObject()->className()); diff --git a/src/utils/valuehandler.cpp b/src/utils/valuehandler.cpp index 83a81b75..9a31d496 100644 --- a/src/utils/valuehandler.cpp +++ b/src/utils/valuehandler.cpp @@ -3,6 +3,7 @@ #include "confighandler.h" #include #include +#include #include #include #include @@ -431,3 +432,40 @@ QString UserColors::expected() { return QStringLiteral("list of colors separated by comma"); } + +// SET SAVE FILE AS EXTENSION + +bool SaveFileExtension::check(const QVariant& val) +{ + if (!val.canConvert(QVariant::String) || val.toString().isEmpty()) + return false; + + QString extension = val.toString(); + + if (extension.startsWith(".")) + extension.remove(0, 1); + + QStringList imageFormatList; + foreach (auto imageFormat, QImageWriter::supportedImageFormats()) + imageFormatList.append(imageFormat); + + if (!imageFormatList.contains(extension)) + return false; + + return true; +} + +QVariant SaveFileExtension::process(const QVariant& val) +{ + QString extension = val.toString(); + + if (extension.startsWith(".")) + extension.remove(0, 1); + + return QVariant::fromValue(extension); +} + +QString SaveFileExtension::expected() +{ + return QStringLiteral("supported image extension"); +} \ No newline at end of file diff --git a/src/utils/valuehandler.h b/src/utils/valuehandler.h index bbae7c1e..d6ad65a1 100644 --- a/src/utils/valuehandler.h +++ b/src/utils/valuehandler.h @@ -198,3 +198,10 @@ class UserColors : public ValueHandler QVariant fallback() override; QString expected() override; }; + +class SaveFileExtension : public ValueHandler +{ + bool check(const QVariant& val) override; + QVariant process(const QVariant& val) override; + QString expected() override; +};