Libksysguard

SensorDataModel.h
1/*
2 SPDX-FileCopyrightText: 2019 Eike Hein <hein@kde.org>
3 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#pragma once
9
10#include "sensors_export.h"
11#include <QAbstractTableModel>
12#include <QDateTime>
13#include <QQmlParserStatus>
14#include <memory>
15#include <qqmlregistration.h>
16
17namespace KSysGuard
18{
19class SensorInfo;
20
21/**
22 * A model representing a table of sensors.
23 *
24 * This model will expose the metadata and values of a list of sensors as a
25 * table, using one column for each sensor. The metadata and values are
26 * represented as different roles.
27 */
28class SENSORS_EXPORT SensorDataModel : public QAbstractTableModel, public QQmlParserStatus
29{
31 QML_ELEMENT
33
34 /**
35 * The list of sensors to watch.
36 */
37 Q_PROPERTY(QStringList sensors READ sensors WRITE setSensors NOTIFY sensorsChanged)
38 /**
39 * The minimum value of all sensors' minimum property.
40 */
41 Q_PROPERTY(qreal minimum READ minimum NOTIFY sensorMetaDataChanged)
42 /**
43 * The maximum value of all sensors' maximum property.
44 */
45 Q_PROPERTY(qreal maximum READ maximum NOTIFY sensorMetaDataChanged)
46 /**
47 * Should this model be updated or not. Defaults to true.
48 */
49 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
50 /**
51 * Is this model ready?
52 *
53 * Ready means it has retrieved the metadata for the sensors specified in
54 * \ref sensors.
55 */
56 Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged)
57
58 /**
59 * Used by the model to provide data for the Color role if set.
60 */
61 Q_PROPERTY(QVariantMap sensorColors READ sensorColors WRITE setSensorColors NOTIFY sensorColorsChanged)
62
63 /**
64 * Provides custom labels for Sensors that are used instead of the name and short name of the sensors.
65 */
66 Q_PROPERTY(QVariantMap sensorLabels READ sensorLabels WRITE setSensorLabels NOTIFY sensorLabelsChanged)
67
68 /**
69 * The minimum time between updates, in milliseconds.
70 *
71 * If this is set to a positive non-zero value, at least this many
72 * milliseconds need to elapse before another value update happens, otherwise
73 * it is ignored. This effectively rate-limits the updates and will prevent
74 * value updates.
75 */
76 Q_PROPERTY(int updateRateLimit READ updateRateLimit WRITE setUpdateRateLimit NOTIFY updateRateLimitChanged RESET resetUpdateRateLimit)
77
78public:
79 /**
80 * The roles that this model exposes
81 * @see Sensor
82 */
84 SensorId = Qt::UserRole + 1, ///< The backend path to the sensor.
85 Name, ///< The name of the sensor.
86 ShortName, ///< A shorter name for the sensor. This is equal to name if not set.
87 Description, ///< A description for the sensor.
88 Unit, ///< The unit of the sensor.
89 Minimum, ///< The minimum value this sensor can have.
90 Maximum, ///< The maximum value this sensor can have.
91 Type, ///< The QVariant::Type of the sensor.
92 Value, ///< The value of the sensor.
93 FormattedValue, ///< A formatted string of the value of the sensor.
94 Color, ///< A color of the sensor, if sensorColors is set
95 UpdateInterval, ///< The time in milliseconds between each update of the sensor.
96 };
97 Q_ENUM(AdditionalRoles)
98
99 explicit SensorDataModel(const QStringList &sensorIds = {}, QObject *parent = nullptr);
100 ~SensorDataModel() override;
101
102 QHash<int, QByteArray> roleNames() const override;
103
104 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
105 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
106
107 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
108 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
109
110 QStringList sensors() const;
111 void setSensors(const QStringList &sensorIds);
112 Q_SIGNAL void sensorsChanged() const;
113 Q_SIGNAL void sensorMetaDataChanged();
114
115 bool enabled() const;
116 void setEnabled(bool newEnabled);
117 Q_SIGNAL void enabledChanged();
118
119 qreal minimum() const;
120 qreal maximum() const;
121
122 QVariantMap sensorColors() const;
123 void setSensorColors(const QVariantMap &sensorColors);
124 Q_SIGNAL void sensorColorsChanged();
125
126 QVariantMap sensorLabels() const;
127 void setSensorLabels(const QVariantMap &sensorLabels);
128 Q_SIGNAL void sensorLabelsChanged();
129
130 int updateRateLimit() const;
131 void setUpdateRateLimit(int newUpdateRateLimit);
132 void resetUpdateRateLimit();
133 Q_SIGNAL void updateRateLimitChanged();
134
135 bool isReady() const;
136 Q_SIGNAL void readyChanged();
137
138 Q_INVOKABLE void addSensor(const QString &sensorId);
139 Q_INVOKABLE void removeSensor(const QString &sensorId);
140 Q_INVOKABLE int column(const QString &sensorId) const;
141
142 void classBegin() override;
143 void componentComplete() override;
144
145private:
146 void onSensorAdded(const QString &sensorId);
147 void onSensorRemoved(const QString &sensorId);
148 void onMetaDataChanged(const QString &sensorId, const SensorInfo &info);
149 void onValueChanged(const QString &sensorId, const QVariant &value);
150
151 class Private;
152 const std::unique_ptr<Private> d;
153};
154
155} // namespace KSysGuard
A model representing a table of sensors.
bool ready
Is this model ready?
AdditionalRoles
The roles that this model exposes.
@ Value
The value of the sensor.
@ Description
A description for the sensor.
@ Name
The name of the sensor.
@ ShortName
A shorter name for the sensor. This is equal to name if not set.
@ UpdateInterval
The time in milliseconds between each update of the sensor.
@ Color
A color of the sensor, if sensorColors is set.
@ FormattedValue
A formatted string of the value of the sensor.
@ Minimum
The minimum value this sensor can have.
@ SensorId
The backend path to the sensor.
@ Maximum
The maximum value this sensor can have.
@ Unit
The unit of the sensor.
@ Type
The QVariant::Type of the sensor.
int updateRateLimit
The minimum time between updates, in milliseconds.
qreal maximum
The maximum value of all sensors' maximum property.
QML_ELEMENTQStringList sensors
The list of sensors to watch.
bool enabled
Should this model be updated or not.
QVariantMap sensorLabels
Provides custom labels for Sensors that are used instead of the name and short name of the sensors.
qreal minimum
The minimum value of all sensors' minimum property.
QVariantMap sensorColors
Used by the model to provide data for the Color role if set.
QAbstractTableModel(QObject *parent)
Q_INTERFACES(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
UserRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:59:47 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.