File indexing completed on 2024-04-28 17:04:34

0001 /*****************************************************************************
0002  *   Copyright 2010 Craig Drummond <craig.p.drummond@gmail.com>              *
0003  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0004  *                                                                           *
0005  *   This program is free software; you can redistribute it and/or modify    *
0006  *   it under the terms of the GNU Lesser General Public License as          *
0007  *   published by the Free Software Foundation; either version 2.1 of the    *
0008  *   License, or (at your option) version 3, or any later version accepted   *
0009  *   by the membership of KDE e.V. (or its successor approved by the         *
0010  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0011  *   Section 6 of version 3 of the license.                                  *
0012  *                                                                           *
0013  *   This program is distributed in the hope that it will be useful,         *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0016  *   Lesser General Public License for more details.                         *
0017  *                                                                           *
0018  *   You should have received a copy of the GNU Lesser General Public        *
0019  *   License along with this library. If not,                                *
0020  *   see <http://www.gnu.org/licenses/>.                                     *
0021  *****************************************************************************/
0022 
0023 #include "imagepropertiesdialog.h"
0024 
0025 #include <common/common.h>
0026 #include <common/kf5_utils.h>
0027 
0028 #include <QFile>
0029 #include <QFileInfo>
0030 #include <QGridLayout>
0031 #include <QVBoxLayout>
0032 #include <QDialogButtonBox>
0033 #include <QLabel>
0034 
0035 #define MIN_SIZE 16
0036 #define MAX_SIZE 1024
0037 #define DEF_SIZE 256
0038 
0039 static void
0040 insertPosEntries(QComboBox *combo)
0041 {
0042     combo->insertItem(PP_TL, i18n("Top left"));
0043     combo->insertItem(PP_TM, i18n("Top middle"));
0044     combo->insertItem(PP_TR, i18n("Top right"));
0045     combo->insertItem(PP_BL, i18n("Bottom left"));
0046     combo->insertItem(PP_BM, i18n("Bottom middle"));
0047     combo->insertItem(PP_BR, i18n("Bottom right"));
0048     combo->insertItem(PP_LM, i18n("Left middle"));
0049     combo->insertItem(PP_RM, i18n("Right middle"));
0050     combo->insertItem(PP_CENTRED, i18n("Centred"));
0051 }
0052 
0053 CImagePropertiesDialog::CImagePropertiesDialog(const QString &title,
0054                                                QWidget *parent, int props)
0055     : QDialog(parent),
0056       properties(props)
0057 {
0058     auto mainLayout = new QVBoxLayout(this);
0059     auto page = new QWidget(this);
0060     auto buttonBox = QtCurve::createDialogButtonBox(this);
0061 
0062     setupUi(page);
0063     mainLayout->addWidget(page);
0064     mainLayout->addWidget(buttonBox);
0065     if (QWidget *win = window()) {
0066         win->setWindowTitle(i18n("Edit %1", title));
0067     }
0068     fileRequester->setMode(KFile::File | KFile::ExistingOnly |
0069                            KFile::LocalOnly);
0070     fileRequester->setFilter("image/svg+xml image/png image/jpeg image/bmp "
0071                              "image/gif image/xpixmap");
0072 
0073     if (props & SCALE) {
0074         scaleWidth->setRange(MIN_SIZE, MAX_SIZE);
0075         scaleHeight->setRange(MIN_SIZE, MAX_SIZE);
0076     }
0077 
0078     if (props & POS) {
0079         insertPosEntries(posCombo);
0080     }
0081 
0082     scaleControls->setVisible(props&SCALE);
0083     scaleImage->setVisible(props&SCALE);
0084     onBorder->setVisible(props&BORDER);
0085     onBorderLabel->setVisible(props&BORDER);
0086     posCombo->setVisible(props&POS);
0087     posLabel->setVisible(props&POS);
0088 
0089     set(QString(), DEF_SIZE, DEF_SIZE, PP_TR, false);
0090 }
0091 
0092 bool CImagePropertiesDialog::run()
0093 {
0094     QString oldFile = fileName();
0095     int oldWidth = imgWidth();
0096     int oldHeight = imgHeight();
0097     int oldPos = imgPos();
0098     bool oldOnBorder = onBorder->isChecked();
0099 
0100     if (exec() == QDialog::Accepted)
0101         return true;
0102 
0103     set(oldFile, oldWidth, oldHeight, oldPos, oldOnBorder);
0104     return false;
0105 }
0106 
0107 void
0108 CImagePropertiesDialog::set(const QString &file, int width, int height,
0109                             int pos, bool onWindowBorder)
0110 {
0111     if (properties & SCALE) {
0112         scaleImage->setChecked(width || height);
0113         scaleWidth->setValue(width < MIN_SIZE || width > MAX_SIZE ?
0114                              DEF_SIZE : width);
0115         scaleHeight->setValue(height < MIN_SIZE || height > MAX_SIZE ?
0116                               DEF_SIZE : height);
0117     }
0118 
0119     if (properties & BORDER)
0120         onBorder->setChecked(onWindowBorder);
0121     if(properties & POS)
0122         posCombo->setCurrentIndex(pos);
0123 
0124     fileRequester->setUrl(QFile::exists(file) && !QFileInfo(file).isDir() ?
0125                           QUrl(file) : QUrl());
0126 }
0127 
0128 QSize
0129 CImagePropertiesDialog::sizeHint() const
0130 {
0131     return QSize(400, 120);
0132 }