summaryrefslogtreecommitdiffstats
path: root/examples/declarative/slideswitch/Switch.qml
blob: b5de9ec5f99f353f2bb7239349d6cd85c8fd7980 (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: Switch
    width: Groove.width; height: Groove.height

    property var on

    Script {

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

            if(Knob.x == 78) {
                if(Switch.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: "x"; drag.minimumX: 1; drag.maximumX: 78
    }
    states: [
        State {
            name: "On"
            SetProperties { target: Knob; x: 78 }
            SetProperties { target: Switch; on: true }
        },
        State {
            name: "Off"
            SetProperties { target: Knob; x: 1 }
            SetProperties { target: Switch; on: false }
        }
    ]
    transitions: [
        Transition {
            NumberAnimation { properties: "x"; easing: "easeInOutQuad"; duration: 200 }
        }
    ]
}