File indexing completed on 2024-05-12 05:49:26

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 #include <KLocale>
0025 #include <KUrlRequester>
0026 #include <KFileDialog>
0027 #include <QFile>
0028 #include <QFileInfo>
0029 #include <QGridLayout>
0030 #include <QLabel>
0031 #include <common/common.h>
0032 
0033 #define MIN_SIZE 16
0034 #define MAX_SIZE 1024
0035 #define DEF_SIZE 256
0036 
0037 static void insertPosEntries(QComboBox *combo)
0038 {
0039     combo->insertItem(PP_TL, i18n("Top left"));
0040     combo->insertItem(PP_TM, i18n("Top middle"));
0041     combo->insertItem(PP_TR, i18n("Top right"));
0042     combo->insertItem(PP_BL, i18n("Bottom left"));
0043     combo->insertItem(PP_BM, i18n("Bottom middle"));
0044     combo->insertItem(PP_BR, i18n("Bottom right"));
0045     combo->insertItem(PP_LM, i18n("Left middle"));
0046     combo->insertItem(PP_RM, i18n("Right middle"));
0047     combo->insertItem(PP_CENTRED, i18n("Centred"));
0048 }
0049 
0050 CImagePropertiesDialog::CImagePropertiesDialog(const QString &title, QWidget *parent, int props)
0051                       : KDialog(parent)
0052                       , properties(props)
0053 {
0054     QWidget *page = new QWidget(this);
0055 
0056     setButtons(Ok|Cancel);
0057     setDefaultButton(Ok);
0058     setupUi(page);
0059     setMainWidget(page);
0060     setCaption(i18n("Edit %1", title));
0061     fileRequester->setMode(KFile::File|KFile::ExistingOnly|KFile::LocalOnly);
0062     fileRequester->fileDialog()->setFilter("image/svg+xml image/png image/jpeg image/bmp image/gif image/xpixmap");
0063 
0064     if(props&SCALE)
0065     {
0066         scaleWidth->setRange(MIN_SIZE, MAX_SIZE);
0067         scaleHeight->setRange(MIN_SIZE, MAX_SIZE);
0068     }
0069 
0070     if(props&POS)
0071         insertPosEntries(posCombo);
0072 
0073     scaleControls->setVisible(props&SCALE);
0074     scaleImage->setVisible(props&SCALE);
0075     onBorder->setVisible(props&BORDER);
0076     onBorderLabel->setVisible(props&BORDER);
0077     posCombo->setVisible(props&POS);
0078     posLabel->setVisible(props&POS);
0079 
0080     set(QString(), DEF_SIZE, DEF_SIZE, PP_TR, false);
0081 }
0082 
0083 bool CImagePropertiesDialog::run()
0084 {
0085     QString oldFile=fileName();
0086     int     oldWidth=imgWidth(),
0087             oldHeight=imgHeight(),
0088             oldPos=imgPos();
0089     bool    oldOnBorder=onBorder->isChecked();
0090 
0091     if(QDialog::Accepted==exec())
0092         return true;
0093 
0094     set(oldFile, oldWidth, oldHeight, oldPos, oldOnBorder);
0095     return false;
0096 }
0097 
0098 void CImagePropertiesDialog::set(const QString &file, int width, int height, int pos, bool onWindowBorder)
0099 {
0100     if(properties&SCALE)
0101     {
0102         scaleImage->setChecked(0!=width || 0!=height);
0103         scaleWidth->setValue(width<MIN_SIZE || width>MAX_SIZE ? DEF_SIZE : width);
0104         scaleHeight->setValue(height<MIN_SIZE || height>MAX_SIZE ? DEF_SIZE : height);
0105     }
0106 
0107     if(properties&BORDER)
0108         onBorder->setChecked(onWindowBorder);
0109     if(properties&POS)
0110         posCombo->setCurrentIndex(pos);
0111 
0112     fileRequester->setUrl(QFile::exists(file) && !QFileInfo(file).isDir() ? KUrl(file) : KUrl());
0113 }
0114 
0115 QSize CImagePropertiesDialog::sizeHint() const
0116 {
0117     return QSize(400, 120);
0118 }