Libksysguard

FaceLoader.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "FaceLoader.h"
8#include <QQmlEngine>
9
10using namespace KSysGuard;
11
12class Q_DECL_HIDDEN FaceLoader::Private
13{
14public:
15 Private(FaceLoader *qq)
16 : q(qq)
17 {
18 }
19 void setupController();
20
21 FaceLoader *q;
22
23 SensorFaceController *parentController = nullptr;
24 SensorFaceController *controller = nullptr;
25
26 QString groupName;
27
28 bool complete = false;
29
30 QJsonArray sensors;
31 QString faceId;
32 QVariantMap colors;
33 QVariantMap labels;
34 int updateRateLimit = 0;
35 bool readOnly = true;
36 bool showTitle = false;
37};
38
39FaceLoader::FaceLoader(QObject *parent)
40 : QObject(parent)
41 , d(new Private{this})
42{
43}
44
45FaceLoader::~FaceLoader() = default;
46
48{
49 return d->parentController;
50}
51
52void FaceLoader::setParentController(SensorFaceController *newParentController)
53{
54 if (newParentController == d->parentController) {
55 return;
56 }
57
58 if (d->parentController) {
59 d->parentController->disconnect(this);
60 }
61
62 if (d->controller) {
63 d->controller->deleteLater();
64 }
65
66 d->parentController = newParentController;
67
68 d->setupController();
69
70 Q_EMIT parentControllerChanged();
71}
72
74{
75 return d->faceId;
76}
77
78void FaceLoader::setFaceId(const QString &newFaceId)
79{
80 if (newFaceId == d->faceId) {
81 return;
82 }
83
84 d->faceId = newFaceId;
85 if (d->controller) {
86 d->controller->setFaceId(d->faceId);
87 }
88
89 Q_EMIT faceIdChanged();
90}
91
93{
94 return d->groupName;
95}
96
97void FaceLoader::setGroupName(const QString &newGroupName)
98{
99 if (newGroupName == d->groupName) {
100 return;
101 }
102
103 d->groupName = newGroupName;
104
105 d->setupController();
106
107 Q_EMIT groupNameChanged();
108}
109
111{
112 return d->sensors;
113}
114
115void FaceLoader::setSensors(const QJsonArray &newSensors)
116{
117 if (newSensors == d->sensors) {
118 return;
119 }
120
121 d->sensors = newSensors;
122
123 if (d->controller) {
124 d->controller->setHighPrioritySensorIds(d->sensors);
125 }
126
127 Q_EMIT sensorsChanged();
128}
129
130QVariantMap FaceLoader::colors() const
131{
132 return d->colors;
133}
134
135void FaceLoader::setColors(const QVariantMap &newColors)
136{
137 if (newColors == d->colors) {
138 return;
139 }
140
141 d->colors = newColors;
142 if (d->controller) {
143 d->controller->setSensorColors(d->colors);
144 // Ensure we emit a change signal for colors even if the controller thinks
145 // we shouldn't, to ensure all instances of the face are correctly updated.
146 Q_EMIT d->controller->sensorColorsChanged();
147 }
148 Q_EMIT colorsChanged();
149}
150
151QVariantMap FaceLoader::labels() const
152{
153 return d->labels;
154}
155
156void FaceLoader::setLabels(const QVariantMap &newLabels)
157{
158 if (newLabels == d->labels) {
159 return;
160 }
161
162 d->labels = newLabels;
163 if (d->controller) {
164 d->controller->setSensorLabels(d->labels);
165 // Ensure we emit a change signal for labels even if the controller thinks
166 // we shouldn't, to ensure all instances of the face are correctly updated.
167 Q_EMIT d->controller->sensorLabelsChanged();
168 }
169 Q_EMIT labelsChanged();
170}
171
173{
174 return d->updateRateLimit;
175}
176
177void FaceLoader::setUpdateRateLimit(int newLimit)
178{
179 if (newLimit == d->updateRateLimit) {
180 return;
181 }
182
183 d->updateRateLimit = newLimit;
184 if (d->controller) {
185 d->controller->setUpdateRateLimit(d->updateRateLimit);
186 // Ensure we emit a change signal for the limit even if the controller thinks
187 // we shouldn't, to ensure all instances of the face are correctly updated.
188 Q_EMIT d->controller->updateRateLimitChanged();
189 }
190 Q_EMIT updateRateLimitChanged();
191}
192
193bool FaceLoader::readOnly() const
194{
195 return d->readOnly;
196}
197
198void FaceLoader::setReadOnly(bool newReadOnly)
199{
200 if (newReadOnly == d->readOnly) {
201 return;
202 }
203
204 d->readOnly = newReadOnly;
205 if (d->controller) {
206 d->controller->setShouldSync(!d->readOnly);
207 }
208 Q_EMIT readOnlyChanged();
209}
210
212{
213 return d->controller;
214}
215
216void FaceLoader::reload()
217{
218 d->controller->reloadFaceConfiguration();
219}
220
221void FaceLoader::classBegin()
222{
223}
224
225void FaceLoader::componentComplete()
226{
227 d->complete = true;
228 d->setupController();
229}
230
231void FaceLoader::Private::setupController()
232{
233 if (!parentController || groupName.isEmpty() || !complete) {
234 return;
235 }
236
237 auto configGroup = parentController->configGroup().group(groupName);
238 controller = new SensorFaceController(configGroup, qmlEngine(q), new QQmlEngine(q));
239 controller->setShouldSync(readOnly);
240 controller->setHighPrioritySensorIds(sensors);
241 controller->setSensorColors(colors);
242 controller->setSensorLabels(labels);
243 controller->setShowTitle(showTitle);
244 controller->setFaceId(faceId);
245 controller->setUpdateRateLimit(updateRateLimit);
246
247 Q_EMIT q->controllerChanged();
248}
KConfigGroup group(const QString &group)
A helper class to make it easier to load faces when used inside a face.
Definition FaceLoader.h:32
bool readOnly
Whether to allow modifying the face configuration.
Definition FaceLoader.h:84
KSysGuard::SensorFaceController * controller
The face controller that provides the loaded face.
Definition FaceLoader.h:88
QJsonArray sensors
The sensors to use for this face.
Definition FaceLoader.h:49
QVariantMap labels
A map of sensor labels to be used by the face.
Definition FaceLoader.h:67
QVariantMap colors
A map of sensor colors to be used by the face.
Definition FaceLoader.h:61
QString groupName
The name of the config group to read configuration from.
Definition FaceLoader.h:43
QString faceId
The face to use.
Definition FaceLoader.h:55
QML_ELEMENTKSysGuard::SensorFaceController * parentController
The parent SensorFaceController that will be used for configuration storage.
Definition FaceLoader.h:39
int updateRateLimit
The minimum time that needs to elapse, in milliseconds, between updates of the face.
Definition FaceLoader.h:73
The SensorFaceController links sensor faces and applications in which these faces are shown.
void setShouldSync(bool sync)
Specifies if the controller should automatically sync configuration changes.
KConfigGroup configGroup() const
Retrieve the KConfigGroup this controller is using to store configuration.
Q_EMITQ_EMIT
bool isEmpty() const const
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.