Add keyboard support to comboBox, HistoryList. Fix some issues with keyboard support.
[mdictionary] / src / mdictionary / qml / ComboBox.qml
1 import Qt 4.7
2
3 Rectangle {
4     id: rectangle1
5     radius: 10
6     border.color: "#000666";
7     property alias value:text1.text
8     property alias index: list1.currentIndex
9     property alias model: list1.model
10     property bool expanded: false
11     property bool disabled: false
12     property int expandedHeight
13     property int basicHeight
14     property string startValue
15     height: 20
16
17     property variant parentField : rectangle1
18
19     function show(Boolean){
20         expanded = Boolean
21     }
22
23     function setStartValue(val, idx){
24         startValue = val
25         list1.currentIndex = idx
26     }
27
28     signal valueSelected(string selected);
29
30    Keys.onPressed: {
31         if (event.key == Qt.Key_Space)
32             rectangle1.expanded=true;
33     }
34
35     Text {
36         id: text1
37         width: rectangle1.width-15
38         height: rectangle1.height*0.6;
39         text: list1.selected
40         anchors.centerIn: parent
41         font.pixelSize: rectangle1.height * .5;
42         onTextChanged: { rectangle1.valueSelected(text) }
43         z: expanded?0:1;
44     }
45
46     Rectangle {
47         id: shadeDisable
48         width:  parent.width;
49         height: parent.height;
50         anchors.centerIn: parent;
51         radius: parent.radius
52         color: "grey";
53         opacity: 0.5
54     }
55
56     Image {
57         id: imageDown
58         z:15;
59         width: 11;
60         height: 0.5 * rectangle1.height;
61         anchors.top: parent.top
62         anchors.right: parent.right
63         anchors.topMargin: 4
64         anchors.rightMargin: 6
65         anchors.bottomMargin: 4
66         source: "qrc:/button/down_enable.png";
67
68         MouseArea{
69             id: imgMouseArea
70             anchors.fill: parent
71             onClicked: {
72                 rectangle1.show(!rectangle1.expanded)
73             }
74         }
75     }
76
77     MouseArea{
78         id: topMouseArea
79         z: 5
80         anchors.fill: parent
81         onClicked: {
82             rectangle1.show(!rectangle1.expanded)
83         }
84     }
85
86     ElementsListView{
87         id: list1
88         visible: false
89         property string selected: rectangle1.startValue
90
91         Keys.onPressed: {
92
93             if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){
94                 selectedValue(currentIndex, model.valueOnPosition(currentIndex))
95             }
96             if (event.key == Qt.Key_Escape){
97                 rectangle1.show(!rectangle1.expanded)
98                 event.accepted = true
99             }
100         }
101
102         function selectedValue(nr, value) {
103             currentIndex = nr
104             selected = value
105             rectangle1.show(false)
106         }
107
108         anchors.rightMargin: 5
109         anchors.leftMargin: 5
110         anchors.bottomMargin: 10
111         anchors.topMargin: 10
112         anchors.fill: parent
113
114         highlightResizeSpeed: 1000
115         delegate: Component{
116             id: list1Delegate
117             Item {
118                 width: rectangle1.width
119                 height: contentText.height
120
121                 MouseArea{
122                     id: listMouseArea
123                     anchors.fill: parent
124                     z: 1
125                     onClicked: {
126                         list1.selectedValue(number, content)
127                     }
128                     hoverEnabled: true
129                     onEntered: {
130                         list1.currentIndex = number
131                     }
132                 }
133
134                 Row{
135                     anchors.fill: parent
136
137                     Text{
138                         id: contentText
139                         anchors.verticalCenter: parent.verticalCenter
140                         anchors.leftMargin: 5
141                         elide: Text.ElideRight;
142                         text: content
143                     }
144                 }
145             }
146         }
147
148     }
149
150     states: [
151         State {
152             name: "basic";
153             when: (rectangle1.expanded == false && rectangle1.disabled == false)
154             PropertyChanges { target: list1; z: 0; visible: false }
155             PropertyChanges { target: text1; z: 0; visible: true }
156             PropertyChanges { target: rectangle1; border.width: 1}
157             PropertyChanges { target: rectangle1; height: rectangle1.basicHeight}
158             PropertyChanges { target: imageDown; height: 0.5 * rectangle1.basicHeight}
159             PropertyChanges { target: shadeDisable; visible: false; z:-1}
160         },
161         State {
162             name: "expanded"
163             when: (rectangle1.expanded == true && rectangle1.disabled == false)
164             PropertyChanges { target: list1; z: 10; visible: true }
165             PropertyChanges { target: text1; z: 10; visible: false }
166             PropertyChanges { target: rectangle1; border.width: 1}
167             PropertyChanges { target: rectangle1; height: rectangle1.expandedHeight}
168             PropertyChanges { target: imageDown; height: 0.5 * rectangle1.basicHeight}
169             PropertyChanges { target: shadeDisable; visible: false; z: -1}
170             PropertyChanges { target: rectangle1; anchors.fill: parentField}
171         },
172         State {
173             name: "disabled";
174             when: rectangle1.disabled == true
175             PropertyChanges { target: list1; z: 0; visible: false }
176             PropertyChanges { target: text1; z: 0; visible: true }
177             PropertyChanges { target: rectangle1; border.width: 1}
178             PropertyChanges { target: rectangle1; expanded: false}
179             PropertyChanges { target: rectangle1; height: rectangle1.basicHeight}
180             PropertyChanges { target: imageDown; visible: true}
181             PropertyChanges { target: shadeDisable; visible: true; z:10}
182         }
183     ]
184
185 }
186