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