-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
411 lines (332 loc) · 17.2 KB
/
Copy pathmainwindow.cpp
File metadata and controls
411 lines (332 loc) · 17.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QWidget>
#include <signal.h>
#include <QMouseEvent>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
qRegisterMetaType<SystemStats>("SystemStats"); // Register custom struct
setupUI();
// --- Threading Architecture ---
worker = new SystemWorker();
worker->moveToThread(&workerThread);
// When thread starts, tell worker to start polling
connect(&workerThread, &QThread::started, worker, &SystemWorker::startPolling);
// Connect worker's signal to UI update slot
connect(worker, &SystemWorker::statsUpdated, this, &MainWindow::updateUI);
// Clean up memory when thread quits
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
workerThread.start();
}
MainWindow::~MainWindow() {
workerThread.quit();
workerThread.wait();
}
// Multi-tabbed UI
void MainWindow::setupUI() {
this->setWindowTitle("System Core Monitor");
this->resize(450, 150);
this->setStyleSheet("QMainWindow { background-color: #1e1e2e; }"); // Dark theme
// Initialize tab widget
tabWidget = new QTabWidget(this);
tabWidget->setStyleSheet(
"QTabBar::tab { background: #313244; color: #cdd6f4; padding: 8px 20px; border: 1px solid #45475a; }"
"QTabBar::tab:selected { background: #45475a; font-weight: bold; border-bottom-color: #45475a; }"
"QTabWidget::pane { border: 1px solid #45475a; top: -1px; }"
"QWidget { background-color: #1e1e2e; }"
);
// =======================================
// TAB: CPU
// =======================================
QWidget* cpuTab = new QWidget();
QVBoxLayout* cpuLayout = new QVBoxLayout(cpuTab);
cpuLabel = new QLabel("CPU Load: Calculating...", this);
cpuLabel->setStyleSheet("color: #cdd6f4; font-size: 14px; font-weight: bold;");
cpuProgressBar = new QProgressBar(this);
cpuProgressBar->setRange(0, 100);
cpuProgressBar->setStyleSheet(
"QProgressBar { border: 1px solid #45475a; border-radius: 5px; background: #313244; color: white; text-align: center; }"
"QProgressBar::chunk { background-color: #89b4fa; border-radius: 4px; }" // Blue for CPU
);
// --- Process Table ---
cpuProcessTable = new QTableWidget(0, 3, this); // 3 Columns: PID, Name, CPU %
cpuProcessTable->setHorizontalHeaderLabels({"PID", "Process Name", "CPU (%)"});
cpuProcessTable->setSelectionBehavior(QAbstractItemView::SelectRows);
cpuProcessTable->setSelectionMode(QAbstractItemView::SingleSelection);
cpuProcessTable->horizontalHeader()->setStretchLastSection(true);
cpuProcessTable->verticalHeader()->setVisible(false);
cpuProcessTable->viewport()->installEventFilter(this);
cpuProcessTable->installEventFilter(this);
cpuProcessTable->setStyleSheet(
"QTableWidget { background-color: #1e1e2e; color: #cdd6f4; gridline-color: #45475a; border: 1px solid #45475a; }"
"QHeaderView::section { background-color: #313244; color: white; padding: 4px; border: 1px solid #45475a; font-weight: bold; }"
"QTableWidget::item:selected { background-color: #89b4fa; color: black; }"
);
// --- Kill Button ---
killCpuProcessButton = new QPushButton("Kill Process (CPU)", this);
killCpuProcessButton->setEnabled(false);
killCpuProcessButton->setStyleSheet(
"QPushButton { background-color: #f38ba8; color: #1e1e2e; font-weight: bold; padding: 8px; border-radius: 4px; }"
"QPushButton:hover { background-color: #ffb4c8; }"
"QPushButton:disabled { background-color: #45475a; color: #7f849c; }"
);
// Connect the kill process button to the kill process function through lambda bridging
connect(killCpuProcessButton, &QPushButton::clicked, this, [this]() {
killProcessFromTable(cpuProcessTable);
});
// Table row selection listener; if a row is selected, kill process button is enabled and vice versa
connect(cpuProcessTable, &QTableWidget::itemSelectionChanged, this, [this]() {
// check whether any row is highlighted
bool hasSelection = !cpuProcessTable->selectedItems().isEmpty();
killCpuProcessButton->setEnabled(hasSelection);
});
cpuLayout->addWidget(cpuLabel);
cpuLayout->addWidget(cpuProgressBar);
cpuLayout->addWidget(cpuProcessTable);
cpuLayout->addWidget(killCpuProcessButton);
cpuLayout->addStretch(); // Pushes UI to the top
// =======================================
// TAB: Memory
// =======================================
QWidget* ramTab = new QWidget();
QVBoxLayout* ramLayout = new QVBoxLayout(ramTab);
ramLabel = new QLabel("RAM Usage: Calculating...", this);
ramLabel->setStyleSheet("color: #cdd6f4; font-size: 14px; font-weight: bold;");
ramProgressBar = new QProgressBar(this);
ramProgressBar->setRange(0, 100);
ramProgressBar->setStyleSheet(
"QProgressBar { border: 1px solid #45475a; border-radius: 5px; background: #313244; color: white; text-align: center; }"
"QProgressBar::chunk { background-color: #a6e3a1; border-radius: 4px; }"
);
// --- Process Table ---
ramProcessTable = new QTableWidget(0, 3, this); // 0 rows, 3 columns
ramProcessTable->setHorizontalHeaderLabels({"PID", "Process Name", "Memory (MB)"});
ramProcessTable->setSelectionBehavior(QAbstractItemView::SelectRows); // Select entire row, not individual cells
ramProcessTable->setSelectionMode(QAbstractItemView::SingleSelection);
ramProcessTable->horizontalHeader()->setStretchLastSection(true); // Fills the remaining space
ramProcessTable->verticalHeader()->setVisible(false); // Hide the row numbers on the left
ramProcessTable->viewport()->installEventFilter(this);
ramProcessTable->installEventFilter(this);
ramProcessTable->setStyleSheet(
"QTableWidget { background-color: #1e1e2e; color: #cdd6f4; gridline-color: #45475a; border: 1px solid #45475a; }"
"QHeaderView::section { background-color: #313244; color: white; padding: 4px; border: 1px solid #45475a; font-weight: bold; }"
"QTableWidget::item:selected { background-color: #89b4fa; color: black; }"
);
// --- Kill Button ---
killRamProcessButton = new QPushButton("Kill Selected Process", this);
killRamProcessButton->setEnabled(false);
killRamProcessButton->setStyleSheet(
"QPushButton { background-color: #f38ba8; color: #1e1e2e; font-weight: bold; padding: 8px; border-radius: 4px; }"
"QPushButton:hover { background-color: #ffb4c8; }"
"QPushButton:disabled { background-color: #45475a; color: #7f849c; }"
);
connect(killRamProcessButton, &QPushButton::clicked, this, [this]() {
killProcessFromTable(ramProcessTable);
});
// Table row selection listener; if a row is selected, kill process button is enabled and vice versa
connect(ramProcessTable, &QTableWidget::itemSelectionChanged, this, [this]() {
bool hasSelection = !ramProcessTable->selectedItems().isEmpty();
killRamProcessButton->setEnabled(hasSelection);
});
ramLayout->addWidget(ramLabel);
ramLayout->addWidget(ramProgressBar);
ramLayout->addWidget(ramProcessTable);
ramLayout->addWidget(killRamProcessButton);
ramLayout->addStretch(); // Pushes UI to the top
// =======================================
// TAB: Network
// =======================================
QWidget* networkTab = new QWidget();
QVBoxLayout* networkLayout = new QVBoxLayout(networkTab);
networkLabel = new QLabel("Network: Calculating...", this);
networkLabel->setStyleSheet("color: #cdd6f4; font-size: 14px; font-weight: bold; margin-top: 10px;");
// --- Process Table ---
networkProcessTable = new QTableWidget(0, 4, this);
networkProcessTable->setHorizontalHeaderLabels({"PID", "Process Name", "Down (KB/s)", "Up (KB/s)"});
networkProcessTable->setSelectionBehavior(QAbstractItemView::SelectRows);
networkProcessTable->setSelectionMode(QAbstractItemView::SingleSelection);
networkProcessTable->horizontalHeader()->setStretchLastSection(true);
networkProcessTable->verticalHeader()->setVisible(false);
networkProcessTable->viewport()->installEventFilter(this);
networkProcessTable->installEventFilter(this);
networkProcessTable->setStyleSheet(
"QTableWidget { background-color: #1e1e2e; color: #cdd6f4; border: 1px solid #45475a; }"
"QHeaderView::section { background-color: #313244; color: white; padding: 4px; border: 1px solid #45475a; font-weight: bold; }"
"QTableWidget::item:selected { background-color: #89b4fa; color: black; }"
);
// --- Kill Button ---
killNetworkProcessButton = new QPushButton("Kill Process (Network)", this);
killNetworkProcessButton->setEnabled(false);
killNetworkProcessButton->setStyleSheet(
"QPushButton { background-color: #f38ba8; color: #1e1e2e; font-weight: bold; padding: 8px; border-radius: 4px; }"
"QPushButton:hover { background-color: #ffb4c8; }"
"QPushButton:disabled { background-color: #45475a; color: #7f849c; }"
);
connect(killNetworkProcessButton, &QPushButton::clicked, this, [this]() {
killProcessFromTable(networkProcessTable);
});
connect(networkProcessTable, &QTableWidget::itemSelectionChanged, this, [this]() {
bool hasSelection = !networkProcessTable->selectedItems().isEmpty();
killNetworkProcessButton->setEnabled(hasSelection);
});
networkLayout->addWidget(networkLabel);
networkLayout->addWidget(networkProcessTable);
networkLayout->addWidget(killNetworkProcessButton);
networkLayout->addStretch(); // Pushes UI to the top
// Adding all the created tabs to the TabWidget
tabWidget->addTab(cpuTab, "CPU");
tabWidget->addTab(ramTab, "Memory");
tabWidget->addTab(networkTab, "Network");
// Setting the TabWidget as the main view
this->setCentralWidget(tabWidget);
}
void MainWindow::updateUI(SystemStats stats) {
// Updating RAM Info
ramProgressBar->setValue(static_cast<int>(stats.ramUsagePercent));
ramLabel->setText(stats.ramLabelText);
// Dynamically change color based on usage
if (stats.ramUsagePercent > 85) {
ramProgressBar->setStyleSheet("QProgressBar::chunk { background-color: #f38ba8; }"); // Red
}
else if (stats.ramUsagePercent > 60) {
ramProgressBar->setStyleSheet("QProgressBar::chunk { background-color: #f9e2af; }"); // Yellow
}
else {
ramProgressBar->setStyleSheet("QProgressBar::chunk { background-color: #a6e3a1; }"); // Green
}
// Updating CPU Info
cpuProgressBar->setValue(static_cast<int>(stats.cpuUsagePercent));
cpuLabel->setText(stats.cpuLabelText);
if(stats.cpuUsagePercent > 85){
cpuProgressBar->setStyleSheet("QProgressBar::chunk { background-color: #f38ba8; }"); // Red
}
else if(stats.cpuUsagePercent > 60){
cpuProgressBar->setStyleSheet("QProgressbar::chunk { background-color: #f9e2af; }"); // Yellow
}
else{
cpuProgressBar->setStyleSheet("QProgressbar::chunk { background-color: #a6e3a1; }"); // Green
}
// Updating Network Info
networkLabel->setText(stats.networkLabelText);
// Updating RAM Process Table without flickering
ramProcessTable->setRowCount(stats.processList.size());
for (int i = 0; i < stats.processList.size(); ++i) {
const ProcessInfo &pInfo = stats.processList[i];
// Check if the cell already exists to avoid memory reallocation
QTableWidgetItem *pidItem = ramProcessTable->item(i, 0);
if (!pidItem) {
// First time drawing this row: create the items
ramProcessTable->setItem(i, 0, new QTableWidgetItem(QString::number(pInfo.pid)));
ramProcessTable->setItem(i, 1, new QTableWidgetItem(pInfo.name));
ramProcessTable->setItem(i, 2, new QTableWidgetItem(QString::number(pInfo.memoryMB)));
} else {
// Row exists: just update the text (much faster!)
pidItem->setText(QString::number(pInfo.pid));
ramProcessTable->item(i, 1)->setText(pInfo.name);
ramProcessTable->item(i, 2)->setText(QString::number(pInfo.memoryMB));
}
}
// Updating CPU Process Table
cpuProcessTable->setRowCount(stats.processList.size());
for (int i = 0; i < stats.processList.size(); ++i) {
const ProcessInfo &pInfo = stats.processList[i];
QTableWidgetItem *pidItem = cpuProcessTable->item(i, 0);
if (!pidItem) {
cpuProcessTable->setItem(i, 0, new QTableWidgetItem(QString::number(pInfo.pid)));
cpuProcessTable->setItem(i, 1, new QTableWidgetItem(pInfo.name));
cpuProcessTable->setItem(i, 2, new QTableWidgetItem(QString::number(pInfo.cpuUsage, 'f', 1)));
} else {
pidItem->setText(QString::number(pInfo.pid));
cpuProcessTable->item(i, 1)->setText(pInfo.name);
cpuProcessTable->item(i, 2)->setText(QString::number(pInfo.cpuUsage, 'f', 1));
}
}
// Updating Network Process Table
// --- Sort the list by Network Download internally ---
std::sort(stats.processList.begin(), stats.processList.end(),
[](const ProcessInfo& a, const ProcessInfo& b) {
return a.netDownKBs > b.netDownKBs;
});
// --- Update Network Process Table without index scrambling ---
networkProcessTable->setSortingEnabled(false);
networkProcessTable->setRowCount(stats.processList.size());
for (int i = 0; i < stats.processList.size(); ++i) {
const ProcessInfo &pInfo = stats.processList[i];
QTableWidgetItem *pidItem = networkProcessTable->item(i, 0);
if (!pidItem) {
networkProcessTable->setItem(i, 0, new QTableWidgetItem(QString::number(pInfo.pid)));
networkProcessTable->setItem(i, 1, new QTableWidgetItem(pInfo.name));
QTableWidgetItem *dlItem = new QTableWidgetItem();
dlItem->setData(Qt::EditRole, pInfo.netDownKBs);
networkProcessTable->setItem(i, 2, dlItem);
QTableWidgetItem *ulItem = new QTableWidgetItem();
ulItem->setData(Qt::EditRole, pInfo.netUpKBs);
networkProcessTable->setItem(i, 3, ulItem);
} else {
pidItem->setText(QString::number(pInfo.pid));
networkProcessTable->item(i, 1)->setText(pInfo.name);
networkProcessTable->item(i, 2)->setData(Qt::EditRole, pInfo.netDownKBs);
networkProcessTable->item(i, 3)->setData(Qt::EditRole, pInfo.netUpKBs);
}
}
}
void MainWindow::killProcessFromTable(QTableWidget* table)
{
// Finding out the highlighted row
int currentRow = table->currentRow();
if (currentRow >= 0) {
// Extract the text from the PID column (Column 0)
QTableWidgetItem *pidItem = table->item(currentRow, 0);
if (pidItem) {
// Convert the string to an integer
pid_t pid = pidItem->text().toInt();
// Fire the UNIX signal to terminate the app forcefully
kill(pid, SIGKILL);
}
}
}
// Capturing the mouse click outside the QTableWidget; on the main window and deselect the table row
void MainWindow::mousePressEvent(QMouseEvent *event) {
// qDebug() << "[UI] Main Window background clicked! Forcing global deselect.";
// 1. Clear visuals
cpuProcessTable->clearSelection();
ramProcessTable->clearSelection();
networkProcessTable->clearSelection();
// 2. Clear internal focus
cpuProcessTable->setCurrentItem(nullptr);
ramProcessTable->setCurrentItem(nullptr);
networkProcessTable->setCurrentItem(nullptr);
// Pass to base class
QMainWindow::mousePressEvent(event);
}
// Capturing and stealing away the mouse click on the table viewport's empty space and deselect the table row
bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
// // DEBUG: Tell us exactly what is being clicked!
// qDebug() << "[CLICK TRACE] Mouse hit object:" << obj->metaObject()->className();
QTableWidget *clickedTable = nullptr;
// Checking if user clicked the viewport OR the outer table frame
if (obj == cpuProcessTable || obj == cpuProcessTable->viewport()) {
clickedTable = cpuProcessTable;
} else if (obj == ramProcessTable || obj == ramProcessTable->viewport()) {
clickedTable = ramProcessTable;
} else if (obj == networkProcessTable || obj == networkProcessTable->viewport()) {
clickedTable = networkProcessTable;
}
if (clickedTable) {
// Mapping the coordinates to the viewport just in case they clicked the outer frame
QPoint clickPos = clickedTable->viewport()->mapFromGlobal(mouseEvent->globalPosition().toPoint());
QTableWidgetItem *item = clickedTable->itemAt(clickPos);
if (!item) {
// qDebug() << "[UI] Empty table space clicked! Forcing deselect.";
clickedTable->clearSelection();
clickedTable->setCurrentItem(nullptr);
return true;
}
// else {
// qDebug() << "[UI] Valid process clicked at row" << item->row();
// }
}
}
return QMainWindow::eventFilter(obj, event);
}