summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@theqtcompany.com>2015-10-13 17:53:33 +0200
committerEdward Welbourne <edward.welbourne@theqtcompany.com>2015-10-21 14:07:36 +0000
commit32d11f2d679b811c9af349fe9f0b0b29d4f598c0 (patch)
treeb06172d8797d3b401e824861d51ab4cb6b57ca30
parent61889f06fa828b483513b8315b4f0870532ece66 (diff)
downloadqtquickcontrols-32d11f2d679b811c9af349fe9f0b0b29d4f598c0.tar.gz
Avoid real-rounding glitches near ends of a slider's range.
Floating point doesn't quite reliably satisfy certain standard identities of arithmetic ... Task-number: QTBUG-42358 Change-Id: I7417feedbfc1fd6f59510e0ee6d07d98ff7ddc46 Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
-rw-r--r--src/controls/Private/qquickrangemodel_p_p.h6
-rw-r--r--tests/auto/controls/data/slider/rounder.qml75
-rw-r--r--tests/auto/controls/data/tst_slider.qml27
3 files changed, 107 insertions, 1 deletions
diff --git a/src/controls/Private/qquickrangemodel_p_p.h b/src/controls/Private/qquickrangemodel_p_p.h
index 67f0745c..235ba069 100644
--- a/src/controls/Private/qquickrangemodel_p_p.h
+++ b/src/controls/Private/qquickrangemodel_p_p.h
@@ -93,7 +93,11 @@ public:
return minimum;
const qreal scale = (maximum - minimum) / posRange;
- return (pos - effectivePosAtMin()) * scale + minimum;
+ // Avoid perverse rounding glitches when at an end:
+ const qreal mid = (effectivePosAtMax() + effectivePosAtMin()) * 0.5;
+ if (pos < mid)
+ return (pos - effectivePosAtMin()) * scale + minimum;
+ return maximum - scale * (effectivePosAtMax() - pos);
}
qreal publicPosition(qreal position) const;
diff --git a/tests/auto/controls/data/slider/rounder.qml b/tests/auto/controls/data/slider/rounder.qml
new file mode 100644
index 00000000..b7191892
--- /dev/null
+++ b/tests/auto/controls/data/slider/rounder.qml
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.2
+import QtQuick.Controls 1.2
+import QtQuick.Controls.Styles 1.2
+
+/* Rounding errors in the value at end of range are elusive.
+ * They're sensitive to fine details in hard-to-anticipate ways.
+ */
+Item {
+ visible: true
+ /* Sensitive to slider width */
+ width: 800
+ height: 400
+
+ /* Let test know value and where to click */
+ property int waist: slider.y + slider.height / 2
+ property alias value: slider.value
+
+ Slider {
+ id: slider
+ width: parent.width
+
+ /* Sensitive to value range. */
+ minimumValue: 0
+ maximumValue: 100
+
+ style: SliderStyle {
+ /* Sensitive to handle size. */
+ handle: Rectangle {
+ implicitHeight: 26
+ implicitWidth: 26
+ color: "salmon"
+ }
+ }
+ }
+}
diff --git a/tests/auto/controls/data/tst_slider.qml b/tests/auto/controls/data/tst_slider.qml
index 305605f6..1589f2ab 100644
--- a/tests/auto/controls/data/tst_slider.qml
+++ b/tests/auto/controls/data/tst_slider.qml
@@ -330,5 +330,32 @@ Item {
control.destroy()
}
+
+ function test_dragRounding() {
+ // Regression test: ends of range should be exact, not 99.99999999 &c.
+ var component = Qt.createComponent("slider/rounder.qml")
+ compare(component.status, Component.Ready)
+ var control = component.createObject(container)
+
+ // Does moving to maximum (100) actually reach it ?
+ mousePress(control, 0, control.waist)
+ mouseMove(control, control.width, control.waist)
+ mouseRelease(control, control.width, control.waist)
+ // Equality checks are dodgy with floats, but this should still be exact:
+ verify(control.value == 100)
+ // Neither of the following caught the bug, with value 100 -1.421e-14
+ // compare(control.value, 100)
+ // fuzzyCompare(control.value, 100, 1e-16)
+
+ // Now check it all works going the other way, too:
+ mousePress(control, control.width, control.waist)
+ mouseMove(control, 0, control.waist)
+ mouseRelease(control, 0, control.waist)
+ verify(control.value == 0)
+
+ // Tidy up.
+ control.destroy()
+ component.destroy()
+ }
}
}