Allow SpinBox components to contain fractional values

This commit is contained in:
casper 2022-02-08 15:52:49 +01:00
parent a0e5d66299
commit 9c2d370e72
2 changed files with 58 additions and 28 deletions

View File

@ -243,7 +243,7 @@ Item
value: properties.density value: properties.density
decimals: 2 decimals: 2
suffix: " g/cm³" suffix: " g/cm³"
// stepSize: 0.01 // spinboxes can only cointain reals, a non-integer value can not be entered as the step size stepSize: 0.01
onEditingFinished: base.setMetaDataEntry("properties/density", properties.density, value) onEditingFinished: base.setMetaDataEntry("properties/density", properties.density, value)
onValueChanged: updateCostPerMeter() onValueChanged: updateCostPerMeter()
@ -258,7 +258,7 @@ Item
value: properties.diameter value: properties.diameter
decimals: 2 decimals: 2
suffix: " mm" suffix: " mm"
// stepSize: 0.01 // spinboxes can only cointain reals, a non-integer value can not be entered as the step size stepSize: 0.01
onEditingFinished: onEditingFinished:
{ {
@ -389,12 +389,6 @@ Item
Item { width: parent.width; height: UM.Theme.getSize("default_margin").height } Item { width: parent.width; height: UM.Theme.getSize("default_margin").height }
} }
function updateCostPerMeter()
{
base.spoolLength = calculateSpoolLength(diameterSpinBox.value, densitySpinBox.value, spoolWeightSpinBox.value);
base.costPerMeter = calculateCostPerMeter(spoolCostSpinBox.value);
}
} }
ListView ListView
@ -606,4 +600,10 @@ Item
base.setMetaDataEntry("brand", old_brand, new_brand) base.setMetaDataEntry("brand", old_brand, new_brand)
properties.brand = new_brand properties.brand = new_brand
} }
function updateCostPerMeter()
{
base.spoolLength = calculateSpoolLength(diameterSpinBox.value, densitySpinBox.value, spoolWeightSpinBox.value);
base.costPerMeter = calculateCostPerMeter(spoolCostSpinBox.value);
}
} }

View File

@ -4,35 +4,64 @@
import QtQuick 2.2 import QtQuick 2.2
import QtQuick.Controls 2.15 import QtQuick.Controls 2.15
SpinBox Item
{ {
id: base id: base
height: spinBox.height
property string prefix: "" property string prefix: ""
property string suffix: "" property string suffix: ""
property int decimals: 0 property int decimals: 0
property real stepSize: 1
property real value: 0
property real from: 0
property real to: 99
signal editingFinished() property alias wrap: spinBox.wrap
valueFromText: function(text) property bool editable: true
{
return parseFloat(text.substring(prefix.length, text.length - suffix.length), decimals);
}
textFromValue: function(value) property var validator: RegExpValidator
{
return prefix + value.toFixed(decimals) + suffix
}
validator: RegExpValidator
{ {
regExp: new RegExp("^" + prefix + "([0-9]+[.|,]?[0-9]*)?" + suffix + "$") regExp: new RegExp("^" + prefix + "([0-9]+[.|,]?[0-9]*)?" + suffix + "$")
} }
signal editingFinished()
SpinBox
{
id: spinBox
anchors.fill: base
stepSize: 1
value: Math.floor(base.value / base.stepSize)
from: Math.floor(base.from / base.stepSize)
to: Math.floor(base.to / base.stepSize)
editable: base.editable
valueFromText: function(text)
{
return parseFloat(text.substring(prefix.length, text.length - suffix.length)) / base.stepSize;
}
textFromValue: function(value)
{
return prefix + (value * base.stepSize).toFixed(decimals) + suffix;
}
validator: base.validator
onValueModified:
{
base.value = value * base.stepSize;
}
contentItem: TextField contentItem: TextField
{ {
text: base.textFromValue(base.value, base.locale) text: spinBox.textFromValue(spinBox.value, spinBox.locale)
selectByMouse: true selectByMouse: base.editable
background: Item {} background: Item {}
validator: base.validator validator: base.validator
@ -40,7 +69,8 @@ SpinBox
{ {
if(!activeFocus) if(!activeFocus)
{ {
base.editingFinished() base.editingFinished();
}
} }
} }
} }