KSane

labeledslider.cpp
1/*
2 * SPDX-FileCopyrightText: 2007-2011 Kare Sars <kare.sars@iki .fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "labeledslider.h"
9// Qt includes
10
11#include <QLabel>
12#include <QSlider>
13
14// KDE includes
15
16#include <KPluralHandlingSpinBox>
17#include <KSaneCore/Option>
18
19namespace KSaneIface
20{
22 int min, int max, int ste)
23 : KSaneOptionWidget(parent, ltext)
24{
25 initSlider(min, max, ste);
26}
27
29 : KSaneOptionWidget(parent, option)
30{
31 int maxValue = option->maximumValue().toInt();
32 int minValue = option->minimumValue().toInt();
33 int stepValue = option->stepValue().toInt();
34
35 initSlider(minValue, maxValue, stepValue);
36
37 KLocalizedString unitSuffix;
39 switch (unit) {
40
41 case KSaneCore::Option::UnitPixel:
42 unitSuffix = ki18ncp("SpinBox parameter unit", " Pixel", " Pixels");
43 break;
44 case KSaneCore::Option::UnitBit:
45 unitSuffix = ki18ncp("SpinBox parameter unit", " Bit", " Bits");
46 break;
47 case KSaneCore::Option::UnitMilliMeter:
48 unitSuffix = ki18ncp("SpinBox parameter unit (Millimeter)", " mm", " mm");
49 break;
50 case KSaneCore::Option::UnitDPI:
51 unitSuffix = ki18ncp("SpinBox parameter unit (Dots Per Inch)", " DPI", " DPI");
52 break;
53 case KSaneCore::Option::UnitPercent:
54 unitSuffix = ki18ncp("SpinBox parameter unit (Percentage)", " %", " %");
55 break;
56 case KSaneCore::Option::UnitMicroSecond:
57 unitSuffix = ki18ncp("SpinBox parameter unit (Microseconds)", " µs", " µs");
58 break;
59 case KSaneCore::Option::UnitSecond:
60 unitSuffix = ki18ncp("SpinBox parameter unit (seconds)", " s", " s");
61 break;
62 default:
63 unitSuffix = KLocalizedString();
64 break;
65 }
66
67 setSuffix(unitSuffix);
68 setLabelText(option->title());
69 setToolTip(option->description());
72 int value = option->value().toInt();
73 setValue(value);
74}
75
76LabeledSlider::~LabeledSlider()
77{
78}
79
80void LabeledSlider::initSlider(int minValue, int maxValue, int stepValue)
81{
82 m_step = stepValue;
83 if (m_step == 0) {
84 m_step = 1;
85 }
86
87 m_slider = new QSlider(this);
89 m_slider->setMinimum(minValue);
90 m_slider->setMaximum(maxValue);
91 m_slider->setSingleStep(m_step);
92
93 m_spinb = new KPluralHandlingSpinBox(this);
94 m_spinb->setMinimum(minValue);
95 m_spinb->setMaximum(maxValue);
96 m_slider->setSingleStep(m_step);
97 m_spinb->setValue(maxValue);
98 //m_spinb->setMinimumWidth(m_spinb->sizeHint().width()+35);
100 m_spinb->setValue(minValue);
101
102 m_spinb->setValue(minValue);
103 m_label->setBuddy(m_spinb);
104
105 connect(m_spinb, QOverload<int>::of(&QSpinBox::valueChanged), this, &LabeledSlider::syncValues);
106 connect(m_slider, &QSlider::valueChanged, this, &LabeledSlider::syncValues);
107 connect(m_slider, &QSlider::sliderReleased, this, &LabeledSlider::fixValue);
108
109 m_layout->addWidget(m_slider, 0, 2);
110 m_layout->addWidget(m_spinb, 0, 1);
111 m_layout->setColumnStretch(1, 0);
112 m_layout->setColumnStretch(2, 50);
113}
114
116{
117 m_spinb->setSuffix(text);
118}
119
121{
122 bool ok;
123 int value = val.toInt(&ok);
124 if (!ok) {
125 return;
126 }
127 if (value != m_slider->value()) {
128 m_slider->setValue(value);
129 } else if (value != m_spinb->value()) {
130 m_spinb->setValue(value);
131 }
132}
133
134void LabeledSlider::setRange(int min, int max)
135{
136 //std::cout << "min=" << min << ", max=" << max << std::endl;
137 m_slider->setRange(min, max);
138 m_spinb->setRange(min, max);
139}
140
141void LabeledSlider::setStep(int st)
142{
143 m_step = st;
144 if (m_step == 0) {
145 m_step = 1;
146 }
147 m_slider->setSingleStep(m_step);
148 m_spinb->setSingleStep(m_step);
149}
150
151void LabeledSlider::syncValues(int value)
152{
153 if (value != m_spinb->value()) {
154 m_spinb->setValue(value);
155 } else if (value != m_slider->value()) {
156 //ensure that the value m_step is followed also for the m_slider
157 if ((value - m_slider->minimum()) % m_step != 0) {
158 if (value > m_spinb->value()) {
159 m_slider->setValue(m_slider->value() + (m_step - (value - m_spinb->value())));
160 } else {
161 m_slider->setValue(m_slider->value() - (m_step - (m_spinb->value() - value)));
162 }
163 // this function will be reentered with the signal fom the m_slider
164 } else {
165 m_slider->setValue(value);
166 }
167 } else {
168 Q_EMIT valueChanged(value);
169 }
170}
171
172void LabeledSlider::fixValue()
173{
174 //ensure that the value m_step is followed also for the m_slider
175 int rest = (m_slider->value() - m_slider->minimum()) % m_step;
176
177 if (rest != 0) {
178 if (rest > (m_step / 2)) {
179 m_slider->setValue(m_slider->value() + (m_step - rest));
180 } else {
181 m_slider->setValue(m_slider->value() - rest);
182 }
183 m_spinb->setValue(m_slider->value());
184 }
185}
186int LabeledSlider::value() const
187{
188 return (m_slider->value());
189}
190
191} // NameSpace KSaneIface
192
193#include "moc_labeledslider.cpp"
void setSuffix(const KLocalizedString &suffix)
void valueChanged(const QVariant &value)
OptionUnit valueUnit() const
QVariant minimumValue() const
bool setValue(const QVariant &value)
QVariant stepValue() const
QString title() const
QVariant value() const
QString description() const
QVariant maximumValue() const
Base class for option widgets.
void setValue(const QVariant &val)
Set the slider value.
void setSuffix(const KLocalizedString &text)
Set the unit.
void valueChanged(const QVariant &val)
Emit the slider value changes.
LabeledSlider(QWidget *parent, const QString &text, int min, int max, int st)
Create the slider.
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
void setMaximum(int)
void setMinimum(int)
void setOrientation(Qt::Orientation)
void setRange(int min, int max)
void setSingleStep(int)
void sliderReleased()
void valueChanged(int value)
void setAlignment(Qt::Alignment flag)
void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment)
void setColumnStretch(int column, int stretch)
void setBuddy(QWidget *buddy)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setMaximum(int max)
void setMinimum(int min)
void setRange(int minimum, int maximum)
void setSingleStep(int val)
void setValue(int val)
void valueChanged(int i)
AlignRight
Horizontal
int toInt(bool *ok) const const
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.