forked from epasveer/seer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QProcessInfoWidget.cpp
236 lines (171 loc) · 7.26 KB
/
QProcessInfoWidget.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "QProcessInfoWidget.h"
#include "QProcessInfo.h"
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QTreeWidgetItemIterator>
#include <QtWidgets/QApplication>
#include <QtGui/QFont>
#include <QtCore/QDebug>
#include <QRegularExpression>
//
// Custom tree item with numeric ordering on column 0.
//
class QProcessInfoWidgetItem : public QTreeWidgetItem {
public:
QProcessInfoWidgetItem(QTreeWidget* parent = 0) : QTreeWidgetItem(parent) {
}
private:
bool operator< (const QTreeWidgetItem& other) const {
int column = treeWidget()->sortColumn();
if (column == 0) {
return text(column).toLong() < other.text(column).toLong();
}
return text(column) < other.text(column);
}
};
//
// The main widget starts here.
//
QProcessInfoWidget::QProcessInfoWidget (QWidget* parent) : QWidget(parent) {
// Construct the UI.
setupUi(this);
// Setup the widgets
processTreeWidget->setMouseTracking(true);
processTreeWidget->resizeColumnToContents(0); // PID
processTreeWidget->resizeColumnToContents(1); // User name
processTreeWidget->resizeColumnToContents(2); // Path name
processTreeWidget->resizeColumnToContents(3); // Program name
//processTreeWidget->resizeColumnToContents(4); // Command line
processTreeWidget->setSortingEnabled(true);
processTreeWidget->clear();
systemProcessesCheckBox->setChecked(false);
// Connect things.
QObject::connect(programNameLineEdit, &QLineEdit::textChanged, this, &QProcessInfoWidget::refreshView);
QObject::connect(userNameLineEdit, &QLineEdit::textChanged, this, &QProcessInfoWidget::refreshView);
QObject::connect(systemProcessesCheckBox, &QCheckBox::clicked, this, &QProcessInfoWidget::refreshView);
QObject::connect(refreshToolButton, &QToolButton::clicked, this, &QProcessInfoWidget::refreshList);
QObject::connect(processTreeWidget, &QTreeWidget::itemDoubleClicked, this, &QProcessInfoWidget::handleDoubleClicked);
// Give focus to the program line edit.
programNameLineEdit->setFocus(Qt::OtherFocusReason);
// Load the initial process list.
refreshList();
}
QProcessInfoWidget::~QProcessInfoWidget () {
}
int QProcessInfoWidget::selectedPid () const {
QList<QTreeWidgetItem*> items = processTreeWidget->selectedItems();
if (items.size() == 0) {
return -1;
}
return items[0]->text(0).toLong();
}
QString QProcessInfoWidget::selectedUsername () const {
QList<QTreeWidgetItem*> items = processTreeWidget->selectedItems();
if (items.size() == 0) {
return "";
}
return items[0]->text(1);
}
QString QProcessInfoWidget::selectedName () const {
QList<QTreeWidgetItem*> items = processTreeWidget->selectedItems();
if (items.size() == 0) {
return "";
}
return items[0]->text(3);
}
QString QProcessInfoWidget::selectedFullname () const {
QList<QTreeWidgetItem*> items = processTreeWidget->selectedItems();
if (items.size() == 0) {
return "";
}
return items[0]->text(2) + "/" + items[0]->text(3);
}
QString QProcessInfoWidget::selectedCommandLine () const {
QList<QTreeWidgetItem*> items = processTreeWidget->selectedItems();
if (items.size() == 0) {
return "";
}
return items[0]->text(4);
}
void QProcessInfoWidget::refreshList () {
QApplication::setOverrideCursor(Qt::BusyCursor);
// Scan the /proc file system.
QProcessList list = QProcessInfo::populate();
// Loop through each entry and add it to our view.
processTreeWidget->clear();
for (const QProcessInfo& info : list) {
QTreeWidgetItem* item = new QProcessInfoWidgetItem;
item->setText(0, QString::number(info.pid()));
item->setText(1, info.username());
item->setText(2, info.path());
item->setText(3, info.name());
item->setText(4, info.commandLine());
processTreeWidget->addTopLevelItem(item);
}
// Adjust the column widths.
processTreeWidget->clearSelection();
processTreeWidget->resizeColumnToContents(0);
processTreeWidget->resizeColumnToContents(1);
processTreeWidget->resizeColumnToContents(2);
processTreeWidget->resizeColumnToContents(3);
//processTreeWidget->resizeColumnToContents(4);
// Don't clear the line edits.
// programNameLineEdit->clear();
// userNameLineEdit->clear();
refreshView();
QApplication::restoreOverrideCursor();
}
void QProcessInfoWidget::refreshView () {
// Get this list of program name matches. Or all if there is no program name provided.
QList<QTreeWidgetItem*> programNameMatches;
if (programNameLineEdit->text() == "") {
programNameMatches = processTreeWidget->findItems("*", Qt::MatchWildcard | Qt::MatchRecursive, 3);
}else{
if (programNameLineEdit->text().contains('*')) {
programNameMatches = processTreeWidget->findItems(programNameLineEdit->text(), Qt::MatchWildcard | Qt::MatchRecursive, 3);
}else{
programNameMatches = processTreeWidget->findItems(programNameLineEdit->text(), Qt::MatchStartsWith | Qt::MatchRecursive, 3);
}
}
// Get this list of user name matches. Or all if there is no user name provided.
QList<QTreeWidgetItem*> userNameMatches;
if (userNameLineEdit->text() == "") {
userNameMatches = processTreeWidget->findItems("*", Qt::MatchWildcard | Qt::MatchRecursive, 1);
}else{
if (userNameLineEdit->text().contains('*')) {
userNameMatches = processTreeWidget->findItems(userNameLineEdit->text(), Qt::MatchWildcard | Qt::MatchRecursive, 1);
}else{
userNameMatches = processTreeWidget->findItems(userNameLineEdit->text(), Qt::MatchStartsWith | Qt::MatchRecursive, 1);
}
}
// Get this list of process matches. Include one's with [xxx]) or not.
QList<QTreeWidgetItem*> processMatches;
if (systemProcessesCheckBox->isChecked() == true) {
processMatches = processTreeWidget->findItems("*", Qt::MatchWildcard | Qt::MatchRecursive, 3);
//qDebug() << "Checkbox is on." << processMatches.size();
}else{
// To find [xxx] processes : "^(\\[).*(\\])$"
// To exclude [xxx] processes: "^(?!\\[).*(?!\\])$"
processMatches = processTreeWidget->findItems("^(?!\\[).*(?!\\])$", Qt::MatchRegularExpression | Qt::MatchRecursive, 3);
//qDebug() << "Checkbox is off." << processMatches.size();
}
// Go through each item in the tree. If it's in the user, program, and process matches, show it.
// Otherwise, hide it.
QTreeWidgetItemIterator it(processTreeWidget);
while (*it) {
if (programNameMatches.contains(*it) && userNameMatches.contains(*it) && processMatches.contains(*it)) {
(*it)->setHidden(false);
}else{
(*it)->setHidden(true);
}
++it;
}
// Resize the columns.
processTreeWidget->resizeColumnToContents(0);
processTreeWidget->resizeColumnToContents(1);
processTreeWidget->resizeColumnToContents(2);
processTreeWidget->resizeColumnToContents(3);
//processTreeWidget->resizeColumnToContents(4);
}
void QProcessInfoWidget::handleDoubleClicked () {
emit pidSelected(selectedPid());
}