File indexing completed on 2024-04-28 05:31:32

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QJsonArray>
0010 #include <QObject>
0011 #include <QQmlParserStatus>
0012 
0013 #include "SensorFaceController.h"
0014 #include "sensorfaces_export.h"
0015 
0016 namespace KSysGuard
0017 {
0018 
0019 /**
0020  * A helper class to make it easier to load faces when used inside a face.
0021  *
0022  * This is primarily intended to support use cases where there is a sensor face
0023  * that wants to load a different face as a child, for example the Grid face
0024  * uses this to creates individual faces for each grid cell.
0025  *
0026  * This will create a new SensorFaceController that makes use of the
0027  * parentController's config group for reading settings. The child group name
0028  * is set by the groupName property. By default, the new controller is read
0029  * only and does not write to the config group.
0030  */
0031 class SENSORFACES_EXPORT FaceLoader : public QObject, public QQmlParserStatus
0032 {
0033     Q_OBJECT
0034     Q_INTERFACES(QQmlParserStatus)
0035     /**
0036      * The parent SensorFaceController that will be used for configuration storage.
0037      */
0038     Q_PROPERTY(KSysGuard::SensorFaceController *parentController READ parentController WRITE setParentController NOTIFY parentControllerChanged)
0039     /**
0040      * The name of the config group to read configuration from.
0041      */
0042     Q_PROPERTY(QString groupName READ groupName WRITE setGroupName NOTIFY groupNameChanged)
0043     /**
0044      * The sensors to use for this face.
0045      *
0046      * This will set `highPrioritySensorIds` on the internal SensorFaceController.
0047      */
0048     Q_PROPERTY(QJsonArray sensors READ sensors WRITE setSensors NOTIFY sensorsChanged)
0049     /**
0050      * The face to use.
0051      *
0052      * This sets the faceId of the internal SensorFaceController.
0053      */
0054     Q_PROPERTY(QString faceId READ faceId WRITE setFaceId NOTIFY faceIdChanged)
0055     /**
0056      * A map of sensor colors to be used by the face.
0057      *
0058      * This forwards to the internal SensorFaceController.
0059      */
0060     Q_PROPERTY(QVariantMap colors READ colors WRITE setColors NOTIFY colorsChanged)
0061     /**
0062      * A map of sensor labels to be used by the face.
0063      *
0064      * This forwards to the internal SensorFaceController.
0065      */
0066     Q_PROPERTY(QVariantMap labels READ labels WRITE setLabels NOTIFY labelsChanged)
0067     /**
0068      * Whether to allow modifying the face configuration.
0069      *
0070      * If false (the default), any changes to configuration will be ignored. If
0071      * true, changes will be written and stored in the config group.
0072      *
0073      * \note If multiple FaceLoaders share the same configuration, the face will
0074      * need to be recreated after configuration has changed, as there is
0075      * currently no way to properly reload the configuration.
0076      */
0077     Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly NOTIFY readOnlyChanged)
0078     /**
0079      * The face controller that provides the loaded face.
0080      */
0081     Q_PROPERTY(KSysGuard::SensorFaceController *controller READ controller NOTIFY controllerChanged)
0082 
0083 public:
0084     FaceLoader(QObject *parent = nullptr);
0085     ~FaceLoader() override;
0086 
0087     SensorFaceController *parentController() const;
0088     void setParentController(SensorFaceController *newParentController);
0089     Q_SIGNAL void parentControllerChanged();
0090 
0091     QString groupName() const;
0092     void setGroupName(const QString &newGroupName);
0093     Q_SIGNAL void groupNameChanged();
0094 
0095     QJsonArray sensors() const;
0096     void setSensors(const QJsonArray &newSensors);
0097     Q_SIGNAL void sensorsChanged();
0098 
0099     QString faceId() const;
0100     void setFaceId(const QString &newFaceId);
0101     Q_SIGNAL void faceIdChanged();
0102 
0103     QVariantMap colors() const;
0104     void setColors(const QVariantMap &newColors);
0105     Q_SIGNAL void colorsChanged();
0106 
0107     QVariantMap labels() const;
0108     void setLabels(const QVariantMap &newLabels);
0109     Q_SIGNAL void labelsChanged();
0110 
0111     bool readOnly() const;
0112     void setReadOnly(bool newReadOnly);
0113     Q_SIGNAL void readOnlyChanged();
0114 
0115     SensorFaceController *controller() const;
0116     Q_SIGNAL void controllerChanged();
0117 
0118     Q_INVOKABLE void reload();
0119 
0120     void classBegin() override;
0121     void componentComplete() override;
0122 
0123 private:
0124     class Private;
0125     const std::unique_ptr<Private> d;
0126 };
0127 
0128 }