File indexing completed on 2024-04-28 16:49:54

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractItemModel>
0010 #include <processcore/processes.h>
0011 
0012 namespace KSysGuard
0013 {
0014 class Process;
0015 class ProcessAttributeModel;
0016 
0017 /**
0018  * This class contains a model of all running processes
0019  * Rows represent processes
0020  * Columns represent a specific attribute, such as CPU usage
0021  * Attributes can be enabled or disabled
0022  *
0023  * This class abstracts the process data so that it can be presented without the client
0024  * needing to understand the semantics of each column
0025  * It is designed to be consumable by a QML API
0026  */
0027 class Q_DECL_EXPORT ProcessDataModel : public QAbstractItemModel
0028 {
0029     Q_OBJECT
0030 
0031     /**
0032      * A list of ids of all available attributes.
0033      */
0034     Q_PROPERTY(QStringList availableAttributes READ availableAttributes CONSTANT)
0035     /**
0036      * A list of attributes that should be displayed by this model.
0037      *
0038      * Each attribute will correspond to a column, assuming the attribute exists.
0039      * \property availableAttributes provides a list of all attributes that are
0040      * available.
0041      *
0042      * By default, this is empty and thus nothing will be shown.
0043      */
0044     Q_PROPERTY(QStringList enabledAttributes READ enabledAttributes WRITE setEnabledAttributes NOTIFY enabledAttributesChanged)
0045     /**
0046      * Provides an instance of a model that lists all available attributes for this model.
0047      *
0048      * It provides extra information on top of the list of ids in availableAttributes.
0049      *
0050      * \sa ProcessAttributeModel
0051      */
0052     Q_PROPERTY(QAbstractItemModel *attributesModel READ attributesModel CONSTANT)
0053     /**
0054      * Should this model be updated or not. Defaults to true.
0055      */
0056     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
0057 
0058     /**
0059      * If true this model is a flat list, otherwise is a tree following the process tree structure. Default is true
0060      */
0061     Q_PROPERTY(bool flatList READ flatList WRITE setFlatList NOTIFY flatListChanged)
0062 
0063 public:
0064     enum AdditionalRoles {
0065         Value = Qt::UserRole, /// The raw value of the attribute. This is unformatted and could represent an int, real or string
0066         FormattedValue, /// A string containing the value in a locale friendly way with appropriate suffix "eg. 5Mb" "20%"
0067 
0068         PIDs, /// The PIDs associated with this row
0069         Minimum, /// Smallest value this reading can be in normal situations. A hint for graphing utilities
0070         Maximum, /// Largest value this reading can be in normal situations. A hint for graphing utilities
0071 
0072         Attribute, /// The attribute id associated with this column
0073         Name, /// The full name of this attribute
0074         ShortName, /// A shorter name of this attribute, compressed for viewing
0075         Unit, /// The unit associated with this attribute. Returned value is of the type KSysGuard::Unit
0076 
0077         UpdateInterval, /// The amount of time in milliseconds between each update of the model.
0078     };
0079     Q_ENUM(AdditionalRoles)
0080 
0081     explicit ProcessDataModel(QObject *parent = nullptr);
0082     ~ProcessDataModel() override;
0083 
0084     /**
0085      * A list of attribute IDs that can be enabled
0086      */
0087     QStringList availableAttributes() const;
0088 
0089     /**
0090      * The list of available attributes that can be enabled, presented as a model
0091      * See @availableAttributes
0092      */
0093     ProcessAttributeModel *attributesModel();
0094 
0095     /**
0096      * The currently enabled attributes
0097      */
0098     QStringList enabledAttributes() const;
0099     /**
0100      * Select which process attributes should be enabled
0101      * The order determines the column order
0102      *
0103      * The default value is empty
0104      */
0105     void setEnabledAttributes(const QStringList &enabledAttributes);
0106 
0107     bool enabled() const;
0108     void setEnabled(bool newEnabled);
0109     Q_SIGNAL void enabledChanged();
0110 
0111     bool flatList() const;
0112     void setFlatList(bool flat);
0113     Q_SIGNAL void flatListChanged();
0114 
0115     QHash<int, QByteArray> roleNames() const override;
0116     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0117     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0118     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0119     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0120     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0121     QModelIndex parent(const QModelIndex &index) const override;
0122 
0123 Q_SIGNALS:
0124     void enabledAttributesChanged();
0125 
0126 private:
0127     class Private;
0128     QScopedPointer<Private> d;
0129 };
0130 
0131 }