This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProcessItemModel.cpp
More file actions
168 lines (141 loc) · 5.2 KB
/
Copy pathProcessItemModel.cpp
File metadata and controls
168 lines (141 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2020 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "SessionSetup/ProcessItemModel.h"
#include <QFlags>
#include <algorithm>
#include <functional>
#include <iterator>
#include "GrpcProtos/process.pb.h"
#include "OrbitBase/Logging.h"
#include "OrbitBase/Sort.h"
#include "google/protobuf/util/message_differencer.h"
namespace orbit_session_setup {
using orbit_grpc_protos::ProcessInfo;
int ProcessItemModel::columnCount(const QModelIndex& parent) const {
return parent.isValid() ? 0 : static_cast<int>(Column::kEnd);
}
QVariant ProcessItemModel::data(const QModelIndex& idx, int role) const {
ORBIT_CHECK(idx.isValid());
ORBIT_CHECK(idx.model() == static_cast<const QAbstractItemModel*>(this));
ORBIT_CHECK(idx.row() >= 0 && idx.row() < static_cast<int>(processes_.size()));
ORBIT_CHECK(idx.column() >= 0 && idx.column() < static_cast<int>(Column::kEnd));
const auto& process = processes_[idx.row()];
if (role == Qt::UserRole) {
return QVariant::fromValue(&process);
}
if (role == Qt::DisplayRole) {
switch (static_cast<Column>(idx.column())) {
case Column::kPid:
return process.pid();
case Column::kName:
return QString::fromStdString(process.name());
case Column::kCpu:
return QString("%1 %").arg(process.cpu_usage(), 0, 'f', 1);
case Column::kEnd:
ORBIT_UNREACHABLE();
}
}
// When the EditRole is requested, we return the unformatted raw value, which
// means the CPU Usage is returned as a double.
if (role == Qt::EditRole) {
switch (static_cast<Column>(idx.column())) {
case Column::kPid:
return process.pid();
case Column::kName:
return QString::fromStdString(process.name());
case Column::kCpu:
return process.cpu_usage();
case Column::kEnd:
ORBIT_UNREACHABLE();
}
}
if (role == Qt::ToolTipRole) {
// We don't care about the column when showing tooltip. It's the same for
// the whole row.
return QString::fromStdString(process.command_line());
}
if (role == Qt::TextAlignmentRole) {
switch (static_cast<Column>(idx.column())) {
case Column::kPid:
return QVariant::fromValue(Qt::AlignVCenter | Qt::AlignRight);
case Column::kName:
return {};
case Column::kCpu:
return QVariant::fromValue(Qt::AlignVCenter | Qt::AlignRight);
case Column::kEnd:
ORBIT_UNREACHABLE();
}
}
return {};
}
QVariant ProcessItemModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation == Qt::Vertical) {
return {};
}
if (role == Qt::DisplayRole) {
switch (static_cast<Column>(section)) {
case Column::kPid:
return "PID";
case Column::kName:
return "Name";
case Column::kCpu:
return "CPU %";
case Column::kEnd:
ORBIT_UNREACHABLE();
}
}
return {};
}
QModelIndex ProcessItemModel::index(int row, int column, const QModelIndex& parent) const {
if (parent.isValid()) return {};
if (row < 0 || row >= static_cast<int>(processes_.size())) return {};
if (column < 0 || column >= static_cast<int>(Column::kEnd)) return {};
return createIndex(row, column);
}
QModelIndex ProcessItemModel::parent(const QModelIndex& /*parent*/) const { return {}; }
int ProcessItemModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) {
return 0;
}
return processes_.size();
}
void ProcessItemModel::SetProcesses(QVector<ProcessInfo> new_processes) {
orbit_base::sort(new_processes.begin(), new_processes.end(), &ProcessInfo::pid);
auto* old_iter = processes_.begin();
auto* new_iter = new_processes.begin();
while (old_iter != processes_.end() && new_iter != new_processes.end()) {
const int current_row = static_cast<int>(std::distance(processes_.begin(), old_iter));
if (old_iter->pid() == new_iter->pid()) {
if (!google::protobuf::util::MessageDifferencer::Equivalent(*old_iter, *new_iter)) {
*old_iter = *new_iter;
emit dataChanged(index(current_row, 0, {}),
index(current_row, static_cast<int>(Column::kEnd) - 1, {}));
}
++old_iter;
++new_iter;
} else if (old_iter->pid() < new_iter->pid()) {
beginRemoveRows({}, current_row, current_row);
old_iter = processes_.erase(old_iter);
endRemoveRows();
} else {
beginInsertRows({}, current_row, current_row);
old_iter = processes_.insert(old_iter, *new_iter);
++old_iter;
++new_iter;
endInsertRows();
}
}
if (old_iter == processes_.end() && new_iter != new_processes.end()) {
beginInsertRows({}, processes_.size(), new_processes.size() - 1);
std::copy(new_iter, new_processes.end(), std::back_inserter(processes_));
ORBIT_CHECK(processes_.size() == new_processes.size());
endInsertRows();
} else if (old_iter != processes_.end() && new_iter == new_processes.end()) {
beginRemoveRows({}, new_processes.size(), processes_.size() - 1);
processes_.erase(old_iter, processes_.end());
ORBIT_CHECK(processes_.size() == new_processes.size());
endRemoveRows();
}
}
} // namespace orbit_session_setup