add focus in qml
[mdictionary] / src / mdictionary / qml / Button.qml
1 import Qt 4.7
2
3 Rectangle {
4     id: button
5     property alias textInButton: buttonText.text
6     property bool checkable: false
7     property bool checked: false
8
9     height: 20;
10     width: 60;
11
12     signal clicked;
13     signal changeCheck;
14
15     function setText(string) { textInButton = qsTr(string); }
16     function press() { mouseArea.pressed }
17
18     Keys.onReturnPressed: {
19         button.clicked();
20         if(checkable){
21             button.checked=!button.checked;
22             button.changeCheck();
23         }
24     }
25
26     MouseArea {
27         id: mouseArea
28         anchors.centerIn: parent;
29         width:  parent.width;
30         height: parent.height;
31         onClicked: {
32             button.clicked();
33             if(checkable){
34                 button.checked=!button.checked;
35                 button.changeCheck();
36             }
37         }
38     }
39
40     Text {
41         z:1
42         id: buttonText
43         width: parent.width;
44         anchors.centerIn: parent;
45         horizontalAlignment: Text.AlignHCenter
46         font.pixelSize: parent.height * .5
47         elide: Text.ElideRight;
48         style: Text.Sunken; color: "white"; styleColor: "black"; smooth: true
49     }
50
51     Image {
52         id: image1
53         width:  (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1
54         height: parent.height
55         anchors.left: parent.left
56         source: "qrc:/button/buttonLeft.png"
57         smooth: true
58         fillMode:Image.Stretch
59     }
60
61     Image {
62         id: image3
63         smooth: true
64         height: parent.height
65         anchors.right: image2.left
66         anchors.rightMargin: -1
67         anchors.left: image1.right
68         anchors.leftMargin: -1
69         source: "qrc:/button/buttonCenter.png"
70         //fillMode:Image.Stretch
71         fillMode: Image.TileHorizontally
72
73     }
74
75     Image {
76         id: image2
77         smooth: true
78         width: (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1
79         height: parent.height
80         anchors.right: parent.right
81         source: "qrc:/button/buttonR.png"
82         fillMode:Image.Stretch
83     }
84
85     states: [
86         State {
87             name: "pressed";
88             when: ((button.enabled) && (mouseArea.pressed || (button.checkable && button.checked) ) )
89
90             PropertyChanges { target: image1; source: "qrc:/button/buttonLeftPushed.png" }
91             PropertyChanges { target: image3; source: "qrc:/button/buttonCenterPushed.png" }
92             PropertyChanges { target: image2; source: "qrc:/button/buttonRightPushed.png" }
93         },
94         State {
95             name: "disabled";
96             when: (button.enabled == false);
97
98             PropertyChanges { target: image1; source: "qrc:/button/buttonLeftDisabled.png" }
99             PropertyChanges { target: image3; source: "qrc:/button/buttonCenterDisabled.png" }
100             PropertyChanges { target: image2; source: "qrc:/button/buttonRightDisabled.png" }
101         },
102         State {
103             name: "focused";
104             when: (button.enabled && !mouseArea.pressed && button.focus)
105
106             PropertyChanges { target: image1; source: "qrc:/button/buttonLeftFocus.png" }
107             PropertyChanges { target: image3; source: "qrc:/button/buttonCenterFocus.png" }
108             PropertyChanges { target: image2; source: "qrc:/button/buttonRFocus.png" }
109         }
110
111     ]
112 }