summaryrefslogtreecommitdiff
path: root/gui/qml/skins/navit/ToggleSwitch.qml.in
blob: fc87402bfeb2275f40d280ac51186ffa0d843ed2 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Qt 4.6

 Item {
     id: toggleswitch
     width: background.width + label.width + gui.width/24; height: background.height

     property string stOn: "false"
     property string text: "Toggle switch"
     signal changed

     function toggle() {
         if (toggleswitch.state == "on")
             toggleswitch.state = "off";
         else toggleswitch.state = "on";
     }

     function startup () {
	 if (toggleswitch.stOn == "1" ) 
       		toggleswitch.stOn = "true";
	 else if (toggleswitch.stOn == "0" ) 
		toggleswitch.stOn = "false";
         if (toggleswitch.stOn == "true")
             toggleswitch.state = "on";
         else
             toggleswitch.state = "off";
     }

     Component.onCompleted: startup();

     Text {
        id: label; text: toggleswitch.text; color: "White"; font.pointSize: gui.height/32;
        anchors.left: background.right; anchors.leftMargin: gui.width/24;
        anchors.verticalCenter: background.verticalCenter
     }


     Image {
         id: background; source: "background.svg"
         MouseRegion { anchors.fill: parent; onClicked: toggle() }
	 height: gui.height/7.5; width: height*2.4;
     }

     Image {
         id: knob; source: "knob.svg"; x: 1; y: 2
	 height: gui.height/8; width: gui.height/8;

         MouseRegion {
             anchors.fill: parent
             drag.target: knob; drag.axis: "XAxis"; drag.minimumX: 1; drag.maximumX: background.width-knob.width
	     hoverEnabled: false; onReleased: toggle()
         }
     }

     states: [
         State {
             name: "on"
             PropertyChanges { target: knob; x: background.width-knob.width }
	     PropertyChanges { target: toggleswitch; stOn: "true" }
	     StateChangeScript { script: toggleswitch.changed(); }
         },
         State {
             name: "off"
             PropertyChanges { target: knob; x: 1 }
	     PropertyChanges { target: toggleswitch; stOn: "false" }
	     StateChangeScript { script: toggleswitch.changed(); }
         }
     ]

     transitions: Transition {
         NumberAnimation { matchProperties: "x"; easing: "easeInOutQuad"; duration: 200 }
     }
 }