Debian Codegraph @

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