Merge branch 'qml'
[mdictionary] / src / mdictionary / qml / MySpinBox.qml
index 9f0d47c..a30ecba 100644 (file)
@@ -7,16 +7,22 @@ Rectangle {
     property int maxValue:500;
     property int minValue:0;
     property alias value:text_input1.text
+    property bool isTextInMinValue:true;
+    property string textInMinValue:"inf";
+    property int singleStep: 2;
 
     signal valueChange(int intiger);
 
     function stringToInt(string){
         var value=0;
         var pow10=1;
-        for (var i=string.length-1;i>=0;i--){
-            value+=(string.charCodeAt(i)-48)*pow10;
-            pow10= pow10*10;
-        }
+        if(isTextInMinValue && textInMinValue==string)
+            value=minValue;
+        else
+            for (var i=string.length-1;i>=0;i--){
+                value+=(string.charCodeAt(i)-48)*pow10;
+                pow10= pow10*10;
+            }
         if(value>maxValue)
             return maxValue;
         if(value<minValue)
@@ -24,6 +30,10 @@ Rectangle {
         return value;
     }
 
+    function setFocus(){
+        text_input1.focus=true;
+    }
+
     TextInput {
         id: text_input1
         x: 1
@@ -36,11 +46,25 @@ Rectangle {
         selectByMouse: true;
         font.pixelSize: rectangle1.height * .5;
         onCursorPositionChanged:  moveCursorSelection(cursorPosition);
-        onTextChanged: rectangle1.valueChange(stringToInt(text_input1.text));
+        onTextChanged:{
+            if(isTextInMinValue && text_input1.text!="" && stringToInt(text_input1.text)==minValue)
+                text_input1.text=textInMinValue;
+
+            rectangle1.valueChange(stringToInt(text_input1.text));
+        }
         onFocusChanged: {
             if(focus==false)
                text=stringToInt(text);
         }
+        Keys.onPressed: {
+            if (event.key == Qt.Key_Up)
+                text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
+            else if (event.key == Qt.Key_Down){
+                text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
+                if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
+                    text_input1.text=textInMinValue
+            }
+        }
     }
 
     Timer {
@@ -50,7 +74,7 @@ Rectangle {
         repeat: true
         onTriggered:{
             if(mouseAreaUp.pressedButtons==Qt.LeftButton)
-                text_input1.text=((stringToInt(text_input1.text)+1)>maxValue)?(maxValue):(stringToInt(text_input1.text)+1);
+                text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
             else
                 running=false;
         }
@@ -62,8 +86,11 @@ Rectangle {
         running: false;
         repeat: true
         onTriggered:{
-            if(mouseAreaDown.pressedButtons==Qt.LeftButton)
-                text_input1.text=((stringToInt(text_input1.text)-1)<minValue)?(minValue):(stringToInt(text_input1.text)-1);
+            if(mouseAreaDown.pressedButtons==Qt.LeftButton){
+                text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
+                if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
+                    text_input1.text=textInMinValue
+            }
             else
                 running=false;
         }
@@ -110,7 +137,7 @@ Rectangle {
         height: rectangle1.height/2;
         anchors.right: parent.right
         anchors.top: parent.top
-        onClicked: text_input1.text=((stringToInt(text_input1.text)+1)>maxValue)?(maxValue):(stringToInt(text_input1.text)+1);
+        onClicked: text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
         onPressAndHold:{
             timerUp.restart;
             timerUp.running=true;
@@ -124,19 +151,21 @@ Rectangle {
         height: rectangle1.height/2;
         anchors.right: parent.right
         anchors.bottom: parent.bottom
-        onClicked: text_input1.text=((stringToInt(text_input1.text)-1)<minValue)?(minValue):(stringToInt(text_input1.text)-1);
+        onClicked:{
+            text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
+            if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
+                text_input1.text=textInMinValue
+        }
         onPressAndHold:{
             timerDown.restart;
             timerDown.running=true;
         }
     }
-
-   /* states: [
+    states: [
         State {
-            name: "DisableState"; when: rectangle1.enabled==false;
-            PropertyChanges { target: shadeDisable; z: 3; opacity: 0.5 }
+            name: "focusState"; when: text_input1.focus && rectangle1.enabled;
+            PropertyChanges { target: rectangle1; border.width: 2 }
         }
     ]
-    */
 }