-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutmgrwnd.cpp
672 lines (540 loc) · 16.4 KB
/
shutmgrwnd.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include "shutmgrwnd.h"
#include "logwnd.h"
#include "ui_shutmgrwnd.h"
// #include "shutprocess.h"
#include <QScreen>
#include <QMoveEvent>
#include <QMessageBox>
#include <Windows.h>
#include <qdatetime.h>
#include <darkqss.h>
#include <QMenu>
#include <QInputDialog>
#include <QFile>
// #include <starterchecker.h>
#define APPEND_LOG(X) logWindow->appendlog(X)
const char* SHUTDOWN_RADIOBUTTON_TEXT[8]=
{"关机","重启","睡眠(本机)","休眠(本机)","锁定(本机)","注销(本机)","立即关机","取消"};
ShutMgrWnd::ShutMgrWnd(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ShutMgrWnd)
{
ui->setupUi(this);
logWindow=new LogWnd;
machineWindow=new MachineWnd;
APPEND_LOG(tr("主程序启动"));
floatBut=new FloatingButton;
qssList[1]=qss_Dark;
qssList[0]=qss_Light;
para=new ShutdownPara;
//获得按钮
ShutdownButtons=new QRadioButton[8];
//遍历,初始化按钮
for(SHUTDOWN_TYPE i=SHUTDOWN;i<=CANCEL;i=(SHUTDOWN_TYPE)(i+1))
{
QString textureOnEachButton= QT_TR_NOOP(SHUTDOWN_RADIOBUTTON_TEXT[i-1]);
textureOnEachButton+=
"["
//如:关机[1]
+QString::number(i)+
"]";
ShutdownButtons[i-1].setText(textureOnEachButton);
//设置快捷键
ShutdownButtons[i-1].setShortcut(QKeySequence(QString::number(i)));
connect(&ShutdownButtons[i-1],&QRadioButton::clicked,[=](){
this->para->shutdownType=i;
if(i==SHUTDOWN or i==RESTART)
{
// #warning here not finished
}
else
{
}
});
//4列2行布局
this->ui->Grid_Basic->addWidget(&ShutdownButtons[i-1],
//分拆8个按钮
((uint)(i)<=4?1:2),((uint)(i)<=4?i:i-4));
}
//连接计算机窗口和主窗口
connect(this->para,SIGNAL(machineListChanged(QList<QString>)),machineWindow,SLOT(updateMachineList(QList<QString>)));
connect(machineWindow,SIGNAL(machineListChanged(QList<QString>)),this->para,SLOT(updateMachineList(QList<QString>)));
//自动添加本机
APPEND_LOG(QString().append("识别到计算机").append(QHostInfo::localHostName()));
this->para->addMachine(QHostInfo::localHostName());
//夜间模式
this->isDark=false;
checker=new NightModeChecker;
connect(ui->BAutoDark, &QPushButton::clicked, [&](){
checker->startCheck();
});
connect(checker, SIGNAL(nightModeChanged(bool)), this, SLOT(setDarkMode(bool)));
connect(ui->BDarkMode, &QPushButton::clicked, [&](){
checker->stopCheck();
});
connect(ui->BLightMode, &QPushButton::clicked, [&](){
checker->stopCheck();
});
checker->startCheck();
//开始菜单联动(失败品)
startChk=new StarterChecker;
connect(startChk,SIGNAL(startMenuOpen()),this,SLOT(openFloatingBut()));
//设置预设菜单
//获取四个按钮地址,方便调用
But_Presets[0]=this->ui->But_Preset1;
But_Presets[1]=this->ui->But_Preset2;
But_Presets[2]=this->ui->But_Preset3;
But_Presets[3]=this->ui->But_Preset4;
//创建菜单
presetSlotMenu=new QMenu;
for (int i=1; i<=4; i++)
{
QAction *eachOne=presetSlotMenu->addAction(QString("预设%1").arg(i));
// QAction::toggled(bool)
connect(eachOne,&QAction::triggered,this,
[=]{
QString name=QInputDialog::getText(this,"设置名称","请输入预设名称");
setPreset(i,name,this->para);
});
}
ui->But_SavePreset->setMenu(presetSlotMenu);
for(int i=1;i<=4;i++)
{
connect(But_Presets[i-1],&QPushButton::clicked,this,
[=]{
this->applyPreset(i);
});
}
//正确设置焦点
this->ui->Group_Basic->setFocus();
loadPreset();
}
ShutMgrWnd::~ShutMgrWnd()
{
delete ui;
delete logWindow;
delete[] ShutdownButtons;
delete checker;
}
void ShutMgrWnd::moveEvent(QMoveEvent *event){
//窗口跟随
if(logWindow->isVisible())
{
int deltax=event->pos().x()-event->oldPos().x();
int deltay=event->pos().y()-event->oldPos().y();
int logWindowx=logWindow->pos().x();
int logWindowy=logWindow->pos().y();
logWindow->move(logWindowx+deltax,logWindowy+deltay);
}
}
void ShutMgrWnd::on_BExit_clicked()
{
exit(0);
}
void ShutMgrWnd::on_BViewLog_clicked()
{
//没显示就显示,显示了就隐藏
if(not logWindow->isVisible())
{
//窗口位置
QPoint mainWindowPosition=this->pos();
//获取屏幕大小
QScreen* screen = QGuiApplication::primaryScreen();
int screenWidth=screen->geometry().width();
//使日志窗口出现在主窗口右侧
//logwindow x pos
int x=mainWindowPosition.x()+this->width();
//logwindow y pos
int y=mainWindowPosition.y();
//窗口右侧不要超过屏幕
while((x+(logWindow->width())) > screenWidth)
{
//如果超过,尝试左移100px
this->move(mainWindowPosition.x()-100,mainWindowPosition.y());
mainWindowPosition=this->pos();
x=mainWindowPosition.x()+this->width();
y=mainWindowPosition.y();
}
logWindow->move(QPoint(x,y));
logWindow->show();
}
else
{
logWindow->close();
}
}
void ShutMgrWnd::on_BExecute_clicked()
{
//连接任务
this->tasks.append(new ShutdownTask);
ShutdownTask* p=tasks[tasks.length()-1];
p->set(para);
connect(p,SIGNAL(appendLog(QString)),logWindow,SLOT(appendlog(QString)));
connect(p,SIGNAL(taskEnd(ShutdownTask*)),this,SLOT(removeTask(ShutdownTask*)));
ui->List_tasks->addItem(p->getDesc());
p->run();
// ShutProcess* sprocess=new ShutProcess("");
// //将命令输出写入到日志
// auto processFinished=&ShutProcess::finished;
// auto logappend=&LogWnd::appendlog;
// connect(sprocess,processFinished,logWindow,logappend);
// if(para->shutdownType!=CANCEL && isShutdownScheduled(this->para->computer))
// {
// QMessageBox::critical(this,tr("错误"),tr("系统已经计划关机,请先取消之!"));
// return;
// }
// task->set("",para);
// task->run();
// QString command;
// switch(this->para->shutdownType){
//case NONE:
// QMessageBox::critical(this,tr("错误"),tr("没有指定任何模式!"));
// return;
// break;
//case SHUTDOWN:
// if(this->para)
// break;
//case RESTART:
// command="shutdown -r";
// break;
//case SLEEP:
// command=""
// break;
//case HIBERNATE:
// break;
//case LOCK:
// break;
//case LOGOUT:
// break;
//case SHUTDOWN_P:
// break;
//case CANCEL:
// break;
//}
// sprocess->run();
}
//------------------按钮槽函数--------------------
#define BUT_CHECKED 2
#define BUT_UNCHECKED 0
//与另一个选项冲突,因而取消勾选
#define CONFLICT_WITH(x) ui->x->setChecked(false)
//延时关机
void ShutMgrWnd::on_Check_timeout_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bTimeout=true;
this->para->iTimeout=ui->Spin_timeout->value();
}
else
{
this->para->bTimeout=false;
}
}
//设置超时
void ShutMgrWnd::on_Spin_timeout_valueChanged(int arg1)
{
this->para->iTimeout=arg1;
ui->Check_timeout->setChecked(true);
}
void ShutMgrWnd::on_Check_force_stateChanged(int arg1)
{
this->para->bForce=arg1;
}
void ShutMgrWnd::on_Check_toAdvanceOpt_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bToAdvancedOpt=true;
CONFLICT_WITH(Check_autoLogin);
}
else
{
this->para->bToAdvancedOpt=false;
}
}
void ShutMgrWnd::on_Check_toFirmware_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bToFirmware=true;
// //与"指定计算机"冲突
// CONFLICT_WITH(Check_machine);
}
else
{
this->para->bToFirmware=false;
}
}
// void ShutMgrWnd::on_Check_machine_stateChanged(int arg1)
// {
// if(arg1==BUT_CHECKED)
// {
// // this->para->bMultiMachine=true;
// //与"转到固件"冲突
// CONFLICT_WITH(Check_toFirmware);
// }
// else
// {
// // this->para->bMultiMachine=false;
// }
// }
void ShutMgrWnd::on_Check_fastboot_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bFastBoot=true;
CONFLICT_WITH(Check_autoLogin);
}
else
{
this->para->bFastBoot=false;
}
}
void ShutMgrWnd::on_Check_autoLogin_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bAutoLogin=true;
CONFLICT_WITH(Check_toAdvanceOpt);
CONFLICT_WITH(Check_fastboot);
}
else
{
this->para->bAutoLogin=false;
}
}
void ShutMgrWnd::on_Check_reason_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bReason=true;
}
else
{
this->para->bReason=false;
}
}
void ShutMgrWnd::on_Combo_reasonType_currentIndexChanged(int index)
{
//0->planned 1->unplanned 2->custom 3->noreason
this->para->reasonType=(REASON_TYPE)index;
ui->Check_reason->setChecked(true);
}
void ShutMgrWnd::on_Spin_majorReason_valueChanged(int arg1)
{
this->para->majorReason=arg1;
ui->Check_reason->setChecked(true);
}
void ShutMgrWnd::on_Spin_minorReason_valueChanged(int arg1)
{
this->para->minorReason=arg1;
ui->Check_reason->setChecked(true);
}
void ShutMgrWnd::on_Check_comment_stateChanged(int arg1)
{
if(arg1==BUT_CHECKED)
{
this->para->bComment=true;
}
else
{
this->para->bComment=false;
}
}
void ShutMgrWnd::on_Edit_Comment_textChanged()
{
this->para->sComment=ui->Edit_Comment->toPlainText();
ui->Check_comment->setChecked(true);
}
void ShutMgrWnd::on_But_machine_clicked()
{
machineWindow->show();
}
void ShutMgrWnd::on_List_tasks_doubleClicked(const QModelIndex &index)
{
int choice=QMessageBox::question(this,"",tr("是否结束任务?"));
if(choice==QMessageBox::No)
return;
//获取描述,以获取id
// QString s=ui->List_tasks->item(index.row())->text();
// uintptr_t id=atoll(s.mid(s.indexOf("id=")+3).toStdString().data());
// ShutdownTask* p=(ShutdownTask*)id;
ShutdownTask* p=tasks[index.row()];
p->taskTerminate();
// #error
}
void ShutMgrWnd::removeTask(ShutdownTask *t){
int index=tasks.indexOf(t);
ui->List_tasks->takeItem(index);
ShutdownTask *p=tasks.takeAt(index);
delete p;
}
void ShutMgrWnd::on_Line_Condition_textChanged(const QString &arg1)
{
this->para->condition=arg1;
}
void ShutMgrWnd::on_BDarkMode_clicked()
{
this->setStyleSheet(qssList[1]);
}
void ShutMgrWnd::on_BLightMode_clicked()
{
this->setStyleSheet(qssList[0]);
}
void ShutMgrWnd::setDarkMode(bool isDark){
if(this->isDark!=isDark)
if(isDark){
this->isDark=true;
this->setStyleSheet(qssList[1]);
}
else
{
this->isDark=false;
this->setStyleSheet(qssList[0]);
}
}
void ShutMgrWnd::openFloatingBut(){
floatBut->show();
}
void ShutMgrWnd::on_But_SavePreset_clicked()
{
ui->But_SavePreset->showMenu();
}
void ShutMgrWnd::setPreset(int index, QString name, ShutdownPara *p){
if(p->shutdownType==NONE)
{
QMessageBox::critical(this,"错误","请选择关机模式!");
return;
}
//p2避免自己拷贝自己
ShutdownPara p2;
p2.set(p);
presets[index-1].set(&p2);
strPresetNames[index-1]=name;
this->But_Presets[index-1]->setText(name.append(QString(" [Alt+%1]").arg(index)));
this->But_Presets[index-1]->setShortcut(QKeySequence(QString("Alt+%1").arg(index)));
//同时保存
QFile PresetFile(QString("preset-%1.json").arg(index));
if(PresetFile.open(QIODevice::ReadWrite | QIODevice::Truncate)){
QJsonDocument jsonDoc(p2.toJsonObject(strPresetNames[index-1]));
PresetFile.write(jsonDoc.toJson().toStdString().c_str());
}
else
{
QMessageBox::warning(this,"警告","配置槽文件打开失败,文件不能保存\n请检查应用的安装位置,重启再试。");
return;
}
}
void ShutMgrWnd::applyPreset(int i){
ShutdownPara p;
p.set(&presets[i-1]);
if(p.shutdownType!=NONE)
ShutdownButtons[p.shutdownType-1].click();
else
{
QMessageBox::critical(this,"错误","槽是空的");
return;
}
//强制
if(p.bForce)
ui->Check_force->setChecked(true);
else
ui->Check_force->setChecked(false);
//延时
if(p.bTimeout)
{
ui->Check_timeout->setChecked(true);
ui->Spin_timeout->setValue(p.iTimeout);
}
else
{
ui->Check_timeout->setChecked(false);
}
//转到固件
if(p.bToFirmware)
ui->Check_toFirmware->setChecked(true);
else
ui->Check_toFirmware->setChecked(false);
//转到固件
if(p.bFastBoot)
ui->Check_fastboot->setChecked(true);
else
ui->Check_fastboot->setChecked(false);
//高级启动
if(p.bToAdvancedOpt)
ui->Check_toAdvanceOpt->setChecked(true);
else
ui->Check_toAdvanceOpt->setChecked(false);
//自动登录
if(p.bAutoLogin)
ui->Check_autoLogin->setChecked(true);
else
ui->Check_autoLogin->setChecked(false);
//注释
if(p.bComment)
{
ui->Check_comment->setChecked(true);
ui->Edit_Comment->setText(p.sComment);
}
else
ui->Check_comment->setChecked(false);
//原因
if(p.bReason)
{
ui->Check_reason->setChecked(true);
ui->Combo_reasonType->setCurrentIndex(p.reasonType);
ui->Spin_majorReason->setValue(p.majorReason);
ui->Spin_minorReason->setValue(p.minorReason);
}
else
ui->Check_reason->setChecked(false);
ui->Line_Condition->setText(p.condition.trimmed());
this->para->updateMachineList(p.computer);
emit this->para->machineListChanged(p.computer);
}
void ShutMgrWnd::loadPreset(){
for( int i=1 ; i<=4 ; i++ )
{
QFile presetFile(QString("preset-%1.json").arg(i));
if(presetFile.open(QIODevice::ReadOnly))
{
QJsonDocument jsonPre;
QJsonParseError err;
QString strJson=presetFile.readAll();
//空文件跳过
if(strJson.trimmed()=="")
continue;
jsonPre=QJsonDocument::fromJson(strJson.toStdString().data(),&err);
if(err.error!=err.NoError)
{
QMessageBox::critical(this,"错误",QString("读取preset-%1.json时出现错误%2\n将清空该文件,请重新保存配置").arg(i).arg(err.errorString()));
if(not presetFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
APPEND_LOG(QString("警告:槽%1预设文件或无法清空").arg(i));
}
continue;
}
ShutdownPara p;
strPresetNames[i-1] = p.fromJsonObject(jsonPre.object());
presets[i-1].set(&p);
}
else
{
APPEND_LOG(QString("警告:找不到槽%1预设文件或无法读取,尝试创建").arg(i));
if(not presetFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
APPEND_LOG(QString("警告:找不到槽%1预设文件或无法读取,且无法创建").arg(i));
}
}
}
//应用设置,优先设置1
for(int i=1 ; i<=4 ;i++){
if(presets[i-1].shutdownType!=NONE)
setPreset(i,strPresetNames[i-1], presets+i-1);
}
}
void ShutMgrWnd::closeEvent(QCloseEvent *event){
event->accept();
exit(0);
}