Index: sources/gui/qml/pages/treatment/sections/TreatmentFlowsComponent.qml =================================================================== diff -u -re36852d9548379bd861d4b0838761d2aa5243dad -ra42e662e05949b63abd4c1e51b814ce476c107d4 --- sources/gui/qml/pages/treatment/sections/TreatmentFlowsComponent.qml (.../TreatmentFlowsComponent.qml) (revision e36852d9548379bd861d4b0838761d2aa5243dad) +++ sources/gui/qml/pages/treatment/sections/TreatmentFlowsComponent.qml (.../TreatmentFlowsComponent.qml) (revision a42e662e05949b63abd4c1e51b814ce476c107d4) @@ -11,29 +11,87 @@ import "qrc:/pages/treatment" Rectangle { id: _root + objectName: "_TreatmentFlowsComponent" - property string title : "" - property string value : "" - property string unitText : "" - property string extraText : "" - property alias value : _value.topText + property string title : "" + property string unitText : "" + property string extraText : "" + property real value : undefined + property real precision : 0 + property real minimum : value + property real maximum : value + property real step : 1 + property bool showButtons : true + property bool buttonsEnabled : true + property bool dropShadowEnabled : true - property bool showButtons : true - property bool buttonsEnabled : true - property bool dropShadowEnabled : true - color : Colors.mainTreatmentLighterBlue radius : 5 - signal increment() - signal decrement() + signal increment(real newValue) + signal decrement(real newValue) + QtObject { id: _private + // fix floating-point precision issue + readonly property int multiplier : Math.pow(10, precision) + readonly property real stepVal : calculatePrecisionValue(step) + readonly property real minVal : minimum !== undefined ? calculatePrecisionValue(minimum) : 0 + readonly property real maxVal : maximum !== undefined ? calculatePrecisionValue(maximum) : 0 + readonly property real val : value !== undefined ? calculatePrecisionValue(value) : 0 + + readonly property bool canDecrement : val > calculateMinimum() + readonly property bool canIncrement : val < calculateMaximum() + + // round the value based on the given precision (not step) + function calculatePrecisionValue(value) { + return Math.round(value * _private.multiplier) / _private.multiplier + } + + // calculate the minimum value rounded up to the next higher step size + function calculateMinimum() { + let fixedMin = _private.fixedValue(_private.minVal) + let fixedStep = _private.fixedValue(_private.stepVal) + return (Math.ceil(fixedMin / fixedStep) * fixedStep) / _private.multiplier + } + + // calculate the maximum value rounded down to the next higher step size + function calculateMaximum() { + let fixedMax = _private.fixedValue(_private.maxVal) + let fixedStep = _private.fixedValue(_private.stepVal) + return (Math.floor(fixedMax / fixedStep) * fixedStep) / _private.multiplier + } + + // return a fixed point int from the inputted float (using the set precision) + function fixedValue(value) { + return Math.round(value * _private.multiplier) + } + + function incrementedValue() { + let fixedVal = _private.fixedValue(_private.val) + let fixedStep = _private.fixedValue(_private.stepVal) + let fixedDelta = fixedStep - (fixedVal % fixedStep) + return Math.max( + _private.minVal, + Math.min(_private.maxVal, (fixedVal + fixedDelta) / _private.multiplier) + ) + } + + function decrementedValue() { + let fixedVal = _private.fixedValue(_private.val) + let fixedStep = _private.fixedValue(_private.stepVal) + let fixedDelta = fixedVal % fixedStep + return Math.min( + _private.maxVal, + Math.max(_private.minVal, (fixedVal - (fixedDelta > 0 ? fixedDelta : fixedStep)) / _private.multiplier) + ) + } + } + Text { id: _title objectName: "title" anchors { horizontalCenter : _root.horizontalCenter - horizontalCenterOffset : Variables.defaultMargin * 1.5 top : parent.top topMargin : Variables.defaultMargin } @@ -47,7 +105,7 @@ height : Variables.contentHeight text : _root.title color : Colors.pressuresText - horizontalAlignment : Text.AlignLeft + horizontalAlignment : Text.AlignHCenter } Row { id: _row @@ -67,7 +125,7 @@ } height : Variables.contentHeight width : Variables.treatmentFlowsComponentWidth - topText : value + topText : value !== undefined ? _root.value.toFixed(_root.precision) : "--" topTextFont.pixelSize : 60 topTextFont.weight : Font.Medium bottomText : _root.unitText @@ -105,17 +163,17 @@ ArrowButton {id : _upArrowIcon objectName : "upArrowIcon" upArrow : true - enabled : buttonsEnabled + enabled : buttonsEnabled && _private.canIncrement - onClicked : _root.increment() + onClicked : { _root.increment(_private.incrementedValue()) } } ArrowButton {id : _downArrowIcon objectName : "downArrowIcon" downArrow : true - enabled : buttonsEnabled + enabled : buttonsEnabled && _private.canDecrement - onClicked : _root.decrement() + onClicked : { _root.decrement(_private.decrementedValue()) } } } }