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