Debian Codegraph @

src/imports/Components/1.3/Slider.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // FIXME(loicm) Add support for keyboard shortcuts (basically left/right).
18
19 import QtQuick 2.4
20 import Lomiri.Components 1.3 as Toolkit
21
22 /*!
23 \qmltype Slider
24 \inqmlmodule Lomiri.Components
25 \ingroup lomiri
26 \brief Slider is a component to select a value from a continuous range of
27 values.
28
29 The slider's sensing area is defined by the width and height, therefore
30 styles should take this into account when defining the visuals, and
31 alter these values to align the graphics' sizes.
32
33 \l {https://design.lomiri.com/apps/building-blocks/selection#slider}{See also the Design Guidelines on Sliders}.
34
35 Example:
36 \qml
37 Item {
38 Slider {
39 function formatValue(v) { return v.toFixed(2) }
40 minimumValue: -3.14
41 maximumValue: 3.14
42 value: 0.0
43 live: true
44 }
45 }
46 \endqml
47 */
48 Toolkit.StyledItem {
49 id: slider
50
51 /*!
52 The minimum value from the continuous range of values. If this value is
53 greater than maximumValue, the component will be in an inconsistent
54 state.
55 */
56 property real minimumValue: 0.0
57
58 /*!
59 The maximum value from the continuous range of values. If this value is
60 lesser than minimumValue, the component will be in an inconsistent state.
61 */
62 property real maximumValue: 100.0
63
64 // FIXME(loicm) Add Support for the stepSize property.
65
66 /*!
67 The distance between two selectable values in the range defined by
68 [minimumValue, maximumValue].
69 */
70 property real stepSize: (Math.abs(minimumValue) + Math.abs(maximumValue)) / 100.0
71
72 /*!
73 The current value of the slider. This property is not changed while the
74 thumb is dragged unless the live property is set to true.
75 */
76 property real value: 0.0
77
78 /*!
79 Defines whether the value is updated while the thumb is dragged or just
80 when the thumb is released.
81 */
82 property bool live: false
83
84 /*!
85 \qmlproperty bool pressed
86
87 Whether the Slider is currently being pressed.
88 */
89 property alias pressed: mouseArea.pressed
90
91 /*!
92 The signal is emitted when there is a click on the slider. The onThumb parameter provides information if the click, was inside of the thumb element.
93 */
94 signal touched(bool onThumb)
95
96
97 /*!
98 This function is used by the value indicator to show the current value.
99 Reimplement this function if you want to show different information. By
100 default, the value v is rounded to the nearest integer value.
101 */
102 function formatValue(v) {
103 return v.toFixed(0)
104 }
105
106 // Private symbols.
107
108 /*! \internal */
109 property alias __internals: mouseArea
110
111 /*! \internal */
112 onValueChanged: mouseArea.liveValue = slider.value
113
114 activeFocusOnPress: true
115 activeFocusOnTab: true
116
117 Binding {
118 target: slider
119 property: "value"
120 value: mouseArea.liveValue
121 when: slider.live
122 }
123
124 MouseArea {
125 id: mouseArea
126 anchors.fill: parent
127
128 property Item bar: slider.__styleInstance ? slider.__styleInstance.bar : null
129 property Item thumb: slider.__styleInstance ? slider.__styleInstance.thumb : null
130 property real thumbSpacing: slider.__styleInstance ? slider.__styleInstance.thumbSpacing : 0
131 property real barMinusThumb: bar && thumb ? bar.width - (thumb.width + 2.0*thumbSpacing) : 0.0
132
133 property real liveValue: 0.0
134 property real normalizedValue: normalizedValueFromValue(liveValue)
135 property real dragInitMouseX: 0.0
136 property real dragInitNormalizedValue: 0.0
137
138 property Flickable flickable: {
139 // traverse parents to catch whether we have an ancestor Flickable
140 var pl = slider.parent;
141 while (pl) {
142 if (pl.hasOwnProperty("flicking")) {
143 return pl;
144 }
145 pl = pl.parent;
146 }
147 return null;
148 }
149
150 states: State {
151 name: "sliding"
152 when: mouseArea.flickable && mouseArea.pressed
153 PropertyChanges {
154 target: mouseArea.flickable
155 interactive: false
156 }
157 }
158 function normalizedValueFromValue(value) {
159 if (Qt.application.layoutDirection == Qt.RightToLeft) {
160 return Toolkit.MathUtils.clampAndProject(value, slider.minimumValue,
161 slider.maximumValue, 1.0, 0.0);
162 } else {
163 return Toolkit.MathUtils.clampAndProject(value, slider.minimumValue,
164 slider.maximumValue, 0.0, 1.0);
165 }
166
167 }
168
169 function valueFromNormalizedValue(normalizedValue) {
170 if (Qt.application.layoutDirection == Qt.RightToLeft) {
171 return Toolkit.MathUtils.lerp(Toolkit.MathUtils.clamp(normalizedValue, 0.0, 1.0),
172 slider.maximumValue, slider.minimumValue);
173 } else {
174 return Toolkit.MathUtils.lerp(Toolkit.MathUtils.clamp(normalizedValue, 0.0, 1.0),
175 slider.minimumValue, slider.maximumValue);
176 }
177 }
178
179 function adjustValue(increment) {
180 if (Qt.application.layoutDirection == Qt.RightToLeft)
181 increment *= -1;
182 slider.value = Toolkit.MathUtils.clamp(slider.value + increment, slider.minimumValue, slider.maximumValue);
183 }
184
185 /* Mimic the behaviour of the 'pressed' property with one important difference:
186 'pressed' is set to true only after the onPressed handler has been executed.
187 That prevents us from doing interesting animations upon press.
188 */
189 property bool isPressed: false
190 onPressed: {
191 isPressed = true;
192 var thumbPressed = mouse.x >= thumb.x && mouse.x <= thumb.x + thumb.width;
193 if (!thumbPressed) {
194 var normalizedX = (mouseX - thumbSpacing - thumb.width * 0.5) / barMinusThumb;
195 liveValue = valueFromNormalizedValue(normalizedX);
196 }
197 dragInitMouseX = mouse.x;
198 dragInitNormalizedValue = normalizedValue;
199 slider.touched(thumbPressed);
200 }
201 onReleased: {
202 isPressed = false;
203 if (!slider.live) {
204 slider.value = liveValue;
205 }
206 }
207 onPositionChanged: {
208 // Left button dragging
209 var normalizedOffsetX = (mouseArea.mouseX - dragInitMouseX) / barMinusThumb;
210 liveValue = valueFromNormalizedValue(dragInitNormalizedValue + normalizedOffsetX);
211 }
212 onClicked: slider.requestFocus(Qt.MouseFocusReason)
213 onLiveValueChanged: if (isPressed) slider.requestFocus(Qt.MouseFocusReason)
214 }
215
216 Keys.onLeftPressed: {
217 if (event.modifiers === Qt.NoModifier)
218 __internals.adjustValue(-slider.stepSize);
219 }
220 Keys.onRightPressed: {
221 if (event.modifiers === Qt.NoModifier)
222 __internals.adjustValue(slider.stepSize);
223 }
224
225 styleName: "SliderStyle"
226 }