Messagelib

imagescalingselectformat.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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
19using namespace MessageComposer;
20
21ImageScalingSelectFormatDialog::ImageScalingSelectFormatDialog(QWidget *parent)
22 : QDialog(parent)
23 , mListWidget(new QListWidget(this))
24{
25 setWindowTitle(i18nc("@title:window", "Select Image Format"));
26 auto mainLayout = new QVBoxLayout(this);
28 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
29 okButton->setDefault(true);
33 mainLayout->addWidget(mListWidget);
34 mainLayout->addWidget(buttonBox);
35
36 initialize();
37}
38
39ImageScalingSelectFormatDialog::~ImageScalingSelectFormatDialog() = default;
40
41void 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
50void 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
58QString 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
73void ImageScalingSelectFormatDialog::setFormat(const QString &format)
74{
75 const QList<QStringView> listFormat = QStringView(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
85ImageScalingSelectFormat::ImageScalingSelectFormat(QWidget *parent)
86 : QWidget(parent)
87 , mFormat(new QLineEdit(this))
88 , mSelectFormat(new QPushButton(i18nc("@action:button", "Select Format..."), this))
89{
90 auto lay = new QHBoxLayout(this);
91 lay->setContentsMargins({});
92 connect(mFormat, &QLineEdit::textChanged, this, &ImageScalingSelectFormat::textChanged);
93 mFormat->setReadOnly(true);
94 lay->addWidget(mFormat);
95 connect(mSelectFormat, &QPushButton::clicked, this, &ImageScalingSelectFormat::slotSelectFormat);
96 lay->addWidget(mSelectFormat);
97}
98
99ImageScalingSelectFormat::~ImageScalingSelectFormat() = default;
100
101void 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
111void ImageScalingSelectFormat::setFormat(const QString &format)
112{
113 mFormat->setText(format);
114}
115
116QString ImageScalingSelectFormat::format() const
117{
118 return mFormat->text();
119}
120
121#include "moc_imagescalingselectformat.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCRASH_EXPORT void initialize()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
virtual void accept()
virtual void reject()
void textChanged(const QString &text)
bool contains(const AT &value) const const
void addItem(QListWidgetItem *item)
QListWidgetItem * item(int row) const const
Qt::CheckState checkState() const const
virtual QVariant data(int role) const const
void setCheckState(Qt::CheckState state)
void setDefault(bool)
bool isEmpty() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Unchecked
ItemIsUserCheckable
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.