Skip to content

Commit

Permalink
Remove SpinBox validators; simplify focus removal and value correction.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTrahearn-Qinetic committed Dec 11, 2024
1 parent 79401d9 commit f501ca7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
29 changes: 16 additions & 13 deletions components/controls/SpinBox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ CT.SpinBox {
} else if (value === from) {
root.minValueReached()
}
// close the Virtual Keyboard on Return/Enter pressed
root.focus = false
}

contentItem: Item {
Expand Down Expand Up @@ -101,6 +99,11 @@ CT.SpinBox {
selectByMouse: !readOnly
validator: root.validator
inputMethodHints: root.inputMethodHints

onAccepted: {
// Note that the text may at this time represent a value out of SpinBox to/from range.
root.focus = false
}
}

Label {
Expand Down Expand Up @@ -168,21 +171,21 @@ CT.SpinBox {
}
}

validator: DoubleValidator {
bottom: Math.min(root.from, root.to)
top: Math.max(root.from, root.to)
notation: DoubleValidator.StandardNotation
}

textFromValue: function(value, locale) {
return Units.formatNumber(value)
}
valueFromText: function(text, locale) {
// close the Virtual Keyboard on Return/Enter pressed
// even if the value hasn't actually changed e.g. in the case
// the value is about to be set out of range.
root.focus = false
return Number.fromLocaleString(locale, text)
const value = Number.fromLocaleString(locale, text)
// need to set spinBox.value to a different temporary valid value
// to illicit a value change so it always updates the displayText properly
// in all cases, to something that is valid
if(value <= root.from) {
root.value = root.to
} else {
// if the value is >= to or any other value above from, set it to from
root.value = root.from
}
return value
}

Timer {
Expand Down
25 changes: 11 additions & 14 deletions components/dialogs/NumberSelectorDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,17 @@ ModalDialog {
return Units.formatNumber(value / root._multiplier(), root.decimals)
}
valueFromText: function(text, locale) {
// close the Virtual Keyboard on Return/Enter pressed
// even if the value hasn't actually changed e.g. in the case
// the value is about to be set out of range.
spinBox.focus = false
return Number.fromLocaleString(locale, text) * root._multiplier()
const value = Number.fromLocaleString(locale, text) * root._multiplier()
// need to set spinBox.value to a different temporary valid value
// to illicit a value change so it always updates the displayText properly
// in all cases, to something that is valid
if(value <= spinBox.from) {
spinBox.value = spinBox.to
} else {
// if the value is >= to or any other value above from, set it to from
spinBox.value = spinBox.from
}
return value
}
from: Math.max(Global.int32Min, root.from * root._multiplier())
to: Math.min(Global.int32Max, root.to * root._multiplier())
Expand All @@ -87,15 +93,6 @@ ModalDialog {
spinBox.value = Math.round(root.value * root._multiplier())
_initialized = true
}

validator: DoubleValidator {
// text editing needs validating in the non-multiplied range
// this stops you entering a value outside this range
bottom: Math.min(root.from, root.to)
top: Math.max(root.from, root.to)
decimals: root.decimals
notation: DoubleValidator.StandardNotation
}
}

SegmentedButtonRow {
Expand Down

0 comments on commit f501ca7

Please sign in to comment.