Debian Codegraph @

src/GUI/qml/IntegerSpinbox.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.4
5 import FlightGear 1.0
6
7 FocusScope {
8 id: root
9 property string label
10 property bool enabled: true
11 property int value: 0
12 property alias min: validator.bottom
13 property int max: validator.top
14 property bool wrap: false
15 property alias suffix: suffix.text
16 property alias prefix: prefix.text
17 property alias maxDigits: edit.maximumLength
18 property int step: 1
19 property bool live: false
20
21 implicitHeight: editFrame.height
22 // we have a margin between the frame and the label, and on each
23 implicitWidth: label.width + editFrame.width + Style.margin
24
25 signal commit(var newValue);
26
27 function incrementValue()
28 {
29 if (edit.activeFocus) {
30 var newValue = Math.min(parseInt(edit.text) + root.step, root.max)
31 edit.text = newValue
32 if (live) {
33 commit(newValue);
34 }
35 } else {
36 commit(Math.min(value + root.step, root.max))
37 }
38 }
39
40 function decrementValue()
41 {
42 if (edit.activeFocus) {
43 var newValue = Math.max(parseInt(edit.text) - root.step, root.min)
44 edit.text = newValue
45 if (live) {
46 commit(newValue);
47 }
48 } else {
49 commit(Math.max(value - root.step, root.min))
50 }
51 }
52
53 TextMetrics {
54 id: metrics
55 text: root.max // use maximum value
56 }
57
58 StyledText {
59 id: label
60 text: root.label
61 anchors.verticalCenter: editFrame.verticalCenter
62 hover: editFrame.activeFocus
63 enabled: root.enabled
64 }
65
66 MouseArea {
67 height: root.height
68 width: root.width
69 enabled: root.enabled
70
71 // use wheel events to adjust up/down
72 onClicked: {
73 edit.forceActiveFocus();
74 }
75
76 onWheel: {
77 var delta = wheel.angleDelta.y
78 if (delta > 0) {
79 root.incrementValue()
80 } else if (delta < 0) {
81 root.decrementValue()
82 }
83 }
84 }
85
86 // timer to commit the value when in live mode
87 Timer {
88 id: liveEditTimer
89 interval: 800
90 onTriggered: {
91 if (edit.activeFocus) {
92 commit(parseInt(edit.text));
93 }
94 }
95 }
96
97 Binding {
98 when: !edit.activeFocus
99 target: edit
100 property: "text"
101 value: root.value
102 }
103
104 Rectangle {
105 id: editFrame
106 clip: true
107 anchors.left: label.right
108 anchors.margins: Style.margin
109
110 height: edit.implicitHeight + Style.margin
111 width: edit.width + prefix.width + suffix.width + upDownArea.width + Style.margin * 2
112 radius: Style.roundRadius
113 border.color: root.enable ? (edit.activeFocus ? Style.frameColor : Style.minorFrameColor) : Style.disabledMinorFrameColor
114 border.width: 1
115
116 StyledText {
117 id: prefix
118 visible: root.prefix !== ""
119 enabled: root.enabled
120 anchors.baseline: edit.baseline
121 anchors.left: parent.left
122 anchors.margins: Style.margin
123 }
124
125 TextInput {
126 id: edit
127 enabled: root.enabled
128 anchors.verticalCenter: parent.verticalCenter
129 anchors.left: prefix.right
130 selectByMouse: true
131 width: metrics.width
132 horizontalAlignment: Text.AlignRight
133 font.pixelSize: Style.baseFontPixelSize
134
135 focus: true
136 color: enabled ? (activeFocus ? Style.themeColor : Style.baseTextColor) : Style.disabledTextColor
137
138 validator: IntValidator {
139 id: validator
140 }
141
142 Keys.onUpPressed: {
143 root.incrementValue();
144 }
145
146 Keys.onDownPressed: {
147 root.decrementValue();
148 }
149
150 onActiveFocusChanged: {
151 if (activeFocus) {
152 selectAll();
153 } else {
154 commit(parseInt(text))
155 liveEditTimer.stop();
156 }
157 }
158
159 onTextChanged: {
160 if (activeFocus && root.live) {
161 liveEditTimer.restart();
162 }
163 }
164 }
165
166 StyledText {
167 id: suffix
168 visible: root.suffix !== ""
169 enabled: root.enabled
170 anchors.baseline: edit.baseline
171 anchors.right: upDownArea.left
172 }
173
174 Item {
175 id: upDownArea
176 // color: "white"
177 anchors.right: parent.right
178 anchors.rightMargin: Style.margin
179 anchors.verticalCenter: editFrame.verticalCenter
180 height: upDownIcon.implicitHeight
181 visible: edit.activeFocus
182 width: upDownIcon.implicitWidth
183
184 Image {
185 id: upDownIcon
186 // show up/down arrows
187 source: "image://colored-icon/up-down?text"
188 }
189
190 MouseArea {
191 width: parent.width
192 height: parent.height / 2
193 onPressed: {
194 root.incrementValue();
195 }
196
197 Rectangle {
198 anchors.fill: parent
199 opacity: 0.5
200 color: Style.themeColor
201 visible: parent.pressed
202 }
203
204 Timer {
205 id: upRepeat
206 interval: 250
207 running: parent.pressed
208 repeat: true
209 onTriggered: root.incrementValue()
210 }
211 }
212
213 MouseArea {
214 width: parent.width
215 height: parent.height / 2
216 anchors.bottom: parent.bottom
217 onPressed: {
218 root.decrementValue();
219 }
220
221 Rectangle {
222 anchors.fill: parent
223 opacity: 0.5
224 color: Style.themeColor
225 visible: parent.pressed
226 }
227
228 Timer {
229 id: downRepeat
230 interval: 250
231 running: parent.pressed
232 repeat: true
233 onTriggered: root.decrementValue()
234 }
235 }
236 }
237 } // of frame rectangle
238 }