-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaptedSwitch.qml
109 lines (97 loc) · 3.13 KB
/
AdaptedSwitch.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
Switch {
implicitHeight: Math.max(focus_indicator.height-1.5 * globalMargin, contentItem.implicitHeight)
background: Rectangle {
id: background
height: 2.5 * globalMargin
x: (focus_indicator.height - height)/2
width: 2.5 * height
radius: height / 2
property int duration: 200
state: "off"
anchors.verticalCenter: parent.verticalCenter
opacity: 0.5
MouseArea {
id: ma
anchors.fill: parent
onClicked: {
checked = !checked
}
}
states: [
State {
name: "on"
when: checked
PropertyChanges { target: handle; x: ma2.drag.maximumX}
PropertyChanges { target: background; color: Material.accent }
},
State {
name: "off"
when: !checked
PropertyChanges { target: handle; x: ma2.drag.minimumX; color: "white" }
PropertyChanges { target: background; color: Material.color(Material.Grey, Material.Shade700)}
}
]
transitions: Transition {
NumberAnimation {
duration: background.duration
property: "x"
}
ColorAnimation {
duration: background.duration
}
}
}
Rectangle {
id: focus_indicator
visible: parent.visualFocus
anchors.centerIn: handle
color: background.color
opacity: 0.3
height: 2 * handle.height
radius: height/2
width: height
z: handle.z - 1
}
indicator: Rectangle {
id: handle
height: 1.5 * background.height
width: height
radius: height/2
color: background.color
anchors.verticalCenter: parent.verticalCenter
z: ma.z + 1
MouseArea {
id: ma2
anchors.fill: parent
drag.target: handle; drag.axis: Drag.XAxis; drag.minimumX: (focus_indicator.height - height)/2; drag.maximumX: background.width - (handle.height - background.height)/2
onReleased: {
if(!drag.active) {
checked = !checked
}
else if(handle.x < drag.maximumX - drag.minimumX) {
checked = true //muss erst false gesetzt werden, damit der state auch
checked = false //wirklich wieder angewendet wird (sonst bleibt die Kugel irgendwo in der Mitte stehen)
}
else {
checked = false
checked = true
}
}
}
Keys.onReleased: {
if(event.key === Qt.Key_Space) {
event.accepted = true
checked = !checked
}
}
}
contentItem: AdaptedText {
text: parent.text
wrapMode: Text.WordWrap
verticalAlignment: Text.AlignVCenter
leftPadding: 2 * background.x + background.width
}
}