-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
1916 lines (1633 loc) · 59.5 KB
/
mainwindow.cpp
File metadata and controls
1916 lines (1633 loc) · 59.5 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
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 "linenumbereditor.h"
#include "findreplacedialog.h"
#include "filemanage.h"
#include <QApplication>
#include <QSplitter>
#include <QTreeWidget>
#include <QTabWidget>
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QFileInfo>
#include <QSettings>
#include <QFont>
#include <QFontDatabase>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QHeaderView>
#include <QDir>
#include <QDebug>
#include <QMap>
#include <QIcon>
#include <QDesktopServices>
#include <QShortcut>
#include <QProcess>
#include <QMenu>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QStringConverter>
#include <qlineedit.h>
#endif
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
// 处理所有键盘事件,不仅仅是编辑器控件
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
// 调试输出所有按键
qDebug() << "Key event - Object:" << obj->metaObject()->className()
<< "Key:" << keyEvent->key()
<< "Modifiers:" << keyEvent->modifiers()
<< "Text:" << keyEvent->text();
// 处理 Ctrl+F - 无论焦点在哪里都响应
if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_F) {
qDebug() << "Ctrl+F detected!";
showFindDialog();
return true;
}
// 处理 Ctrl+R - 无论焦点在哪里都响应
if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_R) {
qDebug() << "Ctrl+R detected!";
showReplaceDialog();
return true;
}
// 处理 F3 和 Shift+F3 - 只在编辑器中有焦点时处理
if (qobject_cast<QPlainTextEdit*>(obj) || qobject_cast<LineNumberEditor*>(obj)) {
if (keyEvent->key() == Qt::Key_F3 && keyEvent->modifiers() == Qt::NoModifier) {
qDebug() << "F3 detected in editor!";
if (m_findReplaceDialog && m_findReplaceDialog->isVisible()) {
m_findReplaceDialog->findNextMatch();
} else {
showFindDialog();
}
return true;
}
else if (keyEvent->key() == Qt::Key_F3 && keyEvent->modifiers() == Qt::ShiftModifier) {
qDebug() << "Shift+F3 detected in editor!";
if (m_findReplaceDialog && m_findReplaceDialog->isVisible()) {
m_findReplaceDialog->findPrevMatch();
}
return true;
}
}
}
return QMainWindow::eventFilter(obj, event);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, previewVisible(false)
, m_findReplaceDialog(nullptr)
, m_fileManage(new FileManage(this))
, fileTreeContextMenu(nullptr)
, editorContextMenu(nullptr)
, removeFileAct(nullptr)
, openFileLocationAct(nullptr)
, openCmdAct(nullptr)
, openGitBashAct(nullptr)
{
// 设置窗口图标
setWindowIcon(QIcon(":/icons/icon1"));
// 设置窗口样式(整体亮色主题)
setStyleSheet(
"QMainWindow {"
" background-color: #f5f5f5;"
"}"
"QMenuBar {"
" background-color: #f0f0f0;"
" color: #333333;"
" border-bottom: 1px solid #d0d0d0;"
"}"
"QMenuBar::item {"
" padding: 4px 8px;"
" background-color: transparent;"
"}"
"QMenuBar::item:selected {"
" background-color: #e0e0e0;"
" border-radius: 2px;"
"}"
"QMenuBar::item:pressed {"
" background-color: #d0d0d0;"
"}"
"QMenu {"
" background-color: #f8f8f8;"
" color: #333333;"
" border: 1px solid #d0d0d0;"
" padding: 2px;"
"}"
"QMenu::item {"
" padding: 4px 20px;"
"}"
"QMenu::item:selected {"
" background-color: #00A663;"
" color: #ffffff;"
"}"
"QMenu::separator {"
" height: 1px;"
" background-color: #d0d0d0;"
" margin: 2px 8px;"
"}"
"QPushButton {"
" color: #333333;"
"}"
"QLabel {"
" color: #333333;"
"}"
);
// 设置窗口属性
setWindowTitle("INK - 无标题");
resize(1200, 800);
// 初始化UI
initUI();
initMenuBar();
initToolBar();
initStatusBar();
// 初始化文件列表
initFileList();
initConnections();
// 安装事件过滤器
installEventFilter(this);
// 加载设置
QSettings settings("INK", "Settings");
restoreGeometry(settings.value("geometry").toByteArray());
// 从配置加载 Git Bash 路径
m_gitBashPath = settings.value("GitBashPath", "").toString();
// 加载上次打开的目录
m_lastOpenedDir = settings.value("LastOpenedDir", "").toString();
mainSplitter->restoreState(settings.value("splitterState").toByteArray());
previewVisible = settings.value("previewVisible", false).toBool();
togglePreviewAct->setChecked(previewVisible);
// 初始状态:没有打开的文件
updateWindowTitle();
updateStatusBar();
updateEditActions();
// 创建查找替换对话框(延迟创建)
m_findReplaceDialog = new FindReplaceDialog(this);
// 连接FileManage信号
connect(m_fileManage, &FileManage::fileTreeUpdated, this, [this]() {
// 文件树更新后可能需要刷新显示
fileTree->update();
});
connect(m_fileManage, &FileManage::recentFilesUpdated, this, [this](const QStringList &recentFiles) {
// 可以在这里更新最近文件菜单
// 暂时不需要实现
});
// 从FileManage加载文件列表
QStringList recentFiles = m_fileManage->loadFileList();
for (const QString &filePath : recentFiles) {
if (QFile::exists(filePath)) {
m_fileManage->addFileToList(filePath, fileTree);
}
}
// 展开所有目录
fileTree->expandAll();
}
MainWindow::~MainWindow()
{
// 保存设置
QSettings settings("INK", "Settings");
settings.setValue("geometry", saveGeometry());
settings.setValue("splitterState", mainSplitter->saveState());
settings.setValue("previewVisible", previewVisible);
settings.setValue("LastOpenedDir", m_lastOpenedDir);
// 保存当前打开的文件列表
QStringList openFiles;
for (int i = 0; i < tabWidget->count(); ++i) {
QPlainTextEdit *editor = qobject_cast<QPlainTextEdit*>(tabWidget->widget(i));
if (editor) {
QString filePath = editor->property("filePath").toString();
if (!filePath.isEmpty() && QFile::exists(filePath)) {
openFiles.append(filePath);
}
}
}
m_fileManage->saveFileList(openFiles, m_fileManage->getRecentFiles());
delete m_findReplaceDialog;
}
void MainWindow::initUI()
{
// 创建主分割器(左右布局)
mainSplitter = new QSplitter(Qt::Horizontal, this);
// 左侧面板:文件列表
leftPanel = new QWidget(mainSplitter);
QVBoxLayout *leftLayout = new QVBoxLayout(leftPanel);
leftLayout->setContentsMargins(0, 0, 0, 0);
leftLayout->setSpacing(0);
// 文件列表工具栏 - 高度与导航栏保持一致
QWidget *toolbarWidget = new QWidget(leftPanel);
toolbarWidget->setFixedHeight(32); // 设置稍高的工具栏
toolbarWidget->setStyleSheet("background-color: #f0f0f0; border-bottom: 1px solid #d0d0d0;");
QHBoxLayout *toolbarLayout = new QHBoxLayout(toolbarWidget);
toolbarLayout->setContentsMargins(4, 0, 4, 0); // 减少边距
toolbarLayout->setSpacing(2); // 减小按钮间距
// 创建左侧按钮容器(用于定位、展开、折叠)
QWidget *leftButtons = new QWidget(toolbarWidget);
leftButtons->setStyleSheet("background: transparent;");
QHBoxLayout *leftButtonLayout = new QHBoxLayout(leftButtons);
leftButtonLayout->setContentsMargins(0, 0, 0, 0);
leftButtonLayout->setSpacing(2);
// 创建右侧按钮容器(用于新建、打开)
QWidget *rightButtons = new QWidget(toolbarWidget);
rightButtons->setStyleSheet("background: transparent;");
QHBoxLayout *rightButtonLayout = new QHBoxLayout(rightButtons);
rightButtonLayout->setContentsMargins(0, 0, 0, 0);
rightButtonLayout->setSpacing(2);
// 创建工具栏按钮 - 使用图标
locateBtn = new QPushButton(toolbarWidget);
locateBtn->setIcon(QIcon(":/icons/location")); // 从资源文件加载图标
locateBtn->setFixedSize(24, 20); // 固定大小
locateBtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;" // 透明背景
" border: none;" // 无边框
" border-radius: 4px;"
" padding: 4px;"
"}"
"QPushButton:hover {"
" background-color: #e0e0e0;" // 浅色悬停背景
"}"
"QPushButton:pressed {"
" background-color: #d0d0d0;" // 按下时显示背景
"}"
);
expandBtn = new QPushButton(toolbarWidget);
expandBtn->setIcon(QIcon(":/icons/expand"));
expandBtn->setFixedSize(24, 20);
expandBtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;"
" border: none;"
" border-radius: 4px;"
" padding: 4px;"
"}"
"QPushButton:hover {"
" background-color: #e0e0e0;"
"}"
"QPushButton:pressed {"
" background-color: #d0d0d0;"
"}"
);
foldBtn = new QPushButton(toolbarWidget);
foldBtn->setIcon(QIcon(":/icons/fold"));
foldBtn->setFixedSize(24, 20);
foldBtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;"
" border: none;"
" border-radius: 4px;"
" padding: 4px;"
"}"
"QPushButton:hover {"
" background-color: #e0e0e0;"
"}"
"QPushButton:pressed {"
" background-color: #d0d0d0;"
"}"
);
// 左侧按钮:定位、展开、折叠(按顺序:定位、折叠、展开)
leftButtonLayout->addWidget(locateBtn);
leftButtonLayout->addWidget(foldBtn);
leftButtonLayout->addWidget(expandBtn);
// 将左右按钮容器添加到主工具栏布局
toolbarLayout->addWidget(rightButtons); // 左侧按钮容器(靠左)
toolbarLayout->addStretch(); // 弹性空间,将右侧按钮推到最右边
toolbarLayout->addWidget(leftButtons); // 右侧按钮容器(靠右)
// 文件树(使用QTreeWidget)
fileTree = new QTreeWidget(leftPanel);
fileTree->setHeaderHidden(true); // 隐藏标题栏,显示空白列表
fileTree->setAlternatingRowColors(true);
fileTree->setAnimated(true);
fileTree->setIndentation(15);
// 添加到左侧布局
leftLayout->addWidget(toolbarWidget);
leftLayout->addWidget(fileTree);
leftPanel->setMinimumWidth(200);
leftPanel->setMaximumWidth(350);
// 中央区域:编辑器标签页
QWidget *centerPanel = new QWidget(mainSplitter);
QVBoxLayout *centerLayout = new QVBoxLayout(centerPanel);
centerLayout->setContentsMargins(0, 0, 0, 0);
tabWidget = new QTabWidget(centerPanel);
tabWidget->setTabsClosable(true);
tabWidget->setMovable(true);
tabWidget->setDocumentMode(true);
// 设置标签页的高度,左侧工具栏与其保持一致
tabWidget->setStyleSheet(
"QTabWidget::pane {"
" border: none;"
" background-color: #ffffff;"
"}"
"QTabBar::tab {"
" background-color: #f0f0f0;"
" color: #555555;"
" padding: 6px 12px;"
" margin-right: 2px;"
" border-top-left-radius: 4px;"
" border-top-right-radius: 4px;"
" border: 1px solid #d0d0d0;"
" border-bottom: none;"
" min-width: 80px;"
" height: 18px;"
"}"
"QTabBar::tab:selected {"
" background-color: #ffffff;"
" color: #00A663;"
" border-bottom: 2px solid #00A663;"
"}"
"QTabBar::tab:hover:!selected {"
" background-color: #e0e0e0;"
"}"
"QTabBar::close-button {"
" subcontrol-origin: padding;"
" subcontrol-position: right;"
" padding: 2px;"
"}"
"QTabBar::close-button:hover {"
" background-color: #e0e0e0;"
" border-radius: 2px;"
"}"
);
centerLayout->addWidget(tabWidget);
// 预览面板(初始隐藏)
previewPanel = new QTextEdit(mainSplitter);
previewPanel->setReadOnly(true);
previewPanel->setVisible(previewVisible);
previewPanel->setMinimumWidth(300);
// 设置预览面板样式
previewPanel->setStyleSheet(
"QTextEdit {"
" background-color: #ffffff;"
" color: #333333;"
" border: none;"
" padding: 10px;"
" font-family: 'Segoe UI', 'Microsoft YaHei', monospace;"
" font-size: 11pt;"
"}"
"QTextEdit:focus {"
" outline: none;"
"}"
// 滚动条样式
"QScrollBar:vertical {"
" border: none;"
" background: #f0f0f0;"
" width: 12px;"
"}"
"QScrollBar::handle:vertical {"
" background: #c0c0c0;"
" min-height: 20px;"
" border-radius: 6px;"
"}"
"QScrollBar::handle:vertical:hover {"
" background: #a0a0a0;"
"}"
);
// 添加到分割器
mainSplitter->addWidget(leftPanel);
mainSplitter->addWidget(centerPanel);
mainSplitter->addWidget(previewPanel);
// 设置分割器比例
QList<int> sizes;
if (previewVisible) {
sizes << 200 << 500 << 300;
} else {
sizes << 200 << 600 << 0;
previewPanel->setVisible(false);
}
mainSplitter->setSizes(sizes);
setCentralWidget(mainSplitter);
}
void MainWindow::initMenuBar()
{
// 文件菜单
fileMenu = menuBar()->addMenu("文件(&F)");
newAct = new QAction("新建(&N)", this);
newAct->setShortcut(QKeySequence::New);
fileMenu->addAction(newAct);
openAct = new QAction("打开(&O)", this);
openAct->setShortcut(QKeySequence::Open);
fileMenu->addAction(openAct);
fileMenu->addSeparator();
saveAct = new QAction("保存(&S)", this);
saveAct->setShortcut(QKeySequence::Save);
saveAct->setEnabled(false);
fileMenu->addAction(saveAct);
saveAsAct = new QAction("另存为(&A)", this);
saveAsAct->setShortcut(QKeySequence::SaveAs);
saveAsAct->setEnabled(false);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
closeAct = new QAction("关闭(&C)", this);
closeAct->setShortcut(QKeySequence::Close);
closeAct->setEnabled(false);
fileMenu->addAction(closeAct);
exitAct = new QAction("退出(&X)", this);
exitAct->setShortcut(QKeySequence::Quit);
fileMenu->addAction(exitAct);
// 编辑菜单
editMenu = menuBar()->addMenu("编辑(&E)");
undoAct = new QAction("撤销(&U)", this);
undoAct->setShortcut(QKeySequence::Undo);
undoAct->setEnabled(false);
editMenu->addAction(undoAct);
redoAct = new QAction("重做(&R)", this);
redoAct->setShortcut(QKeySequence::Redo);
redoAct->setEnabled(false);
editMenu->addAction(redoAct);
editMenu->addSeparator();
QAction *findAct = new QAction("查找(&F)", this);
findAct->setShortcut(QKeySequence::Find);
connect(findAct, &QAction::triggered, this, &MainWindow::showFindDialog);
editMenu->addAction(findAct);
QAction *replaceAct = new QAction("替换(&R)", this);
replaceAct->setShortcut(QKeySequence::Replace);
connect(replaceAct, &QAction::triggered, this, &MainWindow::showReplaceDialog);
editMenu->addAction(replaceAct);
editMenu->addSeparator();
cutAct = new QAction("剪切(&T)", this);
cutAct->setShortcut(QKeySequence::Cut);
cutAct->setEnabled(false);
editMenu->addAction(cutAct);
copyAct = new QAction("复制(&C)", this);
copyAct->setShortcut(QKeySequence::Copy);
copyAct->setEnabled(false);
editMenu->addAction(copyAct);
pasteAct = new QAction("粘贴(&P)", this);
pasteAct->setShortcut(QKeySequence::Paste);
editMenu->addAction(pasteAct);
// 视图菜单
viewMenu = menuBar()->addMenu("视图(&V)");
toggleFileListAct = new QAction("文件列表", this);
toggleFileListAct->setShortcut(QKeySequence("Ctrl+Shift+F"));
toggleFileListAct->setCheckable(true);
toggleFileListAct->setChecked(true);
viewMenu->addAction(toggleFileListAct);
togglePreviewAct = new QAction("预览面板", this);
togglePreviewAct->setShortcut(QKeySequence("Ctrl+Shift+P"));
togglePreviewAct->setCheckable(true);
togglePreviewAct->setChecked(previewVisible);
viewMenu->addAction(togglePreviewAct);
// 帮助菜单
helpMenu = menuBar()->addMenu("帮助(&H)");
aboutAct = new QAction("关于(&A)", this);
helpMenu->addAction(aboutAct);
}
void MainWindow::initToolBar()
{
QToolBar *toolBar = addToolBar("主工具栏");
toolBar->setMovable(false);
toolBar->setIconSize(QSize(20, 20)); // 设置图标大小
// 设置工具栏样式
toolBar->setStyleSheet(
"QToolBar {"
" spacing: 4px;"
" background-color: #f0f0f0;"
" border: none;"
" border-bottom: 1px solid #d0d0d0;"
" padding: 2px;"
"}"
"QToolBar QToolButton {"
" padding: 4px 6px;"
" border-radius: 4px;"
" margin: 1px;"
" color: #333333;"
"}"
"QToolBar QToolButton:hover {"
" background-color: #e0e0e0;"
"}"
"QToolBar QToolButton:pressed {"
" background-color: #d0d0d0;"
"}"
"QToolBar QToolButton:disabled {"
" color: #aaaaaa;"
"}"
"QToolBar::separator {"
" background-color: #d0d0d0;"
" width: 1px;"
" margin: 4px 6px;"
"}"
);
// 为所有动作设置图标
newAct->setIcon(QIcon(":/icons/addfile"));
openAct->setIcon(QIcon(":/icons/openfile"));
// 使用图标模式添加动作
toolBar->addAction(newAct);
toolBar->addAction(openAct);
toolBar->addSeparator();
toolBar->addAction(saveAct);
toolBar->addSeparator();
toolBar->addAction(undoAct);
toolBar->addAction(redoAct);
toolBar->addSeparator();
toolBar->addAction(cutAct);
toolBar->addAction(copyAct);
toolBar->addAction(pasteAct);
toolBar->addSeparator();
QAction *findAct = new QAction(QIcon(":/icons/search"), "查找", this);
connect(findAct, &QAction::triggered, this, &MainWindow::showFindDialog);
toolBar->addAction(findAct);
QAction *replaceAct = new QAction(QIcon(":/icons/replace"), "替换", this);
connect(replaceAct, &QAction::triggered, this, &MainWindow::showReplaceDialog);
toolBar->addAction(replaceAct);
}
void MainWindow::initStatusBar()
{
statusPosition = new QLabel("行: 1, 列: 1", this);
statusFileInfo = new QLabel("就绪", this);
// 设置状态栏样式
statusBar()->setStyleSheet(
"QStatusBar {"
" background-color: #f0f0f0;"
" color: #555555;"
" border-top: 1px solid #d0d0d0;"
" padding: 2px;"
"}"
"QStatusBar::item {"
" border: none;"
"}"
);
// 设置状态栏标签样式
statusPosition->setStyleSheet(
"QLabel {"
" color: #555555;"
" background: transparent;"
" padding: 0 8px;"
" font-size: 10pt;"
"}"
);
statusFileInfo->setStyleSheet(
"QLabel {"
" color: #888888;"
" background: transparent;"
" padding: 0 8px;"
" font-size: 10pt;"
" font-style: italic;"
"}"
);
statusBar()->addWidget(statusPosition, 1);
statusBar()->addWidget(statusFileInfo, 2);
}
void MainWindow::initFileList()
{
// 初始为空,不需要添加任何项目
fileTree->clear();
// 创建文件树上下文菜单
fileTreeContextMenu = new QMenu(this);
removeFileAct = new QAction("移除(&R)", this);
openFileLocationAct = new QAction("打开文件位置(&L)", this);
// 直接创建第一层级的终端菜单项
openCmdAct = new QAction("打开 CMD 命令行(&C)", this);
openGitBashAct = new QAction("打开 Git Bash(&G)", this);
fileTreeContextMenu->addAction(removeFileAct);
fileTreeContextMenu->addSeparator();
fileTreeContextMenu->addAction(openFileLocationAct);
fileTreeContextMenu->addAction(openCmdAct);
fileTreeContextMenu->addAction(openGitBashAct);
// 连接上下文菜单动作
connect(removeFileAct, &QAction::triggered, this, &MainWindow::removeFileFromTree);
connect(openFileLocationAct, &QAction::triggered, this, &MainWindow::openFileLocation);
connect(openCmdAct, &QAction::triggered, this, &MainWindow::openCmdAtFile);
connect(openGitBashAct, &QAction::triggered, this, &MainWindow::openGitBashAtFile);
// 设置文件树样式 - 亮色主题
fileTree->setStyleSheet(
"QTreeWidget {"
" border: none;"
" background-color: #ffffff;"
" outline: 0;"
" font-size: 10pt;"
" font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;"
"}"
"QTreeWidget::item {"
" padding: 5px 8px;"
" border-bottom: 1px solid #E0E0E0;" /* 每个项目之间的分隔线 */
" min-height: 28px;"
" max-height: 28px;"
" color: #555555;"
" background-color: #FFFFFF;" /* 文件默认白色背景 */
"}"
"QTreeWidget::item:selected {"
" background-color: #00A663;"
" color: #ffffff;"
" border: none;"
" border-bottom: 1px solid #00A663;" /* 选中时底部边框也变色 */
"}"
"QTreeWidget::item:hover:!selected {"
" background-color: #f0f0f0;"
" color: #333333;"
"}"
"QTreeWidget::item:has-children {"
" font-weight: bold;"
" color: #333333;"
" background-color: #EEEEF0;" /* 文件夹(有子项的项)使用灰色背景 */
"}"
"QTreeWidget::item:focus {"
" outline: none;"
"}"
// 滚动条样式
"QScrollBar:vertical {"
" border: none;"
" background: #f0f0f0;"
" width: 10px;"
" margin: 0px;"
"}"
"QScrollBar::handle:vertical {"
" background: #c0c0c0;"
" min-height: 20px;"
" border-radius: 5px;"
"}"
"QScrollBar::handle:vertical:hover {"
" background: #a0a0a0;"
"}"
"QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
" height: 0px;"
"}"
// 分支展开/折叠指示器样式
"QTreeView::branch:has-children:!has-siblings:closed,"
"QTreeView::branch:closed:has-children:has-siblings {"
" border-image: none;"
"}"
"QTreeView::branch:open:has-children:!has-siblings,"
"QTreeView::branch:open:has-children:has-siblings {"
" border-image: none;"
"}"
);
}
void MainWindow::initConnections()
{
// 文件操作
connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
// 使用lambda解决openFile名称冲突
connect(openAct, &QAction::triggered, this, [this]() {
// 获取上次打开的目录,如果没有则使用用户主目录
QString startDir = m_lastOpenedDir.isEmpty() ? QDir::homePath() : m_lastOpenedDir;
QString filePath = QFileDialog::getOpenFileName(this, "打开文件",
startDir,
"所有文件 (*.*);;"
"文本文件 (*.txt);;"
"C++文件 (*.cpp *.h *.hpp);;"
"Markdown文件 (*.md);;"
"Python文件 (*.py);;"
"JavaScript文件 (*.js)");
if (!filePath.isEmpty()) {
// 保存文件所在目录
QFileInfo fileInfo(filePath);
m_lastOpenedDir = fileInfo.absolutePath();
openFileFromPath(filePath);
}
});
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
connect(saveAsAct, &QAction::triggered, this, &MainWindow::saveFileAs);
connect(closeAct, &QAction::triggered, this, [this]() { closeTab(); });
connect(exitAct, &QAction::triggered, qApp, &QApplication::quit);
// 编辑操作
connect(undoAct, &QAction::triggered, this, &MainWindow::undo);
connect(redoAct, &QAction::triggered, this, &MainWindow::redo);
connect(cutAct, &QAction::triggered, this, &MainWindow::cut);
connect(copyAct, &QAction::triggered, this, &MainWindow::copy);
connect(pasteAct, &QAction::triggered, this, &MainWindow::paste);
// 视图操作
connect(toggleFileListAct, &QAction::triggered, this, [this](bool checked) {
toggleFileList(checked);
});
connect(togglePreviewAct, &QAction::triggered, this, [this](bool checked) {
togglePreview(checked);
});
// 文件列表工具栏按钮
connect(locateBtn, &QPushButton::clicked, this, &MainWindow::locateCurrentFile);
connect(expandBtn, &QPushButton::clicked, this, &MainWindow::expandAllFiles);
connect(foldBtn, &QPushButton::clicked, this, &MainWindow::collapseAllFiles);
// 帮助
connect(aboutAct, &QAction::triggered, this, &MainWindow::showAboutDialog);
// 文件树 - 使用旧的SIGNAL/SLOT语法避免编译错误
connect(fileTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
this, SLOT(onFileItemClicked(QTreeWidgetItem*, int)));
// 标签页
connect(tabWidget, &QTabWidget::tabCloseRequested, this, &MainWindow::closeTab);
connect(tabWidget, &QTabWidget::currentChanged, this, &MainWindow::currentTabChanged);
// 右键菜单连接
connect(fileTree, &QTreeWidget::customContextMenuRequested, this, &MainWindow::showFileTreeContextMenu);
fileTree->setContextMenuPolicy(Qt::CustomContextMenu);
QShortcut *findNextShortcut = new QShortcut(QKeySequence("F3"), this);
connect(findNextShortcut, &QShortcut::activated, this, [this]() {
if (m_findReplaceDialog && m_findReplaceDialog->isVisible()) {
m_findReplaceDialog->findNextMatch();
} else {
showFindDialog();
}
});
// Shift+F3 查找上一个
QShortcut *findPrevShortcut = new QShortcut(QKeySequence("Shift+F3"), this);
connect(findPrevShortcut, &QShortcut::activated, this, [this]() {
if (m_findReplaceDialog && m_findReplaceDialog->isVisible()) {
m_findReplaceDialog->findPrevMatch();
}
});
}
// 定位当前文件
void MainWindow::locateCurrentFile()
{
QString filePath = getCurrentFilePath();
m_fileManage->locateCurrentFile(filePath, fileTree);
}
// 展开所有文件
void MainWindow::expandAllFiles()
{
m_fileManage->expandAllFiles(fileTree);
}
// 折叠所有文件
void MainWindow::collapseAllFiles()
{
m_fileManage->collapseAllFiles(fileTree);
}
QPlainTextEdit* MainWindow::createEditor()
{
LineNumberEditor *editor = new LineNumberEditor();
// 连接文本改变信号
connect(editor, &QPlainTextEdit::textChanged, this, &MainWindow::onTextChanged);
connect(editor, &QPlainTextEdit::cursorPositionChanged, this, &MainWindow::updateStatusBar);
connect(editor, &QPlainTextEdit::undoAvailable, undoAct, &QAction::setEnabled);
connect(editor, &QPlainTextEdit::redoAvailable, redoAct, &QAction::setEnabled);
connect(editor, &QPlainTextEdit::copyAvailable, cutAct, &QAction::setEnabled);
connect(editor, &QPlainTextEdit::copyAvailable, copyAct, &QAction::setEnabled);
// 为编辑器安装事件过滤器
editor->installEventFilter(this);
// 设置编辑器的上下文菜单
editor->setContextMenuPolicy(Qt::CustomContextMenu);
connect(editor, &QPlainTextEdit::customContextMenuRequested, this, &MainWindow::showEditorContextMenu);
return editor;
}
// 点击左侧文件树项
void MainWindow::onFileItemClicked(QTreeWidgetItem *item, int column)
{
Q_UNUSED(column);
if (!item) return;
QString filePath = item->data(0, Qt::UserRole).toString();
if (filePath.isEmpty()) {
// 目录项,展开/折叠即可
item->setExpanded(!item->isExpanded());
return;
}
// 检查文件是否存在
if (!QFile::exists(filePath)) {
QMessageBox::warning(this, "文件不存在", "文件已不存在或已被移动: " + filePath);
m_fileManage->removeFileFromTree(filePath, fileTree);
return;
}
// 检查是否已经在标签页中打开 - 使用完整路径比较
for (int i = 0; i < tabWidget->count(); ++i) {
QPlainTextEdit *editor = qobject_cast<QPlainTextEdit*>(tabWidget->widget(i));
if (editor && editor->property("filePath").toString() == filePath) {
tabWidget->setCurrentIndex(i);
return;
}
}
// 打开文件
openFileFromPath(filePath);
}
// 打开文件(重载函数)
void MainWindow::openFileFromPath(const QString &filePath)
{
// 检查是否已经打开
for (int i = 0; i < tabWidget->count(); ++i) {
QPlainTextEdit *editor = qobject_cast<QPlainTextEdit*>(tabWidget->widget(i));
if (editor && editor->property("filePath").toString() == filePath) {
tabWidget->setCurrentIndex(i);
return;
}
}
QPlainTextEdit *editor = createEditor();
if (!m_fileManage->openFile(filePath, editor)) {
QMessageBox::warning(this, "错误", "无法打开文件: " + filePath);
delete editor;
return;
}
// 如果是LineNumberEditor,设置文件名以启用语法高亮
LineNumberEditor *lineNumberEditor = qobject_cast<LineNumberEditor*>(editor);
if (lineNumberEditor) {
lineNumberEditor->setFileName(filePath);
}
// 根据文件类型设置是否显示缩进参考线
QStringList codeExtensions = {
"cpp", "h", "hpp", "hxx", "c", "cc",
"py", "pyw", "pyi",
"java", "js", "jsx", "ts", "tsx",
"cs", "rs", "go", "swift", "kt", "kts",
"yaml", "yml", "json", "xml", "sql"
};
bool isCodeFile = false;
for (const QString &ext : codeExtensions) {
if (filePath.endsWith("." + ext)) {
isCodeFile = true;
break;
}
}
if (lineNumberEditor) {
lineNumberEditor->setShowIndentationGuides(isCodeFile);
}
QFileInfo fileInfo(filePath);
int index = tabWidget->addTab(editor, fileInfo.fileName());
tabWidget->setCurrentIndex(index);
// 添加到左侧文件树
m_fileManage->addFileToList(filePath, fileTree);
// 在树中选中该文件
m_fileManage->selectFileInTree(filePath, fileTree);
// 保存当前打开的文件列表
QStringList openFiles;
for (int i = 0; i < tabWidget->count(); ++i) {
QPlainTextEdit *ed = qobject_cast<QPlainTextEdit*>(tabWidget->widget(i));
if (ed) {
QString fp = ed->property("filePath").toString();
if (!fp.isEmpty()) {
openFiles.append(fp);
}
}
}
m_fileManage->saveFileList(openFiles, m_fileManage->getRecentFiles());
saveAct->setEnabled(false);
saveAsAct->setEnabled(true);
closeAct->setEnabled(true);
updateWindowTitle();
updateStatusBar();
}
// 槽函数实现
void MainWindow::newFile()
{
QPlainTextEdit *editor = createEditor();
int index = tabWidget->addTab(editor, "无标题");
tabWidget->setCurrentIndex(index);
// 如果是LineNumberEditor,清除语法高亮
LineNumberEditor *lineNumberEditor = qobject_cast<LineNumberEditor*>(editor);
if (lineNumberEditor) {
lineNumberEditor->setFileName("");