Warning, file /plasma/libksysguard/sensors/SensorDataModel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2019 Eike Hein <hein@kde.org>
0003     SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "sensors_export.h"
0011 #include <QAbstractTableModel>
0012 #include <QDateTime>
0013 #include <QQmlParserStatus>
0014 #include <memory>
0015 
0016 namespace KSysGuard
0017 {
0018 class SensorInfo;
0019 
0020 /**
0021  * A model representing a table of sensors.
0022  *
0023  * This model will expose the metadata and values of a list of sensors as a
0024  * table, using one column for each sensor. The metadata and values are
0025  * represented as different roles.
0026  */
0027 class SENSORS_EXPORT SensorDataModel : public QAbstractTableModel, public QQmlParserStatus
0028 {
0029     Q_OBJECT
0030     Q_INTERFACES(QQmlParserStatus)
0031 
0032     /**
0033      * The list of sensors to watch.
0034      */
0035     Q_PROPERTY(QStringList sensors READ sensors WRITE setSensors NOTIFY sensorsChanged)
0036     /**
0037      * The minimum value of all sensors' minimum property.
0038      */
0039     Q_PROPERTY(qreal minimum READ minimum NOTIFY sensorMetaDataChanged)
0040     /**
0041      * The maximum value of all sensors' maximum property.
0042      */
0043     Q_PROPERTY(qreal maximum READ maximum NOTIFY sensorMetaDataChanged)
0044     /**
0045      * Should this model be updated or not. Defaults to true.
0046      */
0047     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
0048     /**
0049      * Is this model ready?
0050      *
0051      * Ready means it has retrieved the metadata for the sensors specified in
0052      * \ref sensors.
0053      */
0054     Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged)
0055 
0056     /**
0057      * Used by the model to provide data for the Color role if set.
0058      */
0059     Q_PROPERTY(QVariantMap sensorColors READ sensorColors WRITE setSensorColors NOTIFY sensorColorsChanged)
0060 
0061     /**
0062      * Provides custom labels for Sensors that are used instead of the name and short name of the sensors.
0063      */
0064     Q_PROPERTY(QVariantMap sensorLabels READ sensorLabels WRITE setSensorLabels NOTIFY sensorLabelsChanged)
0065 
0066     /**
0067      * The minimum time between updates, in milliseconds.
0068      *
0069      * If this is set to a positive non-zero value, at least this many
0070      * milliseconds need to elapse before another value update happens, otherwise
0071      * it is ignored. This effectively rate-limits the updates and will prevent
0072      * value updates.
0073      */
0074     Q_PROPERTY(int updateRateLimit READ updateRateLimit WRITE setUpdateRateLimit NOTIFY updateRateLimitChanged RESET resetUpdateRateLimit)
0075 
0076 public:
0077     /**
0078      * The roles that this model exposes
0079      * @see Sensor
0080      */
0081     enum AdditionalRoles {
0082         SensorId = Qt::UserRole + 1, ///< The backend path to the sensor.
0083         Name, ///< The name of the sensor.
0084         ShortName, ///< A shorter name for the sensor. This is equal to name if not set.
0085         Description, ///< A description for the sensor.
0086         Unit, ///< The unit of the sensor.
0087         Minimum, ///< The minimum value this sensor can have.
0088         Maximum, ///< The maximum value this sensor can have.
0089         Type, ///< The QVariant::Type of the sensor.
0090         Value, ///< The value of the sensor.
0091         FormattedValue, ///< A formatted string of the value of the sensor.
0092         Color, ///< A color of the sensor, if sensorColors is set
0093         UpdateInterval, ///< The time in milliseconds between each update of the sensor.
0094     };
0095     Q_ENUM(AdditionalRoles)
0096 
0097     explicit SensorDataModel(const QStringList &sensorIds = {}, QObject *parent = nullptr);
0098     ~SensorDataModel() override;
0099 
0100     QHash<int, QByteArray> roleNames() const override;
0101 
0102     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0103     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0104 
0105     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0106     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0107 
0108     QStringList sensors() const;
0109     void setSensors(const QStringList &sensorIds);
0110     Q_SIGNAL void sensorsChanged() const;
0111     Q_SIGNAL void sensorMetaDataChanged();
0112 
0113     bool enabled() const;
0114     void setEnabled(bool newEnabled);
0115     Q_SIGNAL void enabledChanged();
0116 
0117     qreal minimum() const;
0118     qreal maximum() const;
0119 
0120     QVariantMap sensorColors() const;
0121     void setSensorColors(const QVariantMap &sensorColors);
0122     Q_SIGNAL void sensorColorsChanged();
0123 
0124     QVariantMap sensorLabels() const;
0125     void setSensorLabels(const QVariantMap &sensorLabels);
0126     Q_SIGNAL void sensorLabelsChanged();
0127 
0128     int updateRateLimit() const;
0129     void setUpdateRateLimit(int newUpdateRateLimit);
0130     void resetUpdateRateLimit();
0131     Q_SIGNAL void updateRateLimitChanged();
0132 
0133     bool isReady() const;
0134     Q_SIGNAL void readyChanged();
0135 
0136     Q_INVOKABLE void addSensor(const QString &sensorId);
0137     Q_INVOKABLE void removeSensor(const QString &sensorId);
0138     Q_INVOKABLE int column(const QString &sensorId) const;
0139 
0140     void classBegin() override;
0141     void componentComplete() override;
0142 
0143 private:
0144     void onSensorAdded(const QString &sensorId);
0145     void onSensorRemoved(const QString &sensorId);
0146     void onMetaDataChanged(const QString &sensorId, const SensorInfo &info);
0147     void onValueChanged(const QString &sensorId, const QVariant &value);
0148 
0149     class Private;
0150     const std::unique_ptr<Private> d;
0151 };
0152 
0153 } // namespace KSysGuard