File indexing completed on 2024-04-28 05:31:38

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