added android components
[mardrone] / mardrone / imports / com / nokia / android.1.1 / Switch.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 1.1
42 import "." 1.1
43
44 Item {
45     id: root
46
47     // Common Public API
48     property bool checked: false
49     property bool pressed: stateGroup.state == "Pressed" || stateGroup.state == "KeyPressed"
50                            || stateGroup.state == "Dragging"
51
52     // Symbian specific API
53     property bool platformInverted: false
54
55     signal clicked
56
57     QtObject {
58         id: internal
59         objectName: "internal"
60
61         function toggle() {
62             root.checked = !root.checked
63             root.clicked()
64         }
65     }
66
67     StateGroup {
68         id: stateGroup
69
70         states: [
71             State { name: "Pressed" },
72             State { name: "Released" },
73             State { name: "KeyPressed" },
74             State { name: "Dragging" },
75             State { name: "Canceled" }
76         ]
77
78         transitions: [
79             Transition {
80                 to: "Pressed"
81                 ScriptAction { script: privateStyle.play(Android.BasicItem) }
82             },
83             Transition {
84                 from: "Released, Dragging"
85                 to: ""
86                 ScriptAction { script: privateStyle.play(Android.CheckBox) }
87                 ScriptAction { script: internal.toggle() }
88             },
89             Transition {
90                 from: "KeyPressed"
91                 to: ""
92                 ScriptAction { script: internal.toggle() }
93             }
94         ]
95     }
96
97     implicitWidth: track.width
98     implicitHeight: privateStyle.switchButtonHeight
99
100     Image {
101         id: track
102
103         function trackPostfix() {
104             if (!root.enabled && root.checked)
105                 return "disabled_on"
106             else if (!root.enabled && !root.checked)
107                 return "disabled_off"
108             else
109                 return "track"
110         }
111
112         source: privateStyle.imagePath("qtg_graf_switchbutton_" + track.trackPostfix(),
113                                        root.platformInverted)
114         anchors.left: parent.left
115         anchors.verticalCenter: parent.verticalCenter
116         sourceSize.width: Android.UndefinedSourceDimension
117         sourceSize.height: privateStyle.switchButtonHeight
118         scale: root.LayoutMirroring.enabled ? -1 : 1
119     }
120
121     MouseArea {
122         id: mouseArea
123
124         property real lastX
125
126         function isChecked() {
127             if (root.LayoutMirroring.enabled)
128                 return (handle.x + handle.width / 2 < track.x + (track.width / 2))
129             else
130                 return (handle.x + handle.width / 2 > track.x + (track.width / 2))
131         }
132         function updateHandlePos() {
133             // The middle of the handle follows mouse, the handle is bound to the track
134             handle.x = Math.max(track.x, Math.min(mouseArea.lastX - handle.width / 2,
135                                                   track.x + track.width - handle.width))
136         }
137
138         anchors.fill: track
139         onPressed: stateGroup.state = "Pressed"
140         onReleased: stateGroup.state = "Released" // releasing doesn't toggle yet, it is intermediate state
141         onClicked: stateGroup.state = ""
142         onCanceled: stateGroup.state = "Canceled"
143         onPositionChanged: {
144             mouseArea.lastX = mouse.x
145             if (mouseArea.drag.active)
146                 updateHandlePos()
147         }
148         drag {
149             // The handle is moved manually but MouseArea can be used to decide when dragging
150             // should start (QApplication::startDragDistance). A dummy target needs to be bound or
151             // dragging won't get activated.
152             target: Item { visible: false }
153
154             axis: Drag.XandYAxis
155             minimumY: 0; maximumY: 0 // keep dragging active eventhough only x axis switches
156             minimumX: track.x; maximumX: mouseArea.drag.minimumX + track.width - handle.width
157             onActiveChanged: {
158                 if (mouseArea.drag.active) {
159                     updateHandlePos()
160                     stateGroup.state = "Dragging"
161                 }
162                 else {
163                     stateGroup.state = (root.checked != isChecked()) ? "" : "Canceled"
164                 }
165             }
166         }
167     }
168
169     Item {
170         id: fill
171
172         clip: true
173         anchors.left: track.left
174         anchors.right: handle.horizontalCenter
175         anchors.verticalCenter: parent.verticalCenter
176         height: privateStyle.switchButtonHeight
177         visible: root.enabled
178
179         Image {
180             source: privateStyle.imagePath("qtg_graf_switchbutton_fill",
181                                            root.platformInverted)
182             anchors.left: parent.left
183             anchors.top: parent.top
184             height: parent.height
185         }
186     }
187
188     Image {
189         id: handle
190
191         source: privateStyle.imagePath("qtg_graf_switchbutton_"
192                                        + (root.pressed ? "handle_pressed" : "handle_normal"),
193                                        root.platformInverted)
194         anchors.verticalCenter: root.verticalCenter
195         sourceSize.width: privateStyle.switchButtonHeight
196         sourceSize.height: privateStyle.switchButtonHeight
197         visible: root.enabled
198
199         states: [
200             State {
201                 name: "Off"
202                 when: !mouseArea.drag.active && !checked
203                 PropertyChanges {
204                     target: handle
205                     restoreEntryValues: false
206                     x: root.LayoutMirroring.enabled ? mouseArea.drag.maximumX : mouseArea.drag.minimumX
207                 }
208             },
209             State {
210                 name: "On"
211                 when: !mouseArea.drag.active && checked
212                 PropertyChanges {
213                     target: handle
214                     restoreEntryValues: false
215                     x: root.LayoutMirroring.enabled ? mouseArea.drag.minimumX : mouseArea.drag.maximumX
216                 }
217             }
218         ]
219
220         transitions: [
221             Transition {
222                 to: "Off"
223                 SmoothedAnimation {properties: "x"; easing.type: Easing.InOutQuad; duration: 200 }
224             },
225             Transition {
226                 to: "On"
227                 SmoothedAnimation {properties: "x"; easing.type: Easing.InOutQuad; duration: 200 }
228             }
229         ]
230     }
231
232     Keys.onPressed: {
233         if (!event.isAutoRepeat && (event.key == Qt.Key_Select
234                                     || event.key == Qt.Key_Return
235                                     || event.key == Qt.Key_Enter)) {
236             stateGroup.state = "KeyPressed"
237             event.accepted = true
238         }
239     }
240
241
242     Keys.onReleased: {
243         if (!event.isAutoRepeat && (event.key == Qt.Key_Select
244                                     || event.key == Qt.Key_Return
245                                     || event.key == Qt.Key_Enter)) {
246             stateGroup.state = ""
247             event.accepted = true
248         }
249     }
250 }