Add config for jpg/png when copy

This commit is contained in:
Sonu Lohani
2021-01-30 10:01:14 +05:30
committed by borgmanJeremy
parent b121c6c72b
commit a944860d12
5 changed files with 61 additions and 12 deletions

View File

@@ -39,6 +39,7 @@ GeneneralConf::GeneneralConf(QWidget* parent)
initShowDesktopNotification();
initShowTrayIcon();
initAutostart();
initUseJpgInsteadPngWhenCopy();
initSaveAfterCopy();
// this has to be at the end
@@ -54,6 +55,7 @@ void GeneneralConf::updateComponents()
m_sysNotifications->setChecked(config.desktopNotificationValue());
m_autostart->setChecked(config.startupLaunchValue());
m_saveAfterCopy->setChecked(config.saveAfterCopyValue());
m_useJpgInsteadPngCheck->setChecked(config.useJpgInsteadPngWhenCopy());
if (!config.saveAfterCopyPathValue().isEmpty()) {
m_savePath->setText(config.saveAfterCopyPathValue());
@@ -304,6 +306,23 @@ void GeneneralConf::initSaveAfterCopy()
vboxLayout->addWidget(m_screenshotPathFixedCheck);
}
void GeneneralConf::initUseJpgInsteadPngWhenCopy()
{
m_useJpgInsteadPngCheck =
new QCheckBox(tr("Use JPG format instead of PNG when copy"), this);
ConfigHandler config;
bool checked = config.useJpgInsteadPngWhenCopy();
m_useJpgInsteadPngCheck->setChecked(checked);
m_useJpgInsteadPngCheck->setToolTip(
tr("Use JPG format instead of PNG when copy"));
m_layout->addWidget(m_useJpgInsteadPngCheck);
connect(m_useJpgInsteadPngCheck,
&QCheckBox::clicked,
this,
&GeneneralConf::useJpgInsteadPngChanged);
}
void GeneneralConf::saveAfterCopyChanged(bool checked)
{
ConfigHandler().setSaveAfterCopy(checked);
@@ -348,3 +367,8 @@ void GeneneralConf::togglePathFixed()
{
ConfigHandler().setSavePathFixed(m_screenshotPathFixedCheck->isChecked());
}
void GeneneralConf::useJpgInsteadPngChanged(bool checked)
{
ConfigHandler().setUseJpgInsteadPngWhenCopy(checked);
}

View File

@@ -46,6 +46,7 @@ private slots:
void exportFileConfiguration();
void resetConfiguration();
void togglePathFixed();
void useJpgInsteadPngChanged(bool checked);
private:
const QString chooseFolder(const QString currentPath = "");
@@ -57,6 +58,7 @@ private:
void initConfingButtons();
void initAutostart();
void initSaveAfterCopy();
void initUseJpgInsteadPngWhenCopy();
// class members
QVBoxLayout* m_layout;
@@ -72,4 +74,5 @@ private:
QLineEdit* m_savePath;
QPushButton* m_changeSaveButton;
QCheckBox* m_screenshotPathFixedCheck;
QCheckBox* m_useJpgInsteadPngCheck;
};

View File

@@ -431,6 +431,20 @@ void ConfigHandler::setCopyPathAfterSaveEnabled(const bool value)
m_settings.setValue(QStringLiteral("copyPathAfterSave"), value);
}
bool ConfigHandler::useJpgInsteadPngWhenCopy() const
{
if (m_settings.contains(QStringLiteral("useJpgInsteadPngWhenCopy"))) {
return m_settings.value(QStringLiteral("useJpgInsteadPngWhenCopy"))
.toBool();
}
return false;
}
void ConfigHandler::setUseJpgInsteadPngWhenCopy(const bool value)
{
m_settings.setValue(QStringLiteral("useJpgInsteadPngWhenCopy"), value);
}
QString ConfigHandler::saveAfterCopyPathValue()
{
return m_settings.value(QStringLiteral("saveAfterCopyPath")).toString();

View File

@@ -86,6 +86,9 @@ public:
bool copyPathAfterSaveEnabled();
void setCopyPathAfterSaveEnabled(const bool);
bool useJpgInsteadPngWhenCopy() const;
void setUseJpgInsteadPngWhenCopy(const bool);
void setDefaults();
void setAllTheButtons();

View File

@@ -51,22 +51,27 @@ void ScreenshotSaver::saveToClipboard(const QPixmap& capture)
}
// Otherwise only save to clipboard
else {
SystemNotification().sendMessage(
QObject::tr("Capture saved to clipboard"));
if (ConfigHandler().useJpgInsteadPngWhenCopy()) {
QByteArray array;
QBuffer buffer{ &array };
QImageWriter imageWriter{ &buffer, "JPG" };
imageWriter.write(capture.toImage());
QByteArray array;
QBuffer buffer{ &array };
QImageWriter imageWriter{ &buffer, "JPG" };
imageWriter.write(capture.toImage());
QPixmap jpgPixmap;
bool isLoaded = jpgPixmap.loadFromData(
reinterpret_cast<uchar*>(array.data()), array.size(), "JPG");
if (isLoaded) {
QApplication::clipboard()->setPixmap(jpgPixmap);
QPixmap jpgPixmap;
bool isLoaded = jpgPixmap.loadFromData(
reinterpret_cast<uchar*>(array.data()), array.size(), "JPG");
if (isLoaded) {
QApplication::clipboard()->setPixmap(jpgPixmap);
} else {
SystemNotification().sendMessage(
QObject::tr("Error while saving to clipboard"));
return;
}
} else {
QApplication::clipboard()->setPixmap(capture);
}
SystemNotification().sendMessage(
QObject::tr("Capture saved to clipboard"));
}
}