From b2af6308e565bcef875816b3152621d9789f52c1 Mon Sep 17 00:00:00 2001 From: casper Date: Mon, 31 Jan 2022 15:18:58 +0100 Subject: [PATCH] Add validator to material cost/weight spinboxes This way only valid numbers can be entered CURA-8684 --- resources/qml/Preferences/Materials/MaterialsView.qml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/resources/qml/Preferences/Materials/MaterialsView.qml b/resources/qml/Preferences/Materials/MaterialsView.qml index b19814a1a0..ab152a0fe3 100644 --- a/resources/qml/Preferences/Materials/MaterialsView.qml +++ b/resources/qml/Preferences/Materials/MaterialsView.qml @@ -271,13 +271,14 @@ OldControls.TabView text: spoolCostSpinBox.textFromValue(spoolCostSpinBox.value, spoolCostSpinBox.locale) selectByMouse: true background: Item {} + validator: RegExpValidator { regExp: new RegExp("^" + base.currency + " ([0-9]+[.]?[0-9]*)?$") } } property int decimals: 2 valueFromText: function(text) { // remove all non-number tokens from input string so value can be parsed correctly - var value = Number(text.replace(",", ".").replace(/[^0-9.-]+/g, "")); + var value = Number(text.replace(",", ".").replace(/[^0-9.]+/g, "")); var precision = Math.pow(10, spoolCostSpinBox.decimals); return Math.round(value * precision) / precision; } @@ -308,11 +309,12 @@ OldControls.TabView text: spoolWeightSpinBox.textFromValue(spoolWeightSpinBox.value, spoolWeightSpinBox.locale) selectByMouse: true background: Item {} + validator: RegExpValidator { regExp: new RegExp("^([0-9]+[.]?[0-9]*)? g$") } } valueFromText: function(text, locale) { // remove all non-number tokens from input string so value can be parsed correctly - var value = Number(text.replace(",", ".").replace(/[^0-9.-]+/g, "")); + var value = Number(text.replace(",", ".").replace(/[^0-9.]+/g, "")); return Math.round(value); }