Warning, file /plasma/libksysguard/processui/ProcessModel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     KSysGuard, the KDE System Guard
0003 
0004     SPDX-FileCopyrightText: 1999, 2000 Chris Schlaeger <cs@kde.org>
0005     SPDX-FileCopyrightText: 2006 John Tapsell <john.tapsell@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 
0009 */
0010 
0011 #ifndef PROCESSMODEL_H_
0012 #define PROCESSMODEL_H_
0013 
0014 #include <QAbstractItemModel>
0015 
0016 #include <processcore/processes.h>
0017 
0018 namespace KSysGuard
0019 {
0020 class Processes;
0021 class Process;
0022 class ProcessAttribute;
0023 }
0024 
0025 class ProcessModelPrivate;
0026 
0027 #ifdef Q_OS_WIN
0028 // this workaround is needed to make krunner link under msvc
0029 // please keep it this way even if you port this library to have a _export.h header file
0030 #define KSYSGUARD_EXPORT
0031 #else
0032 #define KSYSGUARD_EXPORT Q_DECL_EXPORT
0033 #endif
0034 
0035 class KSYSGUARD_EXPORT ProcessModel : public QAbstractItemModel
0036 {
0037     Q_OBJECT
0038     Q_ENUMS(Units)
0039 
0040 public:
0041     /** Storage for history values. PercentageHistoryRole returns a QVector of this. */
0042     struct PercentageHistoryEntry {
0043         unsigned long timestamp; // in ms, origin undefined as only the delta matters
0044         float value;
0045     };
0046 
0047     explicit ProcessModel(QObject *parent = nullptr, const QString &host = QString());
0048     ~ProcessModel() override;
0049 
0050     /* Functions for our Model for QAbstractItemModel*/
0051     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0052     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0053     QVariant data(const QModelIndex &index, int role) const override;
0054     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0055     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0056     QModelIndex parent(const QModelIndex &index) const override;
0057 
0058     bool hasChildren(const QModelIndex &parent) const override;
0059     /** Returns if (left < right), used by the sort-filter proxy model to sort the columns */
0060     bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
0061 
0062     /* Functions for drag and drop and copying to clipboard, inherited from QAbstractItemModel */
0063     QStringList mimeTypes() const override;
0064     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0065     Qt::ItemFlags flags(const QModelIndex &index) const override;
0066 
0067     /* Functions for setting the model */
0068 
0069     /** Setup the column headings by inserting the appropriate headings into the model.
0070      *  Can be called more than once to retranslate the headings if the system language changes.
0071      */
0072     void setupHeader();
0073 
0074     /** Update data.  You can pass in the time between updates to only update if there hasn't
0075      *  been an update within the last @p updateDurationMSecs milliseconds.  0 indicate to update
0076      *  regardless of when the last update was.
0077      *  The updateFlags indicates what to additional update, as well as the usual details. */
0078     void update(long updateDurationMSecs = 0, KSysGuard::Processes::UpdateFlags updateFlags = KSysGuard::Processes::IOStatistics);
0079     /** Return a string with the pid of the process and the name of the process.  E.g.  13343: ksysguard
0080      */
0081     QString getStringForProcess(KSysGuard::Process *process) const;
0082     KSysGuard::Process *getProcess(qlonglong pid);
0083 
0084     /** This is used from ProcessFilter to get the process at a given index when in flat mode */
0085     KSysGuard::Process *getProcessAtIndex(int index) const;
0086 
0087     /** Returns whether this user can log in or not.
0088      *  @see mUidCanLogin
0089      */
0090     bool canUserLogin(long uid) const;
0091     /** In simple mode, everything is flat, with no icons, few if any colors, no xres etc.
0092      *  This can be changed at any time.  It is a fairly quick operation.  Basically it resets the model
0093      */
0094     void setSimpleMode(bool simple);
0095     /** In simple mode, everything is flat, with no icons, few if any colors, no xres etc
0096      */
0097     bool isSimpleMode() const;
0098 
0099     /** Returns the total amount of physical memory in the machine. */
0100     qlonglong totalMemory() const;
0101 
0102     /** This returns a QModelIndex for the given process.  It has to look up the parent for this pid, find the offset this
0103      *  pid is from the parent, and return that.  It's not that slow, but does involve a couple of hash table lookups.
0104      */
0105     QModelIndex getQModelIndex(KSysGuard::Process *process, int column) const;
0106 
0107     /** Whether this is showing the processes for the current machine
0108      */
0109     bool isLocalhost() const;
0110 
0111     /** The host name that this widget is showing the processes of */
0112     QString hostName() const;
0113 
0114     /** Whether this process has a GUI window */
0115     bool hasGUIWindow(qlonglong pid) const;
0116 
0117     /** Returns for process controller pointer for this model */
0118     KSysGuard::Processes *processController() const; // The processes instance
0119 
0120     /** Returns the list of extra attributes provided by plugins */
0121     const QVector<KSysGuard::ProcessAttribute *> extraAttributes() const;
0122 
0123     /** Convenience function to get the number of processes.
0124      *
0125      *  Equivalent to processController->processCount() */
0126     int processCount() const
0127     {
0128         return processController()->processCount();
0129     }
0130 
0131     /** The headings in the model.  The order here is the order that they are shown
0132      *  in.  If you change this, make sure you also change the
0133      *  setup header function, and make sure you increase PROCESSHEADERVERSION.  This will ensure
0134      *  that old saved settings won't be used
0135      */
0136 #define PROCESSHEADERVERSION 10
0137     enum {
0138         HeadingName = 0,
0139         HeadingUser,
0140         HeadingPid,
0141         HeadingTty,
0142         HeadingNiceness,
0143         HeadingCPUUsage,
0144         HeadingCPUTime,
0145         HeadingIoRead,
0146         HeadingIoWrite,
0147         HeadingVmSize,
0148         HeadingMemory,
0149         HeadingSharedMemory,
0150         HeadingStartTime,
0151         HeadingNoNewPrivileges,
0152         HeadingCommand,
0153         HeadingXMemory,
0154         HeadingXTitle,
0155         HeadingCGroup,
0156         HeadingMACContext,
0157         HeadingVmPSS,
0158         // This entry should always match the actual last entry in this enum + 1.
0159         // It is used to determine where plugin-provided headings start.
0160         HeadingPluginStart = HeadingVmPSS + 1,
0161     };
0162 
0163     enum { UidRole = Qt::UserRole, SortingValueRole, WindowIdRole, PlainValueRole, PercentageRole, PercentageHistoryRole };
0164 
0165     bool showTotals() const;
0166 
0167     /** When displaying memory sizes, this is the units it should be displayed in */
0168     enum Units { UnitsAuto, UnitsKB, UnitsMB, UnitsGB, UnitsTB, UnitsPB, UnitsPercentage };
0169     /** Set the units memory sizes etc should be displayed in */
0170     void setUnits(Units units);
0171     /** The units memory sizes etc should be displayed in */
0172     Units units() const;
0173     /** Set the I/O units sizes etc should be displayed in */
0174     void setIoUnits(Units units);
0175     /** The units I/O sizes etc should be displayed in */
0176     Units ioUnits() const;
0177 
0178     enum IoInformation { Bytes, Syscalls, ActualBytes, BytesRate, SyscallsRate, ActualBytesRate };
0179     /** Set the information to show in the Io Read and Io Write columns */
0180     void setIoInformation(IoInformation ioInformation);
0181     /** The information to show in the Io Read and Io Write columns */
0182     IoInformation ioInformation() const;
0183 
0184     /** Take an amount in kb, and return a string in the units set by setUnits() */
0185     QString formatMemoryInfo(qlonglong amountInKB, Units units, bool returnEmptyIfValueIsZero = false) const;
0186     /** Whether to show the command line options in the process name column */
0187     bool isShowCommandLineOptions() const;
0188     /** Set whether to show the command line options in the process name column */
0189     void setShowCommandLineOptions(bool showCommandLineOptions);
0190 
0191     /** Whether to show tooltips when the mouse hovers over a process */
0192     bool isShowingTooltips() const;
0193     /** Set whether to show tooltips when the mouse hovers over a process */
0194     void setShowingTooltips(bool showTooltips);
0195     /** Whether to divide CPU usage by the number of CPUs */
0196     bool isNormalizedCPUUsage() const;
0197     /** Set whether to divide CPU usage by the number of CPUs */
0198     void setNormalizedCPUUsage(bool normalizeCPUUsage);
0199 
0200     /** Retranslate the GUI, for when the system language changes */
0201     void retranslateUi();
0202 
0203 public Q_SLOTS:
0204     /** Whether to show the total cpu for the process plus all of its children */
0205     void setShowTotals(bool showTotals);
0206 
0207 private:
0208     ProcessModelPrivate *const d;
0209     friend class ProcessModelPrivate;
0210 };
0211 
0212 Q_DECLARE_METATYPE(QVector<ProcessModel::PercentageHistoryEntry>);
0213 Q_DECLARE_TYPEINFO(ProcessModel::PercentageHistoryEntry, Q_PRIMITIVE_TYPE);
0214 
0215 #endif