forked from linuxdeepin/deepin-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtextedit.cpp
More file actions
8202 lines (7049 loc) · 296 KB
/
dtextedit.cpp
File metadata and controls
8202 lines (7049 loc) · 296 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
// SPDX-FileCopyrightText: 2011-2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "../common/utils.h"
#include "../widgets/window.h"
#include "../widgets/bottombar.h"
#include "dtextedit.h"
#include "leftareaoftextedit.h"
#include "editwrapper.h"
#include "showflodcodewidget.h"
#include "deletebackcommond.h"
#include "replaceallcommond.h"
#include "insertblockbytextcommond.h"
#include "indenttextcommond.h"
#include "undolist.h"
#include "changemarkcommand.h"
#include "endlineformatcommond.h"
#include <KSyntaxHighlighting/definition.h>
#include <KSyntaxHighlighting/syntaxhighlighter.h>
#include <KSyntaxHighlighting/theme.h>
#include <QAbstractTextDocumentLayout>
#include <QTextDocumentFragment>
#include <QInputMethodEvent>
#include <DDesktopServices>
#include <QApplication>
#include <DSettingsGroup>
#include <DSettingsOption>
#include <DSettings>
#include <QClipboard>
#include <QFileInfo>
#include <QDebug>
#include <QPainter>
#include <QScroller>
#include <QScrollBar>
#include <QStyleFactory>
#include <QTextBlock>
#include <QMimeData>
#include <QTimer>
#include <QGesture>
#include <QStyleHints>
#include <DSysInfo>
#include <private/qguiapplication_p.h>
#include <qpa/qplatformtheme.h>
#include <QtSvg/qsvgrenderer.h>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusReply>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
TextEdit::TextEdit(QWidget *parent)
: DPlainTextEdit(parent),
m_wrapper(nullptr)
{
// 更新单独添加的高亮格式文件
m_repository.addCustomSearchPath(KF5_HIGHLIGHT_PATH);
setUndoRedoEnabled(false);
//撤销重做栈
m_pUndoStack = new QUndoStack();
m_nLines = 0;
m_nBookMarkHoverLine = -1;
m_bIsFileOpen = false;
m_qstrCommitString = "";
m_bIsShortCut = false;
m_bIsInputMethod = false;
m_isSelectAll = false;
m_touchTapDistance = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::TouchDoubleTapDistance).toInt();
m_fontLineNumberArea.setFamily("SourceHanSansSC-Normal");
m_pLeftAreaWidget = new LeftAreaTextEdit(this);
m_pLeftAreaWidget->m_pFlodArea->setAttribute(Qt::WA_Hover, true); //开启悬停事件
m_pLeftAreaWidget->m_pBookMarkArea->setAttribute(Qt::WA_Hover, true);
m_pLeftAreaWidget->m_pFlodArea->installEventFilter(this);
m_pLeftAreaWidget->m_pBookMarkArea->installEventFilter(this);
m_foldCodeShow = new ShowFlodCodeWidget(this);
m_foldCodeShow->setVisible(false);
viewport()->installEventFilter(this);
viewport()->setCursor(Qt::IBeamCursor);
// Don't draw frame around editor widget.
setFrameShape(QFrame::NoFrame);
setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
setContentsMargins(0, 0, 0, 0);
setEditPalette(palette().foreground().color().name(), palette().foreground().color().name());
grabGesture(Qt::TapGesture);
grabGesture(Qt::TapAndHoldGesture);
grabGesture(Qt::SwipeGesture);
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
// Init widgets.
//左边栏控件 滑动条滚动跟新行号 折叠标记
connect(this->verticalScrollBar(), &QScrollBar::valueChanged, this, &TextEdit::slotValueChanged);
connect(this, &QPlainTextEdit::textChanged, this, &TextEdit::updateLeftAreaWidget);
connect(this, &QPlainTextEdit::textChanged, this, [this]() {
this->m_wrapper->UpdateBottomBarWordCnt(this->characterCount());
});
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &TextEdit::cursorPositionChanged);
connect(this, &QPlainTextEdit::selectionChanged, this, &TextEdit::onSelectionArea);
connect(document(), &QTextDocument::contentsChange, this, &TextEdit::updateMark);
connect(document(), &QTextDocument::contentsChange, this, &TextEdit::checkBookmarkLineMove);
connect(document(), &QTextDocument::contentsChange, this, &TextEdit::onTextContentChanged);
connect(m_pUndoStack, &QUndoStack::canRedoChanged, this, &TextEdit::slotCanRedoChanged);
connect(m_pUndoStack, &QUndoStack::canUndoChanged, this, &TextEdit::slotCanUndoChanged);
QDBusConnection dbus = QDBusConnection::sessionBus();
switch (Utils::getSystemVersion()) {
case Utils::V23:
dbus.systemBus().connect("org.deepin.dde.Gesture1",
"/org/deepin/dde/Gesture1", "org.deepin.dde.Gesture1",
"Event",
this, SLOT(fingerZoom(QString, QString, int)));
break;
default:
dbus.systemBus().connect("com.deepin.daemon.Gesture",
"/com/deepin/daemon/Gesture", "com.deepin.daemon.Gesture",
"Event",
this, SLOT(fingerZoom(QString, QString, int)));
break;
}
// 连接音频设备状态变化信号
dbus.sessionBus().connect("com.deepin.daemon.Audio",
"/com/deepin/daemon/Audio", "com.deepin.daemon.Audio",
"PortEnabledChanged",
this, SLOT(onAudioPortEnabledChanged(quint32, QString, bool)));
//初始化右键菜单
initRightClickedMenu();
// Init scroll animation.
m_scrollAnimation = new QPropertyAnimation(verticalScrollBar(), "value");
m_scrollAnimation->setEasingCurve(QEasingCurve::InOutExpo);
m_scrollAnimation->setDuration(300);
m_cursorMode = Insert;
connect(m_scrollAnimation, &QPropertyAnimation::finished, this, &TextEdit::handleScrollFinish, Qt::QueuedConnection);
// Monitor cursor mark status to update in line number area.
connect(this, &TextEdit::cursorMarkChanged, this, &TextEdit::handleCursorMarkChanged);
// configure content area
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &TextEdit::adjustScrollbarMargins, Qt::QueuedConnection);
// Don't blink the cursor when selecting text
// Recover blink when not selecting text.
connect(this, &TextEdit::selectionChanged, this, &TextEdit::slotSelectionChanged, Qt::QueuedConnection);
}
TextEdit::~TextEdit()
{
if (m_scrollAnimation != nullptr) {
if (m_scrollAnimation->state() != QAbstractAnimation::Stopped) {
m_scrollAnimation->stop();
}
delete m_scrollAnimation;
m_scrollAnimation = nullptr;
}
if (m_colorMarkMenu != nullptr) {
delete m_colorMarkMenu;
m_colorMarkMenu = nullptr;
}
if (m_convertCaseMenu != nullptr) {
delete m_convertCaseMenu;
m_convertCaseMenu = nullptr;
}
if (m_rightMenu != nullptr) {
delete m_rightMenu;
m_rightMenu = nullptr;
}
if (m_pUndoStack != nullptr) {
m_pUndoStack->deleteLater();
}
}
void TextEdit::insertTextEx(QTextCursor cursor, QString text)
{
QUndoCommand *pInsertStack = new InsertTextUndoCommand(cursor, text, this);
m_pUndoStack->push(pInsertStack);
ensureCursorVisible();
}
/**
* @brief 将多组插入信息 \a multiText 压入单个撤销栈,便于撤销栈管理。
* @param multiText 多组插入信息,每组含插入光标位置和插入文本。
*/
void TextEdit::insertMultiTextEx(const QList<QPair<QTextCursor, QString> > &multiText)
{
if (multiText.isEmpty()) {
return;
}
QUndoCommand *pMultiCommand = new QUndoCommand;
// 将所有的插入信息添加到单个撤销项中,便于单次处理
for (auto pairText : multiText) {
// pMultiCommand 析构时会自动析构子撤销项
(void)new InsertTextUndoCommand(pairText.first, pairText.second, this, pMultiCommand);
}
m_pUndoStack->push(pMultiCommand);
ensureCursorVisible();
}
void TextEdit::deleteSelectTextEx(QTextCursor cursor)
{
if (cursor.hasSelection()) {
QUndoCommand *pDeleteStack = new DeleteTextUndoCommand(cursor, this);
m_pUndoStack->push(pDeleteStack);
}
}
void TextEdit::deleteSelectTextEx(QTextCursor cursor, QString text, bool currLine)
{
QUndoCommand *pDeleteStack = new DeleteTextUndoCommand2(cursor, text, this, currLine);
m_pUndoStack->push(pDeleteStack);
}
void TextEdit::deleteTextEx(QTextCursor cursor)
{
QUndoCommand *pDeleteStack = new DeleteTextUndoCommand(cursor, this);
m_pUndoStack->push(pDeleteStack);
}
/**
* @brief 将多组删除信息 \a multiText 压入单个撤销栈,便于撤销栈管理。
* @param multiText 多组删除信息,每组含删除光标信息(删除位置和选取区域)。
*/
void TextEdit::deleteMultiTextEx(const QList<QTextCursor> &multiText)
{
if (multiText.isEmpty()) {
return;
}
QUndoCommand *pMultiCommand = new QUndoCommand;
// 将所有的插入信息添加到单个撤销项中,便于单次处理
for (auto textCursor : multiText) {
// pMultiCommand 析构时会自动析构子撤销项
(void)new DeleteTextUndoCommand(textCursor, this, pMultiCommand);
}
m_pUndoStack->push(pMultiCommand);
}
void TextEdit::insertSelectTextEx(QTextCursor cursor, QString text)
{
QUndoCommand *pInsertStack = new InsertTextUndoCommand(cursor, text, this);
m_pUndoStack->push(pInsertStack);
ensureCursorVisible();
}
void TextEdit::insertColumnEditTextEx(QString text)
{
if (m_isSelectAll) {
QPlainTextEdit::selectAll();
}
for (int i = 0; i < m_altModSelections.size(); i++) {
if (m_altModSelections[i].cursor.hasSelection()) deleteTextEx(m_altModSelections[i].cursor);
}
QUndoCommand *pInsertStack = new InsertTextUndoCommand(m_altModSelections, text, this);
m_pUndoStack->push(pInsertStack);
ensureCursorVisible();
}
void TextEdit::initRightClickedMenu()
{
// Init menu.
m_rightMenu = new DMenu();
m_undoAction = new QAction(tr("Undo"), this);
m_redoAction = new QAction(tr("Redo"), this);
m_cutAction = new QAction(tr("Cut"), this);
m_copyAction = new QAction(tr("Copy"), this);
m_pasteAction = new QAction(tr("Paste"), this);
m_deleteAction = new QAction(tr("Delete"), this);
m_selectAllAction = new QAction(tr("Select All"), this);
m_findAction = new QAction(tr("Find"), this);
m_replaceAction = new QAction(tr("Replace"), this);
m_jumpLineAction = new QAction(tr("Go to Line"), this);
m_enableReadOnlyModeAction = new QAction(tr("Turn on Read-Only mode"), this);
m_disableReadOnlyModeAction = new QAction(tr("Turn off Read-Only mode"), this);
m_fullscreenAction = new QAction(tr("Fullscreen"), this);
m_exitFullscreenAction = new QAction(tr("Exit fullscreen"), this);
m_openInFileManagerAction = new QAction(tr("Display in file manager"), this);
m_toggleCommentAction = new QAction(tr("Add Comment"), this);
m_voiceReadingAction = new QAction(tr("Text to Speech"), this);
m_dictationAction = new QAction(tr("Speech to Text"), this);
// m_translateAction = new QAction(tr("Translate"), this);
m_columnEditAction = new QAction(tr("Column Mode"), this);
m_addBookMarkAction = new QAction(tr("Add bookmark"), this);
m_cancelBookMarkAction = new QAction(tr("Remove Bookmark"), this);
m_preBookMarkAction = new QAction(tr("Previous bookmark"), this);
m_nextBookMarkAction = new QAction(tr("Next bookmark"), this);
m_clearBookMarkAction = new QAction(tr("Remove All Bookmarks"), this);
m_flodAllLevel = new QAction(tr("Fold All"), this);
m_flodCurrentLevel = new QAction(tr("Fold Current Level"), this);
m_unflodAllLevel = new QAction(tr("Unfold All"), this);
m_unflodCurrentLevel = new QAction(tr("Unfold Current Level"), this);
//yanyuhan
//颜色标记、折叠/展开、书签、列编辑、设置注释、取消注释;
//点击颜色标记菜单,显示二级菜单,包括:标记、清除上次标记、清除标记、标记所有;
m_colorMarkMenu = new DMenu(tr("Color Mark"));
// 为颜色标记Menu,增加事件过滤
m_colorMarkMenu->installEventFilter(this);
m_cancleMarkAllLine = new QAction(tr("Clear All Marks"), this);
m_cancleLastMark = new QAction(tr("Clear Last Mark"), this);
//添加当前颜色选择控件 梁卫东
ColorSelectWdg *pColorsSelectWdg = new ColorSelectWdg(QString(), this);
connect(pColorsSelectWdg, &ColorSelectWdg::sigColorSelected, this, &TextEdit::slotSigColorSelected);
m_actionColorStyles = new QWidgetAction(this);
m_actionColorStyles->setDefaultWidget(pColorsSelectWdg);
m_markCurrentAct = new QAction(tr("Mark"), this);
connect(m_markCurrentAct, &QAction::triggered, this, [this, pColorsSelectWdg]() {
isMarkCurrentLine(true, pColorsSelectWdg->getDefaultColor().name());
renderAllSelections();
});
//添加全部颜色选择控件 梁卫东
ColorSelectWdg *pColorsAllSelectWdg = new ColorSelectWdg(QString(), this);
connect(pColorsAllSelectWdg, &ColorSelectWdg::sigColorSelected, this, &TextEdit::slotSigColorAllSelected);
m_actionAllColorStyles = new QWidgetAction(this);
m_actionAllColorStyles->setDefaultWidget(pColorsAllSelectWdg);
m_markAllAct = new QAction(tr("Mark All"), this);
connect(m_markAllAct, &QAction::triggered, this, [this, pColorsAllSelectWdg]() {
m_strMarkAllLineColorName = pColorsAllSelectWdg->getDefaultColor().name();
isMarkAllLine(true, pColorsAllSelectWdg->getDefaultColor().name());
renderAllSelections();
});
m_addComment = new QAction(tr("Add Comment"), this);
m_cancelComment = new QAction(tr("Remove Comment"), this);
connect(m_rightMenu, &DMenu::aboutToHide, this, &TextEdit::removeHighlightWordUnderCursor);
connect(m_undoAction, &QAction::triggered, this, [ = ]() {
this->undo_();
});
connect(m_redoAction, &QAction::triggered, this, [ = ]() {
this->redo_();
});
connect(m_cutAction, &QAction::triggered, this, &TextEdit::slotCutAction);
connect(m_copyAction, &QAction::triggered, this, &TextEdit::slotCopyAction);
connect(m_pasteAction, &QAction::triggered, this, &TextEdit::slotPasteAction);
connect(m_deleteAction, &QAction::triggered, this, &TextEdit::slotDeleteAction);
connect(m_selectAllAction, &QAction::triggered, this, &TextEdit::slotSelectAllAction);
connect(m_findAction, &QAction::triggered, this, &TextEdit::clickFindAction);
connect(m_replaceAction, &QAction::triggered, this, &TextEdit::clickReplaceAction);
connect(m_jumpLineAction, &QAction::triggered, this, &TextEdit::clickJumpLineAction);
connect(m_fullscreenAction, &QAction::triggered, this, &TextEdit::clickFullscreenAction);
connect(m_exitFullscreenAction, &QAction::triggered, this, &TextEdit::clickFullscreenAction);
connect(m_enableReadOnlyModeAction, &QAction::triggered, this, &TextEdit::toggleReadOnlyMode);
connect(m_disableReadOnlyModeAction, &QAction::triggered, this, &TextEdit::toggleReadOnlyMode);
connect(m_openInFileManagerAction, &QAction::triggered, this, &TextEdit::slotOpenInFileManagerAction);
connect(m_addComment, &QAction::triggered, this, &TextEdit::slotAddComment);
connect(m_cancelComment, &QAction::triggered, this, &TextEdit::slotCancelComment);
connect(m_voiceReadingAction, &QAction::triggered, this, &TextEdit::slotVoiceReadingAction);
connect(m_dictationAction, &QAction::triggered, this, &TextEdit::slotdictationAction);
// connect(m_translateAction, &QAction::triggered, this, &TextEdit::slot_translate);
connect(m_columnEditAction, &QAction::triggered, this, &TextEdit::slotColumnEditAction);
connect(m_addBookMarkAction, &QAction::triggered, this, &TextEdit::addOrDeleteBookMark);
connect(m_cancelBookMarkAction, &QAction::triggered, this, &TextEdit::addOrDeleteBookMark);
connect(m_preBookMarkAction, &QAction::triggered, this, &TextEdit::slotPreBookMarkAction);
connect(m_nextBookMarkAction, &QAction::triggered, this, &TextEdit::slotNextBookMarkAction);
connect(m_clearBookMarkAction, &QAction::triggered, this, &TextEdit::slotClearBookMarkAction);
connect(m_flodAllLevel, &QAction::triggered, this, &TextEdit::slotFlodAllLevel);
connect(m_unflodAllLevel, &QAction::triggered, this, &TextEdit::slotUnflodAllLevel);
connect(m_flodCurrentLevel, &QAction::triggered, this, &TextEdit::slotFlodCurrentLevel);
connect(m_unflodCurrentLevel, &QAction::triggered, this, &TextEdit::slotUnflodCurrentLevel);
connect(m_cancleMarkAllLine, &QAction::triggered, this, &TextEdit::slotCancleMarkAllLine);
connect(m_cancleLastMark, &QAction::triggered, this, &TextEdit::slotCancleLastMark);
// Init convert case sub menu.
m_haveWordUnderCursor = false;
m_convertCaseMenu = new DMenu(tr("Change Case"));
m_upcaseAction = new QAction(tr("Upper Case"), this);
m_downcaseAction = new QAction(tr("Lower Case"), this);
m_capitalizeAction = new QAction(tr("Capitalize"), this);
m_convertCaseMenu->addAction(m_upcaseAction);
m_convertCaseMenu->addAction(m_downcaseAction);
m_convertCaseMenu->addAction(m_capitalizeAction);
connect(m_upcaseAction, &QAction::triggered, this, &TextEdit::upcaseWord);
connect(m_downcaseAction, &QAction::triggered, this, &TextEdit::downcaseWord);
connect(m_capitalizeAction, &QAction::triggered, this, &TextEdit::capitalizeWord);
m_canUndo = false;
m_canRedo = false;
connect(this, &TextEdit::undoAvailable, this, &TextEdit::slotUndoAvailable);
connect(this, &TextEdit::redoAvailable, this, &TextEdit::slotRedoAvailable);
}
void TextEdit::popRightMenu(QPoint pos)
{
//清除菜单分割线残影
if (m_rightMenu != nullptr) {
delete m_rightMenu;
m_rightMenu = nullptr;
}
m_rightMenu = new DMenu;
m_rightMenu->clear();
QTextCursor selectionCursor = textCursor();
selectionCursor.movePosition(QTextCursor::StartOfBlock);
selectionCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
QString text = selectionCursor.selectedText();
// init base.
bool isBlankLine = text.trimmed().isEmpty();
bool isAddUndoRedo = false;
if (m_pUndoStack->canUndo() && m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_undoAction);
isAddUndoRedo = true;
}
if (m_pUndoStack->canRedo() && m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_redoAction);
isAddUndoRedo = true;
}
if (isAddUndoRedo) {
m_rightMenu->addSeparator();
}
if (textCursor().hasSelection() || m_hasColumnSelection) {
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_cutAction);
}
m_rightMenu->addAction(m_copyAction);
}
if (canPaste()) {
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_pasteAction);
}
}
if (textCursor().hasSelection() || m_hasColumnSelection) {
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_deleteAction);
}
}
if (!document()->isEmpty()) {
m_rightMenu->addAction(m_selectAllAction);
}
m_rightMenu->addSeparator();
if (!document()->isEmpty()) {
m_rightMenu->addAction(m_findAction);
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_replaceAction);
}
m_rightMenu->addAction(m_jumpLineAction);
m_rightMenu->addSeparator();
}
if (textCursor().hasSelection()) {
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addMenu(m_convertCaseMenu);
}
} else {
m_convertCaseMenu->hide();
}
// intelligent judge whether to support comments.
const auto def = m_repository.definitionForFileName(QFileInfo(m_sFilePath).fileName());
if (characterCount() &&
(textCursor().hasSelection() || !isBlankLine) &&
!def.filePath().isEmpty()) {
/*
* 不支持注释的文件类型,右键菜单不显示“添加注释/取消注释”
* 不支持注释的文件类型:Markdown(.d)/vCard(.vcf)/JSON(.json)
*/
if (def.name() != "Markdown"
&& !def.name().contains(QString("vCard"))
&& !def.name().contains(QString("JSON"))) {
m_rightMenu->addAction(m_addComment);
m_rightMenu->addAction(m_cancelComment);
}
}
if (m_bReadOnlyPermission || m_readOnlyMode) {
m_addComment->setEnabled(false);
m_cancelComment->setEnabled(false);
} else {
m_addComment->setEnabled(true);
m_cancelComment->setEnabled(true);
}
m_rightMenu->addSeparator();
if (m_bReadOnlyPermission == false) {
if (m_readOnlyMode) {
m_rightMenu->addAction(m_disableReadOnlyModeAction);
} else {
m_rightMenu->addAction(m_enableReadOnlyModeAction);
}
}
m_rightMenu->addAction(m_openInFileManagerAction);
m_rightMenu->addSeparator();
if (static_cast<Window *>(this->window())->isFullScreen()) {
m_rightMenu->addAction(m_exitFullscreenAction);
} else {
m_rightMenu->addAction(m_fullscreenAction);
}
/* 专业版/家庭版/教育版鼠标右键菜单支持语音读写 */
/* 更换成只要发现有com.iflytek.aiassistant服务已经注册开启,则开启支持语音读写功能 */
/*if ((DSysInfo::uosEditionType() == DSysInfo::UosEdition::UosProfessional) ||
(DSysInfo::uosEditionType() == DSysInfo::UosEdition::UosHome) ||
(DSysInfo::uosEditionType() == DSysInfo::UosEdition::UosEducation)) {*/
m_rightMenu->addAction(m_voiceReadingAction);
m_voiceReadingAction->setEnabled(true);
if (checkAudioOutputDevice()) {
bool voiceReadingState = false;
QDBusMessage voiceReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant",
"/aiassistant/tts",
"com.iflytek.aiassistant.tts",
"getTTSEnable");
QDBusReply<bool> voiceReadingStateRet = QDBusConnection::sessionBus().asyncCall(voiceReadingMsg, 100);
//没有收到返回就加载配置文件数据
if (voiceReadingStateRet.isValid()) {
voiceReadingState = voiceReadingStateRet.value();
if ((textCursor().hasSelection() && voiceReadingState) || m_hasColumnSelection) {
m_voiceReadingAction->setEnabled(true);
} else {
m_voiceReadingAction->setEnabled(false);
}
} else {
m_voiceReadingAction->setEnabled(true);
}
} else {
m_voiceReadingAction->setEnabled(true);
}
m_rightMenu->addAction(m_dictationAction);
if (checkAudioInputDevice()) {
bool dictationState = false;
QDBusMessage dictationMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant",
"/aiassistant/iat",
"com.iflytek.aiassistant.iat",
"getIatEnable");
QDBusReply<bool> dictationStateRet = QDBusConnection::sessionBus().asyncCall(dictationMsg, 100);
//没有收到返回就加载配置文件数据
if (dictationStateRet.isValid()) {
dictationState = dictationStateRet.value();
m_dictationAction->setEnabled(dictationState);
} else {
m_dictationAction->setEnabled(true);
}
if (m_bReadOnlyPermission || m_readOnlyMode) {
m_dictationAction->setEnabled(false);
}
} else {
m_dictationAction->setEnabled(true);
}
/*
m_translateAction->setEnabled(false);
bool translateState = false;
QDBusMessage translateReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant",
"/aiassistant/trans",
"com.iflytek.aiassistant.trans",
"getTransEnable");
QDBusReply<bool> translateStateRet = QDBusConnection::sessionBus().asyncCall(translateReadingMsg, 100);
//没有收到返回就加载配置文件数据
if (translateStateRet.isValid()) {
m_rightMenu->addAction(m_translateAction);
translateState = translateStateRet.value();
} else {
translateState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-trans");
}
if ((textCursor().hasSelection() && translateState) || m_hasColumnSelection) {
m_translateAction->setEnabled(translateState);
}
*/
if (!this->document()->isEmpty()) {
m_colorMarkMenu->clear();
// 清空 tab order list
m_MarkColorMenuTabOrder.clear();
// 增加 Mark Color Menu Item
m_colorMarkMenu->addAction(m_markCurrentAct);
m_colorMarkMenu->addAction(m_actionColorStyles);
// 将对应 Mark Color Menu Item 加入 Tab Order
// QPair<QAction*, bool> , bool决定tab过程中是否可以获取focus
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_markCurrentAct, true));
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_actionColorStyles, false));
// 增加 Mark Color Menu Item
m_colorMarkMenu->addSeparator();
m_colorMarkMenu->addAction(m_markAllAct);
m_colorMarkMenu->addAction(m_actionAllColorStyles);
m_colorMarkMenu->addSeparator();
// 将对应 Mark Color Menu Item 加入 Tab Order
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_markAllAct, true));
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_actionAllColorStyles, false));
if (m_markOperations.size() > 0) {
// 增加 Mark Color Menu Item
m_colorMarkMenu->addAction(m_cancleLastMark);
m_colorMarkMenu->addSeparator();
m_colorMarkMenu->addAction(m_cancleMarkAllLine);
// 将对应 Mark Color Menu Item 加入 Tab Order
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_cancleLastMark, true));
m_MarkColorMenuTabOrder.append(QPair<QAction *, bool>(m_cancleMarkAllLine, true));
}
m_rightMenu->addSeparator();
if (m_bReadOnlyPermission == false && m_readOnlyMode == false) {
m_rightMenu->addAction(m_columnEditAction);
}
m_rightMenu->addMenu(m_colorMarkMenu);
}
QPoint pos1 = cursorRect().bottomRight();
//当全选大文本 坐标超出屏幕外显示不了 梁卫东 2020-08-19 10:23:29
if (pos1.y() > this->rect().height()) {
pos1.setY((this->rect().height()) / 2);
pos1.setX((this->rect().width()) / 2);
}
if (pos.isNull()) {
m_rightMenu->exec(mapToGlobal(pos1));
} else {
m_rightMenu->exec(pos);
}
}
void TextEdit::setWrapper(EditWrapper *w)
{
m_wrapper = w;
}
EditWrapper *TextEdit::getWrapper()
{
return m_wrapper;
}
bool TextEdit::isUndoRedoOpt()
{
return (m_pUndoStack->canRedo() || m_pUndoStack->canUndo());
}
bool TextEdit::getModified()
{
return (document()->isModified() && (m_pUndoStack->canUndo() || m_pUndoStack->index() != m_lastSaveIndex));
}
int TextEdit::getCurrentLine()
{
return textCursor().blockNumber() + 1;
}
int TextEdit::getCurrentColumn()
{
return textCursor().columnNumber();
}
int TextEdit::getPosition()
{
return textCursor().position();
}
int TextEdit::getScrollOffset()
{
return verticalScrollBar()->value();
}
void TextEdit::forwardChar()
{
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::NextCharacter);
}
}
void TextEdit::backwardChar()
{
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::PreviousCharacter);
}
}
void TextEdit::forwardWord()
{
QTextCursor cursor = textCursor();
if (m_cursorMark) {
cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
} else {
cursor.movePosition(QTextCursor::NextWord, QTextCursor::MoveAnchor);
}
setTextCursor(cursor);
}
void TextEdit::backwardWord()
{
QTextCursor cursor = textCursor();
if (m_cursorMark) {
// cursor.setPosition(getPrevWordPosition(cursor, QTextCursor::KeepAnchor), QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor);
} else {
// cursor.setPosition(getPrevWordPosition(cursor, QTextCursor::MoveAnchor), QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor);
}
setTextCursor(cursor);
}
void TextEdit::forwardPair()
{
// Record cursor and seleciton position before move cursor.
int actionStartPos = textCursor().position();
int selectionStartPos = textCursor().selectionStart();
int selectionEndPos = textCursor().selectionEnd();
// Because find always search start from selection end position.
// So we need clear selection to make search start from cursor.
QTextCursor removeSelectionCursor = textCursor();
removeSelectionCursor.clearSelection();
setTextCursor(removeSelectionCursor);
// Start search.
if (find(QRegExp("[\]>)}]"))) {
int findPos = textCursor().position();
QTextCursor cursor = textCursor();
auto moveMode = m_cursorMark ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;
if (actionStartPos == selectionStartPos) {
cursor.setPosition(selectionEndPos, QTextCursor::MoveAnchor);
cursor.setPosition(findPos, moveMode);
} else {
cursor.setPosition(selectionStartPos, QTextCursor::MoveAnchor);
cursor.setPosition(findPos, moveMode);
}
setTextCursor(cursor);
}
}
void TextEdit::backwardPair()
{
// Record cursor and seleciton position before move cursor.
int actionStartPos = textCursor().position();
int selectionStartPos = textCursor().selectionStart();
int selectionEndPos = textCursor().selectionEnd();
// Because find always search start from selection end position.
// So we need clear selection to make search start from cursor.
QTextCursor removeSelectionCursor = textCursor();
removeSelectionCursor.clearSelection();
setTextCursor(removeSelectionCursor);
QTextDocument::FindFlags options;
options |= QTextDocument::FindBackward;
if (find(QRegExp("[\[<({]"), options)) {
QTextCursor cursor = textCursor();
auto moveMode = m_cursorMark ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor);
int findPos = cursor.position();
if (actionStartPos == selectionStartPos) {
cursor.setPosition(selectionEndPos, QTextCursor::MoveAnchor);
cursor.setPosition(findPos, moveMode);
setTextCursor(cursor);
} else {
cursor.setPosition(selectionStartPos, QTextCursor::MoveAnchor);
cursor.setPosition(findPos, moveMode);
setTextCursor(cursor);
}
}
}
int TextEdit::blockCount() const
{
return document()->blockCount();
}
int TextEdit::characterCount() const
{
return document()->characterCount();
}
QTextBlock TextEdit::firstVisibleBlock()
{
return document()->findBlockByLineNumber(getFirstVisibleBlockId());
}
void TextEdit::moveToStart()
{
verticalScrollBar()->setValue(0);
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::Start);
}
// 移动展示区域,手动高亮文本
m_wrapper->OnUpdateHighlighter();
}
void TextEdit::moveToEnd()
{
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::End);
}
// 移动展示区域,手动高亮文本
m_wrapper->OnUpdateHighlighter();
}
void TextEdit::moveToStartOfLine()
{
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::StartOfBlock);
}
}
void TextEdit::moveToEndOfLine()
{
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::EndOfBlock);
}
}
void TextEdit::moveToLineIndentation()
{
// Init cursor and move type.
QTextCursor cursor = textCursor();
// Get line start position.
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
int startColumn = cursor.columnNumber();
// Get line end position.
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor);
int endColumn = cursor.columnNumber();
// Move the cursor to line start first while keep the anchor to end of block.
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
int column = startColumn;
while (column < endColumn)
{
QChar currentChar = *cursor.selection().toPlainText().data();
if (!currentChar.isSpace())
{
//stop and reset anchor while be at row indentation.
cursor.setPosition(cursor.position(), QTextCursor::MoveAnchor);
break;
}
else
{
//while including 'space',just move ahead.
cursor.setPosition(cursor.position() + 1, QTextCursor::KeepAnchor);
}
column++;
}
cursor.clearSelection();
setTextCursor(cursor);
}
void TextEdit::nextLine()
{
m_isSelectAll = false;
if (!characterCount())
return;
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::Down);
}
if (m_wrapper != nullptr) {
m_wrapper->OnUpdateHighlighter();
if ((m_wrapper->window()->findBarIsVisiable() || m_wrapper->window()->replaceBarIsVisiable()) &&
(QString::compare(m_wrapper->window()->getKeywordForSearchAll(), m_wrapper->window()->getKeywordForSearch(), Qt::CaseInsensitive) == 0)) {
highlightKeywordInView(m_wrapper->window()->getKeywordForSearchAll());
}
markAllKeywordInView();
}
}
void TextEdit::prevLine()
{
m_isSelectAll = false;
if (!characterCount())
return;
if (m_cursorMark) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::Up, QTextCursor::KeepAnchor);
setTextCursor(cursor);
} else {
moveCursorNoBlink(QTextCursor::Up);
}
if (m_wrapper != nullptr) {
m_wrapper->OnUpdateHighlighter();
if ((m_wrapper->window()->findBarIsVisiable() || m_wrapper->window()->replaceBarIsVisiable()) &&
(QString::compare(m_wrapper->window()->getKeywordForSearchAll(), m_wrapper->window()->getKeywordForSearch(), Qt::CaseInsensitive) == 0)) {
highlightKeywordInView(m_wrapper->window()->getKeywordForSearchAll());
}
markAllKeywordInView();
}
}
void TextEdit::moveCursorNoBlink(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode)
{
// Function moveCursorNoBlink will blink cursor when move cursor.
// But function movePosition won't, so we use movePosition to avoid that cursor link when moving cursor.
QTextCursor cursor = textCursor();
cursor.movePosition(operation, mode);
setTextCursor(cursor);
}
void TextEdit::jumpToLine(int line, bool keepLineAtCenter)
{
QTextCursor cursor(document()->findBlockByNumber(line - 1)); // line - 1 because line number starts from 0
//verticalScrollBar()->setValue(fontMetrics().height() * line - height());
// Update cursor.
setTextCursor(cursor);
if (keepLineAtCenter) {
keepCurrentLineAtCenter();
}
m_pLeftAreaWidget->m_pLineNumberArea->update();
}
void TextEdit::newline()
{
// Stop mark if mark is set.
tryUnsetMark();
QTextCursor cursor = textCursor();
auto com = new InsertTextUndoCommand(cursor, "\n", this);
m_pUndoStack->push(com);
setTextCursor(cursor);
}