Debian Codegraph @

src/GUI/qml/ToggleSwitch.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.4
5 import FlightGear 1.0
6
7 Item {
8 id: root
9 property bool checked: false
10 property alias label: label.text
11 property bool enabled: true
12
13 implicitWidth: track.width + label.width + 16
14 implicitHeight: Math.max(label.height, thumb.height)
15
16 // helper to set the value without an animation
17 function setValue(newCheck)
18 {
19 sliderBehaviour.enabled = false;
20 checked = newCheck
21 sliderBehaviour.enabled = true;
22 }
23
24 function toggle(newChecked)
25 {
26 checked = newChecked
27 }
28
29 Rectangle {
30 id: track
31 width: height * 2
32 height: radius * 2
33 radius: Style.roundRadius
34
35 color: (checked && root.enabled) ? Style.frameColor : Style.minorFrameColor
36 anchors.left: parent.left
37 anchors.leftMargin: Style.margin
38 anchors.verticalCenter: parent.verticalCenter
39
40 Rectangle {
41 id: thumb
42 width: radius * 2
43 height: radius * 2
44 radius: Style.roundRadius * 1.5
45
46 anchors.verticalCenter: parent.verticalCenter
47 color: root.enabled && (checked | mouseArea.containsMouse) ? Style.themeColor : "white"
48 border.width: 1
49 border.color: Style.inactiveThemeColor
50
51 x: checked ? parent.width - (track.radius + radius) : (track.radius - radius)
52
53 Behavior on x {
54 id: sliderBehaviour
55 NumberAnimation {
56 duration: 250
57 }
58 }
59 }
60 }
61
62 StyledText {
63 id: label
64 anchors.left: track.right
65 anchors.leftMargin: Style.margin
66 anchors.verticalCenter: parent.verticalCenter
67 enabled: root.enabled
68 hover: mouseArea.containsMouse
69 }
70
71 MouseArea {
72 anchors.fill: parent
73 id: mouseArea
74 enabled: root.enabled
75
76 hoverEnabled: true
77 onClicked: root.toggle(!checked)
78 cursorShape: enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
79
80 }
81 }