QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SliderSwitch.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3
4import QGroundControl
5import QGroundControl.Controls
6
7/// The SliderSwitch control implements a sliding switch control similar to the power off
8/// control on an iPhone. It supports holding the space bar to slide the switch.
9Rectangle {
10 id: _root
11 implicitWidth: label.contentWidth + (_diameter * 2.5) + (_border * 4)
12 implicitHeight: label.height * 2.5
13 radius: height /2
14 color: qgcPal.windowShade
15
16 signal accept ///< Action confirmed
17
18 property string confirmText ///< Text for slider
19 property alias fontPointSize: label.font.pointSize ///< Point size for text
20
21 property real _border: 4
22 property real _diameter: height - (_border * 2)
23 property real _dragStartX: _border
24 property real _dragStopX: _root.width - (_diameter + _border)
25
26 Keys.onSpacePressed: (event) => {
27 if (visible && event.modifiers === Qt.NoModifier && !sliderDragArea.drag.active) {
28 event.accepted = true
29 sliderAnimation.start()
30 }
31 }
32
33 Keys.onReleased: (event) => {
34 if (visible && event.key === Qt.Key_Space && !event.isAutoRepeat) {
35 event.accepted = true
36 resetSpaceBarSliding()
37 }
38 }
39
40 function resetSpaceBarSliding() {
41 slider.reset()
42 }
43
44 QGCPalette { id: qgcPal; colorGroupEnabled: true }
45
46 QGCLabel {
47 id: label
48 x: _diameter + _border
49 width: parent.width - x
50 anchors.verticalCenter: parent.verticalCenter
51 horizontalAlignment: Text.AlignHCenter
52 text: confirmText
53 color: qgcPal.buttonText
54 }
55
56 Rectangle {
57 id: slider
58 x: _border
59 y: _border
60 height: _diameter
61 width: _diameter
62 radius: _diameter / 2
63 color: qgcPal.primaryButton
64
65 QGCColoredImage {
66 anchors.centerIn: parent
67 width: parent.width * 0.8
68 height: parent.height * 0.8
69 sourceSize.height: height
70 fillMode: Image.PreserveAspectFit
71 smooth: false
72 mipmap: false
73 color: qgcPal.buttonText
74 cache: false
75 source: "/res/ArrowRight.svg"
76 }
77
78 PropertyAnimation on x {
79 id: sliderAnimation
80 duration: 1500
81 from: _dragStartX
82 to: _dragStopX
83 running: false
84
85 onFinished: {
86 slider.reset()
87 _root.accept()
88 }
89 }
90
91 function reset() {
92 slider.x = _border
93 sliderAnimation.stop()
94 }
95 }
96
97 QGCMouseArea {
98 id: sliderDragArea
99 anchors.leftMargin: -ScreenTools.defaultFontPixelWidth * 15
100 fillItem: slider
101 drag.target: slider
102 drag.axis: Drag.XAxis
103 drag.minimumX: _dragStartX
104 drag.maximumX: _dragStopX
105 preventStealing: true
106
107 property bool dragActive: drag.active
108
109 onDragActiveChanged: {
110 if (!sliderDragArea.drag.active) {
111 if (slider.x > _dragStopX - _border) {
112 _root.accept()
113 }
114 slider.reset()
115 }
116 }
117 }
118}