4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Milian Wolff <milian.wolff@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "processmodel.h"
27 using namespace GammaRay
;
29 bool operator<(const ProcData
&l
, const ProcData
&r
)
31 return l
.ppid
< r
.ppid
;
34 bool operator==(const ProcData
&l
, const ProcData
&r
)
36 return l
.ppid
== r
.ppid
;
39 QDebug
operator<<(QDebug d
, const ProcData
&data
) {
40 d
<< "ProcData{.ppid=" << data
.ppid
<< ", .name=" << data
.name
<< ", .image=" << data
.image
41 << ", .state=" << data
.state
<< ", .user=" << data
.user
<< ", .type=" << data
.type
<< "}";
45 ProcessModel::ProcessModel(QObject
*parent
)
46 : QAbstractTableModel(parent
)
50 ProcessModel::~ProcessModel()
54 void ProcessModel::setProcesses(const ProcDataList
&processes
)
58 // sort for merging to work properly
63 void ProcessModel::mergeProcesses(const ProcDataList
&processes
)
66 ProcDataList sortedProcesses
= processes
;
67 qStableSort(sortedProcesses
);
69 // iterator over m_data
72 foreach (const ProcData
&newProc
, sortedProcesses
) {
73 bool shouldInsert
= true;
74 while (i
< m_data
.count()) {
75 const ProcData
&oldProc
= m_data
.at(i
);
76 if (oldProc
< newProc
) {
77 // remove old proc, seems to be outdated
78 beginRemoveRows(QModelIndex(), i
, i
);
82 } else if (newProc
== oldProc
) {
83 // already contained, hence increment and break
87 } else { // newProc < oldProc
88 // new entry, break and insert it
93 beginInsertRows(QModelIndex(), i
, i
);
94 m_data
.insert(i
, newProc
);
96 // let i point to old element again
101 // make sure the new data is properly inserted
102 Q_ASSERT(m_data
== sortedProcesses
);
105 void ProcessModel::clear()
107 beginRemoveRows(QModelIndex(), 0, m_data
.count());
112 ProcData
ProcessModel::dataForIndex(const QModelIndex
&index
) const
114 return m_data
.at(index
.row());
117 ProcData
ProcessModel::dataForRow(int row
) const
119 return m_data
.at(row
);
122 QModelIndex
ProcessModel::indexForPid(const QString
&pid
) const
124 for (int i
= 0; i
< m_data
.size(); ++i
) {
125 if (m_data
.at(i
).ppid
== pid
) {
129 return QModelIndex();
132 QVariant
ProcessModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
134 if (role
!= Qt::DisplayRole
|| orientation
!= Qt::Horizontal
) {
138 if (section
== PIDColumn
) {
139 return tr("Process ID");
140 } else if (section
== NameColumn
) {
142 } else if (section
== StateColumn
) {
144 } else if (section
== UserColumn
) {
151 QVariant
ProcessModel::data(const QModelIndex
&index
, int role
) const
153 if (!index
.isValid()) {
157 const ProcData
&data
= m_data
.at(index
.row());
159 if (role
== Qt::DisplayRole
) {
160 if (index
.column() == PIDColumn
) {
162 } else if (index
.column() == NameColumn
) {
163 return data
.image
.isEmpty() ? data
.name
: data
.image
;
164 } else if (index
.column() == StateColumn
) {
166 } else if (index
.column() == UserColumn
) {
169 } else if (role
== PIDRole
) {
171 } else if (role
== NameRole
) {
172 return data
.image
.isEmpty() ? data
.name
: data
.image
;
173 } else if (role
== StateRole
) {
175 } else if (role
== UserRole
) {
182 int ProcessModel::columnCount(const QModelIndex
&parent
) const
184 return parent
.isValid() ? 0 : COLUMN_COUNT
;
187 int ProcessModel::rowCount(const QModelIndex
&parent
) const
189 return parent
.isValid() ? 0 : m_data
.count();
192 ProcDataList
ProcessModel::processes() const