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

KDE's Doxygen guidelines are available online.