c248bbeac16fe5e994a287f53a658b2874aa52c8
[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
11     signal valueChange(int intiger);
12
13     function stringToInt(string){
14         var value=0;
15         var pow10=1;
16         for (var i=string.length-1;i>=0;i--){
17             value+=(string.charCodeAt(i)-48)*pow10;
18             pow10= pow10*10;
19         }
20         if(value>maxValue)
21             return maxValue;
22         if(value<minValue)
23             return minValue;
24         return value;
25     }
26
27     TextInput {
28         id: text_input1
29         x: 1
30         width: rectangle1.width-15
31         height: rectangle1.height*0.6;
32         text: "123"
33         anchors.centerIn: parent
34         validator: IntValidator{bottom: 0; top: 500;}
35         transformOrigin: Item.Left
36         selectByMouse: true;
37         font.pixelSize: rectangle1.height * .5;
38         onCursorPositionChanged:  moveCursorSelection(cursorPosition);
39         //focus: rectangle1.focus;
40         onTextChanged: rectangle1.valueChange(stringToInt(text_input1.text));
41
42     }
43
44     Timer {
45         id:timerUp;
46         interval: 100;
47         running: false;
48         repeat: true
49         onTriggered:{
50             if(mouseAreaUp.pressedButtons==Qt.LeftButton)
51                 text_input1.text=((stringToInt(text_input1.text)+1)>maxValue)?(maxValue):(stringToInt(text_input1.text)+1);
52             else
53                 running=false;
54         }
55     }
56
57     Timer {
58         id:timerDown;
59         interval: 100;
60         running: false;
61         repeat: true
62         onTriggered:{
63             if(mouseAreaDown.pressedButtons==Qt.LeftButton)
64                 text_input1.text=((stringToInt(text_input1.text)-1)<minValue)?(minValue):(stringToInt(text_input1.text)-1);
65             else
66                 running=false;
67         }
68     }
69
70     Rectangle {
71         id: shadeDisable
72         width:  parent.width;
73         height: parent.height;
74         anchors.centerIn: parent;
75         radius: parent.radius
76         color: "grey";
77         opacity: 0
78     }
79
80     Image {
81         id: imageUp
82         z:4;
83         width: 11;
84         height: 6;
85         anchors.right: parent.right
86         anchors.top: parent.top
87         anchors.rightMargin: 2
88         anchors.topMargin: 2
89         source: "qrc:/button/up_enable.png";
90     }
91
92     Image {
93         id: imageDown
94         z:4;
95         width: 11;
96         height: 6;
97         anchors.right: parent.right
98         anchors.bottom: parent.bottom
99         anchors.rightMargin: 2
100         anchors.bottomMargin: 2
101         source: "qrc:/button/down_enable.png";
102     }
103
104     MouseArea {
105         id: mouseAreaUp
106         z:5
107         width: 13;
108         height: rectangle1.height/2;
109         anchors.right: parent.right
110         anchors.top: parent.top
111         onClicked: text_input1.text=((stringToInt(text_input1.text)+1)>maxValue)?(maxValue):(stringToInt(text_input1.text)+1);
112         onPressAndHold:{
113             timerUp.restart;
114             timerUp.running=true;
115         }
116     }
117
118     MouseArea {
119         id: mouseAreaDown
120         z:5
121         width: 13;
122         height: rectangle1.height/2;
123         anchors.right: parent.right
124         anchors.bottom: parent.bottom
125         onClicked: text_input1.text=((stringToInt(text_input1.text)-1)<minValue)?(minValue):(stringToInt(text_input1.text)-1);
126         onPressAndHold:{
127             timerDown.restart;
128             timerDown.running=true;
129         }
130     }
131
132    /* states: [
133         State {
134             name: "DisableState"; when: rectangle1.enabled==false;
135             PropertyChanges { target: shadeDisable; z: 3; opacity: 0.5 }
136         }
137     ]
138     */
139 }
140