Changeset View
Changeset View
Standalone View
Standalone View
sensors/SensorDataModel.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2019 Eike Hein <hein@kde.org> | ||||
3 | Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
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 | public: | ||||
59 | enum AdditionalRoles { | ||||
60 | SensorId = Qt::UserRole + 1, //< The backend path to the sensor. | ||||
61 | Name, //< The name of the sensor. | ||||
62 | ShortName, //< A shorter name for the sensor. This is equal to name if not set. | ||||
63 | Description, //< A description for the sensor. | ||||
64 | Unit, //< The unit of the sensor. | ||||
65 | Minimum, //< The minimum value this sensor can have. | ||||
66 | Maximum, //< The maximum value this sensor can have. | ||||
67 | Type, //< The QVariant::Type of the sensor. | ||||
68 | Value, //< The value of the sensor. | ||||
69 | FormattedValue, //< A formatted string of the value of the sensor. | ||||
70 | }; | ||||
71 | Q_ENUM(AdditionalRoles) | ||||
72 | | ||||
73 | explicit SensorDataModel(const QStringList &sensorIds = {}, QObject *parent = nullptr); | ||||
74 | virtual ~SensorDataModel(); | ||||
75 | | ||||
76 | QHash<int, QByteArray> roleNames() const override; | ||||
77 | | ||||
78 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||||
79 | QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; | ||||
80 | | ||||
81 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||||
82 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; | ||||
83 | | ||||
84 | QStringList sensors() const; | ||||
85 | void setSensors(const QStringList &sensorIds); | ||||
86 | Q_SIGNAL void sensorsChanged() const; | ||||
87 | Q_SIGNAL void sensorMetaDataChanged(); | ||||
88 | | ||||
89 | qreal minimum() const; | ||||
90 | qreal maximum() const; | ||||
91 | | ||||
92 | Q_INVOKABLE void addSensor(const QString &sensorId); | ||||
93 | Q_INVOKABLE void removeSensor(const QString &sensorId); | ||||
94 | Q_INVOKABLE int column(const QString &sensorId) const; | ||||
95 | | ||||
96 | void classBegin() override; | ||||
97 | void componentComplete() override; | ||||
98 | | ||||
99 | private: | ||||
100 | void onSensorAdded(const QString &sensorId); | ||||
101 | void onSensorRemoved(const QString &sensorId); | ||||
102 | void onMetaDataChanged(const QString &sensorId, const SensorInfo &info); | ||||
103 | void onValueChanged(const QString &sensorId, const QVariant &value); | ||||
104 | | ||||
105 | class Private; | ||||
106 | const std::unique_ptr<Private> d; | ||||
107 | }; | ||||
108 | | ||||
109 | } // namespace KSysGuard |