File indexing completed on 2024-06-16 04:09:54

0001 /*
0002    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0003    Copyright (c) 2011 Martin Koller <kollix@aon.at>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG 0
0030 
0031 
0032 #include "kpTransformResizeScaleDialog.h"
0033 
0034 
0035 #include <QButtonGroup>
0036 #include <QCheckBox>
0037 #include <QComboBox>
0038 #include <QDialogButtonBox>
0039 #include <QGridLayout>
0040 #include <QGroupBox>
0041 #include <QLabel>
0042 #include <QPixmap>
0043 #include <QSize>
0044 #include <QToolButton>
0045 #include <QSpinBox>
0046 #include <QDoubleSpinBox>
0047 
0048 #include <KSharedConfig>
0049 #include <KConfigGroup>
0050 #include "kpLogCategories.h"
0051 #include <KLocalizedString>
0052 
0053 #include "layers/selections/kpAbstractSelection.h"
0054 #include "kpDefs.h"
0055 #include "document/kpDocument.h"
0056 #include "layers/selections/text/kpTextSelection.h"
0057 #include "tools/kpTool.h"
0058 #include "environments/dialogs/imagelib/transforms/kpTransformDialogEnvironment.h"
0059 
0060 //---------------------------------------------------------------------
0061 
0062 #define kpSettingResizeScaleLastKeepAspect "Resize Scale - Last Keep Aspect"
0063 #define kpSettingResizeScaleScaleType "Resize Scale - ScaleType"
0064 
0065 //---------------------------------------------------------------------
0066 
0067 #define SET_VALUE_WITHOUT_SIGNAL_EMISSION(knuminput_instance,value)    \
0068 {                                                                      \
0069     knuminput_instance->blockSignals (true);                           \
0070     knuminput_instance->setValue (value);                              \
0071     knuminput_instance->blockSignals (false);                          \
0072 }
0073 
0074 #define IGNORE_KEEP_ASPECT_RATIO(cmd) \
0075 {                                     \
0076     m_ignoreKeepAspectRatio++;        \
0077     cmd;                              \
0078     m_ignoreKeepAspectRatio--;        \
0079 }
0080 
0081 //---------------------------------------------------------------------
0082 
0083 kpTransformResizeScaleDialog::kpTransformResizeScaleDialog (
0084         kpTransformDialogEnvironment *_env, QWidget *parent)
0085     : QDialog (parent),
0086       m_environ (_env),
0087       m_ignoreKeepAspectRatio (0),
0088       m_lastType(kpTransformResizeScaleCommand::Resize)
0089 {
0090     setWindowTitle (i18nc ("@title:window", "Resize / Scale"));
0091     QDialogButtonBox *buttons = new QDialogButtonBox (QDialogButtonBox::Ok |
0092                                                       QDialogButtonBox::Cancel, this);
0093     connect (buttons, &QDialogButtonBox::accepted, this, &kpTransformResizeScaleDialog::accept);
0094     connect (buttons, &QDialogButtonBox::rejected, this, &kpTransformResizeScaleDialog::reject);
0095 
0096     QWidget *baseWidget = new QWidget (this);
0097 
0098     auto *dialogLayout = new QVBoxLayout (this);
0099     dialogLayout->addWidget (baseWidget);
0100     dialogLayout->addWidget (buttons);
0101 
0102     QWidget *actOnBox = createActOnBox(baseWidget);
0103     QGroupBox *operationGroupBox = createOperationGroupBox(baseWidget);
0104     QGroupBox *dimensionsGroupBox = createDimensionsGroupBox(baseWidget);
0105 
0106     auto *baseLayout = new QVBoxLayout (baseWidget);
0107     baseLayout->setContentsMargins(0, 0, 0, 0);
0108     baseLayout->addWidget(actOnBox);
0109     baseLayout->addWidget(operationGroupBox);
0110     baseLayout->addWidget(dimensionsGroupBox);
0111 
0112     KConfigGroup cfg(KSharedConfig::openConfig(), QStringLiteral(kpSettingsGroupGeneral));
0113     setKeepAspectRatio(cfg.readEntry(kpSettingResizeScaleLastKeepAspect, false));
0114     m_lastType = static_cast<kpTransformResizeScaleCommand::Type>
0115                    (cfg.readEntry(kpSettingResizeScaleScaleType,
0116                                   static_cast<int>(kpTransformResizeScaleCommand::Resize)));
0117 
0118     slotActOnChanged ();
0119 
0120     m_newWidthInput->setFocus ();
0121 
0122     //enableButtonOk (!isNoOp ());
0123 }
0124 
0125 //---------------------------------------------------------------------
0126 // private
0127 
0128 kpDocument *kpTransformResizeScaleDialog::document () const
0129 {
0130     return m_environ->document ();
0131 }
0132 
0133 //---------------------------------------------------------------------
0134 // private
0135 
0136 kpAbstractSelection *kpTransformResizeScaleDialog::selection () const
0137 {
0138     Q_ASSERT (document ());
0139     return document ()->selection ();
0140 }
0141 
0142 //---------------------------------------------------------------------
0143 // private
0144 
0145 kpTextSelection *kpTransformResizeScaleDialog::textSelection () const
0146 {
0147     Q_ASSERT (document ());
0148     return document ()->textSelection ();
0149 }
0150 
0151 //---------------------------------------------------------------------
0152 // private
0153 
0154 QWidget *kpTransformResizeScaleDialog::createActOnBox(QWidget *baseWidget)
0155 {
0156     QWidget *actOnBox = new QWidget (baseWidget);
0157 
0158 
0159     auto *actOnLabel = new QLabel (i18n ("Ac&t on:"), actOnBox);
0160     m_actOnCombo = new QComboBox (actOnBox);
0161 
0162 
0163     actOnLabel->setBuddy (m_actOnCombo);
0164 
0165     m_actOnCombo->insertItem (Image, i18n ("Entire Image"));
0166     if (selection ())
0167     {
0168         QString selName = i18n ("Selection");
0169 
0170         if (textSelection ()) {
0171             selName = i18n ("Text Box");
0172         }
0173 
0174         m_actOnCombo->insertItem (Selection, selName);
0175         m_actOnCombo->setCurrentIndex (Selection);
0176     }
0177     else
0178     {
0179         actOnLabel->setEnabled (false);
0180         m_actOnCombo->setEnabled (false);
0181     }
0182 
0183     auto *lay = new QHBoxLayout (actOnBox);
0184     lay->setContentsMargins(0, 0, 0, 0);
0185     lay->addWidget (actOnLabel);
0186     lay->addWidget (m_actOnCombo, 1);
0187 
0188     connect (m_actOnCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
0189              this, &kpTransformResizeScaleDialog::slotActOnChanged);
0190 
0191     return actOnBox;
0192 }
0193 
0194 //---------------------------------------------------------------------
0195 
0196 static void toolButtonSetLook (QToolButton *button,
0197                                const QString &iconName,
0198                                const QString &name)
0199 {
0200     QPixmap icon;
0201     const QString qrcPath = QStringLiteral(":/icons/") + iconName;
0202     if (!icon.load (qrcPath)) {
0203         qWarning() << qrcPath << "not found";
0204     } else {
0205         button->setIconSize (QSize (icon.width (), icon.height ()));
0206         button->setIcon (icon);
0207     }
0208 
0209     button->setToolButtonStyle (Qt::ToolButtonTextUnderIcon);
0210     button->setText (name);
0211     button->setFocusPolicy (Qt::StrongFocus);
0212     button->setCheckable (true);
0213 }
0214 
0215 //---------------------------------------------------------------------
0216 // private
0217 
0218 QGroupBox *kpTransformResizeScaleDialog::createOperationGroupBox (QWidget *baseWidget)
0219 {
0220     QGroupBox *operationGroupBox = new QGroupBox (i18n ("Operation"), baseWidget);
0221     operationGroupBox->setWhatsThis(
0222         i18n ("<qt>"
0223               "<ul>"
0224                   "<li><b>Resize</b>: The size of the picture will be"
0225                   " increased"
0226                   " by creating new areas to the right and/or bottom"
0227                   " (filled in with the background color) or"
0228                   " decreased by cutting"
0229                   " it at the right and/or bottom.</li>"
0230 
0231                   "<li><b>Scale</b>: The picture will be expanded"
0232                   " by duplicating pixels or squashed by dropping pixels.</li>"
0233 
0234                   "<li><b>Smooth Scale</b>: This is the same as"
0235                   " <i>Scale</i> except that it blends neighboring"
0236                   " pixels to produce a smoother looking picture.</li>"
0237               "</ul>"
0238               "</qt>"));
0239 
0240     m_resizeButton = new QToolButton (operationGroupBox);
0241     toolButtonSetLook (m_resizeButton,
0242                        QStringLiteral ("resize"),
0243                        i18n ("&Resize"));
0244 
0245     m_scaleButton = new QToolButton (operationGroupBox);
0246     toolButtonSetLook (m_scaleButton,
0247                        QStringLiteral ("scale"),
0248                        i18n ("&Scale"));
0249 
0250     m_smoothScaleButton = new QToolButton (operationGroupBox);
0251     toolButtonSetLook (m_smoothScaleButton,
0252                        QStringLiteral ("smooth_scale"),
0253                        i18n ("S&mooth Scale"));
0254 
0255     auto *resizeScaleButtonGroup = new QButtonGroup (baseWidget);
0256     resizeScaleButtonGroup->addButton (m_resizeButton);
0257     resizeScaleButtonGroup->addButton (m_scaleButton);
0258     resizeScaleButtonGroup->addButton (m_smoothScaleButton);
0259 
0260 
0261     auto *operationLayout = new QGridLayout (operationGroupBox );
0262     operationLayout->addWidget (m_resizeButton, 0, 0, Qt::AlignCenter);
0263     operationLayout->addWidget (m_scaleButton, 0, 1, Qt::AlignCenter);
0264     operationLayout->addWidget (m_smoothScaleButton, 0, 2, Qt::AlignCenter);
0265 
0266     connect (m_resizeButton, &QToolButton::toggled,
0267              this, &kpTransformResizeScaleDialog::slotTypeChanged);
0268     connect (m_scaleButton, &QToolButton::toggled,
0269              this, &kpTransformResizeScaleDialog::slotTypeChanged);
0270     connect (m_smoothScaleButton, &QToolButton::toggled,
0271              this, &kpTransformResizeScaleDialog::slotTypeChanged);
0272 
0273     return operationGroupBox;
0274 }
0275 
0276 //---------------------------------------------------------------------
0277 // private
0278 
0279 QGroupBox *kpTransformResizeScaleDialog::createDimensionsGroupBox(QWidget *baseWidget)
0280 {
0281     QGroupBox *dimensionsGroupBox = new QGroupBox (i18n ("Dimensions"), baseWidget);
0282 
0283     auto *widthLabel = new QLabel (i18n ("Width:"), dimensionsGroupBox);
0284     widthLabel->setAlignment (widthLabel->alignment () | Qt::AlignHCenter);
0285     auto *heightLabel = new QLabel (i18n ("Height:"), dimensionsGroupBox);
0286     heightLabel->setAlignment (heightLabel->alignment () | Qt::AlignHCenter);
0287 
0288     auto *originalLabel = new QLabel (i18n ("Original:"), dimensionsGroupBox);
0289     m_originalWidthInput = new QSpinBox;
0290     m_originalWidthInput->setRange(1, INT_MAX);
0291     m_originalWidthInput->setValue(document()->width(static_cast<bool> (selection())));
0292     auto *xLabel0 = new QLabel (i18n ("x"), dimensionsGroupBox);
0293     m_originalHeightInput = new QSpinBox;
0294     m_originalHeightInput->setRange(1, INT_MAX);
0295     m_originalHeightInput->setValue(document()->height(static_cast<bool> (selection())));
0296 
0297     auto *newLabel = new QLabel (i18n ("&New:"), dimensionsGroupBox);
0298     m_newWidthInput = new QSpinBox;
0299     m_newWidthInput->setRange(1, INT_MAX);
0300     auto *xLabel1 = new QLabel (i18n ("x"), dimensionsGroupBox);
0301     m_newHeightInput = new QSpinBox;
0302     m_newHeightInput->setRange(1, INT_MAX);
0303 
0304     auto *percentLabel = new QLabel (i18n ("&Percent:"), dimensionsGroupBox);
0305     m_percentWidthInput = new QDoubleSpinBox;
0306     m_percentWidthInput->setRange(0.01, 1000000);
0307     m_percentWidthInput->setValue(100);
0308     m_percentWidthInput->setSingleStep(1);
0309     m_percentWidthInput->setDecimals(2);
0310     m_percentWidthInput->setSuffix(i18n("%"));
0311 
0312     auto *xLabel2 = new QLabel (i18n ("x"), dimensionsGroupBox);
0313 
0314     m_percentHeightInput = new QDoubleSpinBox;
0315     m_percentHeightInput->setRange(0.01, 1000000);
0316     m_percentHeightInput->setValue(100);
0317     m_percentHeightInput->setSingleStep(1);
0318     m_percentHeightInput->setDecimals(2);
0319     m_percentHeightInput->setSuffix(i18n("%"));
0320 
0321     m_keepAspectRatioCheckBox = new QCheckBox (i18n ("Keep &aspect ratio"),
0322                                                dimensionsGroupBox);
0323 
0324 
0325     m_originalWidthInput->setEnabled (false);
0326     m_originalHeightInput->setEnabled (false);
0327     originalLabel->setBuddy (m_originalWidthInput);
0328     newLabel->setBuddy (m_newWidthInput);
0329     m_percentWidthInput->setValue (100);
0330     m_percentHeightInput->setValue (100);
0331     percentLabel->setBuddy (m_percentWidthInput);
0332 
0333 
0334     auto *dimensionsLayout = new QGridLayout (dimensionsGroupBox);
0335     dimensionsLayout->setColumnStretch (1/*column*/, 1);
0336     dimensionsLayout->setColumnStretch (3/*column*/, 1);
0337 
0338 
0339     dimensionsLayout->addWidget (widthLabel, 0, 1);
0340     dimensionsLayout->addWidget (heightLabel, 0, 3);
0341 
0342     dimensionsLayout->addWidget (originalLabel, 1, 0);
0343     dimensionsLayout->addWidget (m_originalWidthInput, 1, 1);
0344     dimensionsLayout->addWidget (xLabel0, 1, 2);
0345     dimensionsLayout->addWidget (m_originalHeightInput, 1, 3);
0346 
0347     dimensionsLayout->addWidget (newLabel, 2, 0);
0348     dimensionsLayout->addWidget (m_newWidthInput, 2, 1);
0349     dimensionsLayout->addWidget (xLabel1, 2, 2);
0350     dimensionsLayout->addWidget (m_newHeightInput, 2, 3);
0351 
0352     dimensionsLayout->addWidget (percentLabel, 3, 0);
0353     dimensionsLayout->addWidget (m_percentWidthInput, 3, 1);
0354     dimensionsLayout->addWidget (xLabel2, 3, 2);
0355     dimensionsLayout->addWidget (m_percentHeightInput, 3, 3);
0356 
0357     dimensionsLayout->addWidget (m_keepAspectRatioCheckBox, 4, 0, 1, 4);
0358     dimensionsLayout->setRowStretch (4/*row*/, 1);
0359     dimensionsLayout->setRowMinimumHeight (4/*row*/, dimensionsLayout->rowMinimumHeight (4) * 2);
0360 
0361 
0362 
0363     connect (m_newWidthInput, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
0364              this, &kpTransformResizeScaleDialog::slotWidthChanged);
0365 
0366     connect (m_newHeightInput, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
0367              this, &kpTransformResizeScaleDialog::slotHeightChanged);
0368 
0369     // COMPAT: KDoubleNumInput only fires valueChanged(double) once per
0370     //         edit.  It should either fire:
0371     //
0372     //             1. At the end of the edit (triggered by clicking or tabbing
0373     //                away), like with KDE 3.
0374     //
0375     //             OR
0376     //
0377     //             2. Once per keystroke.
0378     //
0379     //         Bug in KDoubleNumInput.
0380     connect (m_percentWidthInput,
0381              static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
0382              this, &kpTransformResizeScaleDialog::slotPercentWidthChanged);
0383 
0384     connect (m_percentHeightInput,
0385              static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
0386              this, &kpTransformResizeScaleDialog::slotPercentHeightChanged);
0387 
0388     connect (m_keepAspectRatioCheckBox, &QCheckBox::toggled,
0389              this, &kpTransformResizeScaleDialog::setKeepAspectRatio);
0390 
0391     return dimensionsGroupBox;
0392 }
0393 
0394 //---------------------------------------------------------------------
0395 // private
0396 
0397 void kpTransformResizeScaleDialog::widthFitHeightToAspectRatio ()
0398 {
0399     if (m_keepAspectRatioCheckBox->isChecked () && !m_ignoreKeepAspectRatio)
0400     {
0401         // width / height = oldWidth / oldHeight
0402         // height = width * oldHeight / oldWidth
0403         const int newHeight = qRound (double (imageWidth ()) * double (originalHeight ())
0404                                       / double (originalWidth ()));
0405         IGNORE_KEEP_ASPECT_RATIO (m_newHeightInput->setValue (newHeight));
0406     }
0407 }
0408 
0409 //---------------------------------------------------------------------
0410 // private
0411 
0412 void kpTransformResizeScaleDialog::heightFitWidthToAspectRatio ()
0413 {
0414     if (m_keepAspectRatioCheckBox->isChecked () && !m_ignoreKeepAspectRatio)
0415     {
0416         // width / height = oldWidth / oldHeight
0417         // width = height * oldWidth / oldHeight
0418         const int newWidth = qRound (double (imageHeight ()) * double (originalWidth ())
0419                                      / double (originalHeight ()));
0420         IGNORE_KEEP_ASPECT_RATIO (m_newWidthInput->setValue (newWidth));
0421     }
0422 }
0423 
0424 //---------------------------------------------------------------------
0425 // private
0426 
0427 bool kpTransformResizeScaleDialog::resizeEnabled () const
0428 {
0429     return (!actOnSelection () ||
0430             (actOnSelection () && textSelection ()));
0431 }
0432 
0433 //---------------------------------------------------------------------
0434 // private
0435 
0436 bool kpTransformResizeScaleDialog::scaleEnabled () const
0437 {
0438     return (!(actOnSelection () && textSelection ()));
0439 }
0440 
0441 //---------------------------------------------------------------------
0442 // private
0443 
0444 bool kpTransformResizeScaleDialog::smoothScaleEnabled () const
0445 {
0446     return scaleEnabled ();
0447 }
0448 
0449 //---------------------------------------------------------------------
0450 // public slot
0451 
0452 void kpTransformResizeScaleDialog::slotActOnChanged ()
0453 {
0454 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0455     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::slotActOnChanged()";
0456 #endif
0457 
0458     m_resizeButton->setEnabled (resizeEnabled ());
0459     m_scaleButton->setEnabled (scaleEnabled ());
0460     m_smoothScaleButton->setEnabled (smoothScaleEnabled ());
0461 
0462     // TODO: somehow share logic with (resize|*scale)Enabled()
0463     if (actOnSelection ())
0464     {
0465         if (textSelection ())
0466         {
0467             m_resizeButton->setChecked (true);
0468         }
0469         else
0470         {
0471             if (m_lastType == kpTransformResizeScaleCommand::Scale) {
0472                 m_scaleButton->setChecked (true);
0473             }
0474             else {
0475                 m_smoothScaleButton->setChecked (true);
0476             }
0477         }
0478     }
0479     else
0480     {
0481         if (m_lastType == kpTransformResizeScaleCommand::Resize) {
0482             m_resizeButton->setChecked (true);
0483         }
0484         else if (m_lastType == kpTransformResizeScaleCommand::Scale) {
0485             m_scaleButton->setChecked (true);
0486         }
0487         else {
0488             m_smoothScaleButton->setChecked (true);
0489         }
0490     }
0491 
0492 
0493     m_originalWidthInput->setValue (originalWidth ());
0494     m_originalHeightInput->setValue (originalHeight ());
0495 
0496 
0497     m_newWidthInput->blockSignals (true);
0498     m_newHeightInput->blockSignals (true);
0499 
0500     m_newWidthInput->setMinimum (actOnSelection () ?
0501                                       selection ()->minimumWidth () :
0502                                       1);
0503     m_newHeightInput->setMinimum (actOnSelection () ?
0504                                        selection ()->minimumHeight () :
0505                                        1);
0506 
0507     m_newWidthInput->blockSignals (false);
0508     m_newHeightInput->blockSignals (false);
0509 
0510 
0511     IGNORE_KEEP_ASPECT_RATIO (slotPercentWidthChanged (m_percentWidthInput->value ()));
0512     IGNORE_KEEP_ASPECT_RATIO (slotPercentHeightChanged (m_percentHeightInput->value ()));
0513 
0514     setKeepAspectRatio (m_keepAspectRatioCheckBox->isChecked ());
0515 }
0516 
0517 //---------------------------------------------------------------------
0518 // public slot
0519 
0520 void kpTransformResizeScaleDialog::slotTypeChanged ()
0521 {
0522     m_lastType = type ();
0523 }
0524 
0525 //---------------------------------------------------------------------
0526 
0527 // public slot
0528 void kpTransformResizeScaleDialog::slotWidthChanged (int width)
0529 {
0530 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0531     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::slotWidthChanged("
0532                << width << ")" << endl;
0533 #endif
0534     const double newPercentWidth = double (width) * 100 / double (originalWidth ());
0535 
0536     SET_VALUE_WITHOUT_SIGNAL_EMISSION (m_percentWidthInput,newPercentWidth);
0537 
0538     widthFitHeightToAspectRatio ();
0539 
0540     //enableButtonOk (!isNoOp ());
0541 }
0542 
0543 //---------------------------------------------------------------------
0544 // public slot
0545 
0546 void kpTransformResizeScaleDialog::slotHeightChanged (int height)
0547 {
0548 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0549     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::slotHeightChanged("
0550                << height << ")" << endl;
0551 #endif
0552     const double newPercentHeight = double (height) * 100 / double (originalHeight ());
0553 
0554     SET_VALUE_WITHOUT_SIGNAL_EMISSION (m_percentHeightInput,newPercentHeight);
0555 
0556     heightFitWidthToAspectRatio ();
0557 
0558     //enableButtonOk (!isNoOp ());
0559 }
0560 
0561 //---------------------------------------------------------------------
0562 // public slot
0563 
0564 void kpTransformResizeScaleDialog::slotPercentWidthChanged (double percentWidth)
0565 {
0566 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0567     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::slotPercentWidthChanged("
0568                << percentWidth << ")";
0569 #endif
0570 
0571     SET_VALUE_WITHOUT_SIGNAL_EMISSION (m_newWidthInput,
0572                                        qRound (percentWidth * originalWidth () / 100.0));
0573 
0574     widthFitHeightToAspectRatio ();
0575 
0576     //enableButtonOk (!isNoOp ());
0577 }
0578 
0579 //---------------------------------------------------------------------
0580 // public slot
0581 
0582 void kpTransformResizeScaleDialog::slotPercentHeightChanged (double percentHeight)
0583 {
0584 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0585     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::slotPercentHeightChanged("
0586                << percentHeight << ")";
0587 #endif
0588 
0589     SET_VALUE_WITHOUT_SIGNAL_EMISSION (m_newHeightInput,
0590                                        qRound (percentHeight * originalHeight () / 100.0));
0591 
0592     heightFitWidthToAspectRatio ();
0593 
0594     //enableButtonOk (!isNoOp ());
0595 }
0596 
0597 //---------------------------------------------------------------------
0598 // public slot
0599 
0600 void kpTransformResizeScaleDialog::setKeepAspectRatio (bool on)
0601 {
0602 #if DEBUG_KP_TOOL_RESIZE_SCALE_DIALOG && 1
0603     qCDebug(kpLogDialogs) << "kpTransformResizeScaleDialog::setKeepAspectRatio("
0604                << on << ")";
0605 #endif
0606 
0607     if (on != m_keepAspectRatioCheckBox->isChecked ()) {
0608         m_keepAspectRatioCheckBox->setChecked (on);
0609     }
0610 
0611     if (on) {
0612         widthFitHeightToAspectRatio ();
0613     }
0614 }
0615 
0616 //---------------------------------------------------------------------
0617 
0618 #undef IGNORE_KEEP_ASPECT_RATIO
0619 #undef SET_VALUE_WITHOUT_SIGNAL_EMISSION
0620 
0621 
0622 //---------------------------------------------------------------------
0623 // private
0624 
0625 int kpTransformResizeScaleDialog::originalWidth () const
0626 {
0627     return document ()->width (actOnSelection ());
0628 }
0629 
0630 //---------------------------------------------------------------------
0631 // private
0632 
0633 int kpTransformResizeScaleDialog::originalHeight () const
0634 {
0635     return document ()->height (actOnSelection ());
0636 }
0637 
0638 //---------------------------------------------------------------------
0639 // public
0640 
0641 int kpTransformResizeScaleDialog::imageWidth () const
0642 {
0643     return m_newWidthInput->value ();
0644 }
0645 
0646 //---------------------------------------------------------------------
0647 // public
0648 
0649 int kpTransformResizeScaleDialog::imageHeight () const
0650 {
0651     return m_newHeightInput->value ();
0652 }
0653 
0654 //---------------------------------------------------------------------
0655 // public
0656 
0657 bool kpTransformResizeScaleDialog::actOnSelection () const
0658 {
0659     return (m_actOnCombo->currentIndex () == Selection);
0660 }
0661 
0662 //---------------------------------------------------------------------
0663 // public
0664 
0665 kpTransformResizeScaleCommand::Type kpTransformResizeScaleDialog::type () const
0666 {
0667     if (m_resizeButton->isChecked ()) {
0668         return kpTransformResizeScaleCommand::Resize;
0669     }
0670 
0671     if (m_scaleButton->isChecked ()) {
0672         return kpTransformResizeScaleCommand::Scale;
0673     }
0674 
0675     return kpTransformResizeScaleCommand::SmoothScale;
0676 }
0677 
0678 //---------------------------------------------------------------------
0679 // public
0680 
0681 bool kpTransformResizeScaleDialog::isNoOp () const
0682 {
0683     return (imageWidth () == originalWidth () &&
0684             imageHeight () == originalHeight ());
0685 }
0686 
0687 //---------------------------------------------------------------------
0688 // private slot virtual [base QDialog]
0689 
0690 void kpTransformResizeScaleDialog::accept ()
0691 {
0692     enum { eText, eSelection, eImage } actionTarget = eText;
0693 
0694     if (actOnSelection ())
0695     {
0696         if (textSelection ())
0697         {
0698             actionTarget = eText;
0699         }
0700         else
0701         {
0702             actionTarget = eSelection;
0703         }
0704     }
0705     else
0706     {
0707         actionTarget = eImage;
0708     }
0709 
0710 
0711     KLocalizedString message;
0712     QString caption, continueButtonText;
0713 
0714     // Note: If eText, can't Scale nor SmoothScale.
0715     //       If eSelection, can't Resize.
0716 
0717     switch (type ())
0718     {
0719     default:
0720     case kpTransformResizeScaleCommand::Resize:
0721         if (actionTarget == eText)
0722         {
0723             message =
0724                 ki18n ("<qt><p>Resizing the text box to %1x%2"
0725                       " may take a substantial amount of memory."
0726                       " This can reduce system"
0727                       " responsiveness and cause other application resource"
0728                       " problems.</p>"
0729 
0730                       "<p>Are you sure you want to resize the text box?</p></qt>");
0731 
0732             caption = i18nc ("@title:window", "Resize Text Box?");
0733             continueButtonText = i18n ("R&esize Text Box");
0734         }
0735         else if (actionTarget == eImage)
0736         {
0737             message =
0738                 ki18n ("<qt><p>Resizing the image to %1x%2"
0739                       " may take a substantial amount of memory."
0740                       " This can reduce system"
0741                       " responsiveness and cause other application resource"
0742                       " problems.</p>"
0743 
0744                       "<p>Are you sure you want to resize the image?</p></qt>");
0745 
0746             caption = i18nc ("@title:window", "Resize Image?");
0747             continueButtonText = i18n ("R&esize Image");
0748         }
0749 
0750         break;
0751 
0752     case kpTransformResizeScaleCommand::Scale:
0753         if (actionTarget == eImage)
0754         {
0755             message =
0756                 ki18n ("<qt><p>Scaling the image to %1x%2"
0757                       " may take a substantial amount of memory."
0758                       " This can reduce system"
0759                       " responsiveness and cause other application resource"
0760                       " problems.</p>"
0761 
0762                       "<p>Are you sure you want to scale the image?</p></qt>");
0763 
0764             caption = i18nc ("@title:window", "Scale Image?");
0765             continueButtonText = i18n ("Scal&e Image");
0766         }
0767         else if (actionTarget == eSelection)
0768         {
0769             message =
0770                 ki18n ("<qt><p>Scaling the selection to %1x%2"
0771                       " may take a substantial amount of memory."
0772                       " This can reduce system"
0773                       " responsiveness and cause other application resource"
0774                       " problems.</p>"
0775 
0776                       "<p>Are you sure you want to scale the selection?</p></qt>");
0777 
0778             caption = i18nc ("@title:window", "Scale Selection?");
0779             continueButtonText = i18n ("Scal&e Selection");
0780         }
0781 
0782         break;
0783 
0784     case kpTransformResizeScaleCommand::SmoothScale:
0785         if (actionTarget == eImage)
0786         {
0787             message =
0788                 ki18n ("<qt><p>Smooth Scaling the image to %1x%2"
0789                       " may take a substantial amount of memory."
0790                       " This can reduce system"
0791                       " responsiveness and cause other application resource"
0792                       " problems.</p>"
0793 
0794                       "<p>Are you sure you want to smooth scale the image?</p></qt>");
0795 
0796             caption = i18nc ("@title:window", "Smooth Scale Image?");
0797             continueButtonText = i18n ("Smooth Scal&e Image");
0798         }
0799         else if (actionTarget == eSelection)
0800         {
0801             message =
0802                 ki18n ("<qt><p>Smooth Scaling the selection to %1x%2"
0803                       " may take a substantial amount of memory."
0804                       " This can reduce system"
0805                       " responsiveness and cause other application resource"
0806                       " problems.</p>"
0807 
0808                       "<p>Are you sure you want to smooth scale the selection?</p></qt>");
0809 
0810             caption = i18nc ("@title:window", "Smooth Scale Selection?");
0811             continueButtonText = i18n ("Smooth Scal&e Selection");
0812         }
0813 
0814         break;
0815     }
0816 
0817 
0818     if (kpTool::warnIfBigImageSize (originalWidth (),
0819             originalHeight (),
0820             imageWidth (), imageHeight (),
0821             message.subs (imageWidth ()).subs (imageHeight ()).toString (),
0822             caption,
0823             continueButtonText,
0824             this))
0825     {
0826         QDialog::accept ();
0827     }
0828 
0829     // store settings
0830     KConfigGroup cfg(KSharedConfig::openConfig(), QStringLiteral(kpSettingsGroupGeneral));
0831 
0832     cfg.writeEntry(kpSettingResizeScaleLastKeepAspect, m_keepAspectRatioCheckBox->isChecked());
0833     cfg.writeEntry(kpSettingResizeScaleScaleType, static_cast<int>(m_lastType));
0834     cfg.sync();
0835 }
0836 
0837 //---------------------------------------------------------------------
0838 
0839 #include "moc_kpTransformResizeScaleDialog.cpp"