-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
623 lines (557 loc) · 19.1 KB
/
mainwindow.cpp
File metadata and controls
623 lines (557 loc) · 19.1 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* OpenSpeedy - Open Source Game Speed Controller
* Copyright (C) 2025 Game1024
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "./ui_mainwindow.h"
#include "mainwindow.h"
#include "themeutils.h"
#include <thread>
#include <QCloseEvent>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QMessageBox>
#include <QScreen>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QScreen *screen = QGuiApplication::primaryScreen();
if (screen != nullptr)
{
qreal dpiScale = screen->devicePixelRatio();
QRect screenGeometry = screen->availableGeometry();
int windowWidth = 1024;
int windowHeight = 768;
if (dpiScale >= 2)
{
windowWidth = static_cast<int>(1024 / 1.5);
windowHeight = static_cast<int>(768 / 1.5);
}
windowWidth = qMin(windowWidth, screenGeometry.width()-100);
windowHeight = qMin(windowHeight, screenGeometry.height()-100);
this->resize(windowWidth, windowHeight);
int x = (screenGeometry.width() - this->frameGeometry().width()) / 2 ;
int y = (screenGeometry.height() - this->frameGeometry().height()) /2;
this->move(x, y);
}
init();
}
MainWindow::~MainWindow()
{
QApplication::instance()->removeNativeEventFilter(this);
m_thread->quit();
m_thread->wait();
delete m_settings;
delete m_aboutDlg;
delete m_preferenceDlg;
delete m_processMonitor;
delete m_cpu;
delete m_mem;
delete ui;
}
void MainWindow::recreate()
{
// step1: 获取主屏幕的缩放比例
// step2: 根据缩放比例调整窗口大小
// step3: 确保窗口不会超出屏幕范围
QScreen* primaryScreen = QApplication::primaryScreen();
qreal dpiScale = primaryScreen->devicePixelRatio();
QRect screenGeometry = primaryScreen->availableGeometry();
int windowWidth = 1024;
int windowHeight = 768;
if (dpiScale >= 2)
{
windowWidth = static_cast<int>(1024 / 1.5);
windowHeight = static_cast<int>(768 / 1.5);
}
windowWidth = qMin(windowWidth, screenGeometry.width()-100);
windowHeight = qMin(windowHeight, screenGeometry.height()-100);
this->resize(windowWidth, windowHeight);
layout()->invalidate();
layout()->activate();
//adjustSize();
recreateTray();
}
void MainWindow::refresh()
{
// 获取当前主题下的高亮文本颜色
QColor highlightColor = QApplication::palette().color(QPalette::Highlight);
// 如果是深色模式,使用亮蓝色;如果是浅色模式,使用深蓝色
QString colorStr = (QApplication::palette().color(QPalette::Window).value() < 128) ? "lightblue" : "blue";
ui->cpuContent->setText(QString("<span style='color:%1'>%2%</span>")
.arg(colorStr)
.arg(m_cpu->getUsage(), 5, 'f', 1, ' '));
double memUsage = m_mem->getUsage();
double memTotal = m_mem->getTotal();
ui->memContent->setText(
QString("<span style='color:%1'>(%2G / %3G) %4%</span>")
.arg(colorStr)
.arg(memUsage, 0, 'f', 1)
.arg(memTotal, 0, 'f', 1)
.arg(memUsage / memTotal * 100, 4, 'f', 1));
}
void MainWindow::on_sliderCtrl_valueChanged(int value)
{
double factor = speedFactor(value);
m_processMonitor->changeSpeed(factor);
if (factor >= 1.0)
{
ui->sliderCtrl->setToolTip(QString(tr("%1倍")).arg(factor, 0, 'f', 2));
ui->sliderLabel->setText(QString(tr("✖️%1倍")).arg(factor, 0, 'f', 2));
}
else
{
ui->sliderCtrl->setToolTip(QString(tr("%1倍")).arg(factor, 0, 'f'));
ui->sliderLabel->setText(QString(tr("✖️%1倍")).arg(factor, 0, 'f'));
}
ui->sliderInputSpinBox->setValue(factor);
m_settings->setValue(CONFIG_SLIDERVALUE_KEY, value);
m_settings->sync();
}
void MainWindow::on_sliderInputSpinBox_editingFinished()
{
double factor = ui->sliderInputSpinBox->value();
ui->sliderInputSpinBox->clearFocus();
ui->sliderCtrl->setValue(sliderValue(factor));
}
void MainWindow::on_processNameFilter_textChanged(const QString &text)
{
m_processMonitor->setFilter(text);
}
void MainWindow::on_sliderLabel_clicked()
{
if (ui->sliderCtrl->value() != 0)
{
m_back = ui->sliderCtrl->value();
ui->sliderCtrl->setValue(0);
}
else
{
ui->sliderCtrl->setValue(m_back);
}
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
{
switch (reason)
{
case QSystemTrayIcon::Trigger: // 单击
if (isVisible())
hide();
else
{
show();
showNormal();
activateWindow();
}
break;
case QSystemTrayIcon::DoubleClick: // 双击
show();
showNormal();
activateWindow();
break;
default:
break;
}
}
}
void MainWindow::createTray()
{
// 创建系统托盘图标
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(":/icons/images/icon.ico"));
trayIcon->setToolTip("OpenSpeedy");
// 创建托盘菜单、创建动作
trayMenu = new QMenu(this);
showAction = new QAction(tr("显示"), this);
hideAction = new QAction(tr("隐藏"), this);
quitAction = new QAction(tr("退出"), this);
// 连接信号和槽
connect(showAction, &QAction::triggered, this,
[&]
{
showNormal();
activateWindow();
});
connect(hideAction, &QAction::triggered, this, &MainWindow::hide);
connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
// 添加动作到菜单
trayMenu->addAction(showAction);
trayMenu->addAction(hideAction);
trayMenu->addSeparator();
trayMenu->addAction(quitAction);
// 设置托盘图标的菜单
trayIcon->setContextMenu(trayMenu);
// 处理托盘图标的点击事件
connect(trayIcon, &QSystemTrayIcon::activated, this,
&MainWindow::iconActivated);
// 显示托盘图标
trayIcon->show();
}
double MainWindow::speedFactor(int sliderValue)
{
double factor = 1.0;
if (sliderValue > 0.0)
{
factor = 1.0 + (double)sliderValue / 100;
}
else if (sliderValue < 0.0)
{
factor = 1.0 + (double)sliderValue / 100000;
}
else
{
factor = 1.0;
}
return factor;
}
int MainWindow::sliderValue(double speedFactor)
{
int sliderValue = 0;
if (speedFactor > 1.0)
{
sliderValue = (long long)(speedFactor * 100) - 100;
}
else if (speedFactor < 1.0)
{
sliderValue = (long long)(speedFactor * 100000) - 100000;
}
else
{
sliderValue = 0;
}
qDebug() << sliderValue;
return sliderValue;
}
void MainWindow::recreateTray()
{
qDebug() << "重绘托盘";
delete trayMenu;
delete trayIcon;
delete showAction;
delete hideAction;
delete quitAction;
createTray();
adjustSize();
}
void MainWindow::init()
{
m_back = 0;
m_settings =
new QSettings(QCoreApplication::applicationDirPath() + "/config.ini",
QSettings::IniFormat);
m_aboutDlg = new AboutDialog(this);
m_preferenceDlg =
new PreferenceDialog((HWND)winId(), m_settings, ui->increaseSpeedLabel,
ui->decreaseSpeedLabel, ui->resetSpeedLabel, this);
// 安装本地事件过滤器以处理全局快捷键
QApplication::instance()->installNativeEventFilter(this);
m_processMonitor = new ProcessMonitor(
m_settings, ui->processMonitorWidget, ui->processMonitorLabel,
ui->injector32Status, ui->injector64Status, nullptr);
m_thread = new QThread(this);
connect(m_thread, &QThread::started, m_processMonitor,
&ProcessMonitor::start);
connect(m_thread, &QThread::finished, m_processMonitor,
&QObject::deleteLater);
connect(m_thread, &QThread::finished, m_thread, &QThread::deleteLater);
connect(QGuiApplication::primaryScreen(),
&QScreen::logicalDotsPerInchChanged, this, &MainWindow::recreate);
m_thread->start();
m_processMonitor->start();
createTray();
QApplication::setQuitOnLastWindowClosed(false);
ui->osContent->setText(winutils::getWindowsVersion());
m_cpu = new CpuUtils();
m_cpu->init();
m_mem = new MemUtils();
m_mem->init();
m_timer = new QTimer();
refresh();
connect(m_timer, &QTimer::timeout, this, &MainWindow::refresh);
m_timer->start(1000);
/* 读取slider值 */
int value = qBound(ui->sliderCtrl->minimum(),
m_settings->value(CONFIG_SLIDERVALUE_KEY, 0).toInt(),
ui->sliderCtrl->maximum());
ui->sliderCtrl->setValue(value);
if (winutils::isAutoStartEnabled(QApplication::applicationName()))
{
ui->autoStartCheckBox->setCheckState(Qt::Checked);
}
else
{
ui->autoStartCheckBox->setCheckState(Qt::Unchecked);
}
/* 首选项菜单 */
connect(ui->menuPreference, &QMenu::aboutToShow,
[this]
{
ui->menuPreference->hide();
QTimer::singleShot(50,
[this]()
{
m_preferenceDlg->show();
m_preferenceDlg->activateWindow();
m_preferenceDlg->raise();
});
});
/* 关于菜单 */
connect(ui->menuAbout, &QMenu::aboutToShow,
[this]
{
ui->menuAbout->hide();
QTimer::singleShot(50,
[this]()
{
m_aboutDlg->show();
m_aboutDlg->activateWindow();
m_aboutDlg->raise();
});
});
m_languageGroup = new QActionGroup(this);
m_languageGroup->setExclusive(true);
m_languageGroup->setEnabled(true);
m_languageGroup->addAction(ui->actionCN);
m_languageGroup->addAction(ui->actionTW);
m_languageGroup->addAction(ui->actionEN);
QString language =
m_settings->value(CONFIG_LANGUAGE, QLocale().system().name())
.toString();
if (language == "zh_CN")
{
ui->actionCN->setChecked(true);
}
else if (language == "zh_TW")
{
ui->actionTW->setChecked(true);
}
else if (language == "en_US")
{
ui->actionEN->setChecked(true);
}
connect(ui->actionCN, &QAction::triggered,
[this]
{
m_settings->setValue(CONFIG_LANGUAGE, "zh_CN");
QMessageBox msgBox(this);
msgBox.setWindowFlags(Qt::Dialog | Qt::WindowTitleHint |
Qt::CustomizeWindowHint);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("直到重启应用后,界面的语言才会生效"));
msgBox.exec();
});
connect(ui->actionTW, &QAction::triggered,
[this]
{
m_settings->setValue(CONFIG_LANGUAGE, "zh_TW");
QMessageBox msgBox(this);
msgBox.setWindowFlags(Qt::Dialog | Qt::WindowTitleHint |
Qt::CustomizeWindowHint);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("直到重启应用后,界面的语言才会生效"));
msgBox.exec();
});
connect(ui->actionEN, &QAction::triggered,
[this]
{
m_settings->setValue(CONFIG_LANGUAGE, "en_US");
QMessageBox msgBox(this);
msgBox.setWindowFlags(Qt::Dialog | Qt::WindowTitleHint |
Qt::CustomizeWindowHint);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("直到重启应用后,界面的语言才会生效"));
msgBox.exec();
});
m_themeGroup = new QActionGroup(this);
m_themeGroup->setExclusive(true);
m_themeGroup->setEnabled(true);
m_themeGroup->addAction(ui->actionLightTheme);
m_themeGroup->addAction(ui->actionDarkTheme);
int theme = m_settings->value(CONFIG_THEME, ThemeUtils::Light).toInt();
if (theme == ThemeUtils::Light)
{
ui->actionLightTheme->setChecked(true);
}
else
{
ui->actionDarkTheme->setChecked(true);
}
connect(ui->actionLightTheme, &QAction::triggered,
[this]
{
m_settings->setValue(CONFIG_THEME, ThemeUtils::Light);
ThemeUtils::applyTheme(ThemeUtils::Light);
refresh(); // 刷新界面以更新颜色
});
connect(ui->actionDarkTheme, &QAction::triggered,
[this]
{
m_settings->setValue(CONFIG_THEME, ThemeUtils::Dark);
ThemeUtils::applyTheme(ThemeUtils::Dark);
refresh(); // 刷新界面以更新颜色
});
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// 如果托盘图标可见,则隐藏窗口而不是关闭
if (trayIcon->isVisible())
{
hide();
// 阻止事件继续传播,防止应用关闭
event->ignore();
}
else
{
// 如果没有托盘图标,则正常关闭
event->accept();
}
}
bool MainWindow::nativeEventFilter(const QByteArray &eventType,
void *message,
long *result)
{
Q_UNUSED(result)
if (eventType == "windows_generic_MSG" ||
eventType == "windows_dispatcher_MSG")
{
MSG *msg = static_cast<MSG *>(message);
if (msg->message == WM_HOTKEY)
{
static qint64 lastSoundTime = 0;
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
bool canPlaySound = (currentTime - lastSoundTime > 500);
int hotkeyId = msg->wParam;
switch (hotkeyId)
{
case HOTKEY_INCREASE_SPEED:
{
int currentValue = ui->sliderCtrl->value();
if (currentValue < ui->sliderCtrl->maximum())
{
if (canPlaySound)
{
std::thread([]() { Beep(400, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
currentValue + m_preferenceDlg->getIncreaseStep());
qDebug() << "全局快捷键: 增加速度到"
<< speedFactor(currentValue +
m_preferenceDlg->getIncreaseStep());
}
}
break;
case HOTKEY_DECREASE_SPEED:
{
int currentValue = ui->sliderCtrl->value();
if (currentValue > ui->sliderCtrl->minimum())
{
if (canPlaySound)
{
std::thread([]() { Beep(400, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
currentValue - m_preferenceDlg->getDecreaseStep());
qDebug() << "全局快捷键: 降低速度到"
<< speedFactor(currentValue -
m_preferenceDlg->getDecreaseStep());
}
}
break;
case HOTKEY_RESET_SPEED:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(0);
qDebug() << "全局快捷键: 重置速度";
break;
case HOTKEY_SHIFT1:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
sliderValue(m_preferenceDlg->getShift1()));
break;
case HOTKEY_SHIFT2:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
sliderValue(m_preferenceDlg->getShift2()));
break;
case HOTKEY_SHIFT3:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
sliderValue(m_preferenceDlg->getShift3()));
break;
case HOTKEY_SHIFT4:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
sliderValue(m_preferenceDlg->getShift4()));
break;
case HOTKEY_SHIFT5:
if (canPlaySound)
{
std::thread([]() { Beep(1600, 5); }).detach();
lastSoundTime = currentTime;
}
ui->sliderCtrl->setValue(
sliderValue(m_preferenceDlg->getShift5()));
break;
}
return true; // 事件已处理
}
}
return false; // 让其他过滤器处理
}
void MainWindow::on_autoStartCheckBox_stateChanged(int state)
{
QString execFilePath =
QDir::toNativeSeparators(QApplication::applicationFilePath());
if (state == Qt::Checked)
{
qDebug() << QApplication::applicationName()
<< QApplication::applicationFilePath();
winutils::setAutoStart(true, QApplication::applicationName(),
execFilePath);
}
else
{
winutils::setAutoStart(false, QApplication::applicationName(),
execFilePath);
}
}