3eb5ad8cd320c98b2afa04b39a9543b71aab3fb8
[ubi] / qml / ubi / components / Button.qml
1 import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
2 import "../UIConstants.js" as Const
3
4 Item {
5     id: root
6     property string label
7     property bool disabled: false
8     property int fontSize: 30
9     property int maxSize: 27
10     property string iconSource
11
12     state: mouseArea.pressed && !root.disabled ? "pressed" : "unpressed"
13
14     width: box.width
15     height: box.height
16
17     signal buttonClicked(string label)
18
19     Rectangle {
20         id: shadow
21         width: box.width
22         height: box.height
23         color: Const.SHADOW_COLOR;
24         radius: 10
25     }
26
27     Rectangle {
28         id: box
29         color: root.disabled ? Const.COOL_GREY_COLOR : "black"
30         height: root.iconSource=="" ? textbox.height+20 : icon.height+20
31         //width: textbox.width<100 ? 120 : textbox.width+20
32         width: root.iconSource=="" ? textbox.width+30 : icon.width+30
33         radius: 10
34     }
35
36     Rectangle {
37         width: box.width
38         height: box.height
39         x: box.x
40         y: box.y
41         color: Const.WARM_GREY_COLOR
42         radius: 10
43         visible: root.state == "pressed"
44         //border.color: "black"
45         //border.width: Const.SHADOW_OFFSET
46     }
47
48     Image {
49         id: icon
50         //width: 30
51         //height: 30
52         anchors.centerIn: box
53         source: root.iconSource == "" ? "" : "../" + root.iconSource
54         sourceSize.width: width
55         sourceSize.height: height
56     }
57
58     onLabelChanged: {
59         if(root.label.length>root.maxSize) {
60             //console.log("root.label: "+root.label)
61             //console.log("root.label.length: "+root.label.length)
62             //console.log("root.maxSize: "+root.maxSize)
63             textbox.text = root.label.substring(0,root.maxSize-3)+"...";
64         } else {
65             textbox.text = root.label;
66         }
67     }
68
69     Text {
70         id: textbox
71         font.pixelSize: root.fontSize
72         color: root.disabled ? "gray" : "white"
73         anchors.centerIn: box
74         visible: root.iconSource == ""
75     }
76
77     MouseArea {
78         id: mouseArea
79         width: box.width
80         height: box.height
81         onClicked: root.buttonClicked(root.label)
82         enabled: !root.disabled
83     }
84
85     states: [
86         State {
87             name: "unpressed"
88             PropertyChanges {target: shadow; x: Const.SHADOW_OFFSET}
89             PropertyChanges {target: shadow; y: Const.SHADOW_OFFSET}
90             PropertyChanges {target: box; x: 0}
91             PropertyChanges {target: box; y: 0}
92         },
93         State {
94             name: "pressed"
95             PropertyChanges {target: shadow; x: Const.SHADOW_OFFSET}
96             PropertyChanges {target: shadow; y: Const.SHADOW_OFFSET}
97             PropertyChanges {target: box; x: Const.SHADOW_OFFSET}
98             PropertyChanges {target: box; y: Const.SHADOW_OFFSET}
99         }
100     ]
101 }
102
103
104