Merge remote-tracking branch 'origin/Applications/15.08'
[kdepim.git] / messagecomposer / imagescaling / imagescalingselectformat.cpp
blob1665696dfa76269179e095f8713216828fb80f25
1 /*
2 Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "imagescalingselectformat.h"
20 #include <QLineEdit>
21 #include <QPushButton>
22 #include <KLocalizedString>
24 #include <QListWidget>
25 #include <QHBoxLayout>
26 #include <QPointer>
27 #include <QDialogButtonBox>
28 #include <QVBoxLayout>
30 using namespace MessageComposer;
32 ImageScalingSelectFormatDialog::ImageScalingSelectFormatDialog(QWidget *parent)
33 : QDialog(parent)
35 setWindowTitle(i18nc("@title:window", "Select Image Format"));
36 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
37 QVBoxLayout *mainLayout = new QVBoxLayout;
38 setLayout(mainLayout);
39 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
40 okButton->setDefault(true);
41 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
42 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
43 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
44 okButton->setDefault(true);
45 mListWidget = new QListWidget;
46 mainLayout->addWidget(mListWidget);
47 mainLayout->addWidget(buttonBox);
49 initialize();
52 ImageScalingSelectFormatDialog::~ImageScalingSelectFormatDialog()
56 void ImageScalingSelectFormatDialog::addImageFormat(const QString &format, const QString &mimetype)
58 QListWidgetItem *item = new QListWidgetItem(format);
59 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
60 item->setData(ImageScalingSelectFormatDialog::ImageRole, mimetype);
61 item->setCheckState(Qt::Unchecked);
62 mListWidget->addItem(item);
65 void ImageScalingSelectFormatDialog::initialize()
67 addImageFormat(QStringLiteral("PNG"), QStringLiteral("image/png"));
68 addImageFormat(QStringLiteral("JPEG"), QStringLiteral("image/jpeg"));
69 addImageFormat(QStringLiteral("GIF"), QStringLiteral("image/gif"));
70 addImageFormat(QStringLiteral("BMP"), QStringLiteral("image/bmp"));
73 QString ImageScalingSelectFormatDialog::format() const
75 const int numberOfElement(mListWidget->count());
76 QString formatStr;
77 for (int i = 0; i < numberOfElement; ++i) {
78 if (mListWidget->item(i)->checkState() == Qt::Checked) {
79 if (!formatStr.isEmpty()) {
80 formatStr += QLatin1Char(';');
82 formatStr += mListWidget->item(i)->data(ImageScalingSelectFormatDialog::ImageRole).toString();
85 return formatStr;
88 void ImageScalingSelectFormatDialog::setFormat(const QString &format)
90 const QStringList listFormat = format.split(QLatin1Char(';'));
91 const int numberOfElement(mListWidget->count());
92 for (int i = 0; i < numberOfElement; ++i) {
93 QListWidgetItem *item = mListWidget->item(i);
94 if (listFormat.contains(item->data(ImageScalingSelectFormatDialog::ImageRole).toString())) {
95 item->setCheckState(Qt::Checked);
100 ImageScalingSelectFormat::ImageScalingSelectFormat(QWidget *parent)
101 : QWidget(parent)
103 QHBoxLayout *lay = new QHBoxLayout(this);
104 lay->setMargin(0);
105 mFormat = new QLineEdit;
106 connect(mFormat, &QLineEdit::textChanged, this, &ImageScalingSelectFormat::textChanged);
107 mFormat->setReadOnly(true);
108 lay->addWidget(mFormat);
109 mSelectFormat = new QPushButton(i18n("Select Format..."));
110 connect(mSelectFormat, &QPushButton::clicked, this, &ImageScalingSelectFormat::slotSelectFormat);
111 lay->addWidget(mSelectFormat);
114 ImageScalingSelectFormat::~ImageScalingSelectFormat()
118 void ImageScalingSelectFormat::slotSelectFormat()
120 QPointer<ImageScalingSelectFormatDialog> dialog = new ImageScalingSelectFormatDialog(this);
121 dialog->setFormat(mFormat->text());
122 if (dialog->exec()) {
123 mFormat->setText(dialog->format());
125 delete dialog;
128 void ImageScalingSelectFormat::setFormat(const QString &format)
130 mFormat->setText(format);
133 QString ImageScalingSelectFormat::format() const
135 return mFormat->text();