Libksysguard

SensorRangeSpinBox.qml
1/*
2 * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick
8import QtQuick.Controls
9import QtQuick.Layouts
10
11import org.kde.ksysguard.formatter as Formatter
12import org.kde.ksysguard.sensors as Sensors
13
14/**
15 * A control to select a value with a unit.
16 *
17 * This is primarily intended for range selection in Face configuration pages.
18 * It allows selecting a value and a unit for that value and provides that
19 * value, a unit and a multiplier for that value.
20 */
21Control {
22 id: control
24 /**
25 * The lower bound for the value.
26 */
27 property alias from: spinBox.from
28 /**
29 * The upper bound for the value.
30 */
31 property alias to: spinBox.to
32 /**
33 * The value.
34 */
35 property real value
36 /**
37 * The unit for the value.
38 */
39 property int unit
40 /**
41 * The multiplier to convert the provided value from its unit to the base unit.
42 */
43 property real multiplier
44 /**
45 * The list of sensors to use for retrieving unit information.
46 */
47 property alias sensors: unitModel.sensors
48 /**
49 * Emitted whenever the value, unit or multiplier changes due to user input.
50 */
51 signal valueModified()
52
53 implicitWidth: leftPadding + spinBox.implicitWidth + comboBox.implicitWidth + rightPadding
54 implicitHeight: topPadding + Math.max(spinBox.implicitHeight, comboBox.implicitHeight) + bottomPadding
55
56 leftPadding: 0
57 rightPadding: 0
58 topPadding: 0
59 bottomPadding: 0
60
61 contentItem: RowLayout {
62 spacing: 0
63
64 SpinBox {
65 id: spinBox
66
67 Layout.fillWidth: true
68 Layout.preferredWidth: 0
69
70 editable: true
71 from: Math.pow(-2, 31) + 1
72 to: Math.pow(2, 31) - 1
73 stepSize: 100
74
75 value: control.value * 100
76
77 Binding {
78 target: control
79 property: "value"
80 value: spinBox.value / 100
81 }
82
83 validator: DoubleValidator {
84 locale: spinBox.locale.name
85 bottom: Math.min(spinBox.from, spinBox.to)
86 top: Math.max(spinBox.from, spinBox.to)
87 }
88
89 textFromValue: function(value, locale) {
90 // "toLocaleString" has no concept of "the minimum amount of
91 // digits to represent this number", so we need to calculate this
92 // manually. This ensures that things like "0" and "10" will be
93 // displayed without any decimals, while things like "2.2" and
94 // "3.87" will be displayed with the right number of decimals.
95
96 let realValue = value / 100
97 let fract = realValue - Math.trunc(realValue)
98
99 let digits = 0
100 if (fract != 0) {
101 digits++;
102 }
103 if ((fract * 10) - Math.trunc(fract * 10) != 0) {
104 digits++;
105 }
106
107 return Number(value / 100).toLocaleString(locale, 'f', digits)
108 }
109
110 valueFromText: function(text, locale) {
111 return Number.fromLocaleString(locale, text) * 100
112 }
113
114 onValueModified: control.valueModified()
115 }
116
117 ComboBox {
118 id: comboBox
119
120 Layout.fillWidth: true
121 Layout.preferredWidth: 0
122
123 visible: unitModel.sensors.length > 0
124
125 textRole: "symbol"
126 valueRole: "unit"
127
128 currentIndex: 0
129
130 onActivated: {
131 control.unit = currentValue
132 control.multiplier = model.data(model.index(currentIndex, 0), Sensors.SensorUnitModel.MultiplierRole)
133 control.valueModified()
134 }
135
136 Component.onCompleted: updateCurrentIndex()
137
138 model: Sensors.SensorUnitModel {
139 id: unitModel
140 onReadyChanged: comboBox.updateCurrentIndex()
141 }
142
143 function updateCurrentIndex() {
144 if (unitModel.ready && control.unit >= 0) {
145 currentIndex = indexOfValue(control.unit)
146 } else {
147 currentIndex = 0;
148 }
149 }
150 }
151 }
152}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.