Warning, /utilities/skanpage/src/qml/DoubleSpinBoxWithSuffix.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 import QtQuick 2.15
0008 import QtQuick.Controls 2.15
0009 import QtQuick.Layouts 1.1
0010 import QtQml 2.15
0011 
0012 import org.kde.kirigami 2.12 as Kirigami
0013 
0014 /* This is way too complicated as it should be and more a giant hack.
0015  * Should be implemented properly in the future */
0016 
0017 Item {
0018     id: container
0019     property real value
0020     property real from
0021     property real to
0022     property real stepSize
0023     property alias suffix: suffixText.text
0024     property alias editable: control.editable
0025 
0026     signal valueModified(real value)
0027 
0028     implicitWidth: control.width
0029     implicitHeight: control.implicitHeight
0030 
0031     onStepSizeChanged: control.findMultiplier() // Needed when reloading scanner
0032     property var approxeq: function(v1, v2, epsilon) { return Math.abs(v1 - v2) < epsilon }
0033 
0034     SpinBox {
0035         id: control
0036 
0037         property int multiplier: 1
0038         property int decimals: 0
0039         property bool setup: true
0040 
0041         from: container.from * multiplier
0042         to: container.to * multiplier
0043         stepSize: container.stepSize * multiplier
0044         Binding on value {value: Math.round(container.value * control.multiplier)}
0045 
0046         onValueModified: {
0047             if (!approxeq(container.value, value / multiplier, stepSize / 1000) && !setup) {
0048                 container.value = value / multiplier
0049                 container.valueModified(container.value)
0050             }
0051         }
0052 
0053         textFromValue: function(value, locale) {
0054             return Number(value / multiplier).toLocaleString(locale, 'f', decimals)
0055         }
0056 
0057         valueFromText: function(text, locale) {
0058             return Number.fromLocaleString(locale, text) * multiplier
0059         }
0060 
0061         Component.onCompleted: findMultiplier()
0062         function findMultiplier() {
0063             while (container.stepSize * multiplier < 1 && container.stepSize !== 0) {
0064                 multiplier = multiplier * 10
0065                 decimals = decimals + 1
0066             }
0067             setup = false
0068         }
0069 
0070         TextMetrics {
0071             id: minTextSize
0072             font: textInput.font
0073             text: control.textFromValue(control.from, control.locale)
0074         }
0075 
0076         TextMetrics {
0077             id: maxTextSize
0078             font: textInput.font
0079             text: control.textFromValue(control.to, control.locale)
0080         }
0081 
0082         contentItem: Row {
0083             spacing: Kirigami.Units.smallSpacing
0084 
0085             TextInput {
0086                 id: textInput
0087                 color: Kirigami.Theme.textColor
0088                 readOnly: !control.editable
0089                 text: control.textFromValue(control.value, control.locale)
0090 
0091                 horizontalAlignment: Qt.AlignRight
0092                 verticalAlignment: Qt.AlignVCenter
0093                 validator: control.validator
0094                 inputMethodHints: Qt.ImhFormattedNumbersOnly
0095 
0096                 onTextEdited: {
0097                     control.value = control.valueFromText(text.replace(control.locale.groupSeparator, ""), control.locale)
0098                     control.valueModified()
0099                 }
0100 
0101                 width: Math.max(minTextSize.width, maxTextSize.width) + Kirigami.Units.smallSpacing
0102             }
0103 
0104             Label {
0105                 id: suffixText
0106                 visible: text !== ''
0107             }
0108         }
0109     }
0110 }