aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2015-06-11 00:16:44 +0200
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2015-06-11 21:24:26 +0000
commit1c1dd5ed4748207d8643ff0530b8281c549d3517 (patch)
tree589d9995b0ef350ee9f02e9b478f8cd515b82886
parentede8e3dcfff4a6028c86ade4d2a2cb1aa9df08c6 (diff)
Slider: add from & to properties
Change-Id: Iee61e89d8519e7e3d27f1bf1c35af210f48bc7c9 Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
-rw-r--r--src/controls/qquickslider.cpp125
-rw-r--r--src/controls/qquickslider_p.h12
-rw-r--r--tests/auto/controls/data/tst_slider.qml78
3 files changed, 191 insertions, 24 deletions
diff --git a/src/controls/qquickslider.cpp b/src/controls/qquickslider.cpp
index 5ce3cb92..889737d0 100644
--- a/src/controls/qquickslider.cpp
+++ b/src/controls/qquickslider.cpp
@@ -74,13 +74,18 @@ class QQuickSliderPrivate : public QQuickControlPrivate
Q_DECLARE_PUBLIC(QQuickSlider)
public:
- QQuickSliderPrivate() : value(0), position(0), stepSize(0), pressed(false),
+ QQuickSliderPrivate() : from(0), to(1), value(0), position(0), stepSize(0), pressed(false),
orientation(Qt::Horizontal), snapMode(QQuickSlider::NoSnap),
handle(Q_NULLPTR), track(Q_NULLPTR) { }
+ qreal valueAt(qreal position) const;
qreal snapPosition(qreal position) const;
qreal positionAt(const QPoint &point) const;
+ void setPosition(qreal position);
+ void updatePosition();
+ qreal from;
+ qreal to;
qreal value;
qreal position;
qreal stepSize;
@@ -92,6 +97,11 @@ public:
QQuickItem *track;
};
+qreal QQuickSliderPrivate::valueAt(qreal position) const
+{
+ return from + (to - from) * position;
+}
+
qreal QQuickSliderPrivate::snapPosition(qreal position) const
{
if (qFuzzyIsNull(stepSize))
@@ -122,6 +132,25 @@ qreal QQuickSliderPrivate::positionAt(const QPoint &point) const
return 0;
}
+void QQuickSliderPrivate::setPosition(qreal pos)
+{
+ Q_Q(QQuickSlider);
+ pos = qBound(0.0, pos, 1.0);
+ if (!qFuzzyCompare(position, pos)) {
+ position = pos;
+ emit q->positionChanged();
+ emit q->visualPositionChanged();
+ }
+}
+
+void QQuickSliderPrivate::updatePosition()
+{
+ qreal pos = 0;
+ if (!qFuzzyCompare(from, to))
+ pos = (value - from) / (to - from);
+ setPosition(pos);
+}
+
QQuickSlider::QQuickSlider(QQuickItem *parent) :
QQuickControl(*(new QQuickSliderPrivate), parent)
{
@@ -130,14 +159,65 @@ QQuickSlider::QQuickSlider(QQuickItem *parent) :
}
/*!
+ \qmlproperty real QtQuickControls2::Slider::from
+
+ This property holds the starting value for the range. The default value is \c 0.0.
+
+ \sa to, value
+*/
+qreal QQuickSlider::from() const
+{
+ Q_D(const QQuickSlider);
+ return d->from;
+}
+
+void QQuickSlider::setFrom(qreal from)
+{
+ Q_D(QQuickSlider);
+ if (!qFuzzyCompare(d->from, from)) {
+ d->from = from;
+ emit fromChanged();
+ if (isComponentComplete()) {
+ setValue(d->value);
+ d->updatePosition();
+ }
+ }
+}
+
+/*!
+ \qmlproperty real QtQuickControls2::Slider::to
+
+ This property holds the end value for the range. The default value is \c 1.0.
+
+ \sa from, value
+*/
+qreal QQuickSlider::to() const
+{
+ Q_D(const QQuickSlider);
+ return d->to;
+}
+
+void QQuickSlider::setTo(qreal to)
+{
+ Q_D(QQuickSlider);
+ if (!qFuzzyCompare(d->to, to)) {
+ d->to = to;
+ emit toChanged();
+ if (isComponentComplete()) {
+ setValue(d->value);
+ d->updatePosition();
+ }
+ }
+}
+
+/*!
\qmlproperty real QtQuickControls2::Slider::value
- This property holds the value in the range \c 0.0 - \c 1.0. The default value is \c 0.0.
+ This property holds the value in the range \c from - \c to. The default value is \c 0.0.
- The value is defined as a percentage of the control's size, scaled
- to \c 0.0 - \c 1.0. Unlike the \l position property, the \c value is
- not updated while the handle is dragged. The value is updated after
- the value has been chosen and the slider has been released.
+ Unlike the \l position property, the \c value is not updated while the
+ handle is dragged. The value is updated after the value has been chosen
+ and the slider has been released.
\sa position
*/
@@ -150,10 +230,12 @@ qreal QQuickSlider::value() const
void QQuickSlider::setValue(qreal value)
{
Q_D(QQuickSlider);
- value = qBound(0.0, value, 1.0);
+ if (isComponentComplete())
+ value = d->from > d->to ? qBound(d->to, value, d->from) : qBound(d->from, value, d->to);
+
if (!qFuzzyCompare(d->value, value)) {
d->value = value;
- setPosition(value);
+ d->updatePosition();
emit valueChanged();
}
}
@@ -164,7 +246,7 @@ void QQuickSlider::setValue(qreal value)
This property holds the logical position of the handle.
The position is defined as a percentage of the control's size, scaled
- to \c 0.0 - \c 1.0. Unlike the \l value property, the \c position is
+ to \c {0.0 - 1.0}. Unlike the \l value property, the \c position is
continuously updated while the handle is dragged. For visualizing a
slider, the right-to-left aware \l visualPosition should be used instead.
@@ -183,7 +265,7 @@ qreal QQuickSlider::position() const
This property holds the visual position of the handle.
The position is defined as a percentage of the control's size, scaled to
- \c 0.0 - \c 1.0. When the control is \l mirrored, the value is equal to
+ \c {0.0 - 1.0}. When the control is \l mirrored, the value is equal to
\c {1.0 - position}. This makes the value suitable for visualizing the
slider, taking right-to-left support into account.
@@ -197,17 +279,6 @@ qreal QQuickSlider::visualPosition() const
return d->position;
}
-void QQuickSlider::setPosition(qreal position)
-{
- Q_D(QQuickSlider);
- position = qBound(0.0, position, 1.0);
- if (!qFuzzyCompare(d->position, position)) {
- d->position = position;
- emit positionChanged();
- emit visualPositionChanged();
- }
-}
-
/*!
\qmlproperty real QtQuickControls2::Slider::stepSize
@@ -445,7 +516,7 @@ void QQuickSlider::mouseMoveEvent(QMouseEvent *event)
qreal pos = d->positionAt(event->pos());
if (d->snapMode == SnapAlways)
pos = d->snapPosition(pos);
- setPosition(pos);
+ d->setPosition(pos);
}
event->accept();
}
@@ -459,7 +530,7 @@ void QQuickSlider::mouseReleaseEvent(QMouseEvent *event)
qreal pos = d->positionAt(event->pos());
if (d->snapMode != NoSnap)
pos = d->snapPosition(pos);
- setValue(pos);
+ setValue(d->valueAt(pos));
setKeepMouseGrab(false);
}
setPressed(false);
@@ -480,4 +551,12 @@ void QQuickSlider::mirrorChange()
emit visualPositionChanged();
}
+void QQuickSlider::componentComplete()
+{
+ Q_D(QQuickSlider);
+ QQuickControl::componentComplete();
+ setValue(d->value);
+ d->updatePosition();
+}
+
QT_END_NAMESPACE
diff --git a/src/controls/qquickslider_p.h b/src/controls/qquickslider_p.h
index bd5411ed..0e0f8e90 100644
--- a/src/controls/qquickslider_p.h
+++ b/src/controls/qquickslider_p.h
@@ -57,6 +57,8 @@ class QQuickSliderPrivate;
class Q_QUICKCONTROLS_EXPORT QQuickSlider : public QQuickControl
{
Q_OBJECT
+ Q_PROPERTY(qreal from READ from WRITE setFrom NOTIFY fromChanged FINAL)
+ Q_PROPERTY(qreal to READ to WRITE setTo NOTIFY toChanged FINAL)
Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged FINAL)
Q_PROPERTY(qreal position READ position NOTIFY positionChanged FINAL)
Q_PROPERTY(qreal visualPosition READ visualPosition NOTIFY visualPositionChanged FINAL)
@@ -70,12 +72,17 @@ class Q_QUICKCONTROLS_EXPORT QQuickSlider : public QQuickControl
public:
explicit QQuickSlider(QQuickItem *parent = Q_NULLPTR);
+ qreal from() const;
+ void setFrom(qreal from);
+
+ qreal to() const;
+ void setTo(qreal to);
+
qreal value() const;
void setValue(qreal value);
qreal position() const;
qreal visualPosition() const;
- void setPosition(qreal position);
qreal stepSize() const;
void setStepSize(qreal step);
@@ -107,6 +114,8 @@ public Q_SLOTS:
void decrease();
Q_SIGNALS:
+ void fromChanged();
+ void toChanged();
void valueChanged();
void positionChanged();
void visualPositionChanged();
@@ -125,6 +134,7 @@ protected:
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseUngrabEvent() Q_DECL_OVERRIDE;
void mirrorChange() Q_DECL_OVERRIDE;
+ void componentComplete() Q_DECL_OVERRIDE;
private:
Q_DISABLE_COPY(QQuickSlider)
diff --git a/tests/auto/controls/data/tst_slider.qml b/tests/auto/controls/data/tst_slider.qml
index 087024f6..2e9dec2b 100644
--- a/tests/auto/controls/data/tst_slider.qml
+++ b/tests/auto/controls/data/tst_slider.qml
@@ -85,6 +85,84 @@ TestCase {
control.destroy()
}
+ function test_value() {
+ var control = slider.createObject(testCase, {value: 0.5})
+ compare(control.value, 0.5)
+ control.value = 1.0
+ compare(control.value, 1.0)
+ control.value = -1.0
+ compare(control.value, 0.0)
+ control.value = 2.0
+ compare(control.value, 1.0)
+ control.destroy()
+ }
+
+ function test_range() {
+ var control = slider.createObject(testCase, {from: 0, to: 100, value: 50})
+ compare(control.from, 0)
+ compare(control.to, 100)
+ compare(control.value, 50)
+ compare(control.position, 0.5)
+
+ control.value = 1000
+ compare(control.value, 100)
+ compare(control.position, 1)
+
+ control.value = -1
+ compare(control.value, 0)
+ compare(control.position, 0)
+
+ control.from = 25
+ compare(control.from, 25)
+ compare(control.value, 25)
+ compare(control.position, 0)
+
+ control.to = 75
+ compare(control.to, 75)
+ compare(control.value, 25)
+ compare(control.position, 0)
+
+ control.value = 50
+ compare(control.value, 50)
+ compare(control.position, 0.5)
+
+ control.destroy()
+ }
+
+ function test_inverted() {
+ var control = slider.createObject(testCase, {from: 1.0, to: -1.0})
+ compare(control.from, 1.0)
+ compare(control.to, -1.0)
+ compare(control.value, 0.0)
+ compare(control.position, 0.5)
+
+ control.value = 2.0
+ compare(control.value, 1.0)
+ compare(control.position, 0.0)
+
+ control.value = -2.0
+ compare(control.value, -1.0)
+ compare(control.position, 1.0)
+
+ control.value = 0.0
+ compare(control.value, 0.0)
+ compare(control.position, 0.5)
+
+ control.destroy()
+ }
+
+ function test_position() {
+ var control = slider.createObject(testCase, {value: 0.25})
+ compare(control.value, 0.25)
+ compare(control.position, 0.25)
+
+ control.value = 0.75
+ compare(control.value, 0.75)
+ compare(control.position, 0.75)
+
+ control.destroy()
+ }
+
function test_visualPosition() {
var control = slider.createObject(testCase, {value: 0.25})
compare(control.value, 0.25)