1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Components project.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
48 property bool checked: false
49 property bool pressed: stateGroup.state == "Pressed" || stateGroup.state == "KeyPressed"
50 || stateGroup.state == "Dragging"
52 // Symbian specific API
53 property bool platformInverted: false
59 objectName: "internal"
62 root.checked = !root.checked
71 State { name: "Pressed" },
72 State { name: "Released" },
73 State { name: "KeyPressed" },
74 State { name: "Dragging" },
75 State { name: "Canceled" }
81 ScriptAction { script: privateStyle.play(Android.BasicItem) }
84 from: "Released, Dragging"
86 ScriptAction { script: privateStyle.play(Android.CheckBox) }
87 ScriptAction { script: internal.toggle() }
92 ScriptAction { script: internal.toggle() }
97 implicitWidth: track.width
98 implicitHeight: privateStyle.switchButtonHeight
103 function trackPostfix() {
104 if (!root.enabled && root.checked)
106 else if (!root.enabled && !root.checked)
107 return "disabled_off"
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
126 function isChecked() {
127 if (root.LayoutMirroring.enabled)
128 return (handle.x + handle.width / 2 < track.x + (track.width / 2))
130 return (handle.x + handle.width / 2 > track.x + (track.width / 2))
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))
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"
144 mouseArea.lastX = mouse.x
145 if (mouseArea.drag.active)
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 }
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
158 if (mouseArea.drag.active) {
160 stateGroup.state = "Dragging"
163 stateGroup.state = (root.checked != isChecked()) ? "" : "Canceled"
173 anchors.left: track.left
174 anchors.right: handle.horizontalCenter
175 anchors.verticalCenter: parent.verticalCenter
176 height: privateStyle.switchButtonHeight
177 visible: root.enabled
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
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
202 when: !mouseArea.drag.active && !checked
205 restoreEntryValues: false
206 x: root.LayoutMirroring.enabled ? mouseArea.drag.maximumX : mouseArea.drag.minimumX
211 when: !mouseArea.drag.active && checked
214 restoreEntryValues: false
215 x: root.LayoutMirroring.enabled ? mouseArea.drag.minimumX : mouseArea.drag.maximumX
223 SmoothedAnimation {properties: "x"; easing.type: Easing.InOutQuad; duration: 200 }
227 SmoothedAnimation {properties: "x"; easing.type: Easing.InOutQuad; duration: 200 }
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
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