Libksysguard

ConfigSensors.qml
1/*
2 SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
4 SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9import QtQuick
10import QtQuick.Layouts
11import QtQuick.Controls as QQC2
12import QtQml.Models
13
14import Qt.labs.platform as Platform
15
16import org.kde.kirigami as Kirigami
17import org.kde.kquickcontrols
18
19import org.kde.kitemmodels as KItemModels
20
21import org.kde.quickcharts as Charts
22import org.kde.ksysguard.sensors as Sensors
23import org.kde.ksysguard.faces as Faces
24
25import "./" as Local
26
27ColumnLayout {
28 id: root
29
30 signal configurationChanged
31
32 property var cfg_totalSensors: []
33 property var cfg_highPrioritySensorIds: []
34 property var cfg_sensorColors: new Object()
35 property var cfg_lowPrioritySensorIds: []
36 property var cfg_sensorLabels: new Object()
37
38 onCfg_totalSensorsChanged: configurationChanged();
39 onCfg_highPrioritySensorIdsChanged: configurationChanged();
40 onCfg_sensorColorsChanged: configurationChanged();
41 onCfg_lowPrioritySensorIdsChanged: configurationChanged();
42 onCfg_sensorLabelsChanged: configurationChanged();
43
44 property Faces.SensorFaceController controller
45
46 // In QML someArray = someOtherArray will always trigger a changed signal
47 // even if the two arrays are the same
48 // to avoid that we implement an explicit check
49 function arrayCompare(array1, array2) {
50 if (array1.length !== array2.length) {
51 return false;
52 }
53 return array1.every(function(value, index) { return value === array2[index]});
54 }
55
56 function saveConfig() {
57 controller.totalSensors = cfg_totalSensors;
58 controller.highPrioritySensorIds = cfg_highPrioritySensorIds;
59 controller.sensorColors = cfg_sensorColors;
60 controller.lowPrioritySensorIds = cfg_lowPrioritySensorIds;
61 controller.sensorLabels = cfg_sensorLabels;
62 }
63
64 function loadConfig() {
65 if (!arrayCompare(cfg_totalSensors, controller.totalSensors)) {
66 cfg_totalSensors = controller.totalSensors;
67 totalChoice.selected = controller.totalSensors;
68 }
69
70 if (!arrayCompare(cfg_highPrioritySensorIds, controller.highPrioritySensorIds)) {
71 cfg_highPrioritySensorIds = controller.highPrioritySensorIds;
72 highPriorityChoice.selected = controller.highPrioritySensorIds;
73 }
74
75 if(JSON.stringify(cfg_sensorColors) != JSON.stringify(controller.sensorColors)) {
76 cfg_sensorColors = controller.sensorColors;
77 }
78
79 if (!arrayCompare(cfg_lowPrioritySensorIds, controller.lowPrioritySensorIds)) {
80 cfg_lowPrioritySensorIds = controller.lowPrioritySensorIds;
81 lowPriorityChoice.selected = controller.lowPrioritySensorIds;
82 }
83 if(JSON.stringify(cfg_sensorLabels) != JSON.stringify(controller.sensorLabels)) {
84 cfg_sensorLabels = controller.sensorLabels;
85 }
86 }
87
88 // When the ui is open in systemsettings and the page is switched around,
89 // it gets reparented to null. use this to reload its config every time the
90 // page is current again. So any non saved change to the sensor list gets forgotten.
91 onParentChanged: {
92 if (parent) {
93 loadConfig()
94 }
95 }
96
97 Component.onCompleted: loadConfig()
98
99 Connections {
100 target: controller
101 function onTotalSensorsChanged() {
102 Qt.callLater(root.loadConfig);
103 }
104 function onHighPrioritySensorIdsChanged() {
105 Qt.callLater(root.loadConfig);
106 }
107 function onSensorColorsChanged() {
108 Qt.callLater(root.loadConfig);
109 }
110 function onLowPrioritySensorIdsChanged() {
111 Qt.callLater(root.loadConfig);
112 }
113 function onSensorLabelsChanged() {
114 Qt.callLater(root.loadConfig);
115 }
116 }
117
118 Platform.ColorDialog {
119 id: colorDialog
120 property string destinationSensor
121
122 currentColor: destinationSensor != "" ? controller.sensorColors[destinationSensor] : ""
123 onAccepted: {
124 cfg_sensorColors[destinationSensor] = Qt.rgba(color.r, color.g, color.b, color.a);
125 root.cfg_sensorColorsChanged();
126 }
127 }
128
129
130 QQC2.Label {
131 text: i18ndp("KSysGuardSensorFaces", "Total Sensor", "Total Sensors", controller.maxTotalSensors)
132 visible: controller.supportsTotalSensors
133 }
134 Faces.Choices {
135 id: totalChoice
136 Layout.fillWidth: true
137 visible: controller.supportsTotalSensors
138 supportsColors: false
139 maxAllowedSensors: controller.maxTotalSensors
140 labels: root.cfg_sensorLabels
141
142 onSelectedChanged: root.cfg_totalSensors = selected
143 onSensorLabelChanged: (sensorId, label) => {
144 cfg_sensorLabels[sensorId] = label
145 root.cfg_sensorLabelsChanged()
146 }
147 }
148
149 QQC2.Label {
150 text: i18nd("KSysGuardSensorFaces", "Sensors")
151 }
152 Faces.Choices {
153 id: highPriorityChoice
154 Layout.fillWidth: true
155 supportsColors: controller.supportsSensorsColors
156 labels: root.cfg_sensorLabels
157
158 onSelectedChanged: root.cfg_highPrioritySensorIds = selected
159
160 colors: root.cfg_sensorColors
161 onSelectColor: sensorId => {
162 colorDialog.destinationSensor = sensorId
163 colorDialog.open()
164 }
165 onColorForSensorGenerated: (sensorId, color) => {
166 cfg_sensorColors[sensorId] = color
167 root.cfg_sensorColorsChanged();
168 }
169 onSensorLabelChanged: (sensorId, label) => {
170 cfg_sensorLabels[sensorId] = label
171 root.cfg_sensorLabelsChanged()
172 }
173 }
174
175 QQC2.Label {
176 text: i18nd("KSysGuardSensorFaces", "Text-Only Sensors")
177 visible: controller.supportsLowPrioritySensors
178 }
179 Faces.Choices {
180 id: lowPriorityChoice
181 Layout.fillWidth: true
182 visible: controller.supportsLowPrioritySensors
183 supportsColors: false
184 labels: root.cfg_sensorLabels
185
186 onSelectedChanged: root.cfg_lowPrioritySensorIds = selected
187 onSensorLabelChanged: (sensorId, label) => {
188 cfg_sensorLabels[sensorId] = label
189 root.cfg_sensorLabelsChanged()
190 }
191 }
192
193 Item {
194 Layout.fillHeight: true
195 }
196}
QString i18ndp(const char *domain, const char *singular, const char *plural, const TYPE &arg...)
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.