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 <QSpinBox> | 31 | #include <QSpinBox> | ||
32 | #include <QFormLayout> | 32 | #include <QFormLayout> | ||
33 | #include <QHBoxLayout> | 33 | #include <QHBoxLayout> | ||
34 | #include <QComboBox> | 34 | #include <QComboBox> | ||
35 | #include <QImageWriter> | 35 | #include <QImageWriter> | ||
36 | #include <QCheckBox> | 36 | #include <QCheckBox> | ||
37 | 37 | | |||
38 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) : | 38 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) : QWidget(parent) | ||
39 | SettingsPage(parent) | | |||
40 | { | 39 | { | ||
41 | QFormLayout *mainLayout = new QFormLayout; | 40 | QFormLayout *mainLayout = new QFormLayout; | ||
42 | setLayout(mainLayout); | 41 | setLayout(mainLayout); | ||
43 | 42 | | |||
44 | // Save location | 43 | // Save location | ||
45 | mUrlRequester = new KUrlRequester; | 44 | auto urlRequester = new KUrlRequester(this); | ||
46 | mUrlRequester->setMode(KFile::Directory); | 45 | urlRequester->setObjectName(QStringLiteral("kcfg_defaultSaveLocation")); | ||
47 | connect(mUrlRequester, &KUrlRequester::textChanged, this, &SaveOptionsPage::markDirty); | 46 | urlRequester->setMode(KFile::Directory); | ||
48 | mainLayout->addRow(i18n("Save Location:"), mUrlRequester); | 47 | mainLayout->addRow(i18n("Save Location:"), urlRequester); | ||
49 | 48 | | |||
50 | // copy file location to clipboard after saving | 49 | // copy file location to clipboard after saving | ||
51 | mCopyPathToClipboard = new QCheckBox(i18n("Copy file location to clipboard after saving"), this); | 50 | auto copyPathToClipboard = new QCheckBox(i18n("Copy file location to clipboard after saving"), this); | ||
52 | connect(mCopyPathToClipboard, &QCheckBox::toggled, this, &SaveOptionsPage::markDirty); | 51 | copyPathToClipboard->setObjectName(QStringLiteral("kcfg_copySaveLocation")); | ||
53 | mainLayout->addRow(QString(), mCopyPathToClipboard); | 52 | mainLayout->addRow(QString(), copyPathToClipboard); | ||
54 | 53 | | |||
55 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | 54 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | ||
56 | 55 | | |||
57 | // Compression quality slider and current value display | 56 | // Compression quality slider and current value display | ||
58 | QHBoxLayout *sliderHorizLayout = new QHBoxLayout(); | 57 | QHBoxLayout *sliderHorizLayout = new QHBoxLayout(this); | ||
59 | QVBoxLayout *sliderVertLayout = new QVBoxLayout(); | 58 | QVBoxLayout *sliderVertLayout = new QVBoxLayout(this); | ||
60 | 59 | | |||
61 | // Current value | 60 | // Current value | ||
62 | QSpinBox *mQualitySpinner = new QSpinBox(); | 61 | auto qualitySpinner = new QSpinBox(this); | ||
63 | mQualitySpinner->setSuffix(QString::fromUtf8("%")); | 62 | qualitySpinner->setSuffix(QString::fromUtf8("%")); | ||
64 | mQualitySpinner->setRange(0, 100); | 63 | qualitySpinner->setRange(0, 100); | ||
65 | mQualitySpinner->setValue(SpectacleConfig::instance()->compressionQuality()); | 64 | qualitySpinner->setObjectName(QStringLiteral("kcfg_compressionQuality")); | ||
66 | connect(mQualitySpinner, QOverload<int>::of(&QSpinBox::valueChanged), this, [=] (int value) {mQualitySlider->setValue(value);}); | | |||
67 | 65 | | |||
68 | // Slider | 66 | // Slider | ||
69 | mQualitySlider = new QSlider(Qt::Horizontal); | 67 | auto qualitySlider = new QSlider(Qt::Horizontal, this); | ||
70 | mQualitySlider->setRange(0, 100); | 68 | qualitySlider->setRange(0, 100); | ||
71 | mQualitySlider->setSliderPosition(SpectacleConfig::instance()->compressionQuality()); | 69 | qualitySlider->setSliderPosition(qualitySpinner->value()); | ||
72 | mQualitySlider->setTracking(true); | 70 | qualitySlider->setTracking(true); | ||
73 | connect(mQualitySlider, &QSlider::valueChanged, this, [=](int value) { | 71 | connect(qualitySlider, &QSlider::valueChanged, this, [=](int value) { | ||
74 | mQualitySpinner->setValue(value); | 72 | qualitySpinner->setValue(value); | ||
75 | markDirty(); | | |||
76 | }); | 73 | }); | ||
77 | 74 | connect(qualitySpinner, QOverload<int>::of(&QSpinBox::valueChanged), this, [=] (int value) {qualitySlider->setValue(value);}); | |||
78 | sliderHorizLayout->addWidget(mQualitySlider); | 75 | sliderHorizLayout->addWidget(qualitySlider); | ||
79 | sliderHorizLayout->addWidget(mQualitySpinner); | 76 | sliderHorizLayout->addWidget(qualitySpinner); | ||
80 | 77 | | |||
81 | sliderVertLayout->addLayout(sliderHorizLayout); | 78 | sliderVertLayout->addLayout(sliderHorizLayout); | ||
82 | 79 | | |||
83 | QLabel *qualitySliderDescription = new QLabel(); | 80 | QLabel *qualitySliderDescription = new QLabel(this); | ||
84 | qualitySliderDescription->setText(i18n("Choose the image quality when saving with lossy image formats like JPEG")); | 81 | qualitySliderDescription->setText(i18n("Choose the image quality when saving with lossy image formats like JPEG")); | ||
85 | 82 | | |||
86 | sliderVertLayout->addWidget(qualitySliderDescription); | 83 | sliderVertLayout->addWidget(qualitySliderDescription); | ||
87 | 84 | | |||
88 | mainLayout->addRow(i18n("Compression Quality:"), sliderVertLayout); | 85 | mainLayout->addRow(i18n("Compression Quality:"), sliderVertLayout); | ||
89 | 86 | | |||
90 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | 87 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | ||
91 | 88 | | |||
92 | // filename chooser text field | 89 | // filename chooser text field | ||
93 | QHBoxLayout *saveFieldLayout = new QHBoxLayout; | 90 | QHBoxLayout *saveFieldLayout = new QHBoxLayout(this); | ||
94 | mSaveNameFormat = new QLineEdit; | 91 | mSaveNameFormat = new QLineEdit(this); | ||
95 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, &SaveOptionsPage::markDirty); | 92 | mSaveNameFormat->setObjectName(QStringLiteral("kcfg_saveFilenameFormat")); | ||
93 | | ||||
96 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | 94 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | ||
97 | QString fmt; | 95 | QString fmt; | ||
98 | const auto imageFormats = QImageWriter::supportedImageFormats(); | 96 | const auto imageFormats = QImageWriter::supportedImageFormats(); | ||
99 | for (const auto &item : imageFormats) { | 97 | for (const auto &item : imageFormats) { | ||
100 | fmt = QString::fromLocal8Bit(item); | 98 | fmt = QString::fromLocal8Bit(item); | ||
101 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | 99 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | ||
102 | QString txtCopy = newText; | 100 | QString txtCopy = newText; | ||
103 | txtCopy.chop(fmt.length() + 1); | 101 | txtCopy.chop(fmt.length() + 1); | ||
104 | mSaveNameFormat->setText(txtCopy); | 102 | mSaveNameFormat->setText(txtCopy); | ||
105 | mSaveImageFormat->setCurrentIndex(mSaveImageFormat->findText(fmt.toUpper())); | 103 | mSaveImageFormat->setCurrentIndex(mSaveImageFormat->findText(fmt.toUpper())); | ||
106 | } | 104 | } | ||
107 | } | 105 | } | ||
108 | }); | 106 | }); | ||
109 | connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | 107 | connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | ||
110 | mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); | 108 | mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); | ||
111 | saveFieldLayout->addWidget(mSaveNameFormat); | 109 | saveFieldLayout->addWidget(mSaveNameFormat); | ||
112 | 110 | | |||
113 | mSaveImageFormat = new QComboBox; | 111 | mSaveImageFormat = new QComboBox(this); | ||
112 | mSaveImageFormat->setObjectName(QStringLiteral("kcfg_defaultSaveImageFormat")); | ||||
113 | mSaveImageFormat->setProperty("kcfg_property", QByteArray("currentText")); | ||||
114 | mSaveImageFormat->addItems([&](){ | 114 | mSaveImageFormat->addItems([&](){ | ||
115 | QStringList items; | 115 | QStringList items; | ||
116 | const auto formats = QImageWriter::supportedImageFormats(); | 116 | const auto formats = QImageWriter::supportedImageFormats(); | ||
117 | for (const auto &fmt : formats) { | 117 | for (const auto &fmt : formats) { | ||
118 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | 118 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | ||
119 | } | 119 | } | ||
120 | return items; | 120 | return items; | ||
121 | }()); | 121 | }()); | ||
122 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::markDirty); | | |||
123 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | 122 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | ||
124 | saveFieldLayout->addWidget(mSaveImageFormat); | 123 | saveFieldLayout->addWidget(mSaveImageFormat); | ||
125 | mainLayout->addRow(i18n("Filename:"), saveFieldLayout); | 124 | mainLayout->addRow(i18n("Filename:"), saveFieldLayout); | ||
126 | 125 | | |||
127 | mPreviewLabel = new QLabel(this); | 126 | mPreviewLabel = new QLabel(this); | ||
128 | mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); | 127 | mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); | ||
129 | // now the save filename format layout | 128 | // now the save filename format layout | ||
130 | QString helpText = i18n( | 129 | QString helpText = i18n( | ||
Show All 10 Lines | |||||
141 | QLabel *fmtHelpText = new QLabel(helpText, this); | 140 | QLabel *fmtHelpText = new QLabel(helpText, this); | ||
142 | fmtHelpText->setWordWrap(true); | 141 | fmtHelpText->setWordWrap(true); | ||
143 | fmtHelpText->setTextFormat(Qt::RichText); | 142 | fmtHelpText->setTextFormat(Qt::RichText); | ||
144 | fmtHelpText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | 143 | fmtHelpText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | ||
145 | connect(fmtHelpText, &QLabel::linkActivated, this, [this](const QString& placeholder) { | 144 | connect(fmtHelpText, &QLabel::linkActivated, this, [this](const QString& placeholder) { | ||
146 | mSaveNameFormat->insert(placeholder); | 145 | mSaveNameFormat->insert(placeholder); | ||
147 | }); | 146 | }); | ||
148 | mainLayout->addWidget(fmtHelpText); | 147 | mainLayout->addWidget(fmtHelpText); | ||
149 | | ||||
150 | // read in the data | | |||
151 | resetChanges(); | | |||
152 | } | | |||
153 | | ||||
154 | void SaveOptionsPage::markDirty() | | |||
155 | { | | |||
156 | mChangesMade = true; | | |||
157 | } | | |||
158 | | ||||
159 | void SaveOptionsPage::saveChanges() | | |||
160 | { | | |||
161 | // bring up the configuration reader | | |||
162 | | ||||
163 | SpectacleConfig *cfgManager = SpectacleConfig::instance(); | | |||
164 | | ||||
165 | // save the data | | |||
166 | | ||||
167 | cfgManager->setDefaultSaveLocation(mUrlRequester->url()); | | |||
168 | cfgManager->setAutoSaveFilenameFormat(mSaveNameFormat->text()); | | |||
169 | cfgManager->setSaveImageFormat(mSaveImageFormat->currentText().toLower()); | | |||
170 | cfgManager->setCopySaveLocationToClipboard(mCopyPathToClipboard->checkState() == Qt::Checked); | | |||
171 | cfgManager->setCompressionQuality(mQualitySlider->value()); | | |||
172 | | ||||
173 | // done | | |||
174 | | ||||
175 | mChangesMade = false; | | |||
176 | } | | |||
177 | | ||||
178 | void SaveOptionsPage::resetChanges() | | |||
179 | { | | |||
180 | // bring up the configuration reader | | |||
181 | | ||||
182 | SpectacleConfig *cfgManager = SpectacleConfig::instance(); | | |||
183 | | ||||
184 | // read in the data | | |||
185 | | ||||
186 | mSaveNameFormat->setText(cfgManager->autoSaveFilenameFormat()); | | |||
187 | mUrlRequester->setUrl(cfgManager->defaultSaveLocation()); | | |||
188 | mCopyPathToClipboard->setChecked(cfgManager->copySaveLocationToClipboard()); | | |||
189 | mQualitySlider->setSliderPosition(cfgManager->compressionQuality()); | | |||
190 | | ||||
191 | // read in the save image format and calculate its index | | |||
192 | | ||||
193 | { | | |||
194 | int index = mSaveImageFormat->findText(cfgManager->saveImageFormat().toUpper()); | | |||
195 | if (index >= 0) { | | |||
196 | mSaveImageFormat->setCurrentIndex(index); | | |||
197 | } | | |||
198 | } | | |||
199 | | ||||
200 | // done | | |||
201 | | ||||
202 | mChangesMade = false; | | |||
203 | } | 148 | } | ||
204 | 149 | | |||
205 | void SaveOptionsPage::updateFilenamePreview() | 150 | void SaveOptionsPage::updateFilenamePreview() | ||
206 | { | 151 | { | ||
207 | auto lExportManager = ExportManager::instance(); | 152 | auto lExportManager = ExportManager::instance(); | ||
208 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | 153 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | ||
209 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | 154 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | ||
210 | 155 | | |||
Show All 14 Lines |