Messagelib

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

KDE's Doxygen guidelines are available online.