Debian Codegraph @

src/GUI/qml/DateTimeValueEdit.qml

raw
1 import QtQuick 2.4
2 import FlightGear 1.0
3
4 FocusScope {
5 id: root
6 property Item nextToFocus
7 property Item previousToFocus
8
9 property int value: 0
10 property int minValue: 0 // might be 1 for day-of-month
11 property int maxValue: 99 // max numerical value
12
13 property int fieldWidth: 2
14 property bool valueWraps: false
15
16 property string widthString: "00"
17
18 width: metrics.width + (input.activeFocus ? upDownArea.width : 0)
19 height: metrics.height
20
21 // promote Z value when focused so our icon decoration
22 // is on top of our sibling edits.
23 z: input.activeFocus ? 10 : 0
24
25 signal commit(var newValue);
26
27 onMaxValueChanged: {
28 // cap current value if the max is now lower
29 value = Math.min(value, maxValue);
30 }
31
32 function doCommit(newValue)
33 {
34 value = newValue;
35 commit(newValue)
36 }
37
38 function zeroPaddedNumber(v)
39 {
40 var s = v.toString();
41 while (s.length < fieldWidth) {
42 s = "0" + s;
43 }
44 return s;
45 }
46
47 readonly property string __valueString: zeroPaddedNumber(value)
48
49 function incrementValue()
50 {
51 if (input.activeFocus) {
52 value = Math.min(parseInt(input.text) + 1, maxValue)
53 input.text = __valueString
54 } else {
55 doCommit(Math.min(value + 1, maxValue));
56 }
57 }
58
59 function decrementValue()
60 {
61 if (input.activeFocus) {
62 value = Math.max(parseInt(input.text) - 1, minValue)
63 input.text = __valueString
64 } else {
65 doCommit(Math.max(value - 1, minValue));
66 }
67 }
68
69 function focusNext()
70 {
71 if (nextToFocus != undefined) {
72 nextToFocus.forceActiveFocus();
73 }
74 }
75
76 TextInput {
77 id: input
78 focus: true
79
80 // validate on integers
81 height: parent.height
82 width: metrics.width
83 maximumLength: fieldWidth
84 font.pixelSize: Style.baseFontPixelSize
85
86 Keys.onUpPressed: {
87 incrementValue();
88 }
89
90 Keys.onDownPressed: {
91 decrementValue();
92 }
93
94 Keys.onTabPressed: { root.focusNext(); }
95
96 Keys.onBacktabPressed: {
97 if (previousToFocus != undefined) {
98 previousToFocus.focus = true;
99 }
100 }
101
102 Keys.onPressed: {
103 if ((event.key === Qt.Key_Colon) || (event.key === Qt.Key_Slash)) {
104 nextToFocus.focus = true;
105 event.accepted = true;
106 }
107 }
108
109 // onTextChanged: {
110 // if (activeFocus && (text.length == root.widthString.length)) {
111 // root.focusNext();
112 // }
113 // }
114
115 onActiveFocusChanged: {
116 if (activeFocus) {
117 selectAll();
118 } else {
119 doCommit(parseInt(text))
120 }
121 }
122
123 validator: IntValidator {
124 id: validator
125 top: root.maxValue
126 bottom: root.minValue
127 }
128 }
129
130 Binding {
131 when: !input.activeFocus
132 target: input
133 property: "text"
134 value: root.__valueString
135 }
136
137 MouseArea {
138 height: root.height
139 width: root.width
140 // use wheel events to adjust up/down
141 onClicked: {
142 input.forceActiveFocus();
143 }
144
145 onWheel: {
146 var delta = wheel.angleDelta.y
147 if (delta > 0) {
148 root.incrementValue()
149 } else if (delta < 0) {
150 root.decrementValue()
151 }
152 }
153 }
154
155 Rectangle {
156 id: upDownArea
157 color: Style.backgroundColor
158 anchors.left: input.right
159 anchors.verticalCenter: input.verticalCenter
160 height: upDownIcon.implicitHeight
161 visible: input.activeFocus
162 width: upDownIcon.implicitWidth
163
164 Image {
165 id: upDownIcon
166 // show up/down arrows
167 source: "image://colored-icon/up-down?text"
168 }
169
170 MouseArea {
171 width: parent.width
172 height: parent.height / 2
173 onPressed: {
174 root.incrementValue();
175 }
176 }
177
178 MouseArea {
179 width: parent.width
180 height: parent.height / 2
181 anchors.bottom: parent.bottom
182 onPressed: {
183 root.decrementValue();
184 }
185 }
186 }
187
188 TextMetrics {
189 id: metrics
190 text: root.widthString
191 }
192 }