Libksysguard

SensorDataModel.h
1 /*
2  Copyright (c) 2019 Eike Hein <[email protected]>
3  Copyright (C) 2020 Arjen Hiemstra <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #pragma once
22 
23 #include "sensors_export.h"
24 #include <QAbstractTableModel>
25 #include <QDateTime>
26 #include <QQmlParserStatus>
27 #include <memory>
28 
29 namespace KSysGuard
30 {
31 class SensorInfo;
32 
33 /**
34  * A model representing a table of sensors.
35  *
36  * This model will expose the metadata and values of a list of sensors as a
37  * table, using one column for each sensor. The metadata and values are
38  * represented as different roles.
39  */
40 class SENSORS_EXPORT SensorDataModel : public QAbstractTableModel, public QQmlParserStatus
41 {
42  Q_OBJECT
43  Q_INTERFACES(QQmlParserStatus)
44 
45  /**
46  * The list of sensors to watch.
47  */
48  Q_PROPERTY(QStringList sensors READ sensors WRITE setSensors NOTIFY sensorsChanged)
49  /**
50  * The minimum value of all sensors' minimum property.
51  */
52  Q_PROPERTY(qreal minimum READ minimum NOTIFY sensorMetaDataChanged)
53  /**
54  * The maximum value of all sensors' maximum property.
55  */
56  Q_PROPERTY(qreal maximum READ maximum NOTIFY sensorMetaDataChanged)
57  /**
58  * Should this model be updated or not. Defaults to true.
59  */
60  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
61  /**
62  * Is this model ready?
63  *
64  * Ready means it has retrieved the metadata for the sensors specified in
65  * \ref sensors.
66  */
67  Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged)
68 
69  /**
70  * Used by the model to provide data for the Color role if set.
71  */
72  Q_PROPERTY(QVariantMap sensorColors READ sensorColors WRITE setSensorColors NOTIFY sensorColorsChanged)
73 
74 public:
75  /**
76  * The roles that this model exposes
77  * @see Sensor
78  */
80  SensorId = Qt::UserRole + 1, ///< The backend path to the sensor.
81  Name, ///< The name of the sensor.
82  ShortName, ///< A shorter name for the sensor. This is equal to name if not set.
83  Description, ///< A description for the sensor.
84  Unit, ///< The unit of the sensor.
85  Minimum, ///< The minimum value this sensor can have.
86  Maximum, ///< The maximum value this sensor can have.
87  Type, ///< The QVariant::Type of the sensor.
88  Value, ///< The value of the sensor.
89  FormattedValue, ///< A formatted string of the value of the sensor.
90  Color, ///< A color of the sensor, if sensorColors is set
91  UpdateInterval, ///< The time in milliseconds between each update of the sensor.
92  };
93  Q_ENUM(AdditionalRoles)
94 
95  explicit SensorDataModel(const QStringList &sensorIds = {}, QObject *parent = nullptr);
96  virtual ~SensorDataModel();
97 
98  QHash<int, QByteArray> roleNames() const override;
99 
100  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
101  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
102 
103  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
104  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
105 
106  QStringList sensors() const;
107  void setSensors(const QStringList &sensorIds);
108  Q_SIGNAL void sensorsChanged() const;
109  Q_SIGNAL void sensorMetaDataChanged();
110 
111  bool enabled() const;
112  void setEnabled(bool newEnabled);
113  Q_SIGNAL void enabledChanged();
114 
115  qreal minimum() const;
116  qreal maximum() const;
117 
118  QVariantMap sensorColors() const;
119  void setSensorColors(const QVariantMap &sensorColors);
120  Q_SIGNAL void sensorColorsChanged();
121 
122  bool isReady() const;
123  Q_SIGNAL void readyChanged();
124 
125  Q_INVOKABLE void addSensor(const QString &sensorId);
126  Q_INVOKABLE void removeSensor(const QString &sensorId);
127  Q_INVOKABLE int column(const QString &sensorId) const;
128 
129  void classBegin() override;
130  void componentComplete() override;
131 
132 private:
133  void onSensorAdded(const QString &sensorId);
134  void onSensorRemoved(const QString &sensorId);
135  void onMetaDataChanged(const QString &sensorId, const SensorInfo &info);
136  void onValueChanged(const QString &sensorId, const QVariant &value);
137 
138  class Private;
139  const std::unique_ptr<Private> d;
140 };
141 
142 } // namespace KSysGuard
The value of the sensor.
The maximum value this sensor can have.
The name of the sensor.
A description for the sensor.
The unit of the sensor.
The minimum value this sensor can have.
A model representing a table of sensors.
The QVariant::Type of the sensor.
UserRole
AdditionalRoles
The roles that this model exposes.
A shorter name for the sensor. This is equal to name if not set.
The time in milliseconds between each update of the sensor.
Orientation
A color of the sensor, if sensorColors is set.
A formatted string of the value of the sensor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Mar 4 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.