Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default file extension configuration #1995

Merged
merged 9 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include "src/core/controller.h"
#include "src/utils/confighandler.h"
#include <QCheckBox>
#include <QComboBox>
#include <QFile>
#include <QFileDialog>
#include <QGroupBox>
#include <QImageWriter>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/config/generalconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class QPushButton;
class QLabel;
class QLineEdit;
class QSpinBox;
class QComboBox;

class GeneralConf : public QWidget
{
Expand Down Expand Up @@ -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 = "");
Expand Down Expand Up @@ -88,4 +90,5 @@ private slots:
QCheckBox* m_useJpgForClipboard;
QSpinBox* m_uploadHistoryMax;
QSpinBox* m_undoLimit;
QComboBox* m_setSaveAsFileExtension;
};
9 changes: 1 addition & 8 deletions src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
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
Expand All @@ -111,7 +112,6 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
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
};
Expand Down Expand Up @@ -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<CaptureTool::Type> buttons =
Expand Down
5 changes: 3 additions & 2 deletions src/utils/confighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class ConfigHandler : public QObject
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,
Expand All @@ -99,8 +102,6 @@ class ConfigHandler : public QObject
// 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);
Expand Down
10 changes: 6 additions & 4 deletions src/utils/screenshotsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
38 changes: 38 additions & 0 deletions src/utils/valuehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "confighandler.h"
#include <QColor>
#include <QFileInfo>
#include <QImageWriter>
#include <QKeySequence>
#include <QStandardPaths>
#include <QVariant>
Expand Down Expand Up @@ -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");
}
7 changes: 7 additions & 0 deletions src/utils/valuehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};