Libksysguard

SensorFaceController.h
1/*
2 SPDX-FileCopyrightText: 2020 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <filesystem>
10
11#include <QAbstractItemModel>
12#include <QObject>
13#include <QPointer>
14#include <QQuickItem>
15
16#include <KConfigGroup>
17
18#include "sensorfaces_export.h"
19
21class QQmlEngine;
22class KDesktopFile;
23class KConfigLoader;
24
25namespace KSysGuard
26{
27class SensorFace;
28class SensorFaceControllerPrivate;
29
30/**
31 * The SensorFaceController links sensor faces and applications in which these faces are shown. It
32 * abstracts the configuration and properties of faces.
33 *
34 * For faces it offers information about which sensors should be displayed and hints set by the
35 * application about how the information should be displayed. It can be accessed by faces using
36 * the `SensorFace::controller` property.
37 *
38 * Applications can use this class to instantiate faces from a given config and for querying the
39 * capabilities of faces.
40 *
41 * @since 5.19
42 */
43class SENSORFACES_EXPORT SensorFaceController : public QObject
44{
45 Q_OBJECT
46 QML_ELEMENT
47 QML_UNCREATABLE("It's not possible to create objects of type SensorFaceController")
48 /**
49 * A title for the face.
50 * @see showTitle
51 */
52 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
53 /** Whether the title should be displayed or if it should be hidden instead
54 * @see title
55 */
56 Q_PROPERTY(bool showTitle READ showTitle WRITE setShowTitle NOTIFY showTitleChanged)
57 /**
58 * The id of the current face. For example `org.kde.ksysguard.textonly`
59 */
60 Q_PROPERTY(QString faceId READ faceId WRITE setFaceId NOTIFY faceIdChanged)
61 /**
62 * Sensors that are typically used to display a total in some way or form. For example in the pie
63 * char face they are not drawn as part of the chart but appear in the centre of it.
64 */
65 Q_PROPERTY(QJsonArray totalSensors READ totalSensors WRITE setTotalSensors NOTIFY totalSensorsChanged)
66 /**
67 * Sensors that should always be shown in the face. This is the main list of sensors that are of
68 * the most interest.
69 */
70 Q_PROPERTY(QJsonArray highPrioritySensorIds READ highPrioritySensorIds WRITE setHighPrioritySensorIds NOTIFY highPrioritySensorIdsChanged)
71 /**
72 * Maps sensorIds to colors that can be used when a color for something relating to a
73 * specific sensor is needed.
74 */
75 Q_PROPERTY(QVariantMap sensorColors READ sensorColors WRITE setSensorColors NOTIFY sensorColorsChanged)
76
77 /**
78 * Maps sensorIds to user configurable labels than should be displayed instead of the name of the sensor.
79 */
80 Q_PROPERTY(QVariantMap sensorLabels READ sensorLabels WRITE setSensorLabels NOTIFY sensorLabelsChanged)
81
82 /**
83 * Secondary list of sensors. These sensors do not necessarily appear in main part of the face.
84 * For example in most faces they are just added to the legend.
85 */
86 Q_PROPERTY(QJsonArray lowPrioritySensorIds READ lowPrioritySensorIds WRITE setLowPrioritySensorIds NOTIFY lowPrioritySensorIdsChanged)
87
88 /**
89 * The name of the current face
90 */
91 Q_PROPERTY(QString name READ name NOTIFY faceIdChanged)
92 /**
93 * The icon of the current face
94 */
95 Q_PROPERTY(QString icon READ icon NOTIFY faceIdChanged)
96 /**
97 * Whether the current face supports sensor colors
98 */
99 Q_PROPERTY(bool supportsSensorsColors READ supportsSensorsColors NOTIFY faceIdChanged)
100 /**
101 * Whether the current face can display total sensors
102 */
103 Q_PROPERTY(bool supportsTotalSensors READ supportsTotalSensors NOTIFY faceIdChanged)
104 /**
105 * Whether the current face can display low priority sensors
106 */
107 Q_PROPERTY(bool supportsLowPrioritySensors READ supportsLowPrioritySensors NOTIFY faceIdChanged)
108 /**
109 * The amount of total sensors the current face supports
110 */
111 Q_PROPERTY(int maxTotalSensors READ maxTotalSensors NOTIFY faceIdChanged)
112 /**
113 * A map of config options and values that are specific to the current face as defined by the
114 * `main.xml` of the face.
115 * @see faceConfigUi
116 */
117 Q_PROPERTY(KConfigPropertyMap *faceConfiguration READ faceConfiguration NOTIFY faceConfigurationChanged)
118
119 /**
120 * The full representation of the current face. Typically includes additional elements like
121 * a legend or title
122 */
123 Q_PROPERTY(QQuickItem *fullRepresentation READ fullRepresentation NOTIFY faceIdChanged)
124 /**
125 * The compact representation of the current face. Typically only includes the main visualization
126 * of the data without legend, title, etc.
127 */
128 Q_PROPERTY(QQuickItem *compactRepresentation READ compactRepresentation NOTIFY faceIdChanged)
129 /**
130 * A user interface that is suited for configuring the face specific options.
131 * Emits `configurationChanged` if a config value changed. To apply the changes call `saveConfig`
132 * on it.
133 * @see faceConfiguration
134 */
135 Q_PROPERTY(QQuickItem *faceConfigUi READ faceConfigUi NOTIFY faceIdChanged)
136 /**
137 * A user interface for configuring the general appearance of a face like the title and the used
138 * face.
139 * Emits `configurationChanged` if a config value changed. To apply the changes call `saveConfig`
140 * on it.
141 */
142 Q_PROPERTY(QQuickItem *appearanceConfigUi READ appearanceConfigUi NOTIFY faceIdChanged)
143 /**
144 * A user interface for configuring which sensors are displayed in a face
145 * Emits `configurationChanged` if a config value changed. To apply the changes call `saveConfig`
146 * on it.
147 */
148 Q_PROPERTY(QQuickItem *sensorsConfigUi READ sensorsConfigUi NOTIFY faceIdChanged)
149
150 /**
151 * A list of all available faces. The name is available as the display role and the id as `pluginId`
152 */
153 Q_PROPERTY(QAbstractItemModel *availableFacesModel READ availableFacesModel CONSTANT)
154 /**
155 * A list of available face presets. The name is available as the display role, the id as `pluginId`.
156 * The properties of the preset can be accessed via the `config` role.
157 */
158 Q_PROPERTY(QAbstractItemModel *availablePresetsModel READ availablePresetsModel CONSTANT)
159 /**
160 * The minimum time that needs to elapse, in milliseconds, between updates of the face.
161 */
162 Q_PROPERTY(int updateRateLimit READ updateRateLimit WRITE setUpdateRateLimit NOTIFY updateRateLimitChanged)
163 /**
164 * Contains the paths of missing sensors, if there are any.
165 */
166 Q_PROPERTY(QJsonArray missingSensors READ missingSensors NOTIFY missingSensorsChanged)
167
168public:
169 /**
170 * Creates a new SensorFaceController.
171 * This is only useful for applications that want display SensorFaces.
172 *
173 * SensorFaces can access the controller that created them using their `SensorFace::controller`
174 * property.
175 * @param config The controller uses this config group to read and save the face configuration
176 * @param engine This engine will be used for creating the Qml components
177 */
178 SensorFaceController(KConfigGroup &config, QQmlEngine *engine, QQmlEngine *configEngine);
179 ~SensorFaceController() override;
180
181 /**
182 * Retrieve the KConfigGroup this controller is using to store configuration.
183 *
184 * This is primarily intended to allow adding child groups to the face
185 * configuration.
186 */
187 KConfigGroup configGroup() const;
188
189 void setFaceId(const QString &face);
190 QString faceId() const;
191
192 QQuickItem *fullRepresentation();
193 QQuickItem *compactRepresentation();
194 QQuickItem *faceConfigUi();
195 QQuickItem *appearanceConfigUi();
196 QQuickItem *sensorsConfigUi();
197
198 KConfigPropertyMap *faceConfiguration() const;
199
200 QString title() const;
201 void setTitle(const QString &title);
202
203 bool showTitle() const;
204 void setShowTitle(bool show);
205
206 QJsonArray totalSensors() const;
207 void setTotalSensors(const QJsonArray &sensor);
208
209 QJsonArray highPrioritySensorIds() const;
210 void setHighPrioritySensorIds(const QJsonArray &ids);
211
212 QJsonArray sensors() const;
213
214 QJsonArray lowPrioritySensorIds() const;
215 void setLowPrioritySensorIds(const QJsonArray &ids);
216
217 QJsonArray missingSensors() const;
218
219 QVariantMap sensorColors() const;
220 void setSensorColors(const QVariantMap &colors);
221
222 QVariantMap sensorLabels() const;
223 void setSensorLabels(const QVariantMap &labels);
224
225 int updateRateLimit() const;
226 void setUpdateRateLimit(int limit);
227
228 // from face config, immutable by the user
229 QString name() const;
230 const QString icon() const;
231
232 bool supportsSensorsColors() const;
233 bool supportsTotalSensors() const;
234 bool supportsLowPrioritySensors() const;
235
236 int maxTotalSensors() const;
237
238 QAbstractItemModel *availableFacesModel();
239 QAbstractItemModel *availablePresetsModel();
240
241 /**
242 * Reload the configuration.
243 */
244 Q_INVOKABLE void reloadConfig();
245 /**
246 * Loads a specific preset
247 * @see availablePresetsModel
248 */
249 Q_INVOKABLE void loadPreset(const QString &preset);
250 /**
251 * Save the current configuration as a preset
252 */
253 Q_INVOKABLE void savePreset();
254 void savePreset(const std::filesystem::path &path);
255 /**
256 * Uninstall a specific preset
257 */
258 Q_INVOKABLE void uninstallPreset(const QString &pluginId);
259
260 /**
261 * Whether the controller should sync configuration changes
262 * @see setShouldSync
263 */
264 bool shouldSync() const;
265 /**
266 * Specifies if the controller should automatically sync configuration changes.
267 * @param sync If `true` applied config changes are written to the KConfigGroup that was
268 * specified in the @ref SensorFaceController::SensorFaceController "constructor". If `false`
269 * config changes are applied after calling `saveConfig` on a configuration ui but not written
270 * to the KConfigGroup.
271 */
272 void setShouldSync(bool sync);
273
274 /**
275 * Reload only the face configuration.
276 *
277 * This does not touch sensors, colors or anything else, only the config
278 * loaded from the face package is reloaded.
279 */
280 Q_INVOKABLE void reloadFaceConfiguration();
281
282 /**
283 * Replace one sensor with another.
284 *
285 * This replaces a configured sensor with a new one. This replacement happens
286 * inside the configuration, bypassing thing like the sensor properties which
287 * are populated with resolved sensor ids rather than the configured entries.
288 *
289 * You should call @ref reloadConfig once you have made all replacements.
290 */
291 Q_INVOKABLE void replaceSensors(const QString &from, const QString &to);
292
293Q_SIGNALS:
294 void faceIdChanged();
295 void titleChanged();
296 void showTitleChanged();
297 void totalSensorsChanged();
298 void highPrioritySensorIdsChanged();
299 void lowPrioritySensorIdsChanged();
300 void sensorsChanged();
301 void sensorColorsChanged();
302 void sensorLabelsChanged();
303 void updateRateLimitChanged();
304 void faceConfigurationChanged();
305 void missingSensorsChanged();
306
307private:
308 const std::unique_ptr<SensorFaceControllerPrivate> d;
309};
310}
The SensorFaceController links sensor faces and applications in which these faces are shown.
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.