add focus in qml
[mdictionary] / src / mdictionary / qml / SearchBarWidget.qml
1 import Qt 4.7
2
3 Rectangle {
4     id: searchBarWidget
5     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
6     color : myPalette.window;
7
8     property alias enableLineEdit: inputSearchText.enabled;
9     property int focusIndex:-1;
10
11
12     function setButtonText(string) { searchButton.setText(string) }
13     function setEnableHistoryPrev(Boolean) { historyPrevToolButton.enabled = Boolean }
14     function setEnableHistoryNext(Boolean) { historyNextToolButton.enabled = Boolean }
15     function setEnableHistoryShow(Boolean) { historyShowToolButton.enabled = Boolean }
16     function setEnableLineEdit(Boolean) { enableLineEdit = Boolean }
17     function clear() { inputSearchText.setText("") }
18
19     function setLineEditText(string) { inputSearchText.setText(string) }
20     function setCompleterText(string) { inputSearchText.setCompleter(string) }
21
22     function setFocus(){
23         if(focusIndex==0){
24             inputSearchText.setFocus();
25             if(!inputSearchText.enabled)
26                 focusIndex++;
27         }
28         if(focusIndex==1)
29             clearButton.focus=true;
30
31         if(focusIndex==2){
32             searchButton.focus = true
33             if(!searchButton.enabled)
34                 focusIndex++;
35         }
36
37         if(focusIndex==3){
38             historyPrevToolButton.focus = true
39             if(!historyPrevToolButton.enabled)
40                 focusIndex++;
41         }
42
43         if(focusIndex==4){
44             historyShowToolButton.focus = true
45             if(!historyShowToolButton.enabled)
46                 focusIndex++;
47         }
48
49         if(focusIndex==5){
50             historyNextToolButton.focus = true
51             if(!historyNextToolButton.enabled)
52                 focusIndex++;
53         }
54
55         if (focusIndex>5){
56             nextFocus();
57             focusIndex=-1;
58             focus=true;
59         }
60     }
61
62     function focusOff(){ focus=true; focusIndex=-1;}
63
64     signal nextFocus();
65     signal checkFocus();
66     signal searchButtonClicked(string text);
67     signal historyNextToolButtonClicked;
68     signal historyShowToolButtonClicked;
69     signal historyPrevToolButtonClicked;
70     signal textChange(string text);
71     signal nextCompleter();
72     signal prevCompleter();
73
74     Keys.onTabPressed: {
75         focusIndex++;
76         setFocus();
77     }
78
79     MyTextLineEdit{
80         id: inputSearchText
81         width: (searchBarWidget.width - (searchBarWidget.height*3.5 + searchButton.width + 9));
82         height: searchBarWidget.height -1;
83         anchors.left: parent.left
84         anchors.verticalCenter: parent.verticalCenter
85         onEnterPressed: searchBarWidget.searchButtonClicked(text);
86         useCompleter: true;
87         IconButton {
88             id: clearButton;
89             width:  inputSearchText.height-6;
90             height: inputSearchText.height-6;
91             anchors.rightMargin: 4
92             anchors.right: parent.right
93             anchors.verticalCenter: parent.verticalCenter
94             enabled: true;
95             pathToIcon: "qrc:/button/go-clear.png";
96             onClicked: clear();
97             onActiveFocusChanged:searchBarWidget.checkFocus();
98         }
99         onTextChange: searchBarWidget.textChange(text)
100         onNextCompleter: searchBarWidget.nextCompleter();
101         onPrevCompleter: searchBarWidget.prevCompleter();
102         onIsFocused: searchBarWidget.focusIndex=0;
103         onCheckFocus: searchBarWidget.checkFocus();
104     }
105
106     Item {
107         id: buttonsBox
108         width: searchBarWidget.height*3.5 + searchButton.width + 9
109         height: searchBarWidget.height
110         anchors.right: parent.right
111         anchors.verticalCenter: parent.verticalCenter
112
113         Button {
114             id: searchButton
115             width: 100
116             height: searchBarWidget.height;
117             anchors.rightMargin: 3
118             anchors.right: historyPrevToolButton.left
119             anchors.verticalCenter: parent.verticalCenter
120             textInButton: qsTr("Search")
121             onClicked:{
122                 searchBarWidget.searchButtonClicked(inputSearchText.textInLineEdit);
123                 inputSearchText.hideCompleter();
124             }
125             onActiveFocusChanged:searchBarWidget.checkFocus();
126         }
127
128         IconButton {
129             id: historyNextToolButton;
130             width:  searchBarWidget.height
131             height: searchBarWidget.height
132             anchors.right: parent.right
133             anchors.verticalCenter: parent.verticalCenter
134             enabled: true;
135             pathToIcon: "qrc:/button/go-next.png";
136             onClicked: searchBarWidget.historyNextToolButtonClicked();
137             onActiveFocusChanged:searchBarWidget.checkFocus();
138         }
139
140         IconButton {
141             id: historyShowToolButton;
142             width:  searchBarWidget.height
143             height: searchBarWidget.height
144             anchors.rightMargin: 3
145             anchors.right: historyNextToolButton.left
146             anchors.verticalCenter: parent.verticalCenter
147             enabled: true;
148             pathToIcon: "qrc:/button/go-show.png";
149             onClicked: searchBarWidget.historyShowToolButtonClicked();
150             onActiveFocusChanged:searchBarWidget.checkFocus();
151         }
152
153         IconButton {
154             id: historyPrevToolButton;
155             width:  searchBarWidget.height
156             height: searchBarWidget.height
157             anchors.rightMargin: 3
158             anchors.right: historyShowToolButton.left
159             anchors.verticalCenter: parent.verticalCenter
160             enabled: true;
161             pathToIcon: "qrc:/button/go-previous.png";
162             onClicked: searchBarWidget.historyPrevToolButtonClicked();
163             onActiveFocusChanged:searchBarWidget.checkFocus();
164         }
165     }
166 }