diff --git a/sensors/Sensor.h b/sensors/Sensor.h index f046031..db70b85 100644 --- a/sensors/Sensor.h +++ b/sensors/Sensor.h @@ -1,165 +1,182 @@ /* Copyright (C) 2019 Vlad Zagorodniy Copyright (C) 2020 Arjen Hiemstra This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include #include #include #include "formatter/Unit.h" #include "sensors_export.h" namespace KSysGuard { class SensorData; class SensorInfo; +/** + * An object encapsulating a backend sensor. + * + * This class represents a sensor as exposed by the backend. It allows querying + * various metadata properties of the sensor as well as the current value. + */ class SENSORS_EXPORT Sensor : public QObject, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) /** * The path to the backend sensor this Sensor represents. */ Q_PROPERTY(QString sensorId READ sensorId WRITE setSensorId NOTIFY sensorIdChanged) /** * The user-visible name of this Sensor. */ Q_PROPERTY(QString name READ name NOTIFY metaDataChanged) /** * A shortened name that can be displayed when space is constrained. * * The value is the same as name if shortName was not provided by the backend. */ Q_PROPERTY(QString shortName READ shortName NOTIFY metaDataChanged) /** * A description of the Sensor. */ Q_PROPERTY(QString description READ description NOTIFY metaDataChanged) /** * The unit of this Sensor. */ Q_PROPERTY(KSysGuard::Unit unit READ unit NOTIFY metaDataChanged) /** * The minimum value this Sensor can have. */ Q_PROPERTY(qreal minimum READ minimum NOTIFY metaDataChanged) /** * The maximum value this Sensor can have. */ Q_PROPERTY(qreal maximum READ maximum NOTIFY metaDataChanged) /** - * The + * The QVariant type for this sensor. + * + * This is used to create proper default values. */ Q_PROPERTY(QVariant::Type type READ type NOTIFY metaDataChanged) /** + * The status of the sensor. + * + * Due to the asynchronous nature of the underlying code, sensors are not + * immediately available on construction. Instead, they need to request data + * from the daemon and wait for it to arrive. This property reflects where + * in that process this sensor is. */ Q_PROPERTY(Status status READ status NOTIFY statusChanged) /** * The current value of this sensor. */ Q_PROPERTY(QVariant value READ value NOTIFY valueChanged) /** * A formatted version of \property value. */ Q_PROPERTY(QString formattedValue READ formattedValue NOTIFY valueChanged) /** * Should this Sensor check for changes? + * + * Note that if set to true, the sensor will only be enabled when the parent + * is also enabled. */ Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) public: /** * This enum type is used to specify status of the Sensor. */ enum class Status { Unknown, ///< The sensor has no ID assigned. Loading, ///< The sensor is currently being loaded. Ready, ///< The sensor has been loaded. Error, ///< An error occurred or the sensor has been removed. Removed, ///< Removed from backend }; Q_ENUM(Status) explicit Sensor(QObject *parent = nullptr); explicit Sensor(const QString &id, QObject *parent = nullptr); ~Sensor() override; bool event(QEvent *event) override; QString sensorId() const; void setSensorId(const QString &id); Q_SIGNAL void sensorIdChanged() const; Status status() const; Q_SIGNAL void statusChanged() const; QString name() const; QString shortName() const; QString description() const; KSysGuard::Unit unit() const; qreal minimum() const; qreal maximum() const; QVariant::Type type() const; /** * This signal is emitted when any of the metadata properties change. */ Q_SIGNAL void metaDataChanged() const; /** * Returns the output of the sensor. * * The returned value is the most recent sensor data received from the ksysguard * daemon, it's not necessarily the actual current output value. * * The client can't control how often the sensor data is sampled. The ksysguard * daemon is in charge of picking the sample rate. When the Sensor receives new * output value, dataChanged signal will be emitted. * * @see dataChanged */ QVariant value() const; QString formattedValue() const; Q_SIGNAL void valueChanged() const; bool enabled() const; void setEnabled(bool newEnabled); Q_SIGNAL void enabledChanged(); void classBegin() override; void componentComplete() override; private: void onMetaDataChanged(const QString &sensorId, const SensorInfo &metaData); void onValueChanged(const QString &sensorId, const QVariant &value); void onEnabledChanged(); class Private; const std::unique_ptr d; }; } // namespace KSysGuard diff --git a/sensors/SensorDaemonInterface_p.h b/sensors/SensorDaemonInterface_p.h index bd3e4dc..2befd0e 100644 --- a/sensors/SensorDaemonInterface_p.h +++ b/sensors/SensorDaemonInterface_p.h @@ -1,64 +1,70 @@ /* Copyright (C) 2020 Arjen Hiemstra This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include "SensorInfo_p.h" class QDBusPendingCallWatcher; namespace KSysGuard { +/** + * Internal helper class to communicate with the daemon. + * + * This is mostly for convenience on top of the auto-generated KSysGuardDaemon + * D-Bus interface. + */ class SensorDaemonInterface : public QObject { Q_OBJECT public: SensorDaemonInterface(QObject *parent = nullptr); ~SensorDaemonInterface() override; void requestMetaData(const QString &sensorId); void requestMetaData(const QStringList &sensorIds); Q_SIGNAL void metaDataChanged(const QString &sensorId, const SensorInfo &info); void requestValue(const QString &sensorId); Q_SIGNAL void valueChanged(const QString &sensorId, const QVariant &value); QDBusPendingCallWatcher *allSensors() const; void subscribe(const QString &sensorId); void subscribe(const QStringList &sensorIds); void unsubscribe(const QString &sensorId); void unsubscribe(const QStringList &sensorIds); Q_SIGNAL void sensorAdded(const QString &sensorId); Q_SIGNAL void sensorRemoved(const QString &sensorId); static SensorDaemonInterface *instance(); private: void onMetaDataChanged(const QHash &metaData); void onValueChanged(const QStringList &, const SensorDataList &values, const QStringList &); class Private; const std::unique_ptr d; }; } diff --git a/sensors/SensorDataModel.h b/sensors/SensorDataModel.h index 70d2e3d..3028491 100644 --- a/sensors/SensorDataModel.h +++ b/sensors/SensorDataModel.h @@ -1,96 +1,110 @@ /* Copyright (c) 2019 Eike Hein Copyright (C) 2020 Arjen Hiemstra This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include #include #include "sensors_export.h" namespace KSysGuard { class SensorInfo; +/** + * A model representing a table of sensors. + * + * This model will expose the metadata and values of a list of sensors as a + * table, using one column for each sensor. The metadata and values are + * represented as different roles. + */ class SENSORS_EXPORT SensorDataModel : public QAbstractTableModel, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) + /** + * The list of sensors to watch. + */ Q_PROPERTY(QStringList sensors READ sensors WRITE setSensors NOTIFY sensorsChanged) - // The minimum value of all sensors' minimum property. + /** + * The minimum value of all sensors' minimum property. + */ Q_PROPERTY(qreal minimum READ minimum NOTIFY sensorMetaDataChanged) - // The maximum value of all sensors' maximum property. + /** + * The maximum value of all sensors' maximum property. + */ Q_PROPERTY(qreal maximum READ maximum NOTIFY sensorMetaDataChanged) public: enum AdditionalRoles { - SensorId = Qt::UserRole + 1, - Name, - ShortName, - Description, - Unit, - Minimum, - Maximum, - Type, - Value, - FormattedValue, + SensorId = Qt::UserRole + 1, //< The backend path to the sensor. + Name, //< The name of the sensor. + ShortName, //< A shorter name for the sensor. This is equal to name if not set. + Description, //< A description for the sensor. + Unit, //< The unit of the sensor. + Minimum, //< The minimum value this sensor can have. + Maximum, //< The maximum value this sensor can have. + Type, //< The QVariant::Type of the sensor. + Value, //< The value of the sensor. + FormattedValue, //< A formatted string of the value of the sensor. }; Q_ENUM(AdditionalRoles) explicit SensorDataModel(const QStringList &sensorIds = {}, QObject *parent = nullptr); virtual ~SensorDataModel(); QHash roleNames() const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QStringList sensors() const; void setSensors(const QStringList &sensorIds); Q_SIGNAL void sensorsChanged() const; Q_SIGNAL void sensorMetaDataChanged(); qreal minimum() const; qreal maximum() const; Q_INVOKABLE void addSensor(const QString &sensorId); Q_INVOKABLE void removeSensor(const QString &sensorId); Q_INVOKABLE int column(const QString &sensorId) const; void classBegin() override; void componentComplete() override; private: void onSensorAdded(const QString &sensorId); void onSensorRemoved(const QString &sensorId); void onMetaDataChanged(const QString &sensorId, const SensorInfo &info); void onValueChanged(const QString &sensorId, const QVariant &value); class Private; const std::unique_ptr d; }; } // namespace KSysGuard diff --git a/sensors/SensorQuery.h b/sensors/SensorQuery.h index 517a18c..bb27b53 100644 --- a/sensors/SensorQuery.h +++ b/sensors/SensorQuery.h @@ -1,54 +1,77 @@ /* Copyright (C) 2020 Arjen Hiemstra This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include "sensors_export.h" namespace KSysGuard { class SensorInfo; +/** + * An object to query the daemon for a list of sensors and their metadata. + * + * This class will request a list of sensors from the daemon, then filter them + * based on the supplied path. The path can include the wildcard "*" to get a + * list of all sensors matching the specified part of their path. In addition, + * if left empty, all sensors will be returned. + */ class SENSORS_EXPORT SensorQuery : public QObject { Q_OBJECT public: SensorQuery(const QString &path = QString{}, QObject *parent = nullptr); ~SensorQuery() override; QString path() const; void setPath(const QString &path); QStringList sensorIds() const; + /** + * The result of the query. + * + * \return a Vector of sensor ID, SensorInfo pairs that match the given path + * expression. + */ QVector> result() const; + /** + * Start processing the query. + */ bool execute(); + /** + * Wait for the query to finish. + * + * Mostly useful for code that needs the result to be available before + * continuing. Ideally the finished() signal should be used instead. + */ bool waitForFinished(); Q_SIGNAL void finished(const SensorQuery *query); private: class Private; const std::unique_ptr d; }; } // namespace KSysGuard diff --git a/sensors/SensorTreeModel.h b/sensors/SensorTreeModel.h index d76c3b0..c5c30fd 100644 --- a/sensors/SensorTreeModel.h +++ b/sensors/SensorTreeModel.h @@ -1,71 +1,79 @@ /* Copyright (c) 2019 Eike Hein Copyright (C) 2020 Arjen Hiemstra This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include #include #include "sensors_export.h" namespace KSysGuard { class SensorInfo; +/** + * A model representing a tree of sensors that are available from the daemon. + * + * This model exposes the daemon's sensors as a tree, based on their path. Each + * sensor is assumed to be structured in a format similar to + * `category/object/sensor`. This model will then expose a tree, with `category` + * as top level, `object` below it and finally `sensor` itself. + */ class SENSORS_EXPORT SensorTreeModel : public QAbstractItemModel { Q_OBJECT public: enum AdditionalRoles { SensorId = Qt::UserRole + 1, }; Q_ENUM(AdditionalRoles) explicit SensorTreeModel(QObject *parent = nullptr); virtual ~SensorTreeModel(); QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation, int role) const override; QStringList mimeTypes() const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QMimeData *mimeData(const QModelIndexList &indexes) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; private: void init(); void onSensorAdded(const QString &sensor); void onSensorRemoved(const QString &sensor); void onMetaDataChanged(const QString &sensorId, const SensorInfo &info); class Private; const std::unique_ptr d; }; }