summaryrefslogtreecommitdiffstats
path: root/multilayer-dashboard/Button.qml
blob: 027785473cedc34f14be01f4614c025c91f6b231 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import Qt 4.7
import Qt.labs.gestures 1.0

Rectangle {
    id: button

    property string text : "Press me, Hold me, Strike me"
    property int counter : 0
    property string bgColor: "steelblue"
    property bool marked : false

    property bool enabled : state != "disabled"

    width: 160
    height: 40
    radius: 10

    Rectangle {
        id: background
        anchors.fill: parent
        radius: 10
        opacity: 0.3
        color: parent.bgColor
    }

    Rectangle {
        id: mark
        x: 10
        y: 5
        width: 0
        height: 0
        radius: 30
        color: "black"
        opacity: 1.0
        smooth: true
    }

    function toggleMark() {
        button.marked = !button.marked
        if (button.marked) {
            mark.width = 30
            mark.height = 30
        } else {
            mark.width = 0
            mark.height = 0
        }
    }

    function increaseCounter() {
        counter = counter + 1
    }

    Text {
        id: labelText
        text: parent.text + (counter ? " "+counter : "")
        anchors.centerIn: parent
    }

    Rectangle {
        id: stroke

        x: parent.width * 0.3 / 2
        anchors.verticalCenter: parent.verticalCenter
        width: 0
        height : 10

        color: "red"
        opacity: 1.0
    }


    states: [
        State {
            name: ""
            PropertyChanges { target: background; opacity: 0.3 }
            PropertyChanges { target: labelText; opacity: 1.0 }
            PropertyChanges { target: stroke; width: 0 }
        },
        State {
            name: "pressed"
            PropertyChanges { target: background; opacity: 1.0 }
        },
        State {
            name: "disabled"
            PropertyChanges { target: background; opacity: 0.3 }
            PropertyChanges { target: mark; opacity: 0.3 }
            PropertyChanges { target: labelText; opacity: 0.3 }
            PropertyChanges { target: stroke; width: stroke.parent.width * 0.7 }
        }
    ]
    transitions: [
        Transition {
            from: "*"
            to: "disabled"
            PropertyAnimation { target: stroke; duration: 1000; property: "width" }
        },
        Transition {
            from: "disabled"
            to: ""
            PropertyAnimation { target: stroke; duration: 1000; property: "width" }
        }
    ]

    property bool handled: false

    function isHorizontalSwipe(angle) {
        if (angle > 150 && angle < 210)
            return true;
        if (angle > 330 || angle < 30)
            return true;
        return false;
    }

    GestureArea {
        anchors.fill: parent

        Tap {
            when: button.enabled
            onStarted: { button.state = "pressed" }
            onCanceled: {
                if (!button.handled) {
                    button.state = "";
                }
            }
            onFinished: {
                if (!button.handled) {
                    button.state = "";
                    button.increaseCounter();
                }
                button.handled = false;
            }
        }

        TapAndHold {
            when: button.enabled
            onFinished: { console.log("tap-and-hold"); button.toggleMark() }
        }

        Swipe {
            when: gesture.speed > 25
            onStarted: button.handled = true
            onFinished: {
                if (isHorizontalSwipe(gesture.swipeAngle)) {
                    if (gesture.horizontalDirection == SwipeGesture.Right) {
                        button.state = "disabled"
                    } else if (gesture.horizontalDirection == SwipeGesture.Left) {
                        script: button.state = ""
                    }
                }
                button.handled = false
            }
        }
    }
}