From e6c0c82ce885bc8cb882bf7a0356944d19f8844f Mon Sep 17 00:00:00 2001 From: Shrief Gabr Date: Thu, 4 Jan 2024 11:08:52 +0200 Subject: [PATCH] QmlDesigner: Remove trailing zeroes from numbers Task-number: QDS-11578 Change-Id: Ifbbe5ef764ad383863db07dc2cfaf444af7c8805 Reviewed-by: Mahmoud Badri --- .../CollectionDetailsEditDelegate.qml | 1 + .../imports/StudioControls/RealSpinBox.qml | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/share/qtcreator/qmldesigner/collectionEditorQmlSource/CollectionDetailsEditDelegate.qml b/share/qtcreator/qmldesigner/collectionEditorQmlSource/CollectionDetailsEditDelegate.qml index 2ed209eaf22..e368bb09331 100644 --- a/share/qtcreator/qmldesigner/collectionEditorQmlSource/CollectionDetailsEditDelegate.qml +++ b/share/qtcreator/qmldesigner/collectionEditorQmlSource/CollectionDetailsEditDelegate.qml @@ -90,6 +90,7 @@ Item { realTo: 9e9 realStepSize: 1.0 decimals: 6 + trailingZeroes: false } } } diff --git a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml index 19496da346d..37612af13f8 100644 --- a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml +++ b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml @@ -37,6 +37,8 @@ T.SpinBox { property bool drag: false property bool sliderDrag: sliderPopup.drag + property bool trailingZeroes: true + property bool dirty: false // user modification flag // TODO Not used anymore. Will be removed when all dependencies were removed. @@ -204,11 +206,14 @@ T.SpinBox { textFromValue: function (value, locale) { locale.numberOptions = Locale.OmitGroupSeparator - return Number(control.realValue).toLocaleString(locale, 'f', control.decimals) + var decimals = trailingZeroes ? control.decimals : decimalCounter(value) + + return Number(control.realValue).toLocaleString(locale, 'f', decimals) } valueFromText: function (text, locale) { control.setRealValue(Number.fromLocaleString(locale, spinBoxInput.text)) + return 0 } @@ -400,4 +405,14 @@ T.SpinBox { if (control.realValue !== currValue) control.realValueModified() } + + function decimalCounter(number) { + var strNumber = Math.abs(number).toString() + var decimalIndex = strNumber.indexOf('.') + + // Set 'index' to a minimum of 1 if there are no fractions + var index = decimalIndex == -1 ? 1 : strNumber.length - decimalIndex - 1 + + return Math.min(index, control.decimals); + } }