summaryrefslogtreecommitdiffstats
path: root/examples/declarative/slideswitch/Switch.qml
blob: 1620805395d5b215520cfeba56fcf9f5fe40c046 (plain)
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
import Qt 4.6

Item {
    id: mySwitch
    width: groove.width; height: groove.height

    property var on

    Script {

        function toggle() {
            if(mySwitch.state == "On")
                mySwitch.state = "Off";
            else
                mySwitch.state = "On";
        }
        function dorelease() {
            if(knob.x == 1) {
                if(mySwitch.state == "Off")
                    return;
            }

            if(knob.x == 78) {
                if(mySwitch.state == "On")
                    return;
            }

            toggle();
        }

    }
    Image { id: groove; source: "background.svg" }
    MouseRegion { anchors.fill: groove; onClicked: { toggle() } }
    Image { id: knob; source: "knob.svg"; x: 1; y: 2 }
    MouseRegion {
        anchors.fill: knob
        onClicked: { toggle() }
        onReleased: { dorelease() }
        drag.target: knob; drag.axis: "XAxis"; drag.minimumX: 1; drag.maximumX: 78
    }
    states: [
        State {
            name: "On"
            PropertyChanges { target: knob; x: 78 }
            PropertyChanges { target: mySwitch; on: true }
        },
        State {
            name: "Off"
            PropertyChanges { target: knob; x: 1 }
            PropertyChanges { target: mySwitch; on: false }
        }
    ]
    transitions: [
        Transition {
            NumberAnimation { properties: "x"; easing: "easeInOutQuad"; duration: 200 }
        }
    ]
}