Imported Upstream version 1.1.0
[gammaray-debian.git] / launcher / processmodel.cpp
blob59c0e2c4ee4708ac2a709a3fa87a5bd7f25c11fb
1 /*
2 processmodel.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
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"
25 #include <QDebug>
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 << "}";
42 return d;
45 ProcessModel::ProcessModel(QObject *parent)
46 : QAbstractTableModel(parent)
50 ProcessModel::~ProcessModel()
54 void ProcessModel::setProcesses(const ProcDataList &processes)
56 beginResetModel();
57 m_data = processes;
58 // sort for merging to work properly
59 qStableSort(m_data);
60 endResetModel();
63 void ProcessModel::mergeProcesses(const ProcDataList &processes)
65 // sort like m_data
66 ProcDataList sortedProcesses = processes;
67 qStableSort(sortedProcesses);
69 // iterator over m_data
70 int i = 0;
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);
79 m_data.removeAt(i);
80 endRemoveRows();
81 continue;
82 } else if (newProc == oldProc) {
83 // already contained, hence increment and break
84 ++i;
85 shouldInsert = false;
86 break;
87 } else { // newProc < oldProc
88 // new entry, break and insert it
89 break;
92 if (shouldInsert) {
93 beginInsertRows(QModelIndex(), i, i);
94 m_data.insert(i, newProc);
95 endInsertRows();
96 // let i point to old element again
97 ++i;
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());
108 m_data.clear();
109 endRemoveRows();
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) {
126 return index(i, 0);
129 return QModelIndex();
132 QVariant ProcessModel::headerData(int section, Qt::Orientation orientation, int role) const
134 if (role != Qt::DisplayRole || orientation != Qt::Horizontal) {
135 return QVariant();
138 if (section == PIDColumn) {
139 return tr("Process ID");
140 } else if (section == NameColumn) {
141 return tr("Name");
142 } else if (section == StateColumn) {
143 return tr("State");
144 } else if (section == UserColumn) {
145 return tr("User");
148 return QVariant();
151 QVariant ProcessModel::data(const QModelIndex &index, int role) const
153 if (!index.isValid()) {
154 return QVariant();
157 const ProcData &data = m_data.at(index.row());
159 if (role == Qt::DisplayRole) {
160 if (index.column() == PIDColumn) {
161 return data.ppid;
162 } else if (index.column() == NameColumn) {
163 return data.image.isEmpty() ? data.name : data.image;
164 } else if (index.column() == StateColumn) {
165 return data.state;
166 } else if (index.column() == UserColumn) {
167 return data.user;
169 } else if (role == PIDRole) {
170 return data.ppid;
171 } else if (role == NameRole) {
172 return data.image.isEmpty() ? data.name : data.image;
173 } else if (role == StateRole) {
174 return data.state;
175 } else if (role == UserRole) {
176 return data.user;
179 return QVariant();
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
194 return m_data;