Messagelib

imagescalingwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "imagescalingwidget.h"
8#include "settings/messagecomposersettings.h"
9#include "ui_imagescalingwidget.h"
10#include <KLineEditEventHandler>
11#include <KLocalizedString>
12#include <KMessageBox>
13
14#include <QButtonGroup>
15#include <QComboBox>
16#include <QImageWriter>
17#include <QList>
18#include <QWhatsThis>
19
20using namespace MessageComposer;
21class MessageComposer::ImageScalingWidgetPrivate
22{
23public:
24 ImageScalingWidgetPrivate()
25 : ui(new Ui::ImageScalingWidget)
26 {
27 }
28
29 ~ImageScalingWidgetPrivate()
30 {
31 delete ui;
32 }
33
34 Ui::ImageScalingWidget *const ui;
35 QButtonGroup *mSourceFilenameFilterGroup = nullptr;
36 QButtonGroup *mRecipientFilterGroup = nullptr;
37 bool mWasChanged = false;
38};
39
40ImageScalingWidget::ImageScalingWidget(QWidget *parent)
41 : QWidget(parent)
42 , d(new MessageComposer::ImageScalingWidgetPrivate)
43{
44 d->ui->setupUi(this);
45 initComboBox(d->ui->CBMaximumWidth);
46 initComboBox(d->ui->CBMaximumHeight);
47 initComboBox(d->ui->CBMinimumWidth);
48 initComboBox(d->ui->CBMinimumHeight);
49
50 initWriteImageFormat();
52 KLineEditEventHandler::catchReturnKey(d->ui->renameResizedImagePattern);
53 KLineEditEventHandler::catchReturnKey(d->ui->resizeEmailsPattern);
54 KLineEditEventHandler::catchReturnKey(d->ui->doNotResizePattern);
55
56 connect(d->ui->enabledAutoResize, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
57 connect(d->ui->KeepImageRatio, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
58 connect(d->ui->AskBeforeResizing, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
59 connect(d->ui->EnlargeImageToMinimum, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
60 connect(d->ui->ReduceImageToMaximum, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
61 connect(d->ui->customMaximumWidth, &QSpinBox::valueChanged, this, &ImageScalingWidget::changed);
62 connect(d->ui->customMaximumHeight, &QSpinBox::valueChanged, this, &ImageScalingWidget::changed);
63 connect(d->ui->customMinimumWidth, &QSpinBox::valueChanged, this, &ImageScalingWidget::changed);
64 connect(d->ui->customMinimumHeight, &QSpinBox::valueChanged, this, &ImageScalingWidget::changed);
65 connect(d->ui->skipImageSizeLower, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
66 connect(d->ui->imageSize, &QSpinBox::valueChanged, this, &ImageScalingWidget::changed);
67 connect(d->ui->pattern, &QLineEdit::textChanged, this, &ImageScalingWidget::changed);
68 connect(d->ui->CBMaximumWidth, &QComboBox::currentIndexChanged, this, &ImageScalingWidget::slotComboboxChanged);
69 connect(d->ui->CBMaximumHeight, &QComboBox::currentIndexChanged, this, &ImageScalingWidget::slotComboboxChanged);
70 connect(d->ui->CBMinimumWidth, &QComboBox::currentIndexChanged, this, &ImageScalingWidget::slotComboboxChanged);
71 connect(d->ui->CBMinimumHeight, &QComboBox::currentIndexChanged, this, &ImageScalingWidget::slotComboboxChanged);
72 connect(d->ui->WriteToImageFormat, &QComboBox::activated, this, &ImageScalingWidget::changed);
73 connect(d->ui->renameResizedImage, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
74 connect(d->ui->renameResizedImage, &QCheckBox::clicked, d->ui->renameResizedImagePattern, &QLineEdit::setEnabled);
75 connect(d->ui->renameResizedImagePattern, &QLineEdit::textChanged, this, &ImageScalingWidget::changed);
76
77 connect(d->ui->resizeEmailsPattern, &QLineEdit::textChanged, this, &ImageScalingWidget::changed);
78 connect(d->ui->doNotResizePattern, &QLineEdit::textChanged, this, &ImageScalingWidget::changed);
79 connect(d->ui->resizeImageWithFormatsType, &MessageComposer::ImageScalingSelectFormat::textChanged, this, &ImageScalingWidget::changed);
80 connect(d->ui->resizeImageWithFormats, &QCheckBox::clicked, this, &ImageScalingWidget::changed);
81 connect(d->ui->resizeImageWithFormats, &QCheckBox::clicked, d->ui->resizeImageWithFormatsType, &MessageComposer::ImageScalingSelectFormat::setEnabled);
82 d->ui->resizeImageWithFormatsType->setEnabled(false);
83
84 d->ui->pattern->setEnabled(false);
85 d->mSourceFilenameFilterGroup = new QButtonGroup(d->ui->filterSourceGroupBox);
86 connect(d->mSourceFilenameFilterGroup, &QButtonGroup::buttonClicked, this, &ImageScalingWidget::slotSourceFilterAbstractButtonClicked);
87 d->mSourceFilenameFilterGroup->addButton(d->ui->notFilterFilename, MessageComposer::MessageComposerSettings::EnumFilterSourceType::NoFilter);
88 d->mSourceFilenameFilterGroup->addButton(d->ui->includeFilesWithPattern,
89 MessageComposer::MessageComposerSettings::EnumFilterSourceType::IncludeFilesWithPattern);
90 d->mSourceFilenameFilterGroup->addButton(d->ui->excludeFilesWithPattern,
91 MessageComposer::MessageComposerSettings::EnumFilterSourceType::ExcludeFilesWithPattern);
92
93 d->mRecipientFilterGroup = new QButtonGroup(d->ui->tab_4);
94 connect(d->mRecipientFilterGroup, &QButtonGroup::buttonClicked, this, &ImageScalingWidget::slotRecipientFilterAbstractClicked);
95 d->ui->doNotResizePattern->setEnabled(false);
96 d->ui->resizeEmailsPattern->setEnabled(false);
97 d->mRecipientFilterGroup->addButton(d->ui->doNotFilterRecipients, MessageComposer::MessageComposerSettings::EnumFilterRecipientType::NoFilter);
98 d->mRecipientFilterGroup->addButton(d->ui->resizeEachEmails,
99 MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeEachEmailsContainsPattern);
100 d->mRecipientFilterGroup->addButton(d->ui->resizeOneEmails,
101 MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeOneEmailContainsPattern);
102 d->mRecipientFilterGroup->addButton(d->ui->doNotResizeEachEmails,
103 MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeEachEmailsContainsPattern);
104 d->mRecipientFilterGroup->addButton(d->ui->doNotResizeOneEmails,
105 MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeOneEmailContainsPattern);
106
107 d->ui->help->setText(i18n("<a href=\"whatsthis\">How does this work?</a>"));
108 connect(d->ui->help, &QLabel::linkActivated, this, &ImageScalingWidget::slotHelpLinkClicked);
109 d->ui->help->setContextMenuPolicy(Qt::NoContextMenu);
110}
111
112ImageScalingWidget::~ImageScalingWidget() = default;
113
114void ImageScalingWidget::slotHelpLinkClicked(const QString &)
115{
116 const QString help = i18n(
117 "<qt>"
118 "<p>Here you can define image filename. "
119 "You can use:</p>"
120 "<ul>"
121 "<li>%t set current time</li>"
122 "<li>%d set current date</li>"
123 "<li>%n original filename</li>"
124 "<li>%e original extension</li>"
125 "<li>%x new extension</li>"
126 "</ul>"
127 "</qt>");
128
130}
131
132void ImageScalingWidget::slotSourceFilterAbstractButtonClicked(QAbstractButton *button)
133{
134 if (button) {
135 const int buttonId = d->mRecipientFilterGroup->id(button);
136 slotSourceFilterClicked(buttonId);
137 }
138}
139
140void ImageScalingWidget::slotSourceFilterClicked(int button)
141{
142 d->ui->pattern->setEnabled(button != 0);
143 Q_EMIT changed();
144}
145
146void ImageScalingWidget::slotRecipientFilterClicked(int button)
147{
148 d->ui->resizeEmailsPattern->setEnabled((button == MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeEachEmailsContainsPattern)
149 || (button == MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeOneEmailContainsPattern));
150 d->ui->doNotResizePattern->setEnabled((button == MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeEachEmailsContainsPattern)
151 || (button == MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeOneEmailContainsPattern));
152 Q_EMIT changed();
153}
154
155void ImageScalingWidget::slotRecipientFilterAbstractClicked(QAbstractButton *button)
156{
157 if (button) {
158 const int buttonId = d->mRecipientFilterGroup->id(button);
159 slotRecipientFilterClicked(buttonId);
160 }
161}
162
163void ImageScalingWidget::slotComboboxChanged(int index)
164{
165 auto combo = qobject_cast<QComboBox *>(sender());
166 if (combo) {
167 const bool isCustom = combo->itemData(index) == -1;
168 if (combo == d->ui->CBMaximumWidth) {
169 d->ui->customMaximumWidth->setEnabled(isCustom);
170 } else if (combo == d->ui->CBMaximumHeight) {
171 d->ui->customMaximumHeight->setEnabled(isCustom);
172 } else if (combo == d->ui->CBMinimumWidth) {
173 d->ui->customMinimumWidth->setEnabled(isCustom);
174 } else if (combo == d->ui->CBMinimumHeight) {
175 d->ui->customMinimumHeight->setEnabled(isCustom);
176 }
177 Q_EMIT changed();
178 }
179}
180
181void ImageScalingWidget::initComboBox(QComboBox *combo)
182{
183 const QList<int> size = {240, 320, 512, 640, 800, 1024, 1600, 2048};
184 for (int val : size) {
185 combo->addItem(QStringLiteral("%1 px").arg(QString::number(val)), val);
186 }
187 combo->addItem(i18n("Custom"), -1);
188}
189
190void ImageScalingWidget::initWriteImageFormat()
191{
192 /* Too many format :)
193 QList<QByteArray> listWriteFormat = QImageWriter::supportedImageFormats();
194 Q_FOREACH(const QByteArray& format, listWriteFormat) {
195 d->ui->WriteToImageFormat->addItem(QString::fromLatin1(format));
196 }
197 */
198 // known by several mailer.
199 d->ui->WriteToImageFormat->addItem(QStringLiteral("JPG"));
200 d->ui->WriteToImageFormat->addItem(QStringLiteral("PNG"));
201}
202
203void ImageScalingWidget::updateSettings()
204{
205 d->ui->enabledAutoResize->setChecked(MessageComposer::MessageComposerSettings::self()->autoResizeImageEnabled());
206 d->ui->KeepImageRatio->setChecked(MessageComposer::MessageComposerSettings::self()->keepImageRatio());
207 d->ui->AskBeforeResizing->setChecked(MessageComposer::MessageComposerSettings::self()->askBeforeResizing());
208 d->ui->EnlargeImageToMinimum->setChecked(MessageComposer::MessageComposerSettings::self()->enlargeImageToMinimum());
209 d->ui->ReduceImageToMaximum->setChecked(MessageComposer::MessageComposerSettings::self()->reduceImageToMaximum());
210 d->ui->skipImageSizeLower->setChecked(MessageComposer::MessageComposerSettings::self()->skipImageLowerSizeEnabled());
211 d->ui->imageSize->setValue(MessageComposer::MessageComposerSettings::self()->skipImageLowerSize());
212
213 d->ui->customMaximumWidth->setValue(MessageComposer::MessageComposerSettings::self()->customMaximumWidth());
214 d->ui->customMaximumHeight->setValue(MessageComposer::MessageComposerSettings::self()->customMaximumHeight());
215 d->ui->customMinimumWidth->setValue(MessageComposer::MessageComposerSettings::self()->customMinimumWidth());
216 d->ui->customMinimumHeight->setValue(MessageComposer::MessageComposerSettings::self()->customMinimumHeight());
217
218 int index = qMax(0, d->ui->CBMaximumWidth->findData(MessageComposer::MessageComposerSettings::self()->maximumWidth()));
219 d->ui->CBMaximumWidth->setCurrentIndex(index);
220 d->ui->customMaximumWidth->setEnabled(d->ui->CBMaximumWidth->itemData(index) == -1);
221
222 index = qMax(0, d->ui->CBMaximumHeight->findData(MessageComposer::MessageComposerSettings::self()->maximumHeight()));
223 d->ui->CBMaximumHeight->setCurrentIndex(index);
224 d->ui->customMaximumHeight->setEnabled(d->ui->CBMaximumHeight->itemData(index) == -1);
225
226 index = qMax(0, d->ui->CBMinimumWidth->findData(MessageComposer::MessageComposerSettings::self()->minimumWidth()));
227 d->ui->CBMinimumWidth->setCurrentIndex(index);
228 d->ui->customMinimumWidth->setEnabled(d->ui->CBMinimumWidth->itemData(index) == -1);
229
230 index = qMax(0, d->ui->CBMinimumHeight->findData(MessageComposer::MessageComposerSettings::self()->minimumHeight()));
231 d->ui->CBMinimumHeight->setCurrentIndex(index);
232 d->ui->customMinimumHeight->setEnabled(d->ui->CBMinimumHeight->itemData(index) == -1);
233
234 index = d->ui->WriteToImageFormat->findData(MessageComposer::MessageComposerSettings::self()->writeFormat());
235 if (index == -1) {
236 d->ui->WriteToImageFormat->setCurrentIndex(0);
237 } else {
238 d->ui->WriteToImageFormat->setCurrentIndex(index);
239 }
240 d->ui->pattern->setText(MessageComposer::MessageComposerSettings::self()->filterSourcePattern());
241
242 d->ui->renameResizedImage->setChecked(MessageComposer::MessageComposerSettings::self()->renameResizedImages());
243
244 d->ui->renameResizedImagePattern->setText(MessageComposer::MessageComposerSettings::self()->renameResizedImagesPattern());
245 d->ui->renameResizedImagePattern->setEnabled(d->ui->renameResizedImage->isChecked());
246
247 d->ui->doNotResizePattern->setText(MessageComposer::MessageComposerSettings::self()->doNotResizeEmailsPattern());
248 d->ui->resizeEmailsPattern->setText(MessageComposer::MessageComposerSettings::self()->resizeEmailsPattern());
249
250 d->ui->resizeImageWithFormats->setChecked(MessageComposer::MessageComposerSettings::self()->resizeImagesWithFormats());
251 d->ui->resizeImageWithFormatsType->setFormat(MessageComposer::MessageComposerSettings::self()->resizeImagesWithFormatsType());
252 d->ui->resizeImageWithFormatsType->setEnabled(d->ui->resizeImageWithFormats->isChecked());
253
254 updateFilterSourceTypeSettings();
255 updateEmailsFilterTypeSettings();
256}
257
258void ImageScalingWidget::loadConfig()
259{
260 updateSettings();
261 d->mWasChanged = false;
262}
263
264void ImageScalingWidget::updateFilterSourceTypeSettings()
265{
266 switch (MessageComposer::MessageComposerSettings::self()->filterSourceType()) {
267 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::NoFilter:
268 d->ui->notFilterFilename->setChecked(true);
269 d->ui->pattern->setEnabled(false);
270 break;
271 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::IncludeFilesWithPattern:
272 d->ui->includeFilesWithPattern->setChecked(true);
273 d->ui->pattern->setEnabled(true);
274 break;
275 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::ExcludeFilesWithPattern:
276 d->ui->excludeFilesWithPattern->setChecked(true);
277 d->ui->pattern->setEnabled(true);
278 break;
279 }
280}
281
282void ImageScalingWidget::updateEmailsFilterTypeSettings()
283{
284 d->ui->doNotResizePattern->setEnabled(false);
285 d->ui->resizeEmailsPattern->setEnabled(false);
286
287 switch (MessageComposer::MessageComposerSettings::self()->filterRecipientType()) {
288 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::NoFilter:
289 d->ui->doNotFilterRecipients->setChecked(true);
290 break;
291 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeEachEmailsContainsPattern:
292 d->ui->resizeEachEmails->setChecked(true);
293 d->ui->resizeEmailsPattern->setEnabled(true);
294 break;
295 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeOneEmailContainsPattern:
296 d->ui->resizeOneEmails->setChecked(true);
297 d->ui->resizeEmailsPattern->setEnabled(true);
298 break;
299 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeEachEmailsContainsPattern:
300 d->ui->doNotResizeEachEmails->setChecked(true);
301 d->ui->doNotResizePattern->setEnabled(true);
302 break;
303 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeOneEmailContainsPattern:
304 d->ui->doNotResizeOneEmails->setChecked(true);
305 d->ui->doNotResizePattern->setEnabled(false);
306 break;
307 }
308}
309
310void ImageScalingWidget::writeConfig()
311{
312 if (d->ui->EnlargeImageToMinimum->isChecked() && d->ui->ReduceImageToMaximum->isChecked()) {
313 if ((d->ui->customMinimumWidth->value() >= d->ui->customMaximumWidth->value())
314 || (d->ui->customMinimumHeight->value() >= d->ui->customMaximumHeight->value())) {
315 KMessageBox::error(this, i18n("Please verify minimum and maximum values."), i18nc("@title:window", "Error in minimum Maximum value"));
316 return;
317 }
318 }
319 MessageComposer::MessageComposerSettings::self()->setAutoResizeImageEnabled(d->ui->enabledAutoResize->isChecked());
320 MessageComposer::MessageComposerSettings::self()->setKeepImageRatio(d->ui->KeepImageRatio->isChecked());
321 MessageComposer::MessageComposerSettings::self()->setAskBeforeResizing(d->ui->AskBeforeResizing->isChecked());
322 MessageComposer::MessageComposerSettings::self()->setEnlargeImageToMinimum(d->ui->EnlargeImageToMinimum->isChecked());
323 MessageComposer::MessageComposerSettings::self()->setReduceImageToMaximum(d->ui->ReduceImageToMaximum->isChecked());
324
325 MessageComposer::MessageComposerSettings::self()->setCustomMaximumWidth(d->ui->customMaximumWidth->value());
326 MessageComposer::MessageComposerSettings::self()->setCustomMaximumHeight(d->ui->customMaximumHeight->value());
327 MessageComposer::MessageComposerSettings::self()->setCustomMinimumWidth(d->ui->customMinimumWidth->value());
328 MessageComposer::MessageComposerSettings::self()->setCustomMinimumHeight(d->ui->customMinimumHeight->value());
329
330 MessageComposer::MessageComposerSettings::self()->setMaximumWidth(d->ui->CBMaximumWidth->itemData(d->ui->CBMaximumWidth->currentIndex()).toInt());
331 MessageComposer::MessageComposerSettings::self()->setMaximumHeight(d->ui->CBMaximumHeight->itemData(d->ui->CBMaximumHeight->currentIndex()).toInt());
332 MessageComposer::MessageComposerSettings::self()->setMinimumWidth(d->ui->CBMinimumWidth->itemData(d->ui->CBMinimumWidth->currentIndex()).toInt());
333 MessageComposer::MessageComposerSettings::self()->setMinimumHeight(d->ui->CBMinimumHeight->itemData(d->ui->CBMinimumHeight->currentIndex()).toInt());
334
335 MessageComposer::MessageComposerSettings::self()->setWriteFormat(d->ui->WriteToImageFormat->currentText());
336 MessageComposer::MessageComposerSettings::self()->setSkipImageLowerSizeEnabled(d->ui->skipImageSizeLower->isChecked());
337 MessageComposer::MessageComposerSettings::self()->setSkipImageLowerSize(d->ui->imageSize->value());
338
339 MessageComposer::MessageComposerSettings::self()->setFilterSourcePattern(d->ui->pattern->text());
340
341 MessageComposer::MessageComposerSettings::self()->setFilterSourceType(d->mSourceFilenameFilterGroup->checkedId());
342
343 MessageComposer::MessageComposerSettings::self()->setRenameResizedImages(d->ui->renameResizedImage->isChecked());
344
345 MessageComposer::MessageComposerSettings::self()->setRenameResizedImagesPattern(d->ui->renameResizedImagePattern->text());
346
347 MessageComposer::MessageComposerSettings::self()->setDoNotResizeEmailsPattern(d->ui->doNotResizePattern->text());
348 MessageComposer::MessageComposerSettings::self()->setResizeEmailsPattern(d->ui->resizeEmailsPattern->text());
349 MessageComposer::MessageComposerSettings::self()->setFilterRecipientType(d->mRecipientFilterGroup->checkedId());
350
351 MessageComposer::MessageComposerSettings::self()->setResizeImagesWithFormats(d->ui->resizeImageWithFormats->isChecked());
352 MessageComposer::MessageComposerSettings::self()->setResizeImagesWithFormatsType(d->ui->resizeImageWithFormatsType->format());
353 d->mWasChanged = false;
354}
355
356void ImageScalingWidget::resetToDefault()
357{
358 const bool bUseDefaults = MessageComposer::MessageComposerSettings::self()->useDefaults(true);
359 updateSettings();
360 MessageComposer::MessageComposerSettings::self()->useDefaults(bUseDefaults);
361}
362
363#include "moc_imagescalingwidget.cpp"
The ImageScalingWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem help()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void clicked(bool checked)
void buttonClicked(QAbstractButton *button)
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void currentIndexChanged(int index)
QPoint pos()
void linkActivated(const QString &link)
void textChanged(const QString &text)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
QObject * sender() const const
void valueChanged(int i)
QString number(double n, char format, int precision)
NoContextMenu
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void showText(const QPoint &pos, const QString &text, QWidget *w)
void setEnabled(bool)
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.