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

0001 /*
0002     SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0004     SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 import QtQuick
0010 import QtQuick.Layouts
0011 import QtQuick.Controls as QQC2
0012 import QtQml.Models
0013 
0014 import Qt.labs.platform as Platform
0015 
0016 import org.kde.kirigami as Kirigami
0017 import org.kde.kquickcontrols
0018 
0019 import org.kde.kitemmodels as KItemModels
0020 
0021 import org.kde.quickcharts as Charts
0022 import org.kde.ksysguard.sensors as Sensors
0023 import org.kde.ksysguard.faces as Faces
0024 
0025 import "./" as Local
0026 
0027 ColumnLayout {
0028     id: root
0029 
0030     signal configurationChanged
0031 
0032     property var cfg_totalSensors: []
0033     property var cfg_highPrioritySensorIds: []
0034     property var cfg_sensorColors: new Object()
0035     property var cfg_lowPrioritySensorIds: []
0036     property var cfg_sensorLabels: new Object()
0037 
0038     onCfg_totalSensorsChanged: configurationChanged();
0039     onCfg_highPrioritySensorIdsChanged: configurationChanged();
0040     onCfg_sensorColorsChanged: configurationChanged();
0041     onCfg_lowPrioritySensorIdsChanged: configurationChanged();
0042     onCfg_sensorLabelsChanged: configurationChanged();
0043 
0044     property Faces.SensorFaceController controller
0045 
0046     // In QML someArray = someOtherArray will always trigger a changed signal
0047     // even if the two arrays are the same
0048     // to avoid that we implement an explicit check
0049     function arrayCompare(array1, array2) {
0050       if (array1.length !== array2.length) {
0051           return false;
0052       }
0053       return array1.every(function(value, index) { return value === array2[index]});
0054   }
0055 
0056   function saveConfig() {
0057         controller.totalSensors = cfg_totalSensors;
0058         controller.highPrioritySensorIds = cfg_highPrioritySensorIds;
0059         controller.sensorColors = cfg_sensorColors;
0060         controller.lowPrioritySensorIds = cfg_lowPrioritySensorIds;
0061         controller.sensorLabels = cfg_sensorLabels;
0062     }
0063 
0064     function loadConfig() {
0065         if (!arrayCompare(cfg_totalSensors, controller.totalSensors)) {
0066             cfg_totalSensors = controller.totalSensors;
0067             totalChoice.selected = controller.totalSensors;
0068         }
0069 
0070         if (!arrayCompare(cfg_highPrioritySensorIds, controller.highPrioritySensorIds)) {
0071             cfg_highPrioritySensorIds = controller.highPrioritySensorIds;
0072             highPriorityChoice.selected = controller.highPrioritySensorIds;
0073         }
0074 
0075         if(JSON.stringify(cfg_sensorColors) != JSON.stringify(controller.sensorColors)) {
0076             cfg_sensorColors = controller.sensorColors;
0077         }
0078 
0079         if (!arrayCompare(cfg_lowPrioritySensorIds, controller.lowPrioritySensorIds)) {
0080             cfg_lowPrioritySensorIds = controller.lowPrioritySensorIds;
0081             lowPriorityChoice.selected = controller.lowPrioritySensorIds;
0082         }
0083         if(JSON.stringify(cfg_sensorLabels) != JSON.stringify(controller.sensorLabels)) {
0084             cfg_sensorLabels = controller.sensorLabels;
0085         }
0086     }
0087 
0088     // When the ui is open in systemsettings and the page is switched around,
0089     // it gets reparented to null. use this to reload its config every time the
0090     // page is current again. So any non saved change to the sensor list gets forgotten.
0091     onParentChanged: {
0092         if (parent) {
0093             loadConfig()
0094         }
0095     }
0096 
0097     Component.onCompleted: loadConfig()
0098 
0099     Connections {
0100         target: controller
0101         function onTotalSensorsChanged() {
0102             Qt.callLater(root.loadConfig);
0103         }
0104         function onHighPrioritySensorIdsChanged() {
0105             Qt.callLater(root.loadConfig);
0106         }
0107         function onSensorColorsChanged() {
0108             Qt.callLater(root.loadConfig);
0109         }
0110         function onLowPrioritySensorIdsChanged() {
0111             Qt.callLater(root.loadConfig);
0112         }
0113         function onSensorLabelsChanged() {
0114             Qt.callLater(root.loadConfig);
0115         }
0116     }
0117 
0118     Platform.ColorDialog {
0119         id: colorDialog
0120         property string destinationSensor
0121 
0122         currentColor: destinationSensor != "" ? controller.sensorColors[destinationSensor] : ""
0123         onAccepted: {
0124             cfg_sensorColors[destinationSensor] = Qt.rgba(color.r, color.g, color.b, color.a);
0125             root.cfg_sensorColorsChanged();
0126         }
0127     }
0128 
0129 
0130     QQC2.Label {
0131         text: i18ndp("KSysGuardSensorFaces", "Total Sensor", "Total Sensors", controller.maxTotalSensors)
0132         visible: controller.supportsTotalSensors
0133     }
0134     Faces.Choices {
0135         id: totalChoice
0136         Layout.fillWidth: true
0137         visible: controller.supportsTotalSensors
0138         supportsColors: false
0139         maxAllowedSensors: controller.maxTotalSensors
0140         labels: root.cfg_sensorLabels
0141 
0142         onSelectedChanged: root.cfg_totalSensors = selected
0143         onSensorLabelChanged: (sensorId, label) => {
0144             cfg_sensorLabels[sensorId] = label
0145             root.cfg_sensorLabelsChanged()
0146         }
0147     }
0148 
0149     QQC2.Label {
0150         text: i18nd("KSysGuardSensorFaces", "Sensors")
0151     }
0152     Faces.Choices {
0153         id: highPriorityChoice
0154         Layout.fillWidth: true
0155         supportsColors: controller.supportsSensorsColors
0156         labels: root.cfg_sensorLabels
0157 
0158         onSelectedChanged: root.cfg_highPrioritySensorIds = selected
0159 
0160         colors: root.cfg_sensorColors
0161         onSelectColor: sensorId => {
0162             colorDialog.destinationSensor = sensorId
0163             colorDialog.open()
0164         }
0165         onColorForSensorGenerated: (sensorId, color) => {
0166             cfg_sensorColors[sensorId] = color
0167             root.cfg_sensorColorsChanged();
0168         }
0169         onSensorLabelChanged: (sensorId, label) => {
0170             cfg_sensorLabels[sensorId] = label
0171             root.cfg_sensorLabelsChanged()
0172         }
0173     }
0174 
0175     QQC2.Label {
0176         text: i18nd("KSysGuardSensorFaces", "Text-Only Sensors")
0177         visible: controller.supportsLowPrioritySensors
0178     }
0179     Faces.Choices {
0180         id: lowPriorityChoice
0181         Layout.fillWidth: true
0182         visible: controller.supportsLowPrioritySensors
0183         supportsColors: false
0184         labels: root.cfg_sensorLabels
0185 
0186         onSelectedChanged: root.cfg_lowPrioritySensorIds = selected
0187         onSensorLabelChanged: (sensorId, label) => {
0188             cfg_sensorLabels[sensorId] = label
0189             root.cfg_sensorLabelsChanged()
0190         }
0191     }
0192 
0193     Item {
0194         Layout.fillHeight: true
0195     }
0196 }