| 1 |
/** =========================================================== |
| 2 |
* @file |
| 3 |
* |
| 4 |
* This file is a part of digiKam project |
| 5 |
* <a href="https://www.digikam.org">https://www.digikam.org</a> |
| 6 |
* |
| 7 |
* @date 2008-08-16 |
| 8 |
* @brief Integer and double num input widget |
| 9 |
* re-implemented with a reset button to switch to |
| 10 |
* a default value |
| 11 |
* |
| 12 |
* @author Copyright (C) 2008-2015 by Gilles Caulier |
| 13 |
* <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a> |
| 14 |
* |
| 15 |
* This program is free software; you can redistribute it |
| 16 |
* and/or modify it under the terms of the GNU General |
| 17 |
* Public License as published by the Free Software Foundation; |
| 18 |
* either version 2, or (at your option) |
| 19 |
* any later version. |
| 20 |
* |
| 21 |
* This program is distributed in the hope that it will be useful, |
| 22 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
* GNU General Public License for more details. |
| 25 |
* |
| 26 |
* ============================================================ */ |
| 27 |
|
| 28 |
#include "rnuminput.h" |
| 29 |
|
| 30 |
// C++ includes |
| 31 |
|
| 32 |
#include <cmath> |
| 33 |
|
| 34 |
// Qt includes |
| 35 |
|
| 36 |
#include <QToolButton> |
| 37 |
#include <QApplication> |
| 38 |
#include <QStyle> |
| 39 |
#include <QHBoxLayout> |
| 40 |
|
| 41 |
// KDE includes |
| 42 |
|
| 43 |
#include <klocalizedstring.h> |
| 44 |
|
| 45 |
// Local includes |
| 46 |
|
| 47 |
#include "rsliderspinbox.h" |
| 48 |
|
| 49 |
#include <kis_icon_utils.h> |
| 50 |
|
| 51 |
namespace KDcrawIface |
| 52 |
{ |
| 53 |
|
| 54 |
class Q_DECL_HIDDEN RIntNumInput::Private |
| 55 |
{ |
| 56 |
|
| 57 |
public: |
| 58 |
|
| 59 |
Private() |
| 60 |
{ |
| 61 |
defaultValue = 0; |
| 62 |
resetButton = 0; |
| 63 |
input = 0; |
| 64 |
} |
| 65 |
|
| 66 |
int defaultValue; |
| 67 |
|
| 68 |
QToolButton* resetButton; |
| 69 |
|
| 70 |
RSliderSpinBox* input; |
| 71 |
}; |
| 72 |
|
| 73 |
RIntNumInput::RIntNumInput(QWidget* const parent) |
| 74 |
: QWidget(parent), |
| 75 |
d(new Private) |
| 76 |
{ |
| 77 |
QHBoxLayout* const hlay = new QHBoxLayout(this); |
| 78 |
d->input = new RSliderSpinBox(this); |
| 79 |
d->resetButton = new QToolButton(this); |
| 80 |
d->resetButton->setAutoRaise(true); |
| 81 |
d->resetButton->setFocusPolicy(Qt::NoFocus); |
| 82 |
d->resetButton->setIcon(KisIconUtils::loadIcon("document-revert").pixmap(16, 16)); |
| 83 |
d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value")); |
| 84 |
|
| 85 |
hlay->addWidget(d->input); |
| 86 |
hlay->addWidget(d->resetButton); |
| 87 |
hlay->setContentsMargins(QMargins()); |
| 88 |
hlay->setStretchFactor(d->input, 10); |
| 89 |
hlay->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing)); |
| 90 |
|
| 91 |
// ------------------------------------------------------------- |
| 92 |
|
| 93 |
connect(d->resetButton, &QToolButton::clicked, |
| 94 |
this, &RIntNumInput::slotReset); |
| 95 |
|
| 96 |
connect(d->input, &RSliderSpinBox::valueChanged, |
| 97 |
this, &RIntNumInput::slotValueChanged); |
| 98 |
} |
| 99 |
|
| 100 |
RIntNumInput::~RIntNumInput() |
| 101 |
{ |
| 102 |
delete d; |
| 103 |
} |
| 104 |
|
| 105 |
void RIntNumInput::setRange(int min, int max, int step) |
| 106 |
{ |
| 107 |
d->input->setRange(min, max); |
| 108 |
d->input->setSingleStep(step); |
| 109 |
} |
| 110 |
|
| 111 |
int RIntNumInput::value() const |
| 112 |
{ |
| 113 |
return d->input->value(); |
| 114 |
} |
| 115 |
|
| 116 |
void RIntNumInput::setValue(int v) |
| 117 |
{ |
| 118 |
d->input->setValue(v); |
| 119 |
} |
| 120 |
|
| 121 |
int RIntNumInput::defaultValue() const |
| 122 |
{ |
| 123 |
return d->defaultValue; |
| 124 |
} |
| 125 |
|
| 126 |
void RIntNumInput::setDefaultValue(int v) |
| 127 |
{ |
| 128 |
d->defaultValue = v; |
| 129 |
d->input->setValue(d->defaultValue); |
| 130 |
slotValueChanged(v); |
| 131 |
} |
| 132 |
|
| 133 |
void RIntNumInput::setSuffix(const QString& suffix) |
| 134 |
{ |
| 135 |
d->input->setSuffix(suffix); |
| 136 |
} |
| 137 |
|
| 138 |
void RIntNumInput::slotReset() |
| 139 |
{ |
| 140 |
d->input->setValue(d->defaultValue); |
| 141 |
d->resetButton->setEnabled(false); |
| 142 |
Q_EMIT reset(); |
| 143 |
} |
| 144 |
|
| 145 |
void RIntNumInput::slotValueChanged(int v) |
| 146 |
{ |
| 147 |
d->resetButton->setEnabled(v != d->defaultValue); |
| 148 |
Q_EMIT valueChanged(v); |
| 149 |
} |
| 150 |
|
| 151 |
// ---------------------------------------------------- |
| 152 |
|
| 153 |
class Q_DECL_HIDDEN RDoubleNumInput::Private |
| 154 |
{ |
| 155 |
|
| 156 |
public: |
| 157 |
|
| 158 |
Private() |
| 159 |
{ |
| 160 |
defaultValue = 0.0; |
| 161 |
resetButton = 0; |
| 162 |
input = 0; |
| 163 |
} |
| 164 |
|
| 165 |
double defaultValue; |
| 166 |
|
| 167 |
QToolButton* resetButton; |
| 168 |
|
| 169 |
RDoubleSliderSpinBox* input; |
| 170 |
}; |
| 171 |
|
| 172 |
RDoubleNumInput::RDoubleNumInput(QWidget* const parent) |
| 173 |
: QWidget(parent), |
| 174 |
d(new Private) |
| 175 |
{ |
| 176 |
QHBoxLayout* const hlay = new QHBoxLayout(this); |
| 177 |
d->input = new RDoubleSliderSpinBox(this); |
| 178 |
d->resetButton = new QToolButton(this); |
| 179 |
d->resetButton->setAutoRaise(true); |
| 180 |
d->resetButton->setFocusPolicy(Qt::NoFocus); |
| 181 |
d->resetButton->setIcon(KisIconUtils::loadIcon("document-revert").pixmap(16, 16)); |
| 182 |
d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value")); |
| 183 |
|
| 184 |
hlay->addWidget(d->input); |
| 185 |
hlay->addWidget(d->resetButton); |
| 186 |
hlay->setContentsMargins(QMargins()); |
| 187 |
hlay->setStretchFactor(d->input, 10); |
| 188 |
hlay->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing)); |
| 189 |
|
| 190 |
// ------------------------------------------------------------- |
| 191 |
|
| 192 |
connect(d->resetButton, &QToolButton::clicked, |
| 193 |
this, &RDoubleNumInput::slotReset); |
| 194 |
|
| 195 |
connect(d->input, &RDoubleSliderSpinBox::valueChanged, |
| 196 |
this, &RDoubleNumInput::slotValueChanged); |
| 197 |
} |
| 198 |
|
| 199 |
RDoubleNumInput::~RDoubleNumInput() |
| 200 |
{ |
| 201 |
delete d; |
| 202 |
} |
| 203 |
|
| 204 |
void RDoubleNumInput::setDecimals(int p) |
| 205 |
{ |
| 206 |
d->input->setRange(d->input->minimum(), d->input->maximum(), p); |
| 207 |
} |
| 208 |
|
| 209 |
void RDoubleNumInput::setRange(double min, double max, double step) |
| 210 |
{ |
| 211 |
d->input->setRange(min, max, (int) -floor(log10(step))); |
| 212 |
d->input->setFastSliderStep(5 * step); |
| 213 |
d->input->setSingleStep(step); |
| 214 |
} |
| 215 |
|
| 216 |
double RDoubleNumInput::value() const |
| 217 |
{ |
| 218 |
return d->input->value(); |
| 219 |
} |
| 220 |
|
| 221 |
void RDoubleNumInput::setValue(double v) |
| 222 |
{ |
| 223 |
d->input->setValue(v); |
| 224 |
} |
| 225 |
|
| 226 |
double RDoubleNumInput::defaultValue() const |
| 227 |
{ |
| 228 |
return d->defaultValue; |
| 229 |
} |
| 230 |
|
| 231 |
void RDoubleNumInput::setDefaultValue(double v) |
| 232 |
{ |
| 233 |
d->defaultValue = v; |
| 234 |
d->input->setValue(d->defaultValue); |
| 235 |
slotValueChanged(v); |
| 236 |
} |
| 237 |
|
| 238 |
void RDoubleNumInput::setSuffix(const QString& suffix) |
| 239 |
{ |
| 240 |
d->input->setSuffix(suffix); |
| 241 |
} |
| 242 |
|
| 243 |
void RDoubleNumInput::slotReset() |
| 244 |
{ |
| 245 |
d->input->setValue(d->defaultValue); |
| 246 |
d->resetButton->setEnabled(false); |
| 247 |
Q_EMIT reset(); |
| 248 |
} |
| 249 |
|
| 250 |
void RDoubleNumInput::slotValueChanged(double v) |
| 251 |
{ |
| 252 |
d->resetButton->setEnabled(v != d->defaultValue); |
| 253 |
Q_EMIT valueChanged(v); |
| 254 |
} |
| 255 |
|
| 256 |
} // namespace KDcrawIface |