Changeset View
Changeset View
Standalone View
Standalone View
src/Gui/SettingsDialog/SaveOptionsPage.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2019 David Redondo <kde@david-redondo.de> | ||||
2 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | 3 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | ||
3 | * | 4 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | 5 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU Lesser General Public License as published by | 6 | * it under the terms of the GNU Lesser General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | 7 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | 8 | * (at your option) any later version. | ||
8 | * | 9 | * | ||
9 | * This program is distributed in the hope that it will be useful, | 10 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | 13 | * GNU General Public License for more details. | ||
13 | * | 14 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public License | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
15 | * along with this program; if not, write to the Free Software | 16 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 19 | */ | ||
19 | 20 | | |||
20 | #include "SaveOptionsPage.h" | 21 | #include "SaveOptionsPage.h" | ||
21 | 22 | | |||
22 | #include "SpectacleCommon.h" | 23 | #include "SpectacleCommon.h" | ||
23 | #include "SpectacleConfig.h" | | |||
24 | #include "ExportManager.h" | 24 | #include "ExportManager.h" | ||
25 | 25 | | |||
26 | #include <KIOWidgets/KUrlRequester> | 26 | #include <KIOWidgets/KUrlRequester> | ||
27 | #include <KLocalizedString> | 27 | #include <KLocalizedString> | ||
28 | 28 | | |||
29 | #include <QLineEdit> | 29 | #include <QLineEdit> | ||
30 | #include <QLabel> | 30 | #include <QLabel> | ||
31 | #include <QFormLayout> | 31 | #include <QFormLayout> | ||
32 | #include <QHBoxLayout> | 32 | #include <QHBoxLayout> | ||
33 | #include <QComboBox> | 33 | #include <QComboBox> | ||
34 | #include <QImageWriter> | 34 | #include <QImageWriter> | ||
35 | #include <QCheckBox> | 35 | #include <QCheckBox> | ||
36 | 36 | | |||
37 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) : | 37 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) : QWidget(parent) | ||
38 | SettingsPage(parent) | | |||
39 | { | 38 | { | ||
40 | QFormLayout *mainLayout = new QFormLayout; | 39 | QFormLayout *mainLayout = new QFormLayout; | ||
41 | setLayout(mainLayout); | 40 | setLayout(mainLayout); | ||
42 | 41 | | |||
43 | // Save location | 42 | // Save location | ||
44 | mUrlRequester = new KUrlRequester; | 43 | auto urlRequester = new KUrlRequester(this); | ||
45 | mUrlRequester->setMode(KFile::Directory); | 44 | urlRequester->setObjectName(QStringLiteral("kcfg_defaultSaveLocation")); | ||
46 | connect(mUrlRequester, &KUrlRequester::textChanged, this, &SaveOptionsPage::markDirty); | 45 | urlRequester->setMode(KFile::Directory); | ||
47 | mainLayout->addRow(i18n("Save Location:"), mUrlRequester); | 46 | mainLayout->addRow(i18n("Save Location:"), urlRequester); | ||
48 | 47 | | |||
49 | // copy file location to clipboard after saving | 48 | // copy file location to clipboard after saving | ||
50 | mCopyPathToClipboard = new QCheckBox(i18n("Copy file location to clipboard after saving"), this); | 49 | auto copyPathToClipboard = new QCheckBox(i18n("Copy file location to clipboard after saving"), this); | ||
51 | connect(mCopyPathToClipboard, &QCheckBox::toggled, this, &SaveOptionsPage::markDirty); | 50 | copyPathToClipboard->setObjectName(QStringLiteral("kcfg_copySaveLocation")); | ||
52 | mainLayout->addRow(QString(), mCopyPathToClipboard); | 51 | mainLayout->addRow(QString(), copyPathToClipboard); | ||
53 | 52 | | |||
54 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | 53 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | ||
55 | 54 | | |||
56 | // Compression quality slider and current value display | 55 | // Compression quality slider and current value display | ||
57 | QHBoxLayout *sliderHorizLayout = new QHBoxLayout(); | 56 | QHBoxLayout *sliderHorizLayout = new QHBoxLayout(this); | ||
58 | QVBoxLayout *sliderVertLayout = new QVBoxLayout(); | 57 | QVBoxLayout *sliderVertLayout = new QVBoxLayout(this); | ||
59 | 58 | | |||
60 | // Current value | 59 | // Current value | ||
61 | QLabel *qualityValue = new QLabel(); | 60 | QLabel *qualityValue = new QLabel(this); | ||
62 | qualityValue->setNum(SpectacleConfig::instance()->compressionQuality()); | | |||
63 | qualityValue->setMinimumWidth(qualityValue->fontInfo().pointSize()*3); | 61 | qualityValue->setMinimumWidth(qualityValue->fontInfo().pointSize()*3); | ||
64 | 62 | | |||
65 | // Slider | 63 | // Slider | ||
66 | mQualitySlider = new QSlider(Qt::Horizontal); | 64 | auto qualitySlider = new QSlider(Qt::Horizontal, this); | ||
67 | mQualitySlider->setRange(0, 100); | 65 | qualitySlider->setObjectName(QStringLiteral("kcfg_compressionQuality")); | ||
68 | mQualitySlider->setTickInterval(5); | 66 | qualitySlider->setRange(0, 100); | ||
69 | mQualitySlider->setSliderPosition(SpectacleConfig::instance()->compressionQuality()); | 67 | qualitySlider->setTickInterval(5); | ||
70 | mQualitySlider->setTickPosition(QSlider::TicksBelow); | 68 | qualitySlider->setTickPosition(QSlider::TicksBelow); | ||
71 | mQualitySlider->setTracking(true); | 69 | qualitySlider->setTracking(true); | ||
72 | connect(mQualitySlider, &QSlider::valueChanged, this, [=](int value) { | 70 | connect(qualitySlider, &QSlider::valueChanged, qualityValue, qOverload<int>(&QLabel::setNum)); | ||
73 | qualityValue->setNum(value); | | |||
74 | markDirty(); | | |||
75 | }); | | |||
76 | 71 | | |||
77 | sliderHorizLayout->addWidget(mQualitySlider); | 72 | sliderHorizLayout->addWidget(qualitySlider); | ||
78 | sliderHorizLayout->addWidget(qualityValue); | 73 | sliderHorizLayout->addWidget(qualityValue); | ||
79 | 74 | | |||
80 | sliderVertLayout->addLayout(sliderHorizLayout); | 75 | sliderVertLayout->addLayout(sliderHorizLayout); | ||
81 | 76 | | |||
82 | QLabel *qualitySliderDescription = new QLabel(); | 77 | QLabel *qualitySliderDescription = new QLabel(this); | ||
83 | qualitySliderDescription->setText(i18n("Choose the image quality when saving with lossy image formats like JPEG")); | 78 | qualitySliderDescription->setText(i18n("Choose the image quality when saving with lossy image formats like JPEG")); | ||
84 | 79 | | |||
85 | sliderVertLayout->addWidget(qualitySliderDescription); | 80 | sliderVertLayout->addWidget(qualitySliderDescription); | ||
86 | 81 | | |||
87 | mainLayout->addRow(i18n("Compression Quality:"), sliderVertLayout); | 82 | mainLayout->addRow(i18n("Compression Quality:"), sliderVertLayout); | ||
88 | 83 | | |||
89 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | 84 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | ||
90 | 85 | | |||
91 | // filename chooser text field | 86 | // filename chooser text field | ||
92 | QHBoxLayout *saveFieldLayout = new QHBoxLayout; | 87 | QHBoxLayout *saveFieldLayout = new QHBoxLayout(this); | ||
93 | mSaveNameFormat = new QLineEdit; | 88 | mSaveNameFormat = new QLineEdit(this); | ||
94 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, &SaveOptionsPage::markDirty); | 89 | mSaveNameFormat->setObjectName(QStringLiteral("kcfg_saveFilenameFormat")); | ||
90 | | ||||
95 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | 91 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | ||
96 | QString fmt; | 92 | QString fmt; | ||
97 | const auto imageFormats = QImageWriter::supportedImageFormats(); | 93 | const auto imageFormats = QImageWriter::supportedImageFormats(); | ||
98 | for (const auto &item : imageFormats) { | 94 | for (const auto &item : imageFormats) { | ||
99 | fmt = QString::fromLocal8Bit(item); | 95 | fmt = QString::fromLocal8Bit(item); | ||
100 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | 96 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | ||
101 | QString txtCopy = newText; | 97 | QString txtCopy = newText; | ||
102 | txtCopy.chop(fmt.length() + 1); | 98 | txtCopy.chop(fmt.length() + 1); | ||
103 | mSaveNameFormat->setText(txtCopy); | 99 | mSaveNameFormat->setText(txtCopy); | ||
104 | mSaveImageFormat->setCurrentIndex(mSaveImageFormat->findText(fmt.toUpper())); | 100 | mSaveImageFormat->setCurrentIndex(mSaveImageFormat->findText(fmt.toUpper())); | ||
105 | } | 101 | } | ||
106 | } | 102 | } | ||
107 | }); | 103 | }); | ||
108 | connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | 104 | connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | ||
109 | mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); | 105 | mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); | ||
110 | saveFieldLayout->addWidget(mSaveNameFormat); | 106 | saveFieldLayout->addWidget(mSaveNameFormat); | ||
111 | 107 | | |||
112 | mSaveImageFormat = new QComboBox; | 108 | mSaveImageFormat = new QComboBox(this); | ||
109 | mSaveImageFormat->setObjectName(QStringLiteral("kcfg_defaultSaveImageFormat")); | ||||
110 | mSaveImageFormat->setProperty("kcfg_property", QByteArray("currentText")); | ||||
113 | mSaveImageFormat->addItems([&](){ | 111 | mSaveImageFormat->addItems([&](){ | ||
114 | QStringList items; | 112 | QStringList items; | ||
115 | const auto formats = QImageWriter::supportedImageFormats(); | 113 | const auto formats = QImageWriter::supportedImageFormats(); | ||
116 | for (const auto &fmt : formats) { | 114 | for (const auto &fmt : formats) { | ||
117 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | 115 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | ||
118 | } | 116 | } | ||
119 | return items; | 117 | return items; | ||
120 | }()); | 118 | }()); | ||
121 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::markDirty); | | |||
122 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | 119 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | ||
123 | saveFieldLayout->addWidget(mSaveImageFormat); | 120 | saveFieldLayout->addWidget(mSaveImageFormat); | ||
124 | mainLayout->addRow(i18n("Filename:"), saveFieldLayout); | 121 | mainLayout->addRow(i18n("Filename:"), saveFieldLayout); | ||
125 | 122 | | |||
126 | mPreviewLabel = new QLabel(this); | 123 | mPreviewLabel = new QLabel(this); | ||
127 | mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); | 124 | mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); | ||
128 | // now the save filename format layout | 125 | // now the save filename format layout | ||
129 | QString helpText = i18n( | 126 | QString helpText = i18n( | ||
Show All 10 Lines | |||||
140 | QLabel *fmtHelpText = new QLabel(helpText, this); | 137 | QLabel *fmtHelpText = new QLabel(helpText, this); | ||
141 | fmtHelpText->setWordWrap(true); | 138 | fmtHelpText->setWordWrap(true); | ||
142 | fmtHelpText->setTextFormat(Qt::RichText); | 139 | fmtHelpText->setTextFormat(Qt::RichText); | ||
143 | fmtHelpText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | 140 | fmtHelpText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | ||
144 | connect(fmtHelpText, &QLabel::linkActivated, this, [this](const QString& placeholder) { | 141 | connect(fmtHelpText, &QLabel::linkActivated, this, [this](const QString& placeholder) { | ||
145 | mSaveNameFormat->insert(placeholder); | 142 | mSaveNameFormat->insert(placeholder); | ||
146 | }); | 143 | }); | ||
147 | mainLayout->addWidget(fmtHelpText); | 144 | mainLayout->addWidget(fmtHelpText); | ||
148 | | ||||
149 | // read in the data | | |||
150 | resetChanges(); | | |||
151 | } | | |||
152 | | ||||
153 | void SaveOptionsPage::markDirty() | | |||
154 | { | | |||
155 | mChangesMade = true; | | |||
156 | } | | |||
157 | | ||||
158 | void SaveOptionsPage::saveChanges() | | |||
159 | { | | |||
160 | // bring up the configuration reader | | |||
161 | | ||||
162 | SpectacleConfig *cfgManager = SpectacleConfig::instance(); | | |||
163 | | ||||
164 | // save the data | | |||
165 | | ||||
166 | cfgManager->setDefaultSaveLocation(mUrlRequester->url()); | | |||
167 | cfgManager->setAutoSaveFilenameFormat(mSaveNameFormat->text()); | | |||
168 | cfgManager->setSaveImageFormat(mSaveImageFormat->currentText().toLower()); | | |||
169 | cfgManager->setCopySaveLocationToClipboard(mCopyPathToClipboard->checkState() == Qt::Checked); | | |||
170 | cfgManager->setCompressionQuality(mQualitySlider->value()); | | |||
171 | | ||||
172 | // done | | |||
173 | | ||||
174 | mChangesMade = false; | | |||
175 | } | | |||
176 | | ||||
177 | void SaveOptionsPage::resetChanges() | | |||
178 | { | | |||
179 | // bring up the configuration reader | | |||
180 | | ||||
181 | SpectacleConfig *cfgManager = SpectacleConfig::instance(); | | |||
182 | | ||||
183 | // read in the data | | |||
184 | | ||||
185 | mSaveNameFormat->setText(cfgManager->autoSaveFilenameFormat()); | | |||
186 | mUrlRequester->setUrl(cfgManager->defaultSaveLocation()); | | |||
187 | mCopyPathToClipboard->setChecked(cfgManager->copySaveLocationToClipboard()); | | |||
188 | mQualitySlider->setSliderPosition(cfgManager->compressionQuality()); | | |||
189 | | ||||
190 | // read in the save image format and calculate its index | | |||
191 | | ||||
192 | { | | |||
193 | int index = mSaveImageFormat->findText(cfgManager->saveImageFormat().toUpper()); | | |||
194 | if (index >= 0) { | | |||
195 | mSaveImageFormat->setCurrentIndex(index); | | |||
196 | } | | |||
197 | } | | |||
198 | | ||||
199 | // done | | |||
200 | | ||||
201 | mChangesMade = false; | | |||
202 | } | 145 | } | ||
203 | 146 | | |||
204 | void SaveOptionsPage::updateFilenamePreview() | 147 | void SaveOptionsPage::updateFilenamePreview() | ||
205 | { | 148 | { | ||
206 | auto lExportManager = ExportManager::instance(); | 149 | auto lExportManager = ExportManager::instance(); | ||
207 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | 150 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | ||
208 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | 151 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | ||
209 | 152 | | |||
Show All 14 Lines |