54fe47df78cab961d262dd1516d4e847d9b0d386
[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     TextInput {
34         id: text_input1
35         x: 1
36         width: rectangle1.width-15
37         height: rectangle1.height*0.6;
38         text: "123"
39         anchors.centerIn: parent
40         validator: IntValidator{bottom: minValue; top: maxValue;}
41         transformOrigin: Item.Left
42         selectByMouse: true;
43         font.pixelSize: rectangle1.height * .5;
44         onCursorPositionChanged:  moveCursorSelection(cursorPosition);
45         onTextChanged:{
46             if(isTextInMinValue && text_input1.text!="" && stringToInt(text_input1.text)==minValue)
47                 text_input1.text=textInMinValue;
48
49             rectangle1.valueChange(stringToInt(text_input1.text));
50         }
51         onFocusChanged: {
52             if(focus==false)
53                text=stringToInt(text);
54         }
55     }
56
57     Timer {
58         id:timerUp;
59         interval: 100;
60         running: false;
61         repeat: true
62         onTriggered:{
63             if(mouseAreaUp.pressedButtons==Qt.LeftButton)
64                 text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
65             else
66                 running=false;
67         }
68     }
69
70     Timer {
71         id:timerDown;
72         interval: 100;
73         running: false;
74         repeat: true
75         onTriggered:{
76             if(mouseAreaDown.pressedButtons==Qt.LeftButton){
77                 text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
78                 if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
79                     text_input1.text=textInMinValue
80             }
81             else
82                 running=false;
83         }
84     }
85
86     Rectangle {
87         id: shadeDisable
88         width:  parent.width;
89         height: parent.height;
90         anchors.centerIn: parent;
91         radius: parent.radius
92         color: "grey";
93         opacity: 0
94     }
95
96     Image {
97         id: imageUp
98         z:4;
99         width: 11;
100         height: 6;
101         anchors.right: parent.right
102         anchors.top: parent.top
103         anchors.rightMargin: 2
104         anchors.topMargin: 2
105         source: "qrc:/button/up_enable.png";
106     }
107
108     Image {
109         id: imageDown
110         z:4;
111         width: 11;
112         height: 6;
113         anchors.right: parent.right
114         anchors.bottom: parent.bottom
115         anchors.rightMargin: 2
116         anchors.bottomMargin: 2
117         source: "qrc:/button/down_enable.png";
118     }
119
120     MouseArea {
121         id: mouseAreaUp
122         z:5
123         width: 13;
124         height: rectangle1.height/2;
125         anchors.right: parent.right
126         anchors.top: parent.top
127         onClicked: text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
128         onPressAndHold:{
129             timerUp.restart;
130             timerUp.running=true;
131         }
132     }
133
134     MouseArea {
135         id: mouseAreaDown
136         z:5
137         width: 13;
138         height: rectangle1.height/2;
139         anchors.right: parent.right
140         anchors.bottom: parent.bottom
141         onClicked:{
142             text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
143             if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
144                 text_input1.text=textInMinValue
145         }
146         onPressAndHold:{
147             timerDown.restart;
148             timerDown.running=true;
149         }
150     }
151 }
152