-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTreeWidget.cpp
More file actions
193 lines (171 loc) · 6.64 KB
/
Copy pathCustomTreeWidget.cpp
File metadata and controls
193 lines (171 loc) · 6.64 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
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
#include "CustomTreeWidget.h"
#include "PropertiesWidget.h"
#include "OpenFiles.h"
#include "process.h"
#include <QMenu>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QHeaderView>
#include <QDebug>
extern int g_num_pids;
extern "C" {
int* get_all_processes();
int* get_my_processes();
int* get_active_processes();
char* get_process_name(int pid);
char* get_status(int pid);
char* get_cpu_info(int pid);
char* get_memory(int pid);
int kill_process(int pid);
int* get_children_pids(int parent_pid, int &num_children);
}
CustomTreeWidget::CustomTreeWidget(QWidget *parent)
: QWidget(parent), treeView(new QTreeView(this)), model(new QStandardItemModel(this)) {
setupModelAndView();
populateModelWithProcessData();
}
void CustomTreeWidget::setupModelAndView() {
QStringList headers = {"Process Name", "Status", "% CPU", "ID", "Memory"};
model->setHorizontalHeaderLabels(headers);
treeView->setModel(model);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(treeView);
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
treeView->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
treeView->header()->setStretchLastSection(true);
treeView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void CustomTreeWidget::populateModelWithProcessData() {
int * pids = get_all_processes();
if (pids != NULL) {
for (int i = 0; i < g_num_pids; ++i) {
QList<QStandardItem*> rowItems = createRowItems(pids[i]);
if (rowItems.isEmpty()) {
continue;
}
model->appendRow(rowItems);
populateChildren(rowItems.first(), pids[i]);
}
free(pids);
pids = NULL;
}
}
void CustomTreeWidget::populateChildren(QStandardItem *parentItem, int pid) {
int num_children = 0;
int *childPids = get_children_pids(pid, num_children);
if (childPids != NULL) {
for (int i = 0; i < num_children; ++i) {
QList<QStandardItem*> childRowItems = createRowItems(childPids[i]);
if (childRowItems.isEmpty()) {
continue;
}
parentItem->appendRow(childRowItems);
populateChildren(childRowItems.first(), childPids[i]);
}
free(childPids);
}
}
QList<QStandardItem*> CustomTreeWidget::createRowItems(int pid) {
char *process_name = get_process_name(pid);
if (process_name == NULL) {
free(process_name);
return QList<QStandardItem*>();
}
QString qprocess_name = process_name ? QString::fromUtf8(process_name) : QString();
free(process_name);
char *process_status = get_status(pid);
QString qprocess_status = process_status ? QString::fromUtf8(process_status) : QString();
free(process_status);
char *process_cpu_info = get_cpu_info(pid);
QString qprocess_cpu_info = process_cpu_info ? QString::fromUtf8(process_cpu_info) : QString();
free(process_cpu_info);
char *process_memory = get_memory(pid);
QString qprocess_memory = process_memory ? QString::fromUtf8(process_memory) : QString();
free(process_memory);
return {
new QStandardItem(qprocess_name),
new QStandardItem(qprocess_status),
new QStandardItem(qprocess_cpu_info),
new QStandardItem(QString::number(pid)),
new QStandardItem(qprocess_memory)
};
}
void CustomTreeWidget::refreshData(ProcessFilter filter) {
// Remove all rows from the model
while (model->rowCount() > 0) {
model->removeRow(0);
}
// Optionally, you can reset the headers here if needed
QStringList headers = {"Process Name", "Status", "% CPU", "ID", "Memory"};
model->setHorizontalHeaderLabels(headers);
int *pids = NULL;
switch (filter) {
case ProcessFilter::AllProcesses:
pids = get_all_processes();
break;
case ProcessFilter::MyProcesses:
pids = get_my_processes();
break;
case ProcessFilter::ActiveProcesses:
pids = get_active_processes();
break;
default:
pids = get_all_processes();
break;
}
if (pids != NULL) {
for (int i = 0; i < g_num_pids; ++i) {
QList<QStandardItem*> rowItems = createRowItems(pids[i]);
if (rowItems.isEmpty()) {
continue;
}
model->appendRow(rowItems);
populateChildren(rowItems.first(), pids[i]);
}
free(pids);
}
}
void CustomTreeWidget::contextMenuEvent(QContextMenuEvent *event) {
QMenu contextMenu(this);
QAction *stopProcess = contextMenu.addAction("Stop Process");
QAction *continueProcess = contextMenu.addAction("Continue Process");
QAction *killProcess = contextMenu.addAction("Kill Process");
QAction *memoryMaps = contextMenu.addAction("Memory Maps");
QAction *openFiles = contextMenu.addAction("Open Files");
QAction *propertiesAction = contextMenu.addAction("See Properties");
QAction *selectedAction = contextMenu.exec(event->globalPos());
QModelIndexList selectedIndexes = treeView->selectionModel()->selectedRows();
if (selectedIndexes.isEmpty()) {
return; // No item is selected
}
QModelIndex selectedIndex = selectedIndexes.first();
// qDebug() << "Selected Index - Row:" << selectedIndex.row() << "Column:" << selectedIndex.column();
// Assuming PID is stored as an integer
int pid = selectedIndex.data().toInt();
// qDebug() << "PID:" << pid;
QString success;
QString error;
// int pid = 0;
if (selectedAction == propertiesAction) {
emit propertiesSelected(pid);
} else if (selectedAction == memoryMaps) {
emit memoryMapSelected(pid);
} else if (selectedAction == killProcess) {
int ret = kill_process(pid);
success = "Process %1 has been killed.";
error = "Failed to kill process: %1";
QMessageBox::information(this, "Process Action", ret ? success.arg(pid) : error.arg(pid));
} else if (selectedAction == continueProcess) {
int ret = continue_process(pid);
success = "Process %1 has been continued.";
error = "Failed to resume process: %1";
QMessageBox::information(this, "Process Action", ret ? success.arg(pid) : error.arg(pid));
} else if (selectedAction == stopProcess) {
int ret = stop_process(pid);
success = "Process %1 has been stopped.";
error = "Failed to stop process: %1";
QMessageBox::information(this, "Process Action", ret ? success.arg(pid) : error.arg(pid));
} else if (selectedAction == openFiles) {
emit openFilesSelected(pid);
}
}