Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
[mdictionary] / src / mdictionary / qml / MyTextLineEdit.qml
index 2514185..004e198 100644 (file)
@@ -5,10 +5,37 @@ Rectangle {
     radius: 10
     border.color: "#000666";
     property alias textInLineEdit:text_input1.text
+    property bool useCompleter:false
 
     signal enterPressed(string text);
+    signal textChange(string text);
+
+    signal nextCompleter();
+    signal prevCompleter();
+
+    signal isFocused();
+    signal checkFocus();
 
     function setText(string) { textInLineEdit = string; }
+    function setCompleter(string) { completerItemText.text=string; }
+    function hideCompleter() { completerItem.visible=false; }
+
+    function addOneChar(){
+        if(completerItemText.text.length>0)
+            text_input1.text=text_input1.text+completerItemText.text.charAt(0);
+    }
+
+    function addAllChars(){
+        text_input1.text=text_input1.text+completerItemText.text;
+        completerItemText.text="";
+    }
+
+    function setFocus(){
+        if(rectangle1.enabled){
+            text_input1.focus=true;
+            isFocused();
+        }
+    }
 
     TextInput {
         id: text_input1
@@ -19,11 +46,58 @@ Rectangle {
         anchors.centerIn: parent
         selectByMouse: true;
         font.pixelSize: rectangle1.height * .5;
-        onCursorPositionChanged:  moveCursorSelection(cursorPosition);
-        focus: rectangle1.focus;
+        onCursorPositionChanged: {
+            moveCursorSelection(cursorPosition);
+            if(cursorPosition==text.length && useCompleter)
+                completerItem.visible=true;
+            else
+               completerItem.visible=false;
+        }
+        Keys.priority : Keys.AfterItem
         Keys.onPressed: {
-            if ((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
+            if ((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return)){
                 rectangle1.enterPressed(text_input1.text)
+                completerItem.visible=false;
+            }
+            if(useCompleter){
+                if (event.key == Qt.Key_Up)
+                    rectangle1.nextCompleter();
+                if (event.key == Qt.Key_Down)
+                    rectangle1.prevCompleter();
+                if ((event.key == Qt.Key_Right) && (cursorPosition==text.length))
+                    addOneChar();
+                if(Qt.ControlModifier){
+                    if (event.key == Qt.Key_Space){
+                        if(completerItem.visible=false)
+                            completerItem.visible=true
+                        else
+                            addAllChars()
+                    }
+                }
+                if(event.key == Qt.Key_Escape)
+                    completerItem.visible=false;
+            }
+        }
+        onTextChanged: rectangle1.textChange(text);
+        onFocusChanged: if(focus) isFocused();
+        onActiveFocusChanged: rectangle1.checkFocus();
+    }
+
+
+    Rectangle {
+        id: completerItem
+        x: text_input1.x + text_input1.positionToRectangle(text_input1.cursorPosition).x +1
+        y: text_input1.y
+        width: completerItemText.paintedWidth;
+        visible: false;
+        height: text_input1.height
+        color: (completerItemText.text.length>0)?"#5e71fb":"#FFFFFF"
+        opacity: 0.5
+        Text {
+            id: completerItemText
+            anchors.fill: parent
+            text:""
+            font.pixelSize: text_input1.font.pixelSize;
         }
     }
 
@@ -39,14 +113,14 @@ Rectangle {
 
     states: [
         State {
-            name: "FokusState"; when: text_input1.focus==true && rectangle1.enabled==true;
+            name: "FokusState"; when: text_input1.focus && rectangle1.enabled;
             PropertyChanges {
                 target: rectangle1
                 border.width: 3
             }
         },
         State {
-            name: "DisableState"; when: rectangle1.enabled==false;
+            name: "DisableState"; when: !rectangle1.enabled;
             PropertyChanges { target: shadeDisable; z: 3; opacity: 0.5 }
         }
     ]