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 JPEG Quality Option #3285

Merged
merged 10 commits into from
Sep 16, 2023
26 changes: 26 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GeneralConf::GeneralConf(QWidget* parent)

initShowMagnifier();
initSquareMagnifier();
initJpegQuality();
// this has to be at the end
initConfigButtons();
updateComponents();
Expand Down Expand Up @@ -777,11 +778,36 @@ void GeneralConf::initShowSelectionGeometry()
vboxLayout->addStretch();
}

void GeneralConf::initJpegQuality()
{
auto* tobox = new QHBoxLayout();

int quality = ConfigHandler().value("jpegQuality").toInt();
m_jpegQuality = new QSpinBox();
m_jpegQuality->setRange(0, 100);
m_jpegQuality->setToolTip(tr("Quality range of 0-100; Higher number is "
"better quality and larger file size"));
m_jpegQuality->setValue(quality);
tobox->addWidget(m_jpegQuality);
tobox->addWidget(new QLabel(tr("JPEG Quality")));

m_scrollAreaLayout->addLayout(tobox);
connect(m_jpegQuality,
static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
this,
&GeneralConf::setJpegQuality);
}

void GeneralConf::setSelGeoHideTime(int v)
{
ConfigHandler().setValue("showSelectionGeometryHideTime", v);
}

void GeneralConf::setJpegQuality(int v)
{
ConfigHandler().setValue("jpegQuality", v);
veracioux marked this conversation as resolved.
Show resolved Hide resolved
}

void GeneralConf::setGeometryLocation(int index)
{
ConfigHandler().setValue("showSelectionGeometry",
veracioux marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 3 additions & 0 deletions src/config/generalconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private slots:
void setSaveAsFileExtension(const QString& extension);
void setGeometryLocation(int index);
void setSelGeoHideTime(int v);
void setJpegQuality(int v);

private:
const QString chooseFolder(const QString& currentPath = "");
Expand Down Expand Up @@ -90,6 +91,7 @@ private slots:
void initUploadClientSecret();
void initSaveLastRegion();
void initShowSelectionGeometry();
void initJpegQuality();

void _updateComponents(bool allowEmptySavePath);

Expand Down Expand Up @@ -133,4 +135,5 @@ private slots:
QCheckBox* m_showSelectionGeometry;
QComboBox* m_selectGeometryLocation;
QSpinBox* m_xywhTimeout;
QSpinBox* m_jpegQuality;
};
3 changes: 2 additions & 1 deletion src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
OPTION("copyOnDoubleClick" ,Bool ( false )),
OPTION("uploadClientSecret" ,String ( "313baf0c7b4d3ff" )),
OPTION("showSelectionGeometry" , BoundedInt (0,5,4)),
OPTION("showSelectionGeometryHideTime", LowerBoundedInt (0, 3000))
OPTION("showSelectionGeometryHideTime", LowerBoundedInt (0, 3000)),
OPTION("jpegQuality", BoundedInt (0,100,75))
veracioux marked this conversation as resolved.
Show resolved Hide resolved
};

static QMap<QString, QSharedPointer<KeySequence>> recognizedShortcuts = {
Expand Down
1 change: 1 addition & 0 deletions src/utils/confighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class ConfigHandler : public QObject
CONFIG_GETTER_SETTER(uploadClientSecret, setUploadClientSecret, QString)
CONFIG_GETTER_SETTER(saveLastRegion, setSaveLastRegion, bool)
CONFIG_GETTER_SETTER(showSelectionGeometry, setShowSelectionGeometry, int)
CONFIG_GETTER_SETTER(jpegQuality, setjpegQuality, int)
veracioux marked this conversation as resolved.
Show resolved Hide resolved
// SPECIAL CASES
bool startupLaunch();
void setStartupLaunch(const bool);
Expand Down
22 changes: 20 additions & 2 deletions src/utils/screenshotsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ bool saveToFilesystem(const QPixmap& capture,
path, ConfigHandler().saveAsFileExtension());
QFile file{ completePath };
file.open(QIODevice::WriteOnly);
bool okay = capture.save(&file);

bool okay;
QString saveExtension;
saveExtension = QFileInfo(completePath).suffix().toLower();
if (saveExtension == "jpg" || saveExtension == "jpeg") {
okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality());
} else {
okay = capture.save(&file);
}

QString saveMessage = messagePrefix;
QString notificationPath = completePath;
if (!saveMessage.isEmpty()) {
Expand Down Expand Up @@ -97,6 +106,9 @@ void saveToClipboardMime(const QPixmap& capture, const QString& imageType)
QByteArray array;
QBuffer buffer{ &array };
QImageWriter imageWriter{ &buffer, imageType.toUpper().toUtf8() };
if (imageType == "jpeg") {
imageWriter.setQuality(ConfigHandler().jpegQuality());
}
imageWriter.write(capture.toImage());

QPixmap formattedPixmap;
Expand Down Expand Up @@ -189,7 +201,13 @@ bool saveToFilesystemGUI(const QPixmap& capture)
QFile file{ savePath };
file.open(QIODevice::WriteOnly);

okay = capture.save(&file);
QString saveExtension;
saveExtension = QFileInfo(savePath).suffix().toLower();
if (saveExtension == "jpg" || saveExtension == "jpeg") {
okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality());
} else {
okay = capture.save(&file);
}

if (okay) {
QString pathNoFile =
Expand Down
Loading