Warning, /plasma/libksysguard/faces/import/SensorRangeSpinBox.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  * 
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick
0008 import QtQuick.Controls
0009 import QtQuick.Layouts
0010 
0011 import org.kde.ksysguard.formatter as Formatter
0012 import org.kde.ksysguard.sensors as Sensors
0013 
0014 /**
0015  * A control to select a value with a unit.
0016  *
0017  * This is primarily intended for range selection in Face configuration pages.
0018  * It allows selecting a value and a unit for that value and provides that
0019  * value, a unit and a multiplier for that value.
0020  */
0021 Control {
0022     id: control
0023 
0024     /**
0025      * The lower bound for the value.
0026      */
0027     property alias from: spinBox.from
0028     /**
0029      * The upper bound for the value.
0030      */
0031     property alias to: spinBox.to
0032     /**
0033      * The value.
0034      */
0035     property real value
0036     /**
0037      * The unit for the value.
0038      */
0039     property int unit
0040     /**
0041      * The multiplier to convert the provided value from its unit to the base unit.
0042      */
0043     property real multiplier
0044     /**
0045      * The list of sensors to use for retrieving unit information.
0046      */
0047     property alias sensors: unitModel.sensors
0048     /**
0049      * Emitted whenever the value, unit or multiplier changes due to user input.
0050      */
0051     signal valueModified()
0052 
0053     implicitWidth: leftPadding + spinBox.implicitWidth + comboBox.implicitWidth + rightPadding
0054     implicitHeight: topPadding + Math.max(spinBox.implicitHeight, comboBox.implicitHeight) + bottomPadding
0055 
0056     leftPadding: 0
0057     rightPadding: 0
0058     topPadding: 0
0059     bottomPadding: 0
0060     
0061     contentItem: RowLayout {
0062         spacing: 0
0063 
0064         SpinBox {
0065             id: spinBox
0066 
0067             Layout.fillWidth: true
0068             Layout.preferredWidth: 0
0069 
0070             editable: true
0071             from: Math.pow(-2, 31) + 1
0072             to: Math.pow(2, 31) - 1
0073             stepSize: 100
0074 
0075             value: control.value * 100
0076 
0077             Binding {
0078                 target: control
0079                 property: "value"
0080                 value: spinBox.value / 100
0081             }
0082 
0083             validator: DoubleValidator {
0084                 locale: spinBox.locale.name
0085                 bottom: Math.min(spinBox.from, spinBox.to)
0086                 top:  Math.max(spinBox.from, spinBox.to)
0087             }
0088 
0089             textFromValue: function(value, locale) {
0090                 // "toLocaleString" has no concept of "the minimum amount of
0091                 // digits to represent this number", so we need to calculate this
0092                 // manually. This ensures that things like "0" and "10" will be
0093                 // displayed without any decimals, while things like "2.2" and
0094                 // "3.87" will be displayed with the right number of decimals.
0095 
0096                 let realValue = value / 100
0097                 let fract = realValue - Math.trunc(realValue)
0098 
0099                 let digits = 0
0100                 if (fract != 0) {
0101                     digits++;
0102                 }
0103                 if ((fract * 10) - Math.trunc(fract * 10) != 0) {
0104                     digits++;
0105                 }
0106 
0107                 return Number(value / 100).toLocaleString(locale, 'f', digits)
0108             }
0109 
0110             valueFromText: function(text, locale) {
0111                 return Number.fromLocaleString(locale, text) * 100
0112             }
0113 
0114             onValueModified: control.valueModified()
0115         }
0116 
0117         ComboBox {
0118             id: comboBox
0119 
0120             Layout.fillWidth: true
0121             Layout.preferredWidth: 0
0122 
0123             visible: unitModel.sensors.length > 0
0124 
0125             textRole: "symbol"
0126             valueRole: "unit"
0127 
0128             currentIndex: 0
0129 
0130             onActivated: {
0131                 control.unit = currentValue
0132                 control.multiplier = model.data(model.index(currentIndex, 0), Sensors.SensorUnitModel.MultiplierRole)
0133                 control.valueModified()
0134             }
0135 
0136             Component.onCompleted: updateCurrentIndex()
0137 
0138             model: Sensors.SensorUnitModel {
0139                 id: unitModel
0140                 onReadyChanged: comboBox.updateCurrentIndex()
0141             }
0142 
0143             function updateCurrentIndex() {
0144                 if (unitModel.ready && control.unit >= 0) {
0145                     currentIndex = indexOfValue(control.unit)
0146                 } else {
0147                     currentIndex = 0;
0148                 }
0149             }
0150         }
0151     }
0152 }