| 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 |
import FlightGear.Launcher 1.0 |
| 7 |
|
| 8 |
FocusScope { |
| 9 |
id: root |
| 10 |
property string label |
| 11 |
property bool enabled: true |
| 12 |
property var quantity |
| 13 |
property bool live: false |
| 14 |
property alias unitsMode: units.mode |
| 15 |
|
| 16 |
readonly property int microPadding: 2 |
| 17 |
|
| 18 |
UnitsModel { |
| 19 |
id: units |
| 20 |
} |
| 21 |
|
| 22 |
implicitHeight: editFrame.height |
| 23 |
// we have a margin between the frame and the label, and on each |
| 24 |
implicitWidth: label.width + editFrame.width + Style.margin |
| 25 |
|
| 26 |
signal commit(var newValue); |
| 27 |
|
| 28 |
function doCommit(newValue) |
| 29 |
{ |
| 30 |
var q = quantity; |
| 31 |
q.value = newValue; |
| 32 |
commit(q); |
| 33 |
} |
| 34 |
|
| 35 |
function parseTextAsValue() |
| 36 |
{ |
| 37 |
if (units.numDecimals == 0) { |
| 38 |
return parseInt(edit.text); |
| 39 |
} |
| 40 |
|
| 41 |
return parseFloat(edit.text); |
| 42 |
} |
| 43 |
|
| 44 |
function clampValue(newValue) |
| 45 |
{ |
| 46 |
if (units.wraps) { |
| 47 |
// integer wrapping for now |
| 48 |
var range = (units.maxValue - units.minValue + 1); |
| 49 |
if (newValue < units.minValue) newValue += range; |
| 50 |
if (newValue > units.maxValue) newValue -= range; |
| 51 |
} |
| 52 |
|
| 53 |
return Math.min(Math.max(units.minValue, newValue), units.maxValue); |
| 54 |
} |
| 55 |
|
| 56 |
function incrementValue() |
| 57 |
{ |
| 58 |
if (edit.activeFocus) { |
| 59 |
var newValue = clampValue(parseTextAsValue() + units.stepSize); |
| 60 |
edit.text = newValue |
| 61 |
if (live) { |
| 62 |
doCommit(newValue); |
| 63 |
} |
| 64 |
} else { |
| 65 |
doCommit(clampValue(quantity.value + units.stepSize)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function decrementValue() |
| 70 |
{ |
| 71 |
if (edit.activeFocus) { |
| 72 |
var newValue = clampValue(parseTextAsValue() - units.stepSize); |
| 73 |
edit.text = newValue |
| 74 |
if (live) { |
| 75 |
doCommit(newValue); |
| 76 |
} |
| 77 |
} else { |
| 78 |
doCommit(clampValue(quantity.value - units.stepSize)); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
function changeToUnits(unitIndex) |
| 83 |
{ |
| 84 |
if (edit.activeFocus) { |
| 85 |
// build up a quantity with the current text value, in the |
| 86 |
// previous units. We need to parse as text before we |
| 87 |
// change the selected unit |
| 88 |
var q = root.quantity; |
| 89 |
q.value = clampValue(parseTextAsValue()); |
| 90 |
|
| 91 |
// convert to the new quantity |
| 92 |
units.selectedIndex = unitIndex; |
| 93 |
var newQuantity = q.convertToUnit(units.selectedUnit) |
| 94 |
|
| 95 |
edit.text = newQuantity.value.toFixed(units.numDecimals) |
| 96 |
commit(newQuantity); |
| 97 |
} else { |
| 98 |
// not focused, easy |
| 99 |
units.selectedIndex = unitIndex; |
| 100 |
root.commit(root.quantity.convertToUnit(units.selectedUnit)); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
function showUnitsMenu() |
| 105 |
{ |
| 106 |
OverlayShared.globalOverlay.showOverlayAtItemOffset(menu, root, |
| 107 |
Qt.point(editFrame.x, editFrame.height)) |
| 108 |
} |
| 109 |
|
| 110 |
Component.onCompleted: { |
| 111 |
// ensure any initial value is accepted by our mode. |
| 112 |
// this stops people passing in completely wrong quantities |
| 113 |
if (!units.isUnitInMode(quantity.unit)) { |
| 114 |
console.warn("NumericalEdit: was initialized with incorrect unit"); |
| 115 |
var q = quantity; |
| 116 |
q.unit = units.selectedUnit; |
| 117 |
commit(q); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
onQuantityChanged: { |
| 122 |
if (units.isUnitInMode(quantity.unit)) { |
| 123 |
// ensure our units model is in sync |
| 124 |
units.selectedUnit = quantity.unit |
| 125 |
} else { |
| 126 |
console.warn("Passed illegal quantity"); |
| 127 |
// reset back to something permitted |
| 128 |
var q = quantity; |
| 129 |
q.unit = units.selectedUnit; |
| 130 |
commit(q); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
TextMetrics { |
| 135 |
id: metrics |
| 136 |
text: units.maxTextForMetrics |
| 137 |
} |
| 138 |
|
| 139 |
StyledText { |
| 140 |
id: label |
| 141 |
text: root.label |
| 142 |
anchors.verticalCenter: editFrame.verticalCenter |
| 143 |
hover: editFrame.activeFocus |
| 144 |
enabled: root.enabled |
| 145 |
} |
| 146 |
|
| 147 |
MouseArea { |
| 148 |
height: root.height |
| 149 |
width: root.width |
| 150 |
enabled: root.enabled |
| 151 |
|
| 152 |
// use wheel events to adjust up/down |
| 153 |
onClicked: { |
| 154 |
edit.forceActiveFocus(); |
| 155 |
} |
| 156 |
|
| 157 |
onWheel: { |
| 158 |
var delta = wheel.angleDelta.y |
| 159 |
if (delta > 0) { |
| 160 |
root.incrementValue() |
| 161 |
} else if (delta < 0) { |
| 162 |
root.decrementValue() |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// timer to commit the value when in live mode |
| 168 |
Timer { |
| 169 |
id: liveEditTimer |
| 170 |
interval: 800 |
| 171 |
onTriggered: { |
| 172 |
if (edit.activeFocus) { |
| 173 |
doCommit(parseTextAsValue()); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Binding { |
| 179 |
when: !edit.activeFocus |
| 180 |
target: edit |
| 181 |
property: "text" |
| 182 |
value: root.quantity.value.toFixed(units.numDecimals) |
| 183 |
} |
| 184 |
|
| 185 |
Rectangle { |
| 186 |
id: editFrame |
| 187 |
clip: true |
| 188 |
anchors.left: label.right |
| 189 |
anchors.margins: Style.margin |
| 190 |
|
| 191 |
height: edit.implicitHeight + Style.margin |
| 192 |
width: edit.width + prefix.width + suffix.width + upDownArea.width + Style.margin * 2 + (microPadding * 2) |
| 193 |
radius: Style.roundRadius |
| 194 |
border.color: root.enable ? (edit.activeFocus ? Style.frameColor : Style.minorFrameColor) : Style.disabledMinorFrameColor |
| 195 |
border.width: 1 |
| 196 |
color: root.enabled ? Style.panelBackground : Style.backgroundColor |
| 197 |
|
| 198 |
ClickableText { |
| 199 |
id: prefix |
| 200 |
visible: units.isPrefix |
| 201 |
enabled: root.enabled |
| 202 |
anchors.baseline: edit.baseline |
| 203 |
anchors.left: parent.left |
| 204 |
anchors.margins: Style.margin |
| 205 |
text: visible ? units.shortText : "" |
| 206 |
onClicked: root.showUnitsMenu() |
| 207 |
clickable: (units.numChoices > 1) |
| 208 |
} |
| 209 |
|
| 210 |
TextInput { |
| 211 |
id: edit |
| 212 |
enabled: root.enabled |
| 213 |
anchors.verticalCenter: parent.verticalCenter |
| 214 |
anchors.left: prefix.right |
| 215 |
anchors.leftMargin: microPadding |
| 216 |
|
| 217 |
selectByMouse: true |
| 218 |
width: metrics.width |
| 219 |
horizontalAlignment: Text.AlignRight |
| 220 |
font.pixelSize: Style.baseFontPixelSize |
| 221 |
|
| 222 |
focus: true |
| 223 |
color: enabled ? (activeFocus ? Style.themeColor : Style.baseTextColor) : Style.disabledTextColor |
| 224 |
|
| 225 |
validator: units.validator |
| 226 |
|
| 227 |
Keys.onUpPressed: { |
| 228 |
root.incrementValue(); |
| 229 |
} |
| 230 |
|
| 231 |
Keys.onDownPressed: { |
| 232 |
root.decrementValue(); |
| 233 |
} |
| 234 |
|
| 235 |
Keys.onReturnPressed: { |
| 236 |
root.focus = false; |
| 237 |
// will trigger onActiveFocusChanged |
| 238 |
} |
| 239 |
|
| 240 |
onActiveFocusChanged: { |
| 241 |
if (activeFocus) { |
| 242 |
selectAll(); |
| 243 |
} else { |
| 244 |
doCommit(parseTextAsValue()) |
| 245 |
liveEditTimer.stop(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
onTextChanged: { |
| 250 |
if (activeFocus && root.live) { |
| 251 |
liveEditTimer.restart(); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
ClickableText { |
| 257 |
id: suffix |
| 258 |
visible: !units.isPrefix |
| 259 |
enabled: root.enabled |
| 260 |
anchors.baseline: edit.baseline |
| 261 |
anchors.right: upDownArea.left |
| 262 |
anchors.rightMargin: microPadding |
| 263 |
|
| 264 |
text: visible ? units.shortText : "" |
| 265 |
onClicked: root.showUnitsMenu(); |
| 266 |
clickable: (units.numChoices > 1) |
| 267 |
} |
| 268 |
|
| 269 |
Item { |
| 270 |
id: upDownArea |
| 271 |
// color: "white" |
| 272 |
anchors.right: parent.right |
| 273 |
anchors.rightMargin: Style.margin |
| 274 |
anchors.verticalCenter: editFrame.verticalCenter |
| 275 |
height: upDownIcon.implicitHeight |
| 276 |
visible: edit.activeFocus |
| 277 |
width: upDownIcon.implicitWidth |
| 278 |
|
| 279 |
Image { |
| 280 |
id: upDownIcon |
| 281 |
// show up/down arrows |
| 282 |
source: "image://colored-icon/up-down?text" |
| 283 |
} |
| 284 |
|
| 285 |
MouseArea { |
| 286 |
width: parent.width |
| 287 |
height: parent.height / 2 |
| 288 |
onPressed: { |
| 289 |
root.incrementValue(); |
| 290 |
} |
| 291 |
|
| 292 |
Rectangle { |
| 293 |
anchors.fill: parent |
| 294 |
opacity: 0.5 |
| 295 |
color: Style.themeColor |
| 296 |
visible: parent.pressed |
| 297 |
} |
| 298 |
|
| 299 |
Timer { |
| 300 |
id: upRepeat |
| 301 |
interval: 250 |
| 302 |
running: parent.pressed |
| 303 |
repeat: true |
| 304 |
onTriggered: root.incrementValue() |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
MouseArea { |
| 309 |
width: parent.width |
| 310 |
height: parent.height / 2 |
| 311 |
anchors.bottom: parent.bottom |
| 312 |
onPressed: { |
| 313 |
root.decrementValue(); |
| 314 |
} |
| 315 |
|
| 316 |
Rectangle { |
| 317 |
anchors.fill: parent |
| 318 |
opacity: 0.5 |
| 319 |
color: Style.themeColor |
| 320 |
visible: parent.pressed |
| 321 |
} |
| 322 |
|
| 323 |
Timer { |
| 324 |
id: downRepeat |
| 325 |
interval: 250 |
| 326 |
running: parent.pressed |
| 327 |
repeat: true |
| 328 |
onTriggered: root.decrementValue() |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
} // of frame rectangle |
| 333 |
|
| 334 |
Component { |
| 335 |
id: menu |
| 336 |
|
| 337 |
OverlayMenu { |
| 338 |
model: units |
| 339 |
displayRole: "longName" |
| 340 |
currentIndex: units.selectedIndex |
| 341 |
onSelect: root.changeToUnits(index); |
| 342 |
} |
| 343 |
} |
| 344 |
} |