Index: sources/gui/qml/compounds/ValueAdjuster.qml =================================================================== diff -u -r3948dd9dd1189b47bbeedf08c351dece2662d600 -r863764371ec0a4569403135975a0a3f44e516163 --- sources/gui/qml/compounds/ValueAdjuster.qml (.../ValueAdjuster.qml) (revision 3948dd9dd1189b47bbeedf08c351dece2662d600) +++ sources/gui/qml/compounds/ValueAdjuster.qml (.../ValueAdjuster.qml) (revision 863764371ec0a4569403135975a0a3f44e516163) @@ -20,99 +20,19 @@ import QtQuick.Controls 2.2 Item { id: _root - property real decimal : 0 - property real minimum : 0 - property real maximum : 0 - property real value : 0 + property string extraText : "" + property alias value : _rangedValue.rawValue + property alias minimum : _rangedValue.rawMin + property alias maximum : _rangedValue.rawMax + property alias step : _rangedValue.rawStep + property alias decimal : _rangedValue.precision + property alias canOff : _rangedValue.canOff property real defaultValue: 0 - property real step : 0 property bool isActive : false property bool editable : true - property bool canOff : false property bool canRefresh : false property alias textColor : _text.color - QtObject { id: _private - // fix floating-point precision issue - readonly property int multiplier : Math.pow(10, decimal) - readonly property real stepVal : calculatePrecisionValue(step) - readonly property real minVal : calculatePrecisionValue(minimum) - readonly property real maxVal : calculatePrecisionValue(maximum) - readonly property real val : calculatePrecisionValue(value) - - readonly property bool canIncrement : isActive ? val < calculateMaximum() : true - readonly property bool canDecrement : isActive ? canOff ? val > 0 : - val > calculateMinimum() : true - - // 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 lower 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 increment() { - if ( ! isActive ) { didActiveChange(true); return } - - let tValue = _private.val - // if value can be 'off' and value is below the min, then just set value to min - if (canOff && _private.val < _private.minVal) { - tValue = _private.minVal - } - else { - let fixedVal = _private.fixedValue(_private.val) - let fixedStep = _private.fixedValue(_private.stepVal) - // if value is not step aligned, then fixedDelta will be next higher step, - // otherwise fixedDelta will be a whole step - let fixedDelta = fixedStep - (fixedVal % fixedStep) - tValue = (fixedVal + fixedDelta) / _private.multiplier - // clamp tValue between [minVal, maxVal] - tValue = Math.max(_private.minVal, Math.min(_private.maxVal, tValue)) - } - - didChange(tValue) - } - - function decrement() { - if ( ! isActive ) { didActiveChange(true); return } - - let tValue = _private.val - // if value can be 'off' and value is already at minimum, just set value to 0 - if (canOff && _private.val === _private.minVal) { - tValue = 0 - } - else { - let fixedVal = _private.fixedValue(_private.val) - let fixedStep = _private.fixedValue(_private.stepVal) - let fixedDelta = fixedVal % fixedStep - // if value is not step aligned, decrement to the next lowest step, - // otherwise decrement by a whole step - tValue = (fixedVal - (fixedDelta > 0 ? fixedDelta : fixedStep)) / _private.multiplier - // clamp tValue between [minVal, maxVal] - tValue = Math.min(_private.maxVal, Math.max(_private.minVal, tValue)) - } - - didChange(tValue) - } - } - signal didChange (real vValue) signal didActiveChange (bool vState) @@ -126,13 +46,17 @@ function clear() { didActiveChange(false) } + RangedValue { id: _rangedValue + objectName: "_rangedValue" + } + Text { id: _text anchors.centerIn: parent text : _root.isActive ? _root.canOff ? _root.value === 0 ? qsTr("OFF") : _root.value.toFixed( _root.decimal ) : _root.value.toFixed( _root.decimal ) : Variables.emptyEntry - color : Colors.offWhite + color : _rangedValue.isValueInRange ? Colors.offWhite : Colors.red font.pixelSize : Fonts.fontPixelValueControl } @@ -236,11 +160,11 @@ leftMargin : Variables.defaultMargin } iconSize : Variables.circleButtonDefaultDiameter - enabled : _private.canDecrement + enabled : _rangedValue.canDecrement visible : _root.editable iconImageSource : enabled ? "qrc:/images/iArrowLeft" : "qrc:/images/iArrowLeftDisabled" - onClicked : _private.decrement() + onClicked : { didChange(_rangedValue.decrementedValue()) } } IconButton { id: _rightArrow @@ -250,10 +174,10 @@ rightMargin : Variables.defaultMargin } iconSize : Variables.circleButtonDefaultDiameter - enabled : _private.canIncrement + enabled : _rangedValue.canIncrement visible : _root.editable iconImageSource : enabled ? "qrc:/images/iArrowRight" : "qrc:/images/iArrowRightDisabled" - onClicked : _private.increment() + onClicked : { didChange(_rangedValue.incrementedValue()) } } }