diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-04-05 11:25:43 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2018-04-05 09:38:43 +0000 |
commit | 98a65a9c74e148758be1619b1fd3844f3f31c789 (patch) | |
tree | 4ab9011782aa3b6168c88ffcb1f2ec52e707e25d | |
parent | 583b7b0324a03337b8f21b5c2eacb6657c16ade7 (diff) |
RangeSlider: add valueAt() function
This is necessary to e.g. show up-to-date values in tooltips for each
handle when live is set to false. It's copied from Slider.
[ChangeLog][Controls][RangeSlider] Added a valueAt() function to allow
accessing each handle's value when the live property is set to false.
Task-number: QTBUG-67317
Change-Id: I8d0ca1a914f983b6b950ece759a102c05c5dd2f0
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickrangeslider.cpp | 17 | ||||
-rw-r--r-- | src/quicktemplates2/qquickrangeslider_p.h | 1 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_rangeslider.qml | 21 |
3 files changed, 39 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickrangeslider.cpp b/src/quicktemplates2/qquickrangeslider.cpp index 075147a1..5b282f2c 100644 --- a/src/quicktemplates2/qquickrangeslider.cpp +++ b/src/quicktemplates2/qquickrangeslider.cpp @@ -689,6 +689,23 @@ void QQuickRangeSlider::resetTouchDragThreshold() } /*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlmethod real QtQuick.Controls::RangeSlider::valueAt(real position) + + Returns the value for the given \a position. + + \sa first.value, second.value, first.position, second.position, live +*/ +qreal QQuickRangeSlider::valueAt(qreal position) const +{ + Q_D(const QQuickRangeSlider); + const qreal value = (d->to - d->from) * position; + if (qFuzzyIsNull(d->stepSize)) + return d->from + value; + return d->from + qRound(value / d->stepSize) * d->stepSize; +} + +/*! \qmlpropertygroup QtQuick.Controls::RangeSlider::first \qmlproperty real QtQuick.Controls::RangeSlider::first.value \qmlproperty real QtQuick.Controls::RangeSlider::first.position diff --git a/src/quicktemplates2/qquickrangeslider_p.h b/src/quicktemplates2/qquickrangeslider_p.h index 5f1d9da4..7958eb84 100644 --- a/src/quicktemplates2/qquickrangeslider_p.h +++ b/src/quicktemplates2/qquickrangeslider_p.h @@ -115,6 +115,7 @@ public: qreal touchDragThreshold() const; void setTouchDragThreshold(qreal touchDragThreshold); void resetTouchDragThreshold(); + Q_REVISION(5) Q_INVOKABLE qreal valueAt(qreal position) const; Q_SIGNALS: void fromChanged(); diff --git a/tests/auto/controls/data/tst_rangeslider.qml b/tests/auto/controls/data/tst_rangeslider.qml index 31ab8332..03b34a2a 100644 --- a/tests/auto/controls/data/tst_rangeslider.qml +++ b/tests/auto/controls/data/tst_rangeslider.qml @@ -1061,4 +1061,25 @@ TestCase { compare(visualPositionSpy2.count, ++visualPositionCount2) touch.release(0, control, x0 + data.dx2, y0 + data.dy2).commit() } + + function test_valueAt_data() { + return [ + { tag: "0.0..1.0", from: 0.0, to: 1.0, values: [0.0, 0.2, 0.5, 1.0] }, + { tag: "0..100", from: 0, to: 100, values: [0, 20, 50, 100] }, + { tag: "100..-100", from: 100, to: -100, values: [100, 60, 0, -100] }, + { tag: "-7..7", from: -7, to: 7, stepSize: 1.0, values: [-7.0, -4.0, 0.0, 7.0] }, + { tag: "-3..7", from: -3, to: 7, stepSize: 5.0, values: [-3.0, -3.0, 2.0, 7.0] }, + ] + } + + function test_valueAt(data) { + var control = createTemporaryObject(sliderComponent, testCase, + { from: data.from, to: data.to, stepSize: data.stepSize }) + verify(control) + + compare(control.valueAt(0.0), data.values[0]) + compare(control.valueAt(0.2), data.values[1]) + compare(control.valueAt(0.5), data.values[2]) + compare(control.valueAt(1.0), data.values[3]) + } } |