File indexing completed on 2024-06-23 05:18:30
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "imagescalingselectformat.h" 0008 0009 #include <KLocalizedString> 0010 #include <QLineEdit> 0011 #include <QPushButton> 0012 0013 #include <QDialogButtonBox> 0014 #include <QHBoxLayout> 0015 #include <QListWidget> 0016 #include <QPointer> 0017 #include <QVBoxLayout> 0018 0019 using namespace MessageComposer; 0020 0021 ImageScalingSelectFormatDialog::ImageScalingSelectFormatDialog(QWidget *parent) 0022 : QDialog(parent) 0023 { 0024 setWindowTitle(i18nc("@title:window", "Select Image Format")); 0025 auto mainLayout = new QVBoxLayout(this); 0026 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0027 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0028 okButton->setDefault(true); 0029 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0030 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0031 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0032 mListWidget = new QListWidget(this); 0033 mainLayout->addWidget(mListWidget); 0034 mainLayout->addWidget(buttonBox); 0035 0036 initialize(); 0037 } 0038 0039 ImageScalingSelectFormatDialog::~ImageScalingSelectFormatDialog() = default; 0040 0041 void ImageScalingSelectFormatDialog::addImageFormat(const QString &format, const QString &mimetype) 0042 { 0043 auto item = new QListWidgetItem(format); 0044 item->setFlags(item->flags() | Qt::ItemIsUserCheckable); 0045 item->setData(ImageScalingSelectFormatDialog::ImageRole, mimetype); 0046 item->setCheckState(Qt::Unchecked); 0047 mListWidget->addItem(item); 0048 } 0049 0050 void ImageScalingSelectFormatDialog::initialize() 0051 { 0052 addImageFormat(QStringLiteral("PNG"), QStringLiteral("image/png")); 0053 addImageFormat(QStringLiteral("JPEG"), QStringLiteral("image/jpeg")); 0054 addImageFormat(QStringLiteral("GIF"), QStringLiteral("image/gif")); 0055 addImageFormat(QStringLiteral("BMP"), QStringLiteral("image/bmp")); 0056 } 0057 0058 QString ImageScalingSelectFormatDialog::format() const 0059 { 0060 const int numberOfElement(mListWidget->count()); 0061 QString formatStr; 0062 for (int i = 0; i < numberOfElement; ++i) { 0063 if (mListWidget->item(i)->checkState() == Qt::Checked) { 0064 if (!formatStr.isEmpty()) { 0065 formatStr += QLatin1Char(';'); 0066 } 0067 formatStr += mListWidget->item(i)->data(ImageScalingSelectFormatDialog::ImageRole).toString(); 0068 } 0069 } 0070 return formatStr; 0071 } 0072 0073 void ImageScalingSelectFormatDialog::setFormat(const QString &format) 0074 { 0075 const QStringList listFormat = format.split(QLatin1Char(';')); 0076 const int numberOfElement(mListWidget->count()); 0077 for (int i = 0; i < numberOfElement; ++i) { 0078 QListWidgetItem *item = mListWidget->item(i); 0079 if (listFormat.contains(item->data(ImageScalingSelectFormatDialog::ImageRole).toString())) { 0080 item->setCheckState(Qt::Checked); 0081 } 0082 } 0083 } 0084 0085 ImageScalingSelectFormat::ImageScalingSelectFormat(QWidget *parent) 0086 : QWidget(parent) 0087 { 0088 auto lay = new QHBoxLayout(this); 0089 lay->setContentsMargins({}); 0090 mFormat = new QLineEdit(this); 0091 connect(mFormat, &QLineEdit::textChanged, this, &ImageScalingSelectFormat::textChanged); 0092 mFormat->setReadOnly(true); 0093 lay->addWidget(mFormat); 0094 mSelectFormat = new QPushButton(i18n("Select Format..."), this); 0095 connect(mSelectFormat, &QPushButton::clicked, this, &ImageScalingSelectFormat::slotSelectFormat); 0096 lay->addWidget(mSelectFormat); 0097 } 0098 0099 ImageScalingSelectFormat::~ImageScalingSelectFormat() = default; 0100 0101 void ImageScalingSelectFormat::slotSelectFormat() 0102 { 0103 QPointer<ImageScalingSelectFormatDialog> dialog = new ImageScalingSelectFormatDialog(this); 0104 dialog->setFormat(mFormat->text()); 0105 if (dialog->exec()) { 0106 mFormat->setText(dialog->format()); 0107 } 0108 delete dialog; 0109 } 0110 0111 void ImageScalingSelectFormat::setFormat(const QString &format) 0112 { 0113 mFormat->setText(format); 0114 } 0115 0116 QString ImageScalingSelectFormat::format() const 0117 { 0118 return mFormat->text(); 0119 } 0120 0121 #include "moc_imagescalingselectformat.cpp"