File indexing completed on 2024-04-21 05:45:47

0001 /*******************************************************************************
0002  * Copyright (C) 2008-2013 Konstantinos Smanis <konstantinos.smanis@gmail.com> *
0003  *                                                                             *
0004  * This program is free software: you can redistribute it and/or modify it     *
0005  * under the terms of the GNU General Public License as published by the Free  *
0006  * Software Foundation, either version 3 of the License, or (at your option)   *
0007  * any later version.                                                          *
0008  *                                                                             *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT *
0010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
0011  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
0012  * more details.                                                               *
0013  *                                                                             *
0014  * You should have received a copy of the GNU General Public License along     *
0015  * with this program. If not, see <http://www.gnu.org/licenses/>.              *
0016  *******************************************************************************/
0017 
0018 //Own
0019 #include "convertDlg.h"
0020 
0021 //Qt
0022 #include <QMimeDatabase>
0023 #include <QMimeType>
0024 
0025 //KDE
0026 #include <KMessageBox>
0027 #include <KLocalizedString>
0028 
0029 //ImageMagick
0030 #include <Magick++.h>
0031 
0032 //Ui
0033 #include "ui_convertDlg.h"
0034 
0035 ConvertDialog::ConvertDialog(QWidget *parent) : QDialog(parent)
0036 {
0037     QWidget *widget = new QWidget(this);
0038     ui = new Ui::ConvertDialog;
0039     ui->setupUi(widget);
0040     ui->gridLayout->setContentsMargins(0, 0, 0, 0);
0041 
0042     auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &ConvertDialog::slotAccepted);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0045 
0046     QVBoxLayout *mainLayout = new QVBoxLayout;
0047     setLayout(mainLayout);
0048     mainLayout->addWidget(widget);
0049     mainLayout->addWidget(buttonBox);
0050 
0051     QString readFilter;
0052     QList<Magick::CoderInfo> coderList;
0053     coderInfoList(&coderList, Magick::CoderInfo::TrueMatch, Magick::CoderInfo::AnyMatch, Magick::CoderInfo::AnyMatch);
0054     for (const Magick::CoderInfo &coder : qAsConst(coderList)) {
0055         readFilter.append(QStringLiteral(" *.%1").arg(QString::fromStdString(coder.name()).toLower()));
0056     }
0057     readFilter.remove(0, 1);
0058     readFilter.append(QLatin1Char('|')).append(i18nc("@item:inlistbox", "ImageMagick supported image formats"));
0059 
0060     QMimeDatabase db;
0061     const QString writeFilter = QStringLiteral("*%1|%5 (%1)\n*%2|%6 (%2)\n*%3 *%4|%7 (%3 %4)")
0062                                 .arg(QLatin1String(".png"), QLatin1String(".tga"), QLatin1String(".jpg"), QLatin1String(".jpeg"),
0063                                     db.mimeTypeForName(QStringLiteral("image/png")).comment(),
0064                                     db.mimeTypeForName(QStringLiteral("image/x-tga")).comment(),
0065                                     db.mimeTypeForName(QStringLiteral("image/jpeg")).comment());
0066 
0067     ui->kurlrequester_image->setMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly);
0068     ui->kurlrequester_image->setAcceptMode(QFileDialog::AcceptOpen);
0069     ui->kurlrequester_image->setFilter(readFilter);
0070     ui->kurlrequester_converted->setMode(KFile::File | KFile::LocalOnly);
0071     ui->kurlrequester_converted->setAcceptMode(QFileDialog::AcceptSave);
0072     ui->kurlrequester_converted->setFilter(writeFilter);
0073 }
0074 ConvertDialog::~ConvertDialog()
0075 {
0076     delete ui;
0077 }
0078 
0079 void ConvertDialog::setResolution(int width, int height)
0080 {
0081     if (width > 0 && height > 0) {
0082         ui->spinBox_width->setValue(width);
0083         ui->spinBox_height->setValue(height);
0084     }
0085 }
0086 
0087 void ConvertDialog::slotAccepted()
0088 {
0089     if (ui->kurlrequester_image->text().isEmpty() || ui->kurlrequester_converted->text().isEmpty()) {
0090         KMessageBox::information(this, i18nc("@info", "Please fill in both <interface>Image</interface> and <interface>Convert To</interface> fields."));
0091         return;
0092     } else if (ui->spinBox_width->value() == 0 || ui->spinBox_height->value() == 0) {
0093         KMessageBox::information(this, i18nc("@info", "Please fill in both <interface>Width</interface> and <interface>Height</interface> fields."));
0094         return;
0095     } else if (!QFileInfo(QFileInfo(ui->kurlrequester_converted->url().toLocalFile()).path()).isWritable()) {
0096         KMessageBox::information(this, i18nc("@info", "You do not have write permissions in this directory, please select another destination."));
0097         return;
0098     }
0099     Magick::Geometry resolution(ui->spinBox_width->value(), ui->spinBox_height->value());
0100     resolution.aspect(ui->checkBox_force->isChecked());
0101     Magick::Image image(ui->kurlrequester_image->url().toLocalFile().toStdString());
0102     image.zoom(resolution);
0103     image.depth(8);
0104     image.classType(Magick::DirectClass);
0105     image.write(ui->kurlrequester_converted->url().toLocalFile().toStdString());
0106     if (ui->checkBox_wallpaper->isChecked()) {
0107         Q_EMIT splashImageCreated(ui->kurlrequester_converted->url().toLocalFile());
0108     }
0109 
0110     accept();
0111 }