Changeset View
Changeset View
Standalone View
Standalone View
src/Gui/SettingsDialog/SaveOptionsPage.cpp
Show All 16 Lines | |||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "SaveOptionsPage.h" | 21 | #include "SaveOptionsPage.h" | ||
22 | 22 | | |||
23 | #include "SpectacleCommon.h" | 23 | #include "SpectacleCommon.h" | ||
24 | #include "ExportManager.h" | 24 | #include "ExportManager.h" | ||
25 | #include "ui_SaveOptions.h" | ||||
25 | 26 | | |||
26 | #include <KIOWidgets/KUrlRequester> | 27 | #include <KIOWidgets/KUrlRequester> | ||
27 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
28 | 29 | | |||
29 | #include <QLineEdit> | 30 | #include <QLineEdit> | ||
30 | #include <QLabel> | 31 | #include <QLabel> | ||
31 | #include <QSpinBox> | 32 | #include <QSpinBox> | ||
32 | #include <QFormLayout> | 33 | #include <QFormLayout> | ||
33 | #include <QHBoxLayout> | 34 | #include <QHBoxLayout> | ||
34 | #include <QComboBox> | 35 | #include <QComboBox> | ||
35 | #include <QImageWriter> | 36 | #include <QImageWriter> | ||
36 | #include <QCheckBox> | 37 | #include <QCheckBox> | ||
37 | 38 | | |||
38 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) : QWidget(parent) | 39 | SaveOptionsPage::SaveOptionsPage(QWidget *parent) | ||
40 | : QWidget(parent) | ||||
41 | , m_ui(new Ui_SaveOptions) | ||||
39 | { | 42 | { | ||
40 | QFormLayout *mainLayout = new QFormLayout; | 43 | m_ui->setupUi(this); | ||
41 | setLayout(mainLayout); | | |||
42 | 44 | | |||
43 | // Save location | 45 | connect(m_ui->kcfg_saveFilenameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | ||
44 | auto urlRequester = new KUrlRequester(this); | | |||
45 | urlRequester->setObjectName(QStringLiteral("kcfg_defaultSaveLocation")); | | |||
46 | urlRequester->setMode(KFile::Directory); | | |||
47 | mainLayout->addRow(i18n("Save Location:"), urlRequester); | | |||
48 | | ||||
49 | // copy file location to clipboard after saving | | |||
50 | auto copyPathToClipboard = new QCheckBox(i18n("Copy file location to clipboard after saving"), this); | | |||
51 | copyPathToClipboard->setObjectName(QStringLiteral("kcfg_copySaveLocation")); | | |||
52 | mainLayout->addRow(QString(), copyPathToClipboard); | | |||
53 | | ||||
54 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | | |||
55 | | ||||
56 | // Compression quality slider and current value display | | |||
57 | QHBoxLayout *sliderHorizLayout = new QHBoxLayout(this); | | |||
58 | QVBoxLayout *sliderVertLayout = new QVBoxLayout(this); | | |||
59 | | ||||
60 | // Current value | | |||
61 | auto qualitySpinner = new QSpinBox(this); | | |||
62 | qualitySpinner->setSuffix(QString::fromUtf8("%")); | | |||
63 | qualitySpinner->setRange(0, 100); | | |||
64 | qualitySpinner->setObjectName(QStringLiteral("kcfg_compressionQuality")); | | |||
65 | | ||||
66 | // Slider | | |||
67 | auto qualitySlider = new QSlider(Qt::Horizontal, this); | | |||
68 | qualitySlider->setRange(0, 100); | | |||
69 | qualitySlider->setSliderPosition(qualitySpinner->value()); | | |||
70 | qualitySlider->setTracking(true); | | |||
71 | connect(qualitySlider, &QSlider::valueChanged, this, [=](int value) { | | |||
72 | qualitySpinner->setValue(value); | | |||
73 | }); | | |||
74 | connect(qualitySpinner, QOverload<int>::of(&QSpinBox::valueChanged), this, [=] (int value) {qualitySlider->setValue(value);}); | | |||
75 | sliderHorizLayout->addWidget(qualitySlider); | | |||
76 | sliderHorizLayout->addWidget(qualitySpinner); | | |||
77 | | ||||
78 | sliderVertLayout->addLayout(sliderHorizLayout); | | |||
79 | | ||||
80 | QLabel *qualitySliderDescription = new QLabel(this); | | |||
81 | qualitySliderDescription->setText(i18n("Choose the image quality when saving with lossy image formats like JPEG")); | | |||
82 | | ||||
83 | sliderVertLayout->addWidget(qualitySliderDescription); | | |||
84 | | ||||
85 | mainLayout->addRow(i18n("Compression Quality:"), sliderVertLayout); | | |||
86 | | ||||
87 | mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); | | |||
88 | | ||||
89 | // filename chooser text field | | |||
90 | QHBoxLayout *saveFieldLayout = new QHBoxLayout(this); | | |||
91 | mSaveNameFormat = new QLineEdit(this); | | |||
92 | mSaveNameFormat->setObjectName(QStringLiteral("kcfg_saveFilenameFormat")); | | |||
93 | | ||||
94 | connect(mSaveNameFormat, &QLineEdit::textEdited, this, [&](const QString &newText) { | | |||
95 | QString fmt; | 46 | QString fmt; | ||
96 | const auto imageFormats = QImageWriter::supportedImageFormats(); | 47 | const auto imageFormats = QImageWriter::supportedImageFormats(); | ||
97 | for (const auto &item : imageFormats) { | 48 | for (const auto &item : imageFormats) { | ||
98 | fmt = QString::fromLocal8Bit(item); | 49 | fmt = QString::fromLocal8Bit(item); | ||
99 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | 50 | if (newText.endsWith(QLatin1Char('.') + fmt, Qt::CaseInsensitive)) { | ||
100 | QString txtCopy = newText; | 51 | QString txtCopy = newText; | ||
101 | txtCopy.chop(fmt.length() + 1); | 52 | txtCopy.chop(fmt.length() + 1); | ||
102 | mSaveNameFormat->setText(txtCopy); | 53 | m_ui->kcfg_saveFilenameFormat->setText(txtCopy); | ||
103 | mSaveImageFormat->setCurrentIndex(mSaveImageFormat->findText(fmt.toUpper())); | 54 | m_ui->kcfg_defaultSaveImageFormat->setCurrentIndex(m_ui->kcfg_defaultSaveImageFormat->findText(fmt.toUpper())); | ||
104 | } | 55 | } | ||
105 | } | 56 | } | ||
106 | }); | 57 | }); | ||
107 | connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | 58 | connect(m_ui->kcfg_saveFilenameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); | ||
108 | mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); | 59 | | ||
109 | saveFieldLayout->addWidget(mSaveNameFormat); | 60 | m_ui->kcfg_defaultSaveImageFormat->addItems([&](){ | ||
110 | | ||||
111 | mSaveImageFormat = new QComboBox(this); | | |||
112 | mSaveImageFormat->setObjectName(QStringLiteral("kcfg_defaultSaveImageFormat")); | | |||
113 | mSaveImageFormat->setProperty("kcfg_property", QByteArray("currentText")); | | |||
114 | mSaveImageFormat->addItems([&](){ | | |||
115 | QStringList items; | 61 | QStringList items; | ||
116 | const auto formats = QImageWriter::supportedImageFormats(); | 62 | const auto formats = QImageWriter::supportedImageFormats(); | ||
117 | for (const auto &fmt : formats) { | 63 | for (const auto &fmt : formats) { | ||
118 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | 64 | items.append(QString::fromLocal8Bit(fmt).toUpper()); | ||
119 | } | 65 | } | ||
120 | return items; | 66 | return items; | ||
121 | }()); | 67 | }()); | ||
122 | connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | 68 | connect( m_ui->kcfg_defaultSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); | ||
ervin: There is an extra space after the opening parenthesis which probably shouldn't be here | |||||
123 | saveFieldLayout->addWidget(mSaveImageFormat); | 69 | | ||
124 | mainLayout->addRow(i18n("Filename:"), saveFieldLayout); | | |||
125 | | ||||
126 | mPreviewLabel = new QLabel(this); | | |||
127 | mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); | | |||
128 | // now the save filename format layout | | |||
129 | QString helpText = i18n( | 70 | QString helpText = i18n( | ||
130 | "You can use the following placeholders in the filename, which will be replaced " | 71 | "You can use the following placeholders in the filename, which will be replaced " | ||
131 | "with actual text when the file is saved:<blockquote>" | 72 | "with actual text when the file is saved:<blockquote>" | ||
132 | ); | 73 | ); | ||
133 | for (auto option = ExportManager::filenamePlaceholders.cbegin(); | 74 | for (auto option = ExportManager::filenamePlaceholders.cbegin(); | ||
134 | option != ExportManager::filenamePlaceholders.cend(); ++option) { | 75 | option != ExportManager::filenamePlaceholders.cend(); ++option) { | ||
135 | helpText += QStringLiteral("<a href=%1>%1</a>: %2<br>").arg(option.key(), | 76 | helpText += QStringLiteral("<a href=%1>%1</a>: %2<br>").arg(option.key(), | ||
136 | option.value().toString()); | 77 | option.value().toString()); | ||
137 | } | 78 | } | ||
138 | helpText += QLatin1String("<a href='/'>/</a>: ") + i18n("To save to a sub-folder"); | 79 | helpText += QLatin1String("<a href='/'>/</a>: ") + i18n("To save to a sub-folder"); | ||
139 | helpText += QStringLiteral("</blockquote>"); | 80 | helpText += QStringLiteral("</blockquote>"); | ||
140 | QLabel *fmtHelpText = new QLabel(helpText, this); | 81 | m_ui->helpTextLabel->setText(helpText); | ||
141 | fmtHelpText->setWordWrap(true); | 82 | connect(m_ui->helpTextLabel, &QLabel::linkActivated, this, [this](const QString& placeholder) { | ||
142 | fmtHelpText->setTextFormat(Qt::RichText); | 83 | m_ui->kcfg_saveFilenameFormat->insert(placeholder); | ||
143 | fmtHelpText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); | | |||
144 | connect(fmtHelpText, &QLabel::linkActivated, this, [this](const QString& placeholder) { | | |||
145 | mSaveNameFormat->insert(placeholder); | | |||
146 | }); | 84 | }); | ||
147 | mainLayout->addWidget(fmtHelpText); | 85 | } | ||
86 | | ||||
87 | SaveOptionsPage::~SaveOptionsPage() | ||||
88 | { | ||||
89 | delete m_ui; | ||||
148 | } | 90 | } | ||
149 | 91 | | |||
150 | void SaveOptionsPage::updateFilenamePreview() | 92 | void SaveOptionsPage::updateFilenamePreview() | ||
151 | { | 93 | { | ||
152 | auto lExportManager = ExportManager::instance(); | 94 | auto lExportManager = ExportManager::instance(); | ||
153 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | 95 | lExportManager->setWindowTitle(QStringLiteral("Spectacle")); | ||
154 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | 96 | Spectacle::CaptureMode lOldMode = lExportManager->captureMode(); | ||
155 | 97 | | |||
156 | // If the grabMode is not one of those below we need to change it to have the placeholder | 98 | // If the grabMode is not one of those below we need to change it to have the placeholder | ||
157 | // replaced by the window title | 99 | // replaced by the window title | ||
158 | bool lSwitchGrabMode = !(lOldMode == Spectacle::CaptureMode::ActiveWindow || | 100 | bool lSwitchGrabMode = !(lOldMode == Spectacle::CaptureMode::ActiveWindow || | ||
159 | lOldMode == Spectacle::CaptureMode::TransientWithParent || | 101 | lOldMode == Spectacle::CaptureMode::TransientWithParent || | ||
160 | lOldMode == Spectacle::CaptureMode::WindowUnderCursor); | 102 | lOldMode == Spectacle::CaptureMode::WindowUnderCursor); | ||
161 | if (lSwitchGrabMode) { | 103 | if (lSwitchGrabMode) { | ||
162 | lExportManager->setCaptureMode(Spectacle::CaptureMode::ActiveWindow); | 104 | lExportManager->setCaptureMode(Spectacle::CaptureMode::ActiveWindow); | ||
163 | } | 105 | } | ||
164 | const QString lFileName = lExportManager->formatFilename(mSaveNameFormat->text()); | 106 | const QString lFileName = lExportManager->formatFilename(m_ui->kcfg_saveFilenameFormat->text()); | ||
165 | mPreviewLabel->setText(xi18nc("@info", "<filename>%1.%2</filename>", lFileName, mSaveImageFormat->currentText().toLower())); | 107 | m_ui->preview->setText(xi18nc("@info", "<filename>%1.%2</filename>", | ||
108 | lFileName, m_ui->kcfg_defaultSaveImageFormat->currentText().toLower())); | ||||
166 | if (lSwitchGrabMode) { | 109 | if (lSwitchGrabMode) { | ||
167 | lExportManager->setCaptureMode(lOldMode); | 110 | lExportManager->setCaptureMode(lOldMode); | ||
168 | } | 111 | } | ||
169 | } | 112 | } |
There is an extra space after the opening parenthesis which probably shouldn't be here