Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
[mdictionary] / src / mdictionary / qml / MySpinBox.qml
index e0d536b..a30ecba 100644 (file)
@@ -7,47 +7,114 @@ 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;
+        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)
+            return minValue;
+        return value;
+    }
+
+    function setFocus(){
+        text_input1.focus=true;
+    }
 
     TextInput {
         id: text_input1
         x: 1
         width: rectangle1.width-15
         height: rectangle1.height*0.6;
-        text: "0"
+        text: "123"
         anchors.centerIn: parent
-        validator: IntValidator{bottom: 0; top: 500;}
+        validator: IntValidator{bottom: minValue; top: maxValue;}
         transformOrigin: Item.Left
         selectByMouse: true;
         font.pixelSize: rectangle1.height * .5;
         onCursorPositionChanged:  moveCursorSelection(cursorPosition);
-        focus: rectangle1.focus;
- /*       Keys.onPressed: {
-            if ((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
-                rectangle1.enterPressed(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 {
+        id:timerUp;
+        interval: 100;
+        running: false;
+        repeat: true
+        onTriggered:{
+            if(mouseAreaUp.pressedButtons==Qt.LeftButton)
+                text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
+            else
+                running=false;
+        }
+    }
+
+    Timer {
+        id:timerDown;
+        interval: 100;
+        running: false;
+        repeat: true
+        onTriggered:{
+            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;
         }
-*/
     }
 
     Rectangle {
         id: shadeDisable
+        width:  parent.width;
+        height: parent.height;
         anchors.centerIn: parent;
         radius: parent.radius
         color: "grey";
         opacity: 0
-        width:  parent.width;
-        height: parent.height;
     }
 
     Image {
         id: imageUp
         z:4;
+        width: 11;
+        height: 6;
         anchors.right: parent.right
         anchors.top: parent.top
         anchors.rightMargin: 2
         anchors.topMargin: 2
-        width: 11;
-        height: 6;
-
         source: "qrc:/button/up_enable.png";
     }
 
@@ -65,37 +132,39 @@ Rectangle {
 
     MouseArea {
         id: mouseAreaUp
+        z:5
+        width: 13;
+        height: rectangle1.height/2;
         anchors.right: parent.right
         anchors.top: parent.top
-        anchors.rightMargin: 2
-        anchors.topMargin: 2
-        width: 11;
-        height: rectangle1/2;
-        onClicked: {
-            console.log("t1");
-            //rectangle.clicked();
+        onClicked: text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
+        onPressAndHold:{
+            timerUp.restart;
+            timerUp.running=true;
         }
     }
 
     MouseArea {
         id: mouseAreaDown
+        z:5
+        width: 13;
+        height: rectangle1.height/2;
         anchors.right: parent.right
-        anchors.top: parent.top
-        anchors.rightMargin: 2
-        anchors.topMargin: 2
-        width: 11;
-        height: rectangle1/2;
-        onClicked: {
-            console.log("t2");
-            //rectangle.clicked();
+        anchors.bottom: parent.bottom
+        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: [
         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 }
         }
     ]
 }