8#include "labeledfslider.h"
12#include <KLocalizedString>
14#define FLOAT_MULTIP 32768.0
15#define TO_DOUBLE(v) (static_cast<double>(v) / FLOAT_MULTIP)
16#define TO_FIX(v) (static_cast<int>(v * FLOAT_MULTIP))
22 double min,
double max,
double step)
25 initFSlider(min, max, step);
33 double stepValue = option->
stepValue().toDouble();
34 initFSlider(minValue, maxValue , stepValue);
39 case KSaneCore::Option::UnitPixel:
40 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit",
" Pixels");
42 case KSaneCore::Option::UnitBit:
43 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit",
" Bits");
45 case KSaneCore::Option::UnitMilliMeter:
46 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit (Millimeter)",
" mm");
48 case KSaneCore::Option::UnitDPI:
49 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit (Dots Per Inch)",
" DPI");
51 case KSaneCore::Option::UnitPercent:
52 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit (Percentage)",
" %");
54 case KSaneCore::Option::UnitMicroSecond:
55 unitSuffix =
i18nc(
"Double numbers. SpinBox parameter unit (Microseconds)",
" µs");
57 case KSaneCore::Option::UnitSecond:
58 unitSuffix =
i18nc(
"SpinBox parameter unit (seconds), float",
" s");
66 setLabelText(option->
title());
74void LabeledFSlider::initFSlider(
double minValue,
double maxValue,
double stepValue)
76 int imin = TO_FIX(minValue);
77 int imax = TO_FIX(maxValue);
78 m_istep = TO_FIX(stepValue);
82 m_fstep = TO_DOUBLE(m_istep);
87 m_slider =
new QSlider(
this);
89 m_slider->setMinimum(imin);
90 m_slider->setMaximum(imax);
91 m_slider->setSingleStep(m_istep);
92 m_slider->setValue(imin);
94 m_spinb =
new QDoubleSpinBox(
this);
95 m_spinb->setMinimum(minValue);
96 m_spinb->setMaximum(maxValue);
97 m_spinb->setSingleStep(m_fstep);
99 double tmp_step = m_fstep;
100 while (tmp_step < 1) {
107 m_spinb->setDecimals(decimals);
108 m_spinb->setValue(maxValue);
110 m_spinb->setMinimumWidth(m_spinb->sizeHint().width());
112 m_spinb->setValue(minValue);
114 m_label->setBuddy(m_spinb);
120 m_layout->addWidget(m_slider, 0, 2);
121 m_layout->addWidget(m_spinb, 0, 1);
122 m_layout->setColumnStretch(1, 0);
123 m_layout->setColumnStretch(2, 50);
124 m_layout->activate();
127LabeledFSlider::~LabeledFSlider()
133 return m_spinb->value();
136double LabeledFSlider::step()
const
143 m_spinb->setSuffix(text);
146void LabeledFSlider::setRange(
double min,
double max)
150 int imin = TO_FIX(min);
151 int imax = TO_FIX(max);
158void LabeledFSlider::setStep(
double step)
160 m_istep = TO_FIX(step);
164 m_fstep = TO_DOUBLE(m_istep);
166 m_slider->setSingleStep(m_istep);
167 m_spinb->setSingleStep(m_fstep);
170 double tmp_step = m_fstep;
171 while (tmp_step < 1) {
178 m_spinb->setDecimals(decimals);
184 double newValue =
value.toDouble(&ok);
188 int ivalue = TO_FIX(newValue);
190 if (((newValue - m_spinb->value()) > m_fstep) || ((m_spinb->value() - newValue) > m_fstep)) {
191 m_spinb->setValue(newValue);
192 }
else if (ivalue != m_slider->value()) {
193 m_slider->setValue(ivalue);
197void LabeledFSlider::syncValues(
int ivalue)
199 double value = TO_DOUBLE(ivalue);
202 }
else if (ivalue != m_slider->
value()) {
209void LabeledFSlider::syncValues(
double value)
211 int ivalue = TO_FIX(
value);
212 if (((
value - m_spinb->value()) > m_fstep) || ((m_spinb->value() -
value) > m_fstep)) {
213 m_spinb->setValue(
value);
214 }
else if (ivalue != m_slider->value()) {
215 m_slider->setValue(ivalue);
221void LabeledFSlider::fixValue()
224 int rest = (m_slider->value() - m_slider->minimum()) % m_istep;
227 if (rest > (m_istep / 2)) {
228 m_slider->setValue(m_slider->value() + (m_istep - rest));
230 m_slider->setValue(m_slider->value() - rest);
232 m_spinb->setValue(TO_DOUBLE(m_slider->value()));
238#include "moc_labeledfslider.cpp"
void valueChanged(const QVariant &value)
OptionUnit valueUnit() const
QVariant minimumValue() const
bool setValue(const QVariant &value)
QVariant stepValue() const
QString description() const
QVariant maximumValue() const
LabeledFSlider(QWidget *parent, const QString &text, double min, double max, double step)
Create the slider.
void valueChanged(const QVariant &val)
Emit the slider value changes.
void setValue(const QVariant &value)
Set the slider/spinbox value.
void setSuffix(const QString &text)
Set the unit.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setRange(int min, int max)
void valueChanged(int value)
void setRange(double minimum, double maximum)
void valueChanged(double d)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
double toDouble(bool *ok) const const