Messagelib

imagescalingselectformat.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "imagescalingselectformat.h"
8 
9 #include <KLocalizedString>
10 #include <QLineEdit>
11 #include <QPushButton>
12 
13 #include <QDialogButtonBox>
14 #include <QHBoxLayout>
15 #include <QListWidget>
16 #include <QPointer>
17 #include <QVBoxLayout>
18 
19 using namespace MessageComposer;
20 
21 ImageScalingSelectFormatDialog::ImageScalingSelectFormatDialog(QWidget *parent)
22  : QDialog(parent)
23 {
24  setWindowTitle(i18nc("@title:window", "Select Image Format"));
25  auto mainLayout = new QVBoxLayout(this);
27  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
28  okButton->setDefault(true);
29  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
30  connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
31  connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
32  mListWidget = new QListWidget(this);
33  mainLayout->addWidget(mListWidget);
34  mainLayout->addWidget(buttonBox);
35 
36  initialize();
37 }
38 
39 ImageScalingSelectFormatDialog::~ImageScalingSelectFormatDialog() = default;
40 
41 void ImageScalingSelectFormatDialog::addImageFormat(const QString &format, const QString &mimetype)
42 {
43  auto item = new QListWidgetItem(format);
44  item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
45  item->setData(ImageScalingSelectFormatDialog::ImageRole, mimetype);
46  item->setCheckState(Qt::Unchecked);
47  mListWidget->addItem(item);
48 }
49 
50 void ImageScalingSelectFormatDialog::initialize()
51 {
52  addImageFormat(QStringLiteral("PNG"), QStringLiteral("image/png"));
53  addImageFormat(QStringLiteral("JPEG"), QStringLiteral("image/jpeg"));
54  addImageFormat(QStringLiteral("GIF"), QStringLiteral("image/gif"));
55  addImageFormat(QStringLiteral("BMP"), QStringLiteral("image/bmp"));
56 }
57 
58 QString ImageScalingSelectFormatDialog::format() const
59 {
60  const int numberOfElement(mListWidget->count());
61  QString formatStr;
62  for (int i = 0; i < numberOfElement; ++i) {
63  if (mListWidget->item(i)->checkState() == Qt::Checked) {
64  if (!formatStr.isEmpty()) {
65  formatStr += QLatin1Char(';');
66  }
67  formatStr += mListWidget->item(i)->data(ImageScalingSelectFormatDialog::ImageRole).toString();
68  }
69  }
70  return formatStr;
71 }
72 
73 void ImageScalingSelectFormatDialog::setFormat(const QString &format)
74 {
75  const QStringList listFormat = format.split(QLatin1Char(';'));
76  const int numberOfElement(mListWidget->count());
77  for (int i = 0; i < numberOfElement; ++i) {
78  QListWidgetItem *item = mListWidget->item(i);
79  if (listFormat.contains(item->data(ImageScalingSelectFormatDialog::ImageRole).toString())) {
81  }
82  }
83 }
84 
85 ImageScalingSelectFormat::ImageScalingSelectFormat(QWidget *parent)
86  : QWidget(parent)
87 {
88  auto lay = new QHBoxLayout(this);
89  lay->setContentsMargins({});
90  mFormat = new QLineEdit;
91  connect(mFormat, &QLineEdit::textChanged, this, &ImageScalingSelectFormat::textChanged);
92  mFormat->setReadOnly(true);
93  lay->addWidget(mFormat);
94  mSelectFormat = new QPushButton(i18n("Select Format..."));
95  connect(mSelectFormat, &QPushButton::clicked, this, &ImageScalingSelectFormat::slotSelectFormat);
96  lay->addWidget(mSelectFormat);
97 }
98 
99 ImageScalingSelectFormat::~ImageScalingSelectFormat() = default;
100 
101 void ImageScalingSelectFormat::slotSelectFormat()
102 {
103  QPointer<ImageScalingSelectFormatDialog> dialog = new ImageScalingSelectFormatDialog(this);
104  dialog->setFormat(mFormat->text());
105  if (dialog->exec()) {
106  mFormat->setText(dialog->format());
107  }
108  delete dialog;
109 }
110 
111 void ImageScalingSelectFormat::setFormat(const QString &format)
112 {
113  mFormat->setText(format);
114 }
115 
116 QString ImageScalingSelectFormat::format() const
117 {
118  return mFormat->text();
119 }
virtual void reject()
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
KCRASH_EXPORT void initialize()
void textChanged(const QString &text)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
virtual QVariant data(int role) const const
void setShortcut(const QKeySequence &key)
void setCheckState(Qt::CheckState state)
QByteArray mimetype() const
mimetype
virtual void accept()
QString i18n(const char *text, const TYPE &arg...)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
Unchecked
void setDefault(bool)
QString toString() const const
Key_Return
ItemIsUserCheckable
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.