Debian Codegraph @

src/imports/Components/1.2/Slider.qml

raw
1 /*
2 * Copyright 2013 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.2
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 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: 1.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 interger 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
116 Binding {
117 target: slider
118 property: "value"
119 value: mouseArea.liveValue
120 when: slider.live
121 }
122
123 MouseArea {
124 id: mouseArea
125 anchors.fill: parent
126
127 property Item bar: slider.__styleInstance ? slider.__styleInstance.bar : null
128 property Item thumb: slider.__styleInstance ? slider.__styleInstance.thumb : null
129 property real thumbSpacing: slider.__styleInstance ? slider.__styleInstance.thumbSpacing : 0
130 property real barMinusThumb: bar && thumb ? bar.width - (thumb.width + 2.0*thumbSpacing) : 0.0
131
132 property real liveValue: 0.0
133 property real normalizedValue: normalizedValueFromValue(liveValue)
134 property real dragInitMouseX: 0.0
135 property real dragInitNormalizedValue: 0.0
136
137 property Flickable flickable: {
138 // traverse parents to catch whether we have an ancestor Flickable
139 var pl = slider.parent;
140 while (pl) {
141 if (pl.hasOwnProperty("flicking")) {
142 return pl;
143 }
144 pl = pl.parent;
145 }
146 return null;
147 }
148
149 states: State {
150 name: "sliding"
151 when: mouseArea.flickable && mouseArea.pressed
152 PropertyChanges {
153 target: mouseArea.flickable
154 interactive: false
155 }
156 }
157 function normalizedValueFromValue(value) {
158 if (Qt.application.layoutDirection == Qt.RightToLeft) {
159 return MathUtils.clampAndProject(value, slider.minimumValue,
160 slider.maximumValue, 1.0, 0.0);
161 } else {
162 return MathUtils.clampAndProject(value, slider.minimumValue,
163 slider.maximumValue, 0.0, 1.0);
164 }
165
166 }
167
168 function valueFromNormalizedValue(normalizedValue) {
169 if (Qt.application.layoutDirection == Qt.RightToLeft) {
170 return MathUtils.lerp(MathUtils.clamp(normalizedValue, 0.0, 1.0),
171 slider.maximumValue, slider.minimumValue);
172 } else {
173 return MathUtils.lerp(MathUtils.clamp(normalizedValue, 0.0, 1.0),
174 slider.minimumValue, slider.maximumValue);
175 }
176 }
177
178 /* Mimic the behaviour of the 'pressed' property with one important difference:
179 'pressed' is set to true only after the onPressed handler has been executed.
180 That prevents us from doing interesting animations upon press.
181 */
182 property bool isPressed: false
183 onPressed: {
184 isPressed = true;
185 var thumbPressed = mouse.x >= thumb.x && mouse.x <= thumb.x + thumb.width;
186 if (!thumbPressed) {
187 var normalizedX = (mouseX - thumbSpacing - thumb.width * 0.5) / barMinusThumb;
188 liveValue = valueFromNormalizedValue(normalizedX);
189 }
190 dragInitMouseX = mouse.x;
191 dragInitNormalizedValue = normalizedValue;
192 slider.touched(thumbPressed);
193 }
194 onReleased: {
195 isPressed = false;
196 if (!slider.live) {
197 slider.value = liveValue;
198 }
199 }
200 onPositionChanged: {
201 // Left button dragging
202 var normalizedOffsetX = (mouseArea.mouseX - dragInitMouseX) / barMinusThumb;
203 liveValue = valueFromNormalizedValue(dragInitNormalizedValue + normalizedOffsetX);
204 }
205 onClicked: slider.requestFocus(Qt.MouseFocusReason)
206 onLiveValueChanged: if (isPressed) slider.requestFocus(Qt.MouseFocusReason)
207 }
208
209 style: Theme.createStyleComponent("SliderStyle.qml", slider)
210 }