-
Notifications
You must be signed in to change notification settings - Fork 26
/
mainwindow.cpp
7570 lines (6146 loc) · 229 KB
/
mainwindow.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include <QTabBar>
#include <QTabWidget>
#include "MyTabBar.h"
#include "MyTabPage.h"
#include "MyTabPopup.h"
#include "filesystemwatcher.h"
#include "mytabwidget.h"
#include "ui_mainwindow.h"
#ifdef __APPLE__
#include "OSXHideTitleBar.h"
#endif
#include "methods.h"
QString CurVersion = "1.1.67";
QString fileName, curFile, dragFileName, findStr, findPath, search_string,
curFindFile;
bool loading = false;
bool thread_end = true;
bool break_run = false;
bool show_s = true;
bool show_d = true;
bool show_m = true;
bool show_n = false;
bool textEditScroll = false;
bool miniEditWheel = false;
bool ReLoad = false;
bool zh_cn = false;
bool isIncludeSubDir, isCaseSensitive;
bool isFinishFind = true;
bool isBreakFind = false;
QList<int> m_searchTextPosList;
QStringList files;
int s_count = 0;
int m_count = 0;
int d_count = 0;
int n_count = 0;
int vs, hs, red, rowDrag, colDrag;
long curFindPos;
long totalPos;
QsciScintilla *textEditBack, *textEditSerach;
QsciScintilla* miniDlgEdit;
miniDialog* miniDlg;
// QVector<QsciScintilla*> textEditList;
QVector<QString> openFileList;
QList<QTreeWidgetItem*> twitems;
QList<QTreeWidgetItem*> tw_scope;
QList<QTreeWidgetItem*> tw_device;
QList<QTreeWidgetItem*> tw_method;
QList<QTreeWidgetItem*> tw_name;
QList<QTreeWidgetItem*> tw_list;
QList<QTreeWidgetItem*> tw_SearchResults;
QTreeWidget* treeWidgetBak;
QVector<QString> filelist;
QWidgetList wdlist;
QsciLexer *myTextLexer, *miniLexer;
extern MainWindow* mw_one;
thread_one::thread_one(QObject* parent) : QThread(parent) {}
SearchThread::SearchThread(QObject* parent) : QThread{parent} {}
MiniEditor::MiniEditor(QWidget* parent) : QsciScintilla(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
connect(this, &QsciScintilla::cursorPositionChanged, this,
&MiniEditor::miniEdit_cursorPositionChanged);
}
MaxEditor::MaxEditor(QWidget* parent) : QsciScintilla(parent) {}
CodeEditor::CodeEditor(QWidget* parent) : QPlainTextEdit(parent) {
lineNumberArea = new LineNumberArea(this);
connect(this, &CodeEditor::blockCountChanged, this,
&CodeEditor::updateLineNumberAreaWidth);
connect(this, &CodeEditor::updateRequest, this,
&CodeEditor::updateLineNumberArea);
connect(this, &CodeEditor::cursorPositionChanged, this,
&CodeEditor::highlightCurrentLine);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
loading = true;
loadLocal();
#ifdef Q_OS_WIN32
regACPI_win();
ui->actionKextstat->setEnabled(false);
win = true;
#endif
#ifdef Q_OS_LINUX
ui->actionKextstat->setEnabled(false);
ui->actionGenerate->setEnabled(false);
linuxOS = true;
#endif
#ifdef Q_OS_MAC
ui->actionGenerate->setEnabled(true);
mac = true;
#if (QT_VERSION <= QT_VERSION_CHECK(5, 15, 0))
osx1012 = true;
mac = false;
#endif
#endif
init_Widget();
init_statusBar();
init_menu();
init_recentFiles();
init_info_edit();
init_treeWidget();
init_UIStyle();
init_UI_Layout();
init_filesystem();
init_Tool_UI();
loadTabFiles();
loadFindString();
readINIProxy();
init_ScrollBox();
loading = false;
}
MainWindow::~MainWindow() {
delete ui;
mythread->quit();
mythread->wait();
mySearchThread->quit();
mySearchThread->wait();
}
void MainWindow::init_Widget() {
strIniFile =
QDir::homePath() + "/.config/" + strAppName + "/" + strAppName + ".ini";
installEventFilter(this);
listMd5.clear();
ver = "Xiasl V" + CurVersion + " ";
setWindowTitle(ver);
ver = "";
//获取背景色
QPalette pal = ui->treeWidget->palette();
QBrush brush = pal.window();
red = brush.color().red();
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
mythread = new thread_one();
connect(mythread, &thread_one::over, this, &MainWindow::dealover);
mySearchThread = new SearchThread();
connect(mySearchThread, &SearchThread::isDone, this, &MainWindow::dealDone);
dlg = new dlgDecompile(this);
dlgAutoUpdate = new AutoUpdateDialog(this);
dlgset = new dlgPreferences(this);
miniDlg = new miniDialog(this);
miniDlg->close();
miniEdit = new MiniEditor(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timer_linkage()));
tmeShowFindProgress = new QTimer(this);
connect(tmeShowFindProgress, SIGNAL(timeout()), this,
SLOT(on_ShowFindProgress()));
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
blAutoCheckUpdate = true;
CheckUpdate();
}
void MainWindow::init_UI_Layout() {
// 分割窗口
QSettings Reg(strIniFile, QSettings::IniFormat);
ui->centralwidget->layout()->setContentsMargins(2, 2, 2, 2);
ui->centralwidget->layout()->setSpacing(1);
QSplitter* splitterH = new QSplitter(Qt::Horizontal, this);
ui->vLayout->addWidget(ui->frameTip);
ui->vLayout->addWidget(ui->tabWidget_textEdit);
splitterH->addWidget(ui->tabWidget_misc);
splitterH->addWidget(ui->frame);
ui->centralwidget->layout()->addWidget(splitterH);
int w0 = Reg.value("w0", 200).toInt();
int w1 = Reg.value("w1", 450).toInt();
if (w0 < 150) w0 = 150;
if (w1 < 150) w1 = 150;
QList<int> list;
list.append(w0);
list.append(w1);
splitterH->setSizes(list);
connect(splitterH, &QSplitter::splitterMoved, [=]() {});
QSplitter* splitterV = new QSplitter(Qt::Vertical, this);
ui->frameInfo->layout()->addWidget(ui->listWidget);
ui->frameInfo->layout()->addWidget(ui->tabWidget);
splitterV->addWidget(splitterH);
splitterV->addWidget(ui->frameInfo);
ui->centralwidget->layout()->addWidget(splitterV);
int h0 = Reg.value("h0", 500).toInt();
int h1 = Reg.value("h1", 100).toInt();
if (h0 < 150) h0 = 150;
if (h1 < 100) h1 = 100;
list.clear();
list.append(h0);
list.append(h1);
splitterV->setSizes(list);
connect(splitterV, &QSplitter::splitterMoved, [=]() {
});
ui->centralwidget->layout()->addWidget(ui->frameBook);
ui->fBox->setMouseTracking(true);
ui->dockMiniEdit->layout()->addWidget(miniEdit);
ui->dockMiniEdit->layout()->addWidget(ui->fBox);
//设置鼠标追踪
ui->centralwidget->setMouseTracking(true);
this->setMouseTracking(true);
ui->frameInfo->setMouseTracking(true);
ui->statusbar->setMouseTracking(true);
}
void MainWindow::loadTabFiles() {
//读取标签页
QSettings Reg(strIniFile, QSettings::IniFormat);
int count = Reg.value("count").toInt();
bool yes = false;
if (count == 0) {
newFile("");
yes = true;
}
for (int i = 0; i < count; i++) {
QString file = Reg.value(QString::number(i) + "/file").toString();
QFileInfo fi(file);
if (fi.exists()) {
int row, col;
row = Reg.value(QString::number(i) + "/row").toInt();
col = Reg.value(QString::number(i) + "/col").toInt();
loadFile(file, row, col);
int vs, hs;
vs = Reg.value(QString::number(i) + "/vs").toInt();
hs = Reg.value(QString::number(i) + "/hs").toInt();
textEdit->verticalScrollBar()->setSliderPosition(vs);
textEdit->horizontalScrollBar()->setSliderPosition(hs);
textEdit->setFocus();
yes = true;
} else {
newFile("");
yes = true;
}
}
if (!yes) newFile("");
int tab_total = ui->tabWidget_textEdit->tabBar()->count(); //以实际存在的为准
int ci = Reg.value("ci").toInt();
if (ci < tab_total) {
ui->tabWidget_textEdit->setCurrentIndex(ci);
on_tabWidget_textEdit_tabBarClicked(ci);
} else {
ui->tabWidget_textEdit->setCurrentIndex(tab_total - 1);
on_tabWidget_textEdit_tabBarClicked(tab_total - 1);
}
}
void MainWindow::about() {
QFileInfo appInfo(qApp->applicationFilePath());
QString str;
str = tr("Last modified: ");
QString last = str + appInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss");
QString str1 =
"<a style='color:blue;' href = "
"https://github.com/ic005k/" +
strAppName +
">Xiasl"
"</a><br><br>";
QMessageBox::about(this, "About",
str1 + "V" + CurVersion + "<br><br>" + last);
}
QString MainWindow::openFile(QString fileName) {
removeFilesWatch();
QFileInfo fInfo(fileName);
if (fInfo.suffix() == "aml" || fInfo.suffix() == "dat") {
//如果之前这个文件被打开过,则返回
QString str = fInfo.path() + "/" + fInfo.baseName() + ".dsl";
for (int i = 0; i < ui->tabWidget_textEdit->tabBar()->count(); i++) {
QWidget* pWidget = ui->tabWidget_textEdit->widget(i);
lblCurrentFile = (QLabel*)pWidget->children().at(
lblNumber); // 2为QLabel,1为textEdit,0为VBoxLayout
if (str == lblCurrentFile->text()) {
addFilesWatch();
return str;
}
}
QFileInfo appInfo(qApp->applicationDirPath());
Decompile = new QProcess;
//显示信息窗口并初始化表头
InfoWinShow = true;
//标记tab头
int info_count = 0;
ui->tabWidget->setTabText(
1, tr("Errors") + " (" + QString::number(info_count) + ")");
ui->tabWidget->setTabText(
2, tr("Warnings") + " (" + QString::number(info_count) + ")");
ui->tabWidget->setTabText(
3, tr("Remarks") + " (" + QString::number(info_count) + ")");
ui->tabWidget->setTabText(4, tr("Scribble"));
ui->actionInfo_win->setChecked(true);
ui->listWidget->clear();
ui->listWidget->addItem(
new QListWidgetItem(QIcon(":/icon/i10.png"), tr("BasicInfo")));
ui->listWidget->addItem(new QListWidgetItem(
QIcon(":/icon/i20.png"), ui->tabWidget->tabBar()->tabText(1)));
ui->listWidget->addItem(new QListWidgetItem(
QIcon(":/icon/i30.png"), ui->tabWidget->tabBar()->tabText(2)));
ui->listWidget->addItem(new QListWidgetItem(
QIcon(":/icon/i40.png"), ui->tabWidget->tabBar()->tabText(3)));
ui->listWidget->addItem(new QListWidgetItem(
QIcon(":/icon/i50.png"), ui->tabWidget->tabBar()->tabText(4)));
QString name;
//设置文件过滤器
QStringList nameFilters;
if (fInfo.suffix() == "aml") {
name = "/*.aml";
//设置文件过滤格式
nameFilters << "*.aml";
}
if (fInfo.suffix() == "dat") {
name = "/*.dat";
//设置文件过滤格式
nameFilters << "*.dat";
}
QDir dir(fInfo.path());
//将过滤后的文件名称存入到files列表中
QStringList files =
dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
int count = files.count();
if (!dlgset->ui->chkAll->isChecked()) {
count = 1;
files.clear();
files.append(fInfo.fileName());
}
for (int i = 0; i < count; i++) {
QString dfile = fInfo.path() + "/" + files.at(i);
try {
#ifdef Q_OS_WIN32
Decompile->start(appInfo.filePath() + "/iasl.exe",
QStringList() << "-d" << dfile);
#endif
#ifdef Q_OS_LINUX
Decompile->start(appInfo.filePath() + "/iasl", QStringList()
<< "-d" << dfile);
#endif
#ifdef Q_OS_MAC
Decompile->start(appInfo.filePath() + "/iasl", QStringList()
<< "-d" << dfile);
#endif
connect(Decompile, SIGNAL(finished(int)), this,
SLOT(readDecompileResult(int)));
#ifdef Q_OS_WIN32
Decompile->execute(appInfo.filePath() + "/iasl.exe",
QStringList() << "-d" << dfile);
#endif
#ifdef Q_OS_LINUX
Decompile->execute(appInfo.filePath() + "/iasl", QStringList()
<< "-d" << dfile);
#endif
#ifdef Q_OS_MAC
Decompile->execute(appInfo.filePath() + "/iasl", QStringList()
<< "-d" << dfile);
#endif
} catch (...) {
qDebug() << "error";
Decompile->terminate();
}
} // for
Decompile->terminate();
fileName = fInfo.path() + "/" + fInfo.baseName() + ".dsl";
}
QFileInfo fi(fileName);
if (fi.suffix().toLower() == "dsl") {
ui->actionAutomatic_Line_Feeds->setChecked(
false); //取消自动换行,影响dsl文件开启速度
textEdit->setWrapMode(QsciScintilla::WrapNone);
}
return fileName;
}
void MainWindow::init_listForRecentFile(QString fileName) {
fileName = QDir::fromNativeSeparators(fileName);
for (int i = 0; i < recentFileList.count(); i++) {
if (fileName == recentFileList.at(i)) recentFileList.removeAt(i);
}
recentFileList.insert(0, fileName);
if (recentFileList.count() == 21) recentFileList.removeAt(20);
init_RecentOpenMenuItem();
QSettings Reg(strIniFile, QSettings::IniFormat);
Reg.setValue("RecentOpenFileCount", recentFileList.count());
for (int i = 0; i < recentFileList.count(); i++) {
Reg.setValue("RecentOpenFile" + QString::number(i), recentFileList.at(i));
}
}
void MainWindow::loadFile(const QString& fileName, int row, int col) {
ui->actionAutomatic_Line_Feeds->setChecked(false);
init_listForRecentFile(fileName);
/*如果之前文件已打开,则返回已打开的文件*/
for (int i = 0; i < ui->tabWidget_textEdit->tabBar()->count(); i++) {
QWidget* pWidget = ui->tabWidget_textEdit->widget(i);
lblCurrentFile = (QLabel*)pWidget->children().at(lblNumber);
if (fileName == lblCurrentFile->text()) {
ui->tabWidget_textEdit->setCurrentIndex(i);
if (!ReLoad) {
on_tabWidget_textEdit_tabBarClicked(i);
addFilesWatch();
return;
} else {
textEdit = getCurrentEditor(i);
}
}
}
if (!ReLoad) newFile(fileName);
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(
this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName), file.errorString()));
addFilesWatch();
return;
}
QTextStream in(&file);
#ifndef QT_NO_CURSOR
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
#endif
QString text;
int ColNum, RowNum;
if (ReLoad) //记录重装前的行号
{
textEdit->getCursorPosition(&RowNum, &ColNum);
}
if (ui->actionUTF_8->isChecked()) in.setCodec("UTF-8");
if (ui->actionGBK->isChecked()) in.setCodec("GBK");
text = in.readAll();
textEdit->setText(text);
miniEdit->clear();
miniEdit->setText(text);
file.close();
if (row != -1 && col != -1) {
textEdit->setCursorPosition(row, col);
}
if (ReLoad) //文本重装之后刷新树并回到之前的位置
{
on_StartRefreshThread();
textEdit->setCursorPosition(RowNum, ColNum);
}
#ifndef QT_NO_CURSOR
QGuiApplication::restoreOverrideCursor();
#endif
//给当前tab里面的lbl赋值
QWidget* pWidget = ui->tabWidget_textEdit->currentWidget();
lblCurrentFile = (QLabel*)pWidget->children().at(
lblNumber); // 2为QLabel,1为textEdit,0为VBoxLayout
lblCurrentFile->setText(fileName);
QFileInfo ft(fileName);
ui->tabWidget_textEdit->tabBar()->setTabToolTip(
ui->tabWidget_textEdit->currentIndex(), ft.filePath());
QSettings Reg(strIniFile, QSettings::IniFormat);
int count = Reg.value("miniMapCount").toInt();
for (int i = 0; i < count; i++) {
miniEdit->setHidden(Reg.value("miniMap" + fileName, false).toBool());
miniEdit->setHidden(false);
if (miniEdit->isHidden())
textEdit->SendScintilla(QsciScintilla::SCI_SETVSCROLLBAR, true);
else
textEdit->SendScintilla(QsciScintilla::SCI_SETVSCROLLBAR, false);
}
//为拖拽tab准备拖动后的标题名
ui->tabWidget_textEdit->currentWidget()->setWindowTitle(" " +
ft.fileName());
setCurrentFile(fileName);
statusBar()->showMessage(
" " + tr("File loaded"),
2000);
ui->editShowMsg->clear();
ui->treeWidget->clear();
ui->treeWidget->repaint();
lblLayer->setText("");
lblMsg->setText("");
ReLoad = false;
QIcon icon(":/icon/md0.svg");
ui->tabWidget_textEdit->tabBar()->setTabIcon(
ui->tabWidget_textEdit->currentIndex(), icon);
updateMd5(fileName);
addFilesWatch();
init_TabList();
getBookmarks();
}
void MainWindow::setRecentFiles(QString fileName) {
//最近打开的文件
QSettings settings;
QFileInfo fInfo(fileName);
QCoreApplication::setOrganizationName("ic005k");
QCoreApplication::setOrganizationDomain("github.com/ic005k");
QCoreApplication::setApplicationName(strAppName);
settings.setValue("currentDirectory", fInfo.absolutePath());
// qDebug() << settings.fileName(); //最近打开的文件所保存的位置
m_recentFiles->setMostRecentFile(fileName);
}
void MainWindow::setCurrentFile(const QString& fileName) {
curFile = fileName;
textEdit->setModified(false);
setWindowModified(false);
shownName = curFile;
if (curFile.isEmpty()) shownName = tr("untitled") + ".dsl";
setWindowFilePath(shownName);
setWindowTitle(ver + shownName);
ui->actionGo_to_previous_error->setEnabled(false);
ui->actionGo_to_the_next_error->setEnabled(false);
//初始化fsm
init_fsmSyncOpenedFile(shownName);
QFileInfo f(shownName);
if (f.suffix().toLower() == "dsl" || f.suffix().toLower() == "asl" ||
f.suffix().toLower() == "cpp" || f.suffix().toLower() == "c") {
ui->actionWrapWord->setChecked(false); //取消自动换行,影响dsl文件开启速度
textEdit->setWrapMode(QsciScintilla::WrapNone);
//设置编译功能使能
ui->actionCompiling->setEnabled(true);
} else {
//设置编译功能屏蔽
ui->actionCompiling->setEnabled(false);
ui->frameInfo->setHidden(true);
}
ui->tabWidget_textEdit->setTabText(ui->tabWidget_textEdit->currentIndex(),
f.fileName());
}
void MainWindow::set_return_text(QString text) {
QFontMetrics elideFont(ui->btnReturn->font());
ui->btnReturn->setText(elideFont.elidedText(
text, Qt::ElideLeft,
ui->tabWidget_misc->width() - 100)); //省略号显示在左边
}
void MainWindow::Open() {
QStringList fileNames = QFileDialog::getOpenFileNames(
this, "DSDT", "", "DSDT(*.aml *.dsl *.dat *.asl);;All(*.*)");
for (int i = 0; i < fileNames.count(); i++) {
if (!fileNames.at(i).isEmpty()) {
loadFile(openFile(fileNames.at(i)), -1, -1);
fileName = fileNames.at(i);
}
}
}
bool MainWindow::maybeSave(QString info) {
if (!textEdit->isModified()) return true;
int ret;
if (!zh_cn) {
ret = QMessageBox::warning(
this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?\n\n") +
info,
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
} else {
QMessageBox box(QMessageBox::Warning, strAppName,
"文件内容已修改,是否保存?\n\n" + info);
box.setStandardButtons(QMessageBox::Save | QMessageBox::Discard |
QMessageBox::Cancel);
box.setButtonText(QMessageBox::Save, QString("保 存"));
box.setButtonText(QMessageBox::Cancel, QString("取 消"));
box.setButtonText(QMessageBox::Discard, QString("放 弃"));
ret = box.exec();
}
switch (ret) {
case QMessageBox::Save:
return Save();
case QMessageBox::Cancel:
return false;
default:
break;
}
return true;
}
bool MainWindow::Save() {
if (curFile.isEmpty()) {
return SaveAs();
} else {
return saveFile(curFile);
}
}
bool MainWindow::SaveAs() {
QFileDialog dialog;
QString fn = dialog.getSaveFileName(this, "DSDT", "",
"DSDT(*.dsl);;DSDT(*.asl);;All(*.*)");
if (fn.isEmpty()) return false;
//去重
for (int i = 0; i < ui->tabWidget_textEdit->tabBar()->count(); i++) {
QWidget* pWidget = ui->tabWidget_textEdit->widget(i);
lblCurrentFile = (QLabel*)pWidget->children().at(
lblNumber); // 2为QLabel,1为textEdit,0为VBoxLayout
if (fn == lblCurrentFile->text()) {
ui->tabWidget_textEdit->removeTab(i);
}
}
setRecentFiles(fn);
return saveFile(fn);
}
bool MainWindow::saveFile(const QString& fileName) {
removeFilesWatch();
QString errorMessage;
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
QSaveFile file(fileName);
if (file.open(QFile::WriteOnly | QFile::Text)) {
QTextStream out(&file);
out << textEdit->text();
if (!file.commit()) {
errorMessage =
tr("Cannot write file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName), file.errorString());
}
} else {
errorMessage =
tr("Cannot open file %1 for writing:\n%2.")
.arg(QDir::toNativeSeparators(fileName), file.errorString());
}
QGuiApplication::restoreOverrideCursor();
if (!errorMessage.isEmpty()) {
QMessageBox::warning(this, tr("Application"), errorMessage);
addFilesWatch();
return false;
}
QIcon icon(":/icon/md0.svg");
ui->tabWidget_textEdit->tabBar()->setTabIcon(
ui->tabWidget_textEdit->currentIndex(), icon);
//刷新文件路径
QWidget* pWidget =
ui->tabWidget_textEdit->widget(ui->tabWidget_textEdit->currentIndex());
lblCurrentFile = (QLabel*)pWidget->children().at(lblNumber);
lblCurrentFile->setText(fileName);
QFileInfo ft(fileName);
ui->tabWidget_textEdit->tabBar()->setTabToolTip(
ui->tabWidget_textEdit->currentIndex(), ft.filePath());
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
textEdit->setFocus();
updateMd5(fileName);
addFilesWatch();
init_listForRecentFile(fileName);
return true;
}
void MainWindow::removeFilesWatch() {
for (int i = 0; i < openFileList.count(); i++) {
FileSystemWatcher::removeWatchPath(openFileList.at(i));
}
}
void MainWindow::addFilesWatch() {
removeFilesWatch();
openFileList.clear();
for (int i = 0; i < ui->tabWidget_textEdit->tabBar()->count(); i++) {
FileSystemWatcher::addWatchPath(getCurrentFileName(i));
openFileList.append(getCurrentFileName(i));
}
}
void MainWindow::getACPITables(bool ssdt) {
QFileInfo appInfo(qApp->applicationDirPath());
QProcess dump;
QProcess iasl;
QStringList ssdtFiles;
QString acpiDir = QDir::homePath() + "/Xiasl/ACPI Tables/";
QDir dir;
if (dir.mkpath(acpiDir)) {
}
if (dir.mkpath(acpiDir + "temp/")) {
}
//设置文件过滤器
QStringList nameFilters;
#ifdef Q_OS_WIN32
dir.setCurrent(acpiDir);
dump.execute(appInfo.filePath() + "/acpidump.exe", QStringList() << "-b");
dir.setCurrent(acpiDir + "temp/");
dump.execute(appInfo.filePath() + "/acpidump.exe", QStringList() << "-b");
//设置文件过滤格式
nameFilters << "ssdt*.dat";
//将过滤后的文件名称存入到files列表中
ssdtFiles =
dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
dir.setCurrent(acpiDir);
if (!ssdt)
iasl.execute(appInfo.filePath() + "/iasl.exe",
QStringList() << "-e" << ssdtFiles << "-d"
<< "dsdt.dat");
#endif
#ifdef Q_OS_LINUX
dump.execute(appInfo.filePath() + "/acpidump", QStringList() << "-b");
// iasl.execute(appInfo.filePath() + "/iasl", QStringList() << "-d"
// << "dsdt.dat");
#endif
#ifdef Q_OS_MAC
QString strExtBin = appInfo.filePath() + "/patchmatic";
if (QFile(strExtBin).exists()) {
dir.setCurrent(acpiDir);
dump.execute(strExtBin, QStringList() << "-extractall" << acpiDir);
dir.setCurrent(acpiDir + "temp/");
dump.execute(strExtBin, QStringList() << "-extract" << acpiDir + "temp/");
//设置文件过滤格式
nameFilters << "ssdt*.aml";
//将过滤后的文件名称存入到files列表中
ssdtFiles =
dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
dir.setCurrent(acpiDir);
if (!ssdt)
iasl.execute(appInfo.filePath() + "/iasl",
QStringList() << "-e" << ssdtFiles << "-d"
<< "dsdt.aml");
}
#endif
deleteDirfile(acpiDir + "temp/");
//获取当前加载的SSDT列表
QCoreApplication::setOrganizationName("ic005k");
QCoreApplication::setOrganizationDomain("github.com/ic005k");
QCoreApplication::setApplicationName("SSDT");
m_ssdtFiles->setNumOfRecentFiles(ssdtFiles.count()); //最多显示最近的文件个数
for (int i = ssdtFiles.count() - 1; i > -1; i--) {
QFileInfo fInfo(acpiDir + ssdtFiles.at(i));
QSettings settings;
settings.setValue("currentDirectory", fInfo.absolutePath());
// qDebug() << settings.fileName(); //最近打开的文件所保存的位置
m_ssdtFiles->setMostRecentFile(acpiDir + ssdtFiles.at(i));
}
if (!ssdt) {
loadFile(acpiDir + "dsdt.dsl", -1, -1);
QString dirAcpi = "file:" + acpiDir;
QDesktopServices::openUrl(QUrl(dirAcpi, QUrl::TolerantMode));
}
}
bool MainWindow::DeleteDirectory(const QString& path) {
if (path.isEmpty()) {
return false;
}
QDir dir(path);
if (!dir.exists()) {
return true;
}
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
foreach (QFileInfo fi, fileList) {
if (fi.isFile()) {
fi.dir().remove(fi.fileName());
} else {
DeleteDirectory(fi.absoluteFilePath());
}
}
return dir.rmpath(dir.absolutePath());
}
int MainWindow::deleteDirfile(QString dirName) {
QDir directory(dirName);
if (!directory.exists()) {
return true;
}
QString srcPath = QDir::toNativeSeparators(dirName);
if (!srcPath.endsWith(QDir::separator())) srcPath += QDir::separator();
QStringList fileNames = directory.entryList(
QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
bool error = false;
for (QStringList::size_type i = 0; i != fileNames.size(); ++i) {
QString filePath = srcPath + fileNames.at(i);
QFileInfo fileInfo(filePath);
if (fileInfo.isFile() || fileInfo.isSymLink()) {
QFile::setPermissions(filePath, QFile::WriteOwner);
if (!QFile::remove(filePath)) {
error = true;
}
} else if (fileInfo.isDir()) {
if (!deleteDirfile(filePath)) {
error = true;
}
}
}
if (!directory.rmdir(QDir::toNativeSeparators(directory.path()))) {
error = true;
}
return !error;
}
void MainWindow::btnGenerate_clicked() { getACPITables(false); }
void MainWindow::btnCompile_clicked() {
QFileInfo cf_info(curFile);
if (cf_info.suffix().toLower() != "dsl" &&
cf_info.suffix().toLower() != "asl" &&
cf_info.suffix().toLower() != "cpp" &&
cf_info.suffix().toLower() != "c") {
return;
}
QFileInfo appInfo(qApp->applicationDirPath());
co = new QProcess;
// if (!curFile.isEmpty()) Save();
Save();
lblMsg->setText(tr("Compiling..."));
qTime.start();
if (cf_info.suffix().toLower() == "dsl" ||
cf_info.suffix().toLower() == "asl") {
QString op = dlgset->ui->cboxCompilationOptions->currentText().trimmed();