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