forked from RunnanZhang/MagicAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicAssistant.cpp
More file actions
582 lines (474 loc) · 16.2 KB
/
MagicAssistant.cpp
File metadata and controls
582 lines (474 loc) · 16.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
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
#include "MagicAssistant.h"
#include "ui_MagicAssistant.h"
#include "ToolBar.h"
#include "defines.h"
#include "NBAAssistant.h"
#include "Settings.h"
#include "InfoBoard.h"
#include <QPainter>
#include <QMouseEvent>
#include <QMessageBox>
#include <QTimer>
#include <QMenu>
#include <QSettings>
#include <QDir>
#include <QActionGroup>
#include <QDesktopWidget>
#include <QTimer>
#include <QDebug>
#include <QProcess>
#include <QInputDialog>
#include <QDateTime>
#include <QDesktopWidget>
#include <QScreen>
#include <QTime>
#include <QQuickView>
#include <QQmlEngine>
#include <QQmlContext>
#include <windows.h>
MagicAssistant::MagicAssistant(QWidget *parent) :
QWidget(parent),
ui(new Ui::MagicAssistant)
{
ui->setupUi(this);
_pixmap.load(":/images/james.png");
_toolbar = new ToolBar;
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground);
#ifdef Q_OS_MAC
setAttribute(Qt::WA_MacAlwaysShowToolWindow);
#endif
_timer = new QTimer(this);
connect(_timer, SIGNAL(timeout()), this, SLOT(updateState()));
initGeometry();
initTray();
initMenu();
initHotKey();
initToolBarFunction();
}
MagicAssistant::~MagicAssistant()
{
delete ui;
delete _toolbar;
//将位置信息写入配置文件.
Settings set(SETTING_PATH);
set.setValue(GEOMETRY_KEY, this->geometry());
// 注销此次注册,否则此快捷键再也注册不了了。除非重启.
QList<quint32> idList = _hotkey.keys();
for(auto vk : idList) {
UnregisterHotKey((HWND)winId(), vk);
}
}
void MagicAssistant::initGeometry()
{
QRect desktop = QApplication::desktop()->geometry();
Settings set(SETTING_PATH);
bool isRestore = false;
if(set.value(GEOMETRY_KEY) != QVariant()) {
_geometry = set.value(GEOMETRY_KEY).toRect();
if(desktop.contains(_geometry)) {
isRestore = true;
}
}
if(!isRestore) {
_geometry = QRect(desktop.width()-200, desktop.height()-250, 120, 150);
set.setValue(GEOMETRY_KEY, _geometry);
}
this->setGeometry(_geometry);
}
void MagicAssistant::initTray()
{
_system_tray = new QSystemTrayIcon(this);
QObject::connect(_system_tray, &QSystemTrayIcon::activated, this, &MagicAssistant::trayActivated);
QMenu *trayMenu = new QMenu(this);
trayMenu->addAction(tr("&Show"), this, SLOT(showNormal()));
trayMenu->addSeparator();
trayMenu->addAction(tr("&Quit"), qApp, SLOT(quit()));
_system_tray->setContextMenu(trayMenu);
QIcon icon(":/images/james_16.ico");
_system_tray->setIcon(icon);
_system_tray->setToolTip("Magic Assistant");
_system_tray->show();
// 先屏蔽启动弹窗功能.每次启动弹窗太烦人.
/*if (_system_tray->isVisible()) {
_system_tray->showMessage(tr("Information"), tr("Magic Assistant stay in system tray"));
}*/
}
void MagicAssistant::initHotKey()
{
quint32 id = 1;
RegisterHotKey((HWND)winId(), id, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 'Z');
id++;
// 注销快捷键时需要用此列表.
_hotkey.insert(id, QString());
// 剩余时间快捷键.
RegisterHotKey((HWND)winId(), id, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 'T');
id++;
_hotkey.insert(id, QString());
Settings set(SETTING_PATH);
QList<QVariant> hotkeyList = set.values(QLatin1String("HotKey"));
for(auto var : hotkeyList) {
QString hotkey = var.toString();
QStringList list = hotkey.split('_', QString::SkipEmptyParts);
quint32 modifiers = MOD_NOREPEAT;
quint32 vk = 0;
for(auto str : list) {
if(str.count() == 1) {
vk = str.at(0).unicode();
} else {
if(str == QLatin1String("Ctrl")) {
modifiers |= MOD_CONTROL;
} else if(str == QLatin1String("Alt")) {
modifiers |= MOD_ALT;
} else if(str == QLatin1String("Shift")) {
modifiers |= MOD_SHIFT;
}
}
}
// 注册快捷键.
bool isOk = RegisterHotKey((HWND)winId(), id, modifiers, vk);
if(isOk) {
_hotkey.insert(id, set.value(hotkey).toString());
id++;
} else {
QMessageBox::warning(this, "warning", QString("Register hotkey %1 failed").arg(hotkey));
}
}
}
void MagicAssistant::initToolBarFunction()
{
//connect(_toolbar, &ToolBar::updateMetaDataRequested, this, &MagicAssistant::updateMetaData);
//connect(_toolbar, &ToolBar::vsbuildRequested, this, &MagicAssistant::execVSBuild);
connect(_toolbar, &ToolBar::openCommandRequested, this, &MagicAssistant::openCommand);
connect(_toolbar, &ToolBar::openProjectDirRequested, this, &MagicAssistant::openProjectDir);
connect(_toolbar, &ToolBar::screenShotRequested, this, &MagicAssistant::screenShot);
connect(_toolbar, &ToolBar::shutdownRequested, this, &MagicAssistant::shutdown);
}
void MagicAssistant::autoStart(bool is_start)
{
QSettings regedit(REGEDIT_AUTO_START_PATH, QSettings::NativeFormat);
if(is_start) {
QString sAppPath = QApplication::applicationFilePath();
regedit.setValue(REGEDIT_KEY, QVariant(QDir::toNativeSeparators(sAppPath)));
} else {
regedit.setValue(REGEDIT_KEY, QVariant());
}
}
void MagicAssistant::paintEvent(QPaintEvent*)
{
QPainter painter(this);
// 绘制不规则背景.
painter.drawPixmap(0, 0, _pixmap.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
void MagicAssistant::showToolBar()
{
QRect rect = this->geometry();
_toolbar->show();
//让toolbar和主窗体有共同的中心线.
int x = rect.x() + 0.5*(rect.width() - _toolbar->width());
int y = rect.y() + rect.height();
_toolbar->move(x, y);
}
void MagicAssistant::hideAll()
{
this->hide();
_toolbar->hide();
}
void MagicAssistant::showAll()
{
this->showNormal();
showToolBar();
}
void MagicAssistant::mousePressEvent(QMouseEvent *event)
{
_start_pos = event->pos();
_toolbar->hide();
QWidget::mousePressEvent(event);
}
void MagicAssistant::mouseReleaseEvent(QMouseEvent *event)
{
showToolBar();
_toolbar->show();
QWidget::mouseReleaseEvent(event);
}
void MagicAssistant::mouseMoveEvent(QMouseEvent *event)
{
move(event->globalPos() - _start_pos);
_geometry = this->geometry();
}
void MagicAssistant::mouseDoubleClickEvent(QMouseEvent*)
{
hideAll();
}
void MagicAssistant::enterEvent(QEvent *event)
{
showToolBar();
// 开启定时器,检测鼠标位置.
_timer->start(1000);
QWidget::enterEvent(event);
}
void MagicAssistant::updateState()
{
//取主窗体和toolbar的边框范围.此运算符是算出两个矩形的边框.由于都没有父窗体,都是全局的坐标.
QRect unite_rect = this->geometry() | _toolbar->geometry();
if(!unite_rect.contains(QCursor::pos())) {
_toolbar->hide();
_timer->stop();
}
}
bool MagicAssistant::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(result);
// windows系统下,message类型才为MSG.
if(eventType == "windows_generic_MSG")
{
MSG* msg = reinterpret_cast<MSG*>(message);
if(msg->message == WM_HOTKEY)
{
switch(msg->wParam)
{
case 1:
showTodayScore();
break;
case 2:
showRestTime();
break;
default:
quint32 id = static_cast<quint32>(msg->wParam);
execProcess(_hotkey.value(id));
break;
}
}
}
return false;
}
void MagicAssistant::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::DoubleClick) {
if(this->isVisible()) {
hideAll();
} else {
showNormal();
}
}
}
void MagicAssistant::setOpacity(qreal opacity)
{
this->setWindowOpacity(opacity);
_toolbar->setWindowOpacity(opacity);
Settings set(SETTING_PATH);
set.setValue(OPACITY_KEY, opacity);
}
void MagicAssistant::openInExplorer()
{
// start是cmd内部命令,必须这么调用,/c是执行完关闭cmd的参数.具体可查阅cmd的帮助(cmd /?).
QString path = QApplication::applicationDirPath();
QProcess::startDetached("cmd /c start " + path);
}
void MagicAssistant::screenshotSetting(bool is_compressed)
{
_is_compressed = is_compressed;
Settings set(SETTING_PATH);
set.setValue(SCREENSHOT_KEY, is_compressed);
}
void MagicAssistant::initMenu()
{
_menu = new QMenu(this);
connect(this, &QWidget::customContextMenuRequested, this, [=] () {_menu->exec(QCursor::pos());});
///< 开机自启动开关.
QAction* autoStartAction = _menu->addAction(tr("Auto Start"), this, SLOT(autoStart(bool)));
autoStartAction->setCheckable(true);
_menu->addSeparator();
// 根据注册表键值初始化菜单checked状态.
QSettings regedit(REGEDIT_AUTO_START_PATH, QSettings::NativeFormat);
if(regedit.value(REGEDIT_KEY) == QVariant()) {
autoStartAction->setChecked(false);
} else {
autoStartAction->setChecked(true);
}
///<Begin 设置透明度菜单.此处目前没有好好的设计,采用hard coding.
Settings set(SETTING_PATH);
QActionGroup *group = new QActionGroup(this);
qreal opacity = set.value(OPACITY_KEY, 1.0).toReal();
QMenu *opacityMenu = _menu->addMenu("Opacity");
QAction *action = opacityMenu->addAction("100%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 1) {
action->setChecked(true);
setOpacity(1);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(1);});
action = opacityMenu->addAction("90%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 0.9) {
action->setChecked(true);
setOpacity(0.9);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(0.9);});
action = opacityMenu->addAction("80%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 0.8) {
action->setChecked(true);
setOpacity(0.8);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(0.8);});
action = opacityMenu->addAction("70%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 0.7) {
action->setChecked(true);
setOpacity(0.7);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(0.7);});
action = opacityMenu->addAction("60%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 0.6) {
action->setChecked(true);
setOpacity(0.6);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(0.6);});
action = opacityMenu->addAction("50%");
group->addAction(action);
action->setCheckable(true);
if(opacity == 0.5) {
action->setChecked(true);
setOpacity(0.5);
}
connect(action, &QAction::triggered, this, [=] () {setOpacity(0.5);});
///<End
///< 打开程序所在位置.
_menu->addAction(tr("Open in Explorer"), this, SLOT(openInExplorer()));
///< 屏幕截图设置:压缩、无损.
QAction* screenshotAction = _menu->addAction(tr("ScreenShot Compressed"), this, SLOT(screenshotSetting(bool)));
screenshotAction->setCheckable(true);
_menu->addSeparator();
// 初始化菜单checked状态.
_is_compressed = set.value(SCREENSHOT_KEY, true).toBool();
screenshotAction->setChecked(_is_compressed);
///< 隐藏窗体.
_menu->addSeparator();
QAction *hideAction = _menu->addAction(tr("&Hide to System Tray"));
connect(hideAction, &QAction::triggered, this, [=] () {hideAll();});
}
void MagicAssistant::execProcess(QString filename)
{
QProcess process;
QString str = QApplication::applicationDirPath() + QLatin1String("/execute/") + filename;qDebug() << str;
process.startDetached(str);
}
void MagicAssistant::openCommand()
{
QProcess::startDetached("cmd");
}
void MagicAssistant::openProjectDir()
{
// start是cmd内部命令,必须这么调用,/c是执行完关闭cmd的参数.具体可查阅cmd的帮助(cmd /?).
//QProcess::startDetached("cmd /c start C:/Workspace/OrigineBeta/Builds/Win/Origine/bin");
//为了可编辑,将此代码也移动到外部的bat中.
QProcess process;
QString str = QApplication::applicationDirPath() + "/execute/open.bat";
process.startDetached(str);
}
void MagicAssistant::screenShot()
{
// 先隐藏所有窗体.
hideAll();
// QScreen没有构造函数,必须通过此获取.
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap fullscreen = screen->grabWindow(QApplication::desktop()->winId());
//以当前时间命名.
QDate date = QDate::currentDate();
QTime time = QTime::currentTime();
QString name = QString("%1%2%3").arg(date.year()).arg(date.month()).arg(date.day());
name += QString("%1%2%3").arg(time.hour()).arg(time.minute()).arg(time.second());
//没有文件夹则新建.
QDir dir("./resources/screenshot");
if(!dir.exists()) {
dir.mkpath(dir.absolutePath());
}
QString file_path = dir.absolutePath() + "/" + name + ".png";
// 可以调第三个形参,为图片质量,100是无压缩,默认会压缩,但是效果不错,不影响图片分辨率大小.
if(_is_compressed) {
fullscreen.save(file_path, "PNG");
} else {
fullscreen.save(file_path, "PNG", 100);
}
// 截图完毕再显示所有窗体.
showAll();
if(fullscreen.isNull()) {
qWarning() << "Screen shot is null.";
}
}
void MagicAssistant::shutdown()
{
bool ok;
int time = QInputDialog::getInt(_toolbar, tr("Auto shutdown setting"), tr("Time(ms):"),
0, 0, 2147483647, 1, &ok);
if(ok) {
//由于shutdown命令,-t后面的参数只要大于0,默认就会执行-f参数,-f是强制关闭程序,所以此句会导致程序退出...
QProcess::startDetached(QString("shutdown -s -t %1").arg(time));
} else {
//QProcess::startDetached("shutdown -a");
}
}
void MagicAssistant::showTodayScore()
{
// qmlRegisterType<TeamScore>("CustomComponents", 1, 0, "TeamScore");
// QQmlEngine engine;
// NBAAssistant nbaobj;
// nbaobj.getTodayScore();
// engine.rootContext()->setContextProperty("nbaobj", &nbaobj);
// QQuickView *view = new QQuickView(&engine, NULL);
// //view->setFlags(Qt::FramelessWindowHint);
// view->setSource(QUrl("qrc:/ScoreBoard.qml"));
// view->show();
NBAAssistant nba;
nba.getTodayScore();
QList<TeamScore*> list = nba.getTeamscore();
InfoBoard *board = new InfoBoard;
board->setAttribute(Qt::WA_DeleteOnClose);
QStringList filterList;
filterList << "骑士" << "湖人" << "勇士" << "火箭";
board->setFilterText(filterList, Qt::red);
for (auto i = list.begin(); i != list.end(); ++i)
{
QString str = (*i)->_homeTeam + QChar::Space;
str += QString::number((*i)->_homeScore);
str += " : ";
str += QString::number((*i)->_awayScore) + QChar::Space;
str += (*i)->_awayTeam;
board->append(str);
}
QDesktopWidget *desk = QApplication::desktop();
board->show();
QRect rect = desk->availableGeometry();
board->move(rect.width() - board->width() - 32, rect.height() - board->height());
}
void MagicAssistant::showRestTime()
{
Settings set(SETTING_PATH);
QTime time;
if(set.value(TIME_KEY) != QVariant()) {
QString s = set.value(TIME_KEY).toString();
time = QTime::fromString(s, "hh:mm:ss");
} else {
time = QTime(18, 0, 0);
QString s = time.toString("hh:mm:ss");
set.setValue(TIME_KEY, s);
}
QTime cTime = QTime::currentTime();
int second = cTime.secsTo(time);
int minute = second/60 + 1;
InfoBoard *board = new InfoBoard;
board->setAttribute(Qt::WA_DeleteOnClose);
QDesktopWidget *desk = QApplication::desktop();
board->show();
QRect rect = desk->availableGeometry();
board->move(rect.width() - board->width() - 32, rect.height() - board->height());
board->append(QString("Minute: %1").arg(minute));
board->append(QString("Second: %1").arg(second));
}