summaryrefslogtreecommitdiffstats
path: root/demos/declarative/calculator/CalcButton.qml
blob: f2400000066265ce721f8daf4958fe71c60a57c0 (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
Item {
    id: Button; width: 50; height: 30

    properties: [
        Property { name: "operation"; type: "string" },
        Property { name: "toggable"; value: false },
        Property { name: "toggled"; value: false }
    ]

    Script {
        function buttonClicked(operation) {
            if (Button.toggable == true) {
                if (Button.toggled == true) {
                    Button.toggled = false;
                    Button.state = 'Toggled';
                } else {
                    Button.toggled = true;
                    Button.state = '';
                }
            }
            else
                doOp(operation);
        }
    }

    Image {
        id: Image
        src: "pics/button.sci"
        width: Button.width; height: Button.height
    }

    Image {
        id: ImagePressed
        src: "pics/button-pressed.sci"
        width: Button.width; height: Button.height
        opacity: 0
    }

    Text {
        anchors.centeredIn: Image
        text: Button.operation
        color: "white"
        font.bold: true
    }

    MouseRegion {
        id: MouseRegion
        anchors.fill: Button
        onClicked: { buttonClicked(Button.operation) }
    }

    states: [
        State {
            name: "Pressed"; when: MouseRegion.pressed == true
            SetProperties { target: ImagePressed; opacity: 1 }
        },
        State {
            name: "Toggled"; when: Button.toggled == true
            SetProperties { target: ImagePressed; opacity: 1 }
        }
    ]
}