Debian Codegraph @

src/imports/Components/Pickers/1.2/DialerHand.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 import QtQuick 2.4
18 import Lomiri.Components 1.2
19
20 /*!
21 \qmltype DialerHand
22 \inqmlmodule Lomiri.Components.Pickers 1.0
23 \ingroup lomiri-pickers
24 \brief DialerHand represents a value selector on a Dialer.
25
26 DialerHand components have meaning only if those are placed inside Dialer
27 components. The dialer hand presents a value selection from the given dialer's
28 minimum and maximum values.
29
30 By default all hands are placed on the dialer's hand space, on the outer dialer
31 disk. By default all hands have the same size, 0.5GU width and height same as
32 the handSpace specified in \l Dialer, however themes can specify preset values
33 for each hand.
34
35 Hands can also be placed onto the inner disk by setting \a hand.toCenterItem
36 property to true.
37
38 \qml
39 Dialer {
40 DialerHand {
41 // this dialer hand will take the space as defined by the theme.
42 }
43 DialerHand {
44 hand.height: units.gu(3)
45 // this hand will have its width as defined by the theme
46 // but height as 3 GU
47 }
48 }
49 \endqml
50
51 Items declared as children will be placed over the hands. These items will not
52 be rotated togehther with the hand, these will always be shown horizontally.
53 The hand can be hidden by setting false to \a hand.visible property, but that
54 does not hide the overlay content.
55
56 The following example demonstrates how to create a hidden dialer hand having
57 an overlay component on the hand.
58 \qml
59 Dialer {
60 DialerHand {
61 id: selector
62 hand.visible: false
63 Rectangle {
64 anchors.centerIn: parent
65 width: height
66 height: units.gu(3)
67 radius: width / 2
68 color: Theme.palette.normal.background
69 antialiasing: true
70 Label {
71 text: Math.round(selector.value)
72 anchors.centerIn: parent
73 }
74 }
75 }
76 }
77 \endqml
78 */
79 StyledItem {
80 id: dialerHand
81
82 /*!
83 The property holds the selected value the dialer hand points to.
84 */
85 property real value
86
87 /*!
88 \qmlproperty real hand.width
89 \qmlproperty real hand.height
90 \qmlproperty bool hand.draggable
91 \qmlproperty bool hand.toCenterItem
92 \qmlproperty bool hand.visible
93
94 The \b hand.width and \b hand.height properties define the size of the hand.
95 The height of the hand must be in the [0..dialer.handSpace] range in order
96 to have the hand displayed in the hand area, however there is no restriction
97 applied on the size of the dialer hand. If no value is set, the width and
98 height will be defined by the style.
99
100 \b draggable property specifies whether the hand is draggable or not. When set to not draggable,
101 the hand is used only to indicate the given value. The default value is true.
102
103 \b toCenterItem property specifies whether the hand should be placed on the hand space (on the outer disk
104 - false) or onto the center disk (inner disk - true). The default value is false, meaning the hand will be placed onto the hand space disk.
105
106 \b visible property specifies whether to show the hand marker or not. The default value is true.
107 */
108 property DialerHandGroup hand: DialerHandGroup {
109 width: __styleInstance.handPreset(index, "width")
110 height: __styleInstance.handPreset(index, "height")
111 draggable: __styleInstance.handPreset(index, "draggable")
112 visible: __styleInstance.handPreset(index, "visible")
113 toCenterItem: __styleInstance.handPreset(index, "toCenterItem")
114 }
115
116 /*!
117 The property holds the dialer instance the hand is assigned to. This is a
118 helper property to enable access to the dialer component hosting the hand.
119 */
120 readonly property Dialer dialer: parent
121
122 /*!
123 \qmlproperty list<QtObject> overlay
124 \default
125 The property holds the items that can be added on top of the hand. Note that
126 these items will not be rotated together with the hand pointer and pointer
127 visibility does not affect the overlay items visibility.
128 */
129 default property alias overlay: contentItem.data
130
131 /*!
132 \qmlproperty int index
133 \readonly
134 The property holds the index of the hand. Note that this is not the child
135 index of the dialer children, this represents the index of the DialerHand
136 component added to the \l dialer.
137 */
138 readonly property alias index: grabber.index
139
140 z: __styleInstance.handPreset(index, "z")
141 anchors.centerIn: parent
142 width: parent.width
143 height: parent.height
144 activeFocusOnPress: true
145 style: Theme.createStyleComponent("DialerHandStyle.qml", dialerHand)
146
147 /*! \internal */
148 onParentChanged: {
149 if (dialer && !dialer.hasOwnProperty("handSpace")) {
150 console.log("WARNING: DialerHand can be a child of Dialer only.");
151 }
152 }
153
154 /*! \internal */
155 onValueChanged: grabber.updateHand();
156 /*! \internal */
157 Component.onCompleted: grabber.updateHand();
158
159 /*! \internal */
160 property alias __grabber: grabber
161 Item {
162 id: grabber
163 property int index: -1
164 parent: __styleInstance.handPointer
165 width: units.gu(4)
166 height: parent.height
167 anchors {
168 top: parent.top
169 horizontalCenter: parent.horizontalCenter
170 }
171 Item {
172 id: contentItem
173 anchors.fill: parent
174 rotation: 360 - __styleInstance.rotation
175 }
176
177 function updateHand() {
178 if (!dialer || !__styleInstance) return;
179 __styleInstance.rotation =
180 MathUtils.projectValue(value,
181 dialer.minimumValue, dialer.maximumValue,
182 0.0, 360.0);
183 dialer.handUpdated(dialerHand);
184 }
185
186 MouseArea{
187 anchors.fill: parent;
188 preventStealing: true;
189 enabled: dialerHand.hand.draggable;
190 property real centerX : dialerHand.width / 2
191 property real centerY : dialerHand.height / 2
192 property bool internalChange: false
193
194 onPositionChanged: {
195 if (internalChange) return;
196 internalChange = true;
197 dialerHand.requestFocus(Qt.MouseFocusReason);
198 var point = mapToItem (dialerHand, mouse.x, mouse.y);
199 var diffX = (point.x - centerX);
200 var diffY = -1 * (point.y - centerY);
201 var rad = Math.atan (diffY / diffX);
202 var deg = (rad * 180 / Math.PI);
203
204 if (diffX > 0 && diffY > 0) {
205 __styleInstance.rotation = 90 - Math.abs (deg);
206 }
207 else if (diffX > 0 && diffY < 0) {
208 __styleInstance.rotation = 90 + Math.abs (deg);
209 }
210 else if (diffX < 0 && diffY > 0) {
211 __styleInstance.rotation = 270 + Math.abs (deg);
212 }
213 else if (diffX < 0 && diffY < 0) {
214 __styleInstance.rotation = 270 - Math.abs (deg);
215 }
216
217 dialerHand.value = MathUtils.projectValue(__styleInstance.rotation,
218 0.0, 360.0,
219 dialer.minimumValue, dialer.maximumValue);
220 internalChange = false;
221 }
222 }
223 }
224 }