File indexing completed on 2025-04-27 03:58:37

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-08-16
0007  * Description : Integer and double num input widget
0008  *               re-implemented with a reset button to switch to
0009  *               a default value
0010  *
0011  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "dnuminput.h"
0018 
0019 // C++ includes
0020 
0021 #include <cmath>
0022 
0023 // Qt includes
0024 
0025 #include <QToolButton>
0026 #include <QApplication>
0027 #include <QStyle>
0028 #include <QHBoxLayout>
0029 #include <QIcon>
0030 
0031 // KDE includes
0032 
0033 #include <klocalizedstring.h>
0034 
0035 // Local includes
0036 
0037 #include "dsliderspinbox.h"
0038 
0039 namespace Digikam
0040 {
0041 
0042 class Q_DECL_HIDDEN DIntNumInput::Private
0043 {
0044 
0045 public:
0046 
0047     explicit Private()
0048       : defaultValue(0),
0049         resetButton(nullptr),
0050         input(nullptr)
0051     {
0052     }
0053 
0054     int             defaultValue;
0055 
0056     QToolButton*    resetButton;
0057 
0058     DSliderSpinBox* input;
0059 };
0060 
0061 DIntNumInput::DIntNumInput(QWidget* const parent)
0062     : QWidget(parent),
0063       d(new Private)
0064 {
0065     QHBoxLayout* const hlay  = new QHBoxLayout(this);
0066     d->input                 = new DSliderSpinBox(this);
0067     d->resetButton           = new QToolButton(this);
0068     d->resetButton->setAutoRaise(true);
0069     d->resetButton->setFocusPolicy(Qt::NoFocus);
0070     d->resetButton->setIcon(QIcon::fromTheme(QLatin1String("document-revert")));
0071     d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value"));
0072 
0073     hlay->addWidget(d->input);
0074     hlay->addWidget(d->resetButton);
0075     hlay->setContentsMargins(QMargins());
0076     hlay->setStretchFactor(d->input, 10);
0077     hlay->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0078                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0079 
0080     // -------------------------------------------------------------
0081 
0082     connect(d->resetButton, &QToolButton::clicked,
0083             this, &DIntNumInput::slotReset);
0084 
0085     connect(d->input, &DSliderSpinBox::valueChanged,
0086             this, &DIntNumInput::slotValueChanged);
0087 }
0088 
0089 DIntNumInput::~DIntNumInput()
0090 {
0091     delete d;
0092 }
0093 
0094 void DIntNumInput::setRange(int min, int max, int step)
0095 {
0096     d->input->setRange(min, max);
0097     d->input->setSingleStep(step);
0098 }
0099 
0100 int DIntNumInput::value() const
0101 {
0102     return d->input->value();
0103 }
0104 
0105 void DIntNumInput::setValue(int v)
0106 {
0107     d->input->setValue(v);
0108 }
0109 
0110 int DIntNumInput::defaultValue() const
0111 {
0112     return d->defaultValue;
0113 }
0114 
0115 void DIntNumInput::setDefaultValue(int v)
0116 {
0117     d->defaultValue = v;
0118     d->input->setValue(d->defaultValue);
0119     slotValueChanged(v);
0120 }
0121 
0122 void DIntNumInput::setSuffix(const QString& suffix)
0123 {
0124     d->input->setSuffix(suffix);
0125 }
0126 
0127 void DIntNumInput::slotReset()
0128 {
0129     d->input->setValue(d->defaultValue);
0130     d->resetButton->setEnabled(false);
0131     Q_EMIT reset();
0132 }
0133 
0134 void DIntNumInput::slotValueChanged(int v)
0135 {
0136     d->resetButton->setEnabled(v != d->defaultValue);
0137     Q_EMIT valueChanged(v);
0138 }
0139 
0140 // ----------------------------------------------------
0141 
0142 class Q_DECL_HIDDEN DDoubleNumInput::Private
0143 {
0144 
0145 public:
0146 
0147     explicit Private()
0148       : defaultValue(0.0),
0149         resetButton(nullptr),
0150         input(nullptr)
0151     {
0152     }
0153 
0154     double                defaultValue;
0155 
0156     QToolButton*          resetButton;
0157 
0158     DDoubleSliderSpinBox* input;
0159 };
0160 
0161 DDoubleNumInput::DDoubleNumInput(QWidget* const parent)
0162     : QWidget(parent),
0163       d(new Private)
0164 {
0165     QHBoxLayout* const hlay  = new QHBoxLayout(this);
0166     d->input                 = new DDoubleSliderSpinBox(this);
0167     d->resetButton           = new QToolButton(this);
0168     d->resetButton->setAutoRaise(true);
0169     d->resetButton->setFocusPolicy(Qt::NoFocus);
0170     d->resetButton->setIcon(QIcon::fromTheme(QLatin1String("document-revert")));
0171     d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value"));
0172 
0173     hlay->addWidget(d->input);
0174     hlay->addWidget(d->resetButton);
0175     hlay->setContentsMargins(QMargins());
0176     hlay->setStretchFactor(d->input, 10);
0177     hlay->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0178                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0179 
0180     // -------------------------------------------------------------
0181 
0182     connect(d->resetButton, &QToolButton::clicked,
0183             this, &DDoubleNumInput::slotReset);
0184 
0185     connect(d->input, &DDoubleSliderSpinBox::valueChanged,
0186             this, &DDoubleNumInput::slotValueChanged);
0187 }
0188 
0189 DDoubleNumInput::~DDoubleNumInput()
0190 {
0191     delete d;
0192 }
0193 
0194 void DDoubleNumInput::setDecimals(int p)
0195 {
0196     d->input->setRange(d->input->minimum(), d->input->maximum(), p);
0197 }
0198 
0199 void DDoubleNumInput::setRange(double min, double max, double step)
0200 {
0201     d->input->setRange(min, max, (int)qAbs(floor(log10(step))));
0202     d->input->setFastSliderStep(5 * step);
0203     d->input->setSingleStep(step);
0204 }
0205 
0206 double DDoubleNumInput::value() const
0207 {
0208     return d->input->value();
0209 }
0210 
0211 void DDoubleNumInput::setValue(double v)
0212 {
0213     d->input->setValue(v);
0214 }
0215 
0216 double DDoubleNumInput::defaultValue() const
0217 {
0218     return d->defaultValue;
0219 }
0220 
0221 void DDoubleNumInput::setDefaultValue(double v)
0222 {
0223     d->defaultValue = v;
0224     d->input->setValue(d->defaultValue);
0225     slotValueChanged(v);
0226 }
0227 
0228 void DDoubleNumInput::setSuffix(const QString& suffix)
0229 {
0230     d->input->setSuffix(suffix);
0231 }
0232 
0233 void DDoubleNumInput::slotReset()
0234 {
0235     d->input->setValue(d->defaultValue);
0236     d->resetButton->setEnabled(false);
0237     Q_EMIT reset();
0238 }
0239 
0240 void DDoubleNumInput::slotValueChanged(double v)
0241 {
0242     d->resetButton->setEnabled(v != d->defaultValue);
0243     Q_EMIT valueChanged(v);
0244 }
0245 
0246 } // namespace Digikam
0247 
0248 #include "moc_dnuminput.cpp"