Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix locale handling in QML parsed spinboxes.
Wolthera van Hövell tot Westerflier5469e2ecab3a
diff --git a/qmlmodules/components/CMakeLists.txt b/qmlmodules/components/CMakeLists.txtindex 6715f796b1..202b260fac 100644--- a/qmlmodules/components/CMakeLists.txt+++ b/qmlmodules/components/CMakeLists.txt@@ -53,7 +53,6 @@ ecm_target_qml_sources(kritaqmlcomponents SOURCESqml/spinbox/KisIntParseSpinBox.qmlqml/spinbox/KisDoubleSpinBox.qmlqml/spinbox/KisDoubleParseSpinBox.qml- qml/spinbox/KisParseSpinBoxUtil.jsqml/sliderspinbox/KisSliderSpinBoxManipulator.qmlqml/sliderspinbox/KisSliderSpinBoxRangeSwitch.qmldiff --git a/qmlmodules/components/qml/sliderspinbox/KisSliderSpinBoxContentItem.qml b/qmlmodules/components/qml/sliderspinbox/KisSliderSpinBoxContentItem.qmlindex 30f7466591..79b0f6c122 100644--- a/qmlmodules/components/qml/sliderspinbox/KisSliderSpinBoxContentItem.qml+++ b/qmlmodules/components/qml/sliderspinbox/KisSliderSpinBoxContentItem.qml@@ -44,11 +44,11 @@ FocusScope {visible: root.showSoftRange;color: root.parentSpinBox.palette.text//: @info:tooltip toggle between soft and hard range in the slider spin box- toolTip: qsTr("Toggle between full range and subrange.\nFull range: [%1, %2]\nSubrange: [%3, %4]")- .arg(root.from.toFixed(root.decimals))- .arg(root.to.toFixed(root.decimals))- .arg(root.softFrom.toFixed(root.decimals))- .arg(root.softTo.toFixed(root.decimals))+ toolTip: i18nc("@info:tooltip","Toggle between full range and subrange.\nFull range: [%1, %2]\nSubrange: [%3, %4]",+ Number(root.from).toLocaleString(Qt.locale(), decimals),+ Number(root.to).toLocaleString(Qt.locale(), decimals),+ Number(root.softFrom).toLocaleString(Qt.locale(), decimals),+ Number(root.softTo).toLocaleString(Qt.locale(), decimals));onSoftRangeActiveChanged: root.softRangeActive = softRangeActive}diff --git a/qmlmodules/components/qml/spinbox/KisDoubleSpinBox.qml b/qmlmodules/components/qml/spinbox/KisDoubleSpinBox.qmlindex a5a83add4c..2a315b588e 100644--- a/qmlmodules/components/qml/spinbox/KisDoubleSpinBox.qml+++ b/qmlmodules/components/qml/spinbox/KisDoubleSpinBox.qml@@ -5,7 +5,6 @@*/import QtQuick 2.15import QtQuick.Controls 2.15-import "KisParseSpinBoxUtil.js" as KisParseSpinBoxUtilSpinBox {id: root@@ -24,8 +23,6 @@ SpinBox {onValueChanged: root.dValue = root.value / root.factor;- onDecimalsChanged: root.dValue = KisParseSpinBoxUtil.truncateValue(root.dValue, root.decimals)-onDValueChanged: {let v = root.dValue;if (v > root.dTo) {@@ -33,7 +30,6 @@ SpinBox {} else if (v < root.dFrom) {v = root.dFrom;}- v = parseFloat(KisParseSpinBoxUtil.truncateValue(v, root.decimals));if (v !== root.dValue) {root.dValue = v;}@@ -45,7 +41,7 @@ SpinBox {root.dFrom = root.dTo;}if (root.dFrom > root.dValue) {- root.dValue = KisParseSpinBoxUtil.truncateValue(root.dFrom, root.decimals);+ root.dValue = root.dFrom;}}@@ -54,10 +50,18 @@ SpinBox {root.dTo = root.dFrom;}if (root.dTo < root.dValue) {- root.dValue = KisParseSpinBoxUtil.truncateValue(root.dTo, root.decimals);+ root.dValue = root.dTo;}}+ textFromValue: function(value, locale) {+ return Number(value / factor).toLocaleString(locale, 'f', decimals)+ }++ valueFromText: function(text, locale) {+ return Number.fromLocaleString(locale, text) * factor;+ }+Component.onCompleted: {root.value = Math.round(root.dValue * root.factor)diff --git a/qmlmodules/components/qml/spinbox/KisParseSpinBoxContentItem.qml b/qmlmodules/components/qml/spinbox/KisParseSpinBoxContentItem.qmlindex bf506aaa88..e4ba9b1b19 100644--- a/qmlmodules/components/qml/spinbox/KisParseSpinBoxContentItem.qml+++ b/qmlmodules/components/qml/spinbox/KisParseSpinBoxContentItem.qml@@ -7,7 +7,6 @@ import QtQml 2.15import QtQuick 2.15import QtQuick.Controls 2.15import org.krita.components 1.0-import "KisParseSpinBoxUtil.js" as KisParseSpinBoxUtilFocusScope {id: root@@ -29,9 +28,9 @@ FocusScope {implicitHeight: textInput.implicitHeightimplicitWidth: textMetrics.boundingRect.width + textInput.padding * 2- onValueChanged: textInput.contentsText = parseFloat(KisParseSpinBoxUtil.truncateValue(root.value, root.decimals));+ onValueChanged: textInput.contentsText = Number(root.value).toLocaleString(Qt.locale(), 'f', decimals);- onDecimalsChanged: textInput.contentsText = KisParseSpinBoxUtil.truncateValue(root.value, root.decimals)+ onDecimalsChanged: textInput.contentsText = Number(root.value).toLocaleString(Qt.locale(), 'f', decimals);KisWarningOverlay {id: warningOverlay@@ -53,7 +52,7 @@ FocusScope {anchors.fill: parentverticalAlignment: Text.AlignVCenterpadding: 4- contentsText: KisParseSpinBoxUtil.truncateValue(root.value, root.decimals)+ contentsText: Number(root.value).toLocaleString(Qt.locale(), 'f', decimals);focus: truefont: root.parentSpinBox.fontcolor: root.parentSpinBox.palette.text@@ -68,7 +67,7 @@ FocusScope {warningOverlay.warn = true;} else {warningOverlay.warn = false;- root.value = KisParseSpinBoxUtil.truncateValue(parsedValue, root.decimals);+ root.value = parsedValue;root.editingFinished();}}@@ -89,7 +88,7 @@ FocusScope {Keys.onEscapePressed: {warningTimer.stop();warningOverlay.warn = false;- textInput.contentsText = KisParseSpinBoxUtil.truncateValue(root.value, root.decimals);+ textInput.contentsText = Number(root.value).toLocaleString(Qt.locale(), 'f', decimals);root.editingCanceled();}@@ -141,8 +140,8 @@ FocusScope {id: textMetricsfont: textInput.fonttext: {- const fromText = KisParseSpinBoxUtil.truncateValue(root.from, root.decimals);- const toText = KisParseSpinBoxUtil.truncateValue(root.to, root.decimals);+ const fromText = Number(root.from).toLocaleString(Qt.locale(), 'f', decimals);+ const toText = Number(root.to).toLocaleString(Qt.locale(), 'f', decimals);return root.prefix+ (fromText.length > toText.length ? fromText : toText)+ root.suffixdiff --git a/qmlmodules/components/qml/spinbox/KisParseSpinBoxUtil.js b/qmlmodules/components/qml/spinbox/KisParseSpinBoxUtil.jsdeleted file mode 100644index a73d19cb88..0000000000--- a/qmlmodules/components/qml/spinbox/KisParseSpinBoxUtil.js+++ /dev/null@@ -1,30 +0,0 @@-/*- * SPDX-FileCopyrightText: 2024 Deif Lou <[email protected]>- *- * SPDX-License-Identifier: GPL-2.0-or-later- */-/**- This function tries to truncate a float value, so that- it will always show the minimum amount of decimals behind- the dot.-- TODO: This isn't locale friendly.- */-function truncateValue(value, decimals)-{- if (isNaN(parseFloat(value))) return 0;- const reg = new RegExp("^-?\\d+(?:\\.\\d{0," + decimals + "})?", "g");- const a = value.toString().match(reg)[0];- const dot = a.indexOf(".");- if (dot === -1) {- if (decimals < 1) {- return a;- }- return a + "." + "0".repeat(decimals);- }- if (decimals < 1) {- return a.slice(0, dot);- }- const b = decimals - (a.length - dot) + 1;- return b > 0 ? (a + "0".repeat(b)) : a;-}