Changeset View
Changeset View
Standalone View
Standalone View
processcore/process_attribute_model.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2020 David Edmundson <davidedmundson@kde.org> | ||||
3 | Copyright (c) 2020 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "process_attribute_model.h" | ||||
22 | | ||||
23 | #include "extended_process_list.h" | ||||
24 | #include "process_attribute.h" | ||||
25 | | ||||
26 | using namespace KSysGuard; | ||||
27 | | ||||
28 | class Q_DECL_HIDDEN ProcessAttributeModel::Private | ||||
29 | { | ||||
30 | public: | ||||
31 | QVector<ProcessAttribute *> m_attributes; | ||||
32 | }; | ||||
33 | | ||||
34 | ProcessAttributeModel::ProcessAttributeModel(ExtendedProcesses *processes, QObject *parent) | ||||
35 | : QAbstractListModel(parent) | ||||
36 | , d(new Private) | ||||
37 | { | ||||
38 | d->m_attributes = processes->attributes(); | ||||
39 | } | ||||
40 | | ||||
41 | ProcessAttributeModel::~ProcessAttributeModel() | ||||
42 | { | ||||
43 | } | ||||
44 | | ||||
45 | int ProcessAttributeModel::rowCount(const QModelIndex &parent) const | ||||
46 | { | ||||
47 | if (parent.isValid()) { | ||||
48 | return 0; // flat list | ||||
49 | } | ||||
50 | return d->m_attributes.count(); | ||||
51 | } | ||||
52 | | ||||
53 | QVariant ProcessAttributeModel::data(const QModelIndex &index, int role) const | ||||
54 | { | ||||
55 | if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { | ||||
56 | return QVariant(); | ||||
57 | } | ||||
58 | | ||||
59 | auto attribute = d->m_attributes[index.row()]; | ||||
60 | switch (static_cast<Role>(role)) { | ||||
61 | case Role::Name: | ||||
62 | return attribute->name(); | ||||
63 | case Role::ShortName: | ||||
64 | if (attribute->shortName().isEmpty()) { | ||||
65 | return attribute->name(); | ||||
66 | } | ||||
67 | return attribute->shortName(); | ||||
68 | case Role::Id: | ||||
69 | return attribute->id(); | ||||
70 | case Role::Description: | ||||
71 | return attribute->description(); | ||||
72 | case Role::Unit: | ||||
73 | return attribute->unit(); | ||||
74 | } | ||||
75 | return QVariant(); | ||||
76 | } | ||||
77 | | ||||
78 | QHash<int, QByteArray> ProcessAttributeModel::roleNames() const | ||||
79 | { | ||||
80 | return QAbstractListModel::roleNames().unite({ | ||||
81 | { static_cast<int>(Role::Id), "id" }, | ||||
82 | { static_cast<int>(Role::Name), "name" }, | ||||
83 | { static_cast<int>(Role::ShortName), "shortName" }, | ||||
84 | { static_cast<int>(Role::Description), "description" }, | ||||
85 | { static_cast<int>(Role::Unit), "unit" }, | ||||
86 | }); | ||||
87 | } |