1
0
Fork 0
flightgear/src/GUI/qml/ToggleSwitch.qml

77 lines
1.9 KiB
QML
Raw Normal View History

import QtQuick 2.4
2017-12-05 20:53:47 +00:00
import "."
Item {
2018-06-28 15:21:01 +01:00
id: root
property bool checked: false
property alias label: label.text
property bool enabled: true
implicitWidth: track.width + label.width + 16
2018-04-09 19:41:44 +01:00
implicitHeight: Math.max(label.height, thumb.height)
// helepr to set the value without an animation
function setValue(newCheck)
{
sliderBehaviour.enabled = false;
checked = newCheck
sliderBehaviour.enabled = true;
}
2018-06-28 15:21:01 +01:00
function toggle(newChecked)
{
checked = newChecked
}
Rectangle {
id: track
width: height * 2
2017-12-05 20:53:47 +00:00
height: radius * 2
radius: Style.roundRadius
color: (checked && enabled) ? Style.frameColor : Style.minorFrameColor
anchors.left: parent.left
2017-12-05 20:53:47 +00:00
anchors.leftMargin: Style.margin
anchors.verticalCenter: parent.verticalCenter
Rectangle {
id: thumb
2017-12-05 20:53:47 +00:00
width: radius * 2
height: radius * 2
radius: Style.roundRadius * 1.5
anchors.verticalCenter: parent.verticalCenter
color: (checked && enabled) ? Style.themeColor : "white"
border.width: 1
2017-12-05 20:53:47 +00:00
border.color: Style.inactiveThemeColor
2017-12-05 20:53:47 +00:00
x: checked ? parent.width - (track.radius + radius) : (track.radius - radius)
Behavior on x {
id: sliderBehaviour
NumberAnimation {
duration: 250
}
}
}
}
StyledText {
id: label
anchors.left: track.right
2017-12-05 20:53:47 +00:00
anchors.leftMargin: Style.margin
anchors.verticalCenter: parent.verticalCenter
enabled: root.enabled
hover: mouseArea.containsMouse
}
MouseArea {
anchors.fill: parent
id: mouseArea
enabled: root.enabled
hoverEnabled: true
2018-06-28 15:21:01 +01:00
onClicked: root.toggle(!checked)
}
}