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
decimals: 2
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)
onValueChanged: updateCostPerMeter()
@ -258,7 +258,7 @@ Item
value: properties.diameter
decimals: 2
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:
{
@ -389,12 +389,6 @@ Item
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
@ -606,4 +600,10 @@ Item
base.setMetaDataEntry("brand", old_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,44 +4,74 @@
import QtQuick 2.2
import QtQuick.Controls 2.15
SpinBox
Item
{
id: base
height: spinBox.height
property string prefix: ""
property string suffix: ""
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)
{
return parseFloat(text.substring(prefix.length, text.length - suffix.length), decimals);
}
property bool editable: true
textFromValue: function(value)
{
return prefix + value.toFixed(decimals) + suffix
}
validator: RegExpValidator
property var validator: RegExpValidator
{
regExp: new RegExp("^" + prefix + "([0-9]+[.|,]?[0-9]*)?" + suffix + "$")
}
contentItem: TextField
signal editingFinished()
SpinBox
{
text: base.textFromValue(base.value, base.locale)
selectByMouse: true
background: Item {}
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
onActiveFocusChanged:
onValueModified:
{
if(!activeFocus)
base.value = value * base.stepSize;
}
contentItem: TextField
{
text: spinBox.textFromValue(spinBox.value, spinBox.locale)
selectByMouse: base.editable
background: Item {}
validator: base.validator
onActiveFocusChanged:
{
base.editingFinished()
if(!activeFocus)
{
base.editingFinished();
}
}
}
}
}
}