add focus in qml
[mdictionary] / src / mdictionary / qml / MySpinBox.qml
1 import Qt 4.7
2
3 Rectangle {
4     id: rectangle1
5     radius: 10
6     border.color: "#000666";
7     property int maxValue:500;
8     property int minValue:0;
9     property alias value:text_input1.text
10     property bool isTextInMinValue:true;
11     property string textInMinValue:"inf";
12     property int singleStep: 2;
13
14     signal valueChange(int intiger);
15
16     function stringToInt(string){
17         var value=0;
18         var pow10=1;
19         if(isTextInMinValue && textInMinValue==string)
20             value=minValue;
21         else
22             for (var i=string.length-1;i>=0;i--){
23                 value+=(string.charCodeAt(i)-48)*pow10;
24                 pow10= pow10*10;
25             }
26         if(value>maxValue)
27             return maxValue;
28         if(value<minValue)
29             return minValue;
30         return value;
31     }
32
33     function setFocus(){
34         text_input1.focus=true;
35     }
36
37     TextInput {
38         id: text_input1
39         x: 1
40         width: rectangle1.width-15
41         height: rectangle1.height*0.6;
42         text: "123"
43         anchors.centerIn: parent
44         validator: IntValidator{bottom: minValue; top: maxValue;}
45         transformOrigin: Item.Left
46         selectByMouse: true;
47         font.pixelSize: rectangle1.height * .5;
48         onCursorPositionChanged:  moveCursorSelection(cursorPosition);
49         onTextChanged:{
50             if(isTextInMinValue && text_input1.text!="" && stringToInt(text_input1.text)==minValue)
51                 text_input1.text=textInMinValue;
52
53             rectangle1.valueChange(stringToInt(text_input1.text));
54         }
55         onFocusChanged: {
56             if(focus==false)
57                text=stringToInt(text);
58         }
59         Keys.onPressed: {
60             if (event.key == Qt.Key_Up)
61                 text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
62             else if (event.key == Qt.Key_Down){
63                 text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
64                 if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
65                     text_input1.text=textInMinValue
66             }
67         }
68     }
69
70     Timer {
71         id:timerUp;
72         interval: 100;
73         running: false;
74         repeat: true
75         onTriggered:{
76             if(mouseAreaUp.pressedButtons==Qt.LeftButton)
77                 text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
78             else
79                 running=false;
80         }
81     }
82
83     Timer {
84         id:timerDown;
85         interval: 100;
86         running: false;
87         repeat: true
88         onTriggered:{
89             if(mouseAreaDown.pressedButtons==Qt.LeftButton){
90                 text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
91                 if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
92                     text_input1.text=textInMinValue
93             }
94             else
95                 running=false;
96         }
97     }
98
99     Rectangle {
100         id: shadeDisable
101         width:  parent.width;
102         height: parent.height;
103         anchors.centerIn: parent;
104         radius: parent.radius
105         color: "grey";
106         opacity: 0
107     }
108
109     Image {
110         id: imageUp
111         z:4;
112         width: 11;
113         height: 6;
114         anchors.right: parent.right
115         anchors.top: parent.top
116         anchors.rightMargin: 2
117         anchors.topMargin: 2
118         source: "qrc:/button/up_enable.png";
119     }
120
121     Image {
122         id: imageDown
123         z:4;
124         width: 11;
125         height: 6;
126         anchors.right: parent.right
127         anchors.bottom: parent.bottom
128         anchors.rightMargin: 2
129         anchors.bottomMargin: 2
130         source: "qrc:/button/down_enable.png";
131     }
132
133     MouseArea {
134         id: mouseAreaUp
135         z:5
136         width: 13;
137         height: rectangle1.height/2;
138         anchors.right: parent.right
139         anchors.top: parent.top
140         onClicked: text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
141         onPressAndHold:{
142             timerUp.restart;
143             timerUp.running=true;
144         }
145     }
146
147     MouseArea {
148         id: mouseAreaDown
149         z:5
150         width: 13;
151         height: rectangle1.height/2;
152         anchors.right: parent.right
153         anchors.bottom: parent.bottom
154         onClicked:{
155             text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
156             if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
157                 text_input1.text=textInMinValue
158         }
159         onPressAndHold:{
160             timerDown.restart;
161             timerDown.running=true;
162         }
163     }
164     states: [
165         State {
166             name: "focusState"; when: text_input1.focus && rectangle1.enabled;
167             PropertyChanges { target: rectangle1; border.width: 2 }
168         }
169     ]
170 }
171