File indexing completed on 2024-04-21 05:31:43

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <memory>
0010 
0011 #include <QAbstractListModel>
0012 
0013 #include "sensors_export.h"
0014 
0015 namespace KSysGuard
0016 {
0017 class SensorInfo;
0018 
0019 /**
0020  * A model that lists the units for a given list of sensors.
0021  *
0022  * This model will provide a list of units for a given list of sensors. It
0023  * includes all the prefixes starting at the base unit of a sensor. For example,
0024  * for a sensor with unit "bytes" it will list "bytes", "kilobytes", "megabytes"
0025  * etc. If instead the unit were "megabytes", the model would list "megabytes",
0026  * "gigabytes", etc. The units for all sensors are listed, with duplicates
0027  * removed. They are sorted based on the order of units in Formatter's Unit.h.
0028  *
0029  * Three roles are exposed:
0030  * - "unit": The actual Unit enum value for this unit.
0031  * - "symbol": The textual symbol for the unit, like "KiB/s".
0032  * - "multiplier": The multiplier to apply to the unit value to convert it to
0033  *                 the base unit. For example, to convert from kilobytes to
0034  *                 bytes the multiplier would be 1024.
0035  */
0036 class SENSORS_EXPORT SensorUnitModel : public QAbstractListModel
0037 {
0038     Q_OBJECT
0039     /**
0040      * The list of sensors to list units for.
0041      */
0042     Q_PROPERTY(QStringList sensors READ sensors WRITE setSensors NOTIFY sensorsChanged)
0043     /**
0044      * Indicates the model has retrieved all the information of the requested sensors.
0045      *
0046      * This will be false right after setting the value of `sensors`. It will
0047      * change to true once the information for all sensors has been retrieved.
0048      */
0049     Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
0050 
0051 public:
0052     enum Roles {
0053         UnitRole = Qt::UserRole,
0054         SymbolRole,
0055         MultiplierRole,
0056     };
0057     Q_ENUM(Roles)
0058 
0059     SensorUnitModel(QObject *parent = nullptr);
0060     ~SensorUnitModel() override;
0061 
0062     QStringList sensors() const;
0063     void setSensors(const QStringList &newSensors);
0064     Q_SIGNAL void sensorsChanged();
0065 
0066     bool ready() const;
0067     Q_SIGNAL void readyChanged();
0068 
0069     QHash<int, QByteArray> roleNames() const override;
0070 
0071     virtual int rowCount(const QModelIndex &parent) const override;
0072     virtual QVariant data(const QModelIndex &index, int role) const override;
0073 
0074 private:
0075     void metaDataChanged(const QString &id, const SensorInfo &info);
0076 
0077     class Private;
0078     const std::unique_ptr<Private> d;
0079 };
0080 
0081 }