add focus in qml
[mdictionary] / src / mdictionary / qml / MyTextLineEdit.qml
1 import Qt 4.7
2
3 Rectangle {
4     id: rectangle1
5     radius: 10
6     border.color: "#000666";
7     property alias textInLineEdit:text_input1.text
8     property bool useCompleter:false
9
10     signal enterPressed(string text);
11     signal textChange(string text);
12
13     signal nextCompleter();
14     signal prevCompleter();
15
16     signal isFocused();
17     signal checkFocus();
18
19     function setText(string) { textInLineEdit = string; }
20     function setCompleter(string) { completerItemText.text=string; }
21     function hideCompleter() { completerItem.visible=false; }
22
23     function addOneChar(){
24         if(completerItemText.text.length>0)
25             text_input1.text=text_input1.text+completerItemText.text.charAt(0);
26     }
27
28     function addAllChars(){
29         text_input1.text=text_input1.text+completerItemText.text;
30         completerItemText.text="";
31     }
32
33     function setFocus(){
34         if(rectangle1.enabled){
35             text_input1.focus=true;
36             isFocused();
37         }
38     }
39
40     TextInput {
41         id: text_input1
42         width: rectangle1.width-20
43         height: rectangle1.height*0.6;
44         text: "textInput"
45         transformOrigin: Item.Left
46         anchors.centerIn: parent
47         selectByMouse: true;
48         font.pixelSize: rectangle1.height * .5;
49         onCursorPositionChanged: {
50             moveCursorSelection(cursorPosition);
51             if(cursorPosition==text.length && useCompleter)
52                 completerItem.visible=true;
53             else
54                completerItem.visible=false;
55         }
56         Keys.priority : Keys.AfterItem
57         Keys.onPressed: {
58             if ((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return)){
59                 rectangle1.enterPressed(text_input1.text)
60                 completerItem.visible=false;
61             }
62             if(useCompleter){
63                 if (event.key == Qt.Key_Up)
64                     rectangle1.nextCompleter();
65                 if (event.key == Qt.Key_Down)
66                     rectangle1.prevCompleter();
67                 if ((event.key == Qt.Key_Right) && (cursorPosition==text.length))
68                     addOneChar();
69                 if(Qt.ControlModifier){
70                     if (event.key == Qt.Key_Space){
71                         if(completerItem.visible=false)
72                             completerItem.visible=true
73                         else
74                             addAllChars()
75                     }
76                 }
77                 if(event.key == Qt.Key_Escape)
78                     completerItem.visible=false;
79             }
80         }
81         onTextChanged: rectangle1.textChange(text);
82         onFocusChanged: if(focus) isFocused();
83         onActiveFocusChanged: rectangle1.checkFocus();
84     }
85
86
87     Rectangle {
88         id: completerItem
89         x: text_input1.x + text_input1.positionToRectangle(text_input1.cursorPosition).x +1
90         y: text_input1.y
91         width: completerItemText.paintedWidth;
92         visible: false;
93         height: text_input1.height
94         color: (completerItemText.text.length>0)?"#5e71fb":"#FFFFFF"
95         opacity: 0.5
96         Text {
97             id: completerItemText
98             anchors.fill: parent
99             text:""
100             font.pixelSize: text_input1.font.pixelSize;
101         }
102     }
103
104     Rectangle {
105         id: shadeDisable
106         anchors.centerIn: parent;
107         radius: parent.radius
108         color: "grey";
109         opacity: 0
110         width:  parent.width;
111         height: parent.height;
112     }
113
114     states: [
115         State {
116             name: "FokusState"; when: text_input1.focus && rectangle1.enabled;
117             PropertyChanges {
118                 target: rectangle1
119                 border.width: 3
120             }
121         },
122         State {
123             name: "DisableState"; when: !rectangle1.enabled;
124             PropertyChanges { target: shadeDisable; z: 3; opacity: 0.5 }
125         }
126     ]
127 }