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