forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCommandLine.cpp
1562 lines (1399 loc) · 59.5 KB
/
TCommandLine.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
/***************************************************************************
* Copyright (C) 2008-2012 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2018-2020, 2022-2024 by Stephen Lyons *
* - slysven@virginmedia.com *
* Copyright (C) 2023 by Lecker Kebap - Leris@mudlet.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TCommandLine.h"
#include "Host.h"
#include "TConsole.h"
#include "TMainConsole.h"
#include "TTabBar.h"
#include "TTextEdit.h"
#include "TEvent.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QKeyEvent>
#include <QRegularExpression>
#include <QScrollBar>
#include <QSaveFile>
#include "post_guard.h"
TCommandLine::TCommandLine(Host* pHost, const QString& name, CommandLineType type, TConsole* pConsole, QWidget* parent)
: QPlainTextEdit(parent)
, mCommandLineName(name)
, mpHost(pHost)
, mType(type)
, mpKeyUnit(pHost->getKeyUnit())
, mpConsole(pConsole)
{
setObjectName(qsl("commandLine_%1_%2").arg(mpHost->getName(), name));
setAutoFillBackground(true);
setFocusPolicy(Qt::StrongFocus);
setFont(mpHost->getDisplayFont());
document()->setDocumentMargin(2);
mRegularPalette.setColor(QPalette::Text, mpHost->mCommandLineFgColor);
mRegularPalette.setColor(QPalette::Highlight, QColor(0, 0, 192));
mRegularPalette.setColor(QPalette::HighlightedText, QColor(Qt::white));
mRegularPalette.setColor(QPalette::Base, mpHost->mCommandLineBgColor);
setPalette(mRegularPalette);
//style subCommandLines by stylesheet
if (mType != MainCommandLine) {
const QColor c = mpHost->mCommandLineBgColor;
const QString styleSheet{qsl("QPlainTextEdit{background-color: rgb(%1, %2, %3);}").arg(c.red()).arg(c.green()).arg(c.blue())};
setStyleSheet(styleSheet);
}
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCenterOnScroll(false);
setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
setContentsMargins(0, 0, 0, 0);
// clear console selection if selection in command line changes
connect(this, &QPlainTextEdit::copyAvailable, this, &TCommandLine::slot_clearSelection);
// We do NOT want the standard context menu to happen as we generate it
// ourself:
setContextMenuPolicy(Qt::PreventContextMenu);
connect(mudlet::self(), &mudlet::signal_adjustAccessibleNames, this, &TCommandLine::slot_adjustAccessibleNames);
slot_adjustAccessibleNames();
// Restore the history settings:
std::tie(mBackingFileName, mSaveCommands) = mpHost->getCmdLineSettings(mType, name);
// Restore any previous historic commands even if we are not going to save
// them under current settings:
restoreHistory();
connect(pHost, &Host::signal_saveCommandLinesHistory, this, &TCommandLine::slot_saveHistory);
}
void TCommandLine::processNormalKey(QEvent* event)
{
QPlainTextEdit::event(event);
adjustHeight();
mHistoryBuffer = 0;
if (mTabCompletionOld != toPlainText()) {
mUserKeptOnTyping = true;
mAutoCompletionCount = -1;
} else {
mUserKeptOnTyping = false;
}
spellCheck();
}
bool TCommandLine::keybindingMatched(QKeyEvent* keyEvent)
{
if (mpKeyUnit->processDataStream(static_cast<Qt::Key>(keyEvent->key()), static_cast<Qt::KeyboardModifiers>(keyEvent->modifiers()))) {
keyEvent->accept();
return true;
}
return false;
}
// This function overrides the QWidget::event() and should return true if the
// event was recognized, otherwise it should return false. If the recognized
// event was accepted (see QEvent::accepted), any further processing such as
// event propagation to the parent widget stops.
bool TCommandLine::event(QEvent* event)
{
const Qt::KeyboardModifiers allModifiers = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier | Qt::GroupSwitchModifier;
if (event->type() == QEvent::KeyPress) {
auto* ke = dynamic_cast<QKeyEvent*>(event);
if (!ke) {
// Something is wrong -
qCritical().noquote() << "TCommandLine::event(QEvent*) CRITICAL - a QEvent that is supposed to be a QKeyEvent is not dynamically castable to the latter - so the processing of this event "
"has been aborted - please report this to Mudlet Makers.";
// Indicate that we don't want to touch this event with a barge-pole!
return false;
}
if (ke->matches(QKeySequence::Copy)){ // Copy is Ctrl+C and possibly Ctrl+Ins, F16
if (mpConsole->mUpperPane->mSelectedRegion != QRegion(0, 0, 0, 0)) {
// Only process if there is a selection active in the TConsole
mpConsole->mUpperPane->slot_copySelectionToClipboard();
ke->accept();
return true;
}
}
if (ke->matches(QKeySequence::Find)){ // Find is Ctrl+F
if (keybindingMatched(ke)) { // If user has set up a keybind then do that instead.
return true;
}
if (mudlet::self()->dactionInputLine->isChecked()) {
// If hidden then reveal as if pressed Alt-L
mudlet::self()->dactionInputLine->setChecked(false);
mudlet::self()->mpCurrentActiveHost->setCompactInputLine(false);
}
mpConsole->mpBufferSearchBox->setFocus();
ke->accept();
return true;
}
// Shortcut for keypad keys
if ((ke->modifiers() & Qt::KeypadModifier) && mpKeyUnit->processDataStream(static_cast<Qt::Key>(ke->key()), static_cast<Qt::KeyboardModifiers>(ke->modifiers()))) {
ke->accept();
return true;
}
switch (ke->key()) {
case Qt::Key_Space:
if ((ke->modifiers() & (allModifiers & ~(Qt::ShiftModifier))) == Qt::NoModifier) {
// Ignore the <SHIFT> modifier only - keeps some users happy!
mTabCompletionCount = -1;
mAutoCompletionCount = -1;
mTabCompletionTyped.clear();
mHistoryBuffer = 0;
mLastCompletion.clear();
break;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers
// other than just a <SHIFT> one; may actually be configured as
// a non-breaking space when used with a modifier!
return true;
}
break;
case Qt::Key_Backtab:
// <BACKTAB> is usually internally generated by SHIFT used in
// conjunction with TAB - so ignore just the SHIFT key:
if ((ke->modifiers() & (allModifiers & ~(Qt::ShiftModifier))) == Qt::ControlModifier) {
// Switch to PREVIOUS profile tab when used with <CTRL> (and
// implicit <SHIFT>):
const int currentIndex = mudlet::self()->mpTabBar->currentIndex();
const int count = mudlet::self()->mpTabBar->count();
const int newIndex = (currentIndex - 1 < 0) ? (count - 1) : (currentIndex - 1);
mudlet::self()->slot_tabChanged(newIndex);
ke->accept();
return true;
}
if ((ke->modifiers() & (allModifiers & ~(Qt::ShiftModifier))) == Qt::NoModifier) {
// Process as plain <BACKTAB> - (ignoring implicit <SHIFT>)
handleTabCompletion(false);
adjustHeight();
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers
// other than just the ignored <SHIFT> and the possible <CTRL>:
return true;
}
break;
case Qt::Key_Tab:
if ((mpHost->mCaretShortcut == Host::CaretShortcut::Tab && !(ke->modifiers() & Qt::ControlModifier)) ||
(mpHost->mCaretShortcut == Host::CaretShortcut::CtrlTab && (ke->modifiers() & Qt::ControlModifier))) {
mpHost->setCaretEnabled(true);
ke->accept();
return true;
}
if ((ke->modifiers() & allModifiers) == Qt::ControlModifier) {
// Switch to NEXT profile tab
const int currentIndex = mudlet::self()->mpTabBar->currentIndex();
const int count = mudlet::self()->mpTabBar->count();
const int newIndex = (currentIndex + 1 < count) ? (currentIndex + 1) : 0;
mudlet::self()->slot_tabChanged(newIndex);
ke->accept();
return true;
}
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
handleTabCompletion(true);
ke->accept();
return true;
}
// Process as a possible key binding if there are ANY modifiers
// other than just the Ctrl one
// CHECKME: What about system foreground application switching?
if (keybindingMatched(ke)) {
return true;
}
break;
case Qt::Key_F6:
if ((mpHost->mCaretShortcut == Host::CaretShortcut::F6) && ((ke->modifiers() & allModifiers) == Qt::NoModifier)) {
mpHost->setCaretEnabled(true);
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
return true;
}
break;
case Qt::Key_unknown:
qWarning() << "ERROR: key unknown!";
break;
case Qt::Key_Backspace:
if ((ke->modifiers() & (allModifiers & ~(Qt::ControlModifier|Qt::ShiftModifier))) == Qt::NoModifier) {
// Ignore state of <CTRL> and <SHIFT> keys
mHistoryBuffer = 0;
if (!mTabCompletionTyped.isEmpty()) {
mTabCompletionTyped.chop(1);
}
mTabCompletionCount = -1;
mAutoCompletionCount = -1;
mLastCompletion.clear();
// This does the actual deletion of the character:
QPlainTextEdit::event(event);
// Recheck spelling of shortened word:
spellCheck();
adjustHeight();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers
// other than <CTRL> and/or <SHIFT>
return true;
}
break;
case Qt::Key_Delete:
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
mHistoryBuffer = 0;
if (!mTabCompletionTyped.isEmpty()) {
mTabCompletionTyped.chop(1);
} else {
mTabCompletionTyped.clear();
mUserKeptOnTyping = false;
}
mAutoCompletionCount = -1;
mTabCompletionCount = -1;
mLastCompletion.clear();
// This does the actual deletion of the character:
QPlainTextEdit::event(event);
// Recheck spelling of shortened word:
spellCheck();
adjustHeight();
return true;
}
if (keybindingMatched(ke)) {
return true;
}
break;
case Qt::Key_Return: // This is the main one (not the keypad)
if ((ke->modifiers() & allModifiers) == Qt::ControlModifier) {
// If Ctrl-Return is pressed - scroll to the bottom of text:
mpConsole->clearSplit();
ke->accept();
return true;
}
if ((ke->modifiers() & allModifiers) == Qt::ShiftModifier) {
textCursor().insertBlock();
ke->accept();
adjustHeight();
return true;
}
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
// Do the normal return key stuff only if NO modifiers are used:
enterCommand(ke);
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
// other than just the Shift or just the Control modifiers
return true;
}
break;
case Qt::Key_Enter:
// This is usually the Keypad one, so may come with
// the keypad modifier - but if so it may never be reached because
// of the keypad modifier interception done before this switch...
if ((ke->modifiers() & (allModifiers & ~(Qt::KeypadModifier))) == Qt::NoModifier) {
// Do the "normal" return key action if no or just the keypad
// modifier is present:
enterCommand(ke);
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
// other than just the Keypad modifier
return true;
}
break;
case Qt::Key_Down:
#if defined(Q_OS_MACOS)
if ((ke->modifiers() & allModifiers) == (Qt::ControlModifier|Qt::KeypadModifier)) {
#else
if ((ke->modifiers() & allModifiers) == Qt::ControlModifier) {
#endif
// If EXACTLY <Ctrl>-Down is pressed (special case for macOs -
// also sets KeyPad modifier)
moveCursor(QTextCursor::Down, QTextCursor::MoveAnchor);
ke->accept();
return true;
}
#if defined(Q_OS_MACOS)
if ((ke->modifiers() & allModifiers) == Qt::KeypadModifier) {
#else
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
#endif
// If EXACTLY Down is pressed without modifiers (special case
// for macOs - also sets KeyPad modifier)
historyMove(MOVE_DOWN);
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
// other than just the Control modifier (or keypad modifier on
// macOs)
return true;
}
break;
case Qt::Key_Up:
#if defined(Q_OS_MACOS)
if ((ke->modifiers() & allModifiers) == (Qt::ControlModifier|Qt::KeypadModifier)) {
#else
if ((ke->modifiers() & allModifiers) == Qt::ControlModifier) {
#endif
// If EXACTLY <Ctrl>-Up is pressed (special case for macOs -
// also sets KeyPad modifier)
moveCursor(QTextCursor::Up, QTextCursor::MoveAnchor);
ke->accept();
return true;
}
#if defined(Q_OS_MACOS)
if ((ke->modifiers() & allModifiers) == Qt::KeypadModifier) {
#else
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
#endif
// If EXACTLY Up is pressed without modifiers (special case for
// macOs - also sets KeyPad modifier)
historyMove(MOVE_UP);
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
// other than just the Control modifier (or keypad modifier on
// macOs)
return true;
}
break;
case Qt::Key_Escape:
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
// Escape from tab completion mode if used with NO modifiers
selectAll();
mTabCompletionTyped.clear();
mUserKeptOnTyping = false;
mTabCompletionCount = -1;
mAutoCompletionCount = -1;
mHistoryBuffer = 0;
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
return true;
}
break;
case Qt::Key_PageUp:
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
mpConsole->scrollUp(0);
QTimer::singleShot(0, this, [this]() { mpConsole->scrollUp(mpConsole->mUpperPane->getScreenHeight()); });
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
return true;
}
break;
case Qt::Key_PageDown:
if ((ke->modifiers() & allModifiers) == Qt::NoModifier) {
mpConsole->scrollDown(mpConsole->mUpperPane->getScreenHeight());
ke->accept();
return true;
}
if (keybindingMatched(ke)) {
// Process as a possible key binding if there are ANY modifiers,
return true;
}
break;
case Qt::Key_1:
if (handleCtrlTabChange(ke, 1)) {
return true;
}
return false;
case Qt::Key_2:
if (handleCtrlTabChange(ke, 2)) {
return true;
}
return false;
case Qt::Key_3:
if (handleCtrlTabChange(ke, 3)) {
return true;
}
return false;
case Qt::Key_4:
if (handleCtrlTabChange(ke, 4)) {
return true;
}
return false;
case Qt::Key_5:
if (handleCtrlTabChange(ke, 5)) {
return true;
}
return false;
case Qt::Key_6:
if (handleCtrlTabChange(ke, 6)) {
return true;
}
return false;
case Qt::Key_7:
if (handleCtrlTabChange(ke, 7)) {
return true;
}
return false;
case Qt::Key_8:
if (handleCtrlTabChange(ke, 8)) {
return true;
}
return false;
case Qt::Key_9:
if (handleCtrlTabChange(ke, 9)) {
return true;
}
return false;
case Qt::Key_0:
if (handleCtrlTabChange(ke, 10)) {
return true;
}
return false;
default:
// Process as a possible key binding if there are ANY modifiers
if (keybindingMatched(ke)) {
return true;
}
processNormalKey(event);
return false;
}
}
return QPlainTextEdit::event(event);
}
void TCommandLine::focusInEvent(QFocusEvent* event)
{
textCursor().movePosition(QTextCursor::Start);
textCursor().movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, mSelectedText.length());
mpConsole->mUpperPane->forceUpdate();
mpConsole->mLowerPane->forceUpdate();
// Record that this is the CommandLine in use for this profile, but NOT
// if it was Qt::ActiveWindowFocusReason as that gets used just by
// switching away and back to the Mudlet application and it messes up
// the record:
if (event->reason() != Qt::ActiveWindowFocusReason) {
mpHost->recordActiveCommandLine(this);
}
QPlainTextEdit::focusInEvent(event);
}
void TCommandLine::focusOutEvent(QFocusEvent* event)
{
if (textCursor().hasSelection()) {
mSelectionStart = textCursor().selectionStart();
mSelectedText = textCursor().selectedText();
} else {
mSelectionStart = 0;
mSelectedText.clear();
}
QPlainTextEdit::focusOutEvent(event);
}
void TCommandLine::hideEvent(QHideEvent* event)
{
QPlainTextEdit::hideEvent(event);
}
void TCommandLine::adjustHeight()
{
// Make sure adjustHeight won't crash if it's used before mpConsole->layerCommandLine has a value
if (!mpConsole->layerCommandLine) {
qWarning() << "TCommandLine::adjustHeight() ERROR: mpConsole->layerCommandLine is NULL!";
return;
}
int lines = document()->size().height();
// Workaround for SubCommandLines textCursor not visible in some situations
// SubCommandLines cannot autoresize
if (mType == SubCommandLine) {
if (lines <= 1) {
verticalScrollBar()->triggerAction(QScrollBar::SliderToMinimum);
}
return;
}
if (lines < 1) {
lines = 1;
}
if (lines > 10) {
lines = 10;
}
const int fontH = QFontMetrics(font()).height();
// Adjust height margin based on font size and if it is more than one row
int marginH = lines > 1 ? 2+fontH/3 : 5;
if (lines > 1 && marginH < 8) {
marginH = 8; // needed for very small fonts
}
int _height = fontH * lines + marginH;
if (_height < mpHost->commandLineMinimumHeight) {
_height = mpHost->commandLineMinimumHeight;
}
if (_height > height() || _height < height()) {
mpConsole->layerCommandLine->setMinimumHeight(_height);
mpConsole->layerCommandLine->setMaximumHeight(_height);
const int x = mpConsole->width();
const int y = mpConsole->height();
const QSize s = QSize(x, y);
QResizeEvent event(s, s);
QApplication::sendEvent(mpConsole, &event);
}
}
void TCommandLine::spellCheck()
{
if (!mpHost || !mpHost->mEnableSpellCheck) {
return;
}
QTextCursor oldCursor = textCursor();
QTextCursor c = textCursor();
spellCheckWord(c);
QTextCharFormat f;
f.setFontUnderline(false);
oldCursor.setCharFormat(f);
setTextCursor(oldCursor);
}
void TCommandLine::slot_popupMenu()
{
auto* pA = qobject_cast<QAction*>(sender());
if (!mpHost || !pA) {
return;
}
#if defined(Q_OS_FREEBSD)
QString t = pA->data().toString();
#else
const QString t = pA->text();
#endif
QTextCursor c = cursorForPosition(mPopupPosition);
c.select(QTextCursor::WordUnderCursor);
c.removeSelectedText();
c.insertText(t);
c.clearSelection();
auto systemDictionaryHandle = mpHost->mpConsole->getHunspellHandle_system();
if (systemDictionaryHandle) {
Hunspell_free_list(mpHost->mpConsole->getHunspellHandle_system(), &mpSystemSuggestionsList, mSystemDictionarySuggestionsCount);
}
auto userDictionaryHandle = mpHost->mpConsole->getHunspellHandle_user();
if (userDictionaryHandle) {
Hunspell_free_list(userDictionaryHandle, &mpUserSuggestionsList, mUserDictionarySuggestionsCount);
}
// Call the function again so that the replaced word gets rechecked:
spellCheck();
}
void TCommandLine::fillSpellCheckList(QMouseEvent* event, QMenu* popup)
{
QTextCursor c = cursorForPosition(event->pos());
c.select(QTextCursor::WordUnderCursor);
mSpellCheckedWord = c.selectedText();
const bool wantSpellCheck = TBuffer::lengthInGraphemes(mSpellCheckedWord) >= mudlet::self()->mMinLengthForSpellCheck;
if (!wantSpellCheck) {
return;
}
auto codec = mpHost->mpConsole->getHunspellCodec_system();
auto handle_system = mpHost->mpConsole->getHunspellHandle_system();
auto handle_profile = mpHost->mpConsole->getHunspellHandle_user();
bool haveAddOption = false;
bool haveRemoveOption = false;
QAction* action_addWord = nullptr;
QAction* action_removeWord = nullptr;
QAction* action_dictionarySeparatorLine = nullptr;
if (handle_profile) {
// if (!qApp->testAttribute(Qt::AA_DontShowIconsInMenus)) {
// action_addWord = new QAction(QIcon(QPixmap(qsl(":/icons/dictionary-add-word.png"))), tr("Add to user dictionary"));
// action_removeWord = new QAction(QIcon(QPixmap(qsl(":/icons/dictionary-remove-word.png"))), tr("Remove from user dictionary"));
// } else {
action_addWord = new QAction(tr("Add to user dictionary"));
action_addWord->setEnabled(false);
action_removeWord = new QAction(tr("Remove from user dictionary"));
action_removeWord->setEnabled(false);
// }
if (mudlet::self()->mUsingMudletDictionaries) {
/*:
This line is shown in the list of spelling suggestions on the profile's command
line context menu to clearly divide up where the suggestions for correct
spellings are coming from. The precise format might be modified as long as it
is clear that the entries below this line in the menu come from the spelling
dictionary that the user has chosen in the profile setting which we have
bundled with Mudlet; the entries about this line are the ones that the user
has personally added.
*/
action_dictionarySeparatorLine = new QAction(tr("▼Mudlet▼ │ dictionary suggestions │ ▲User▲"));
} else {
/*:
This line is shown in the list of spelling suggestions on the profile's command
line context menu to clearly divide up where the suggestions for correct
spellings are coming from. The precise format might be modified as long as it
is clear that the entries below this line in the menu come from the spelling
dictionary that the user has chosen in the profile setting which is provided
as part of the OS; the entries about this line are the ones that the user has
personally added.
*/
action_dictionarySeparatorLine = new QAction(tr("▼System▼ │ dictionary suggestions │ ▲User▲"));
}
action_dictionarySeparatorLine->setEnabled(false);
}
QList<QAction*> spellings_system;
QList<QAction*> spellings_profile;
// We always use UTF-8 for the per profile/shared dictionary so we do not
// need to have a codec prepared for it and can use QString::toUtf8()
// directly:
const QByteArray utf8Text = mSpellCheckedWord.toUtf8();
if (!(handle_system && codec)) {
mSystemDictionarySuggestionsCount = 0;
} else {
// The dictionary used from "the system" may not be UTF-8 encoded so we
// will need to transform the UTF-16BE "QString" to the appropriate encoding
// using "codec" declared previously in this method:
const QByteArray encodedText = codec->fromUnicode(mSpellCheckedWord);
if (!Hunspell_spell(handle_system, encodedText.constData())) {
// The word is NOT in the main system dictionary:
if (handle_profile) {
// Have a user dictionary so check it:
if (!Hunspell_spell(handle_profile, utf8Text.constData())) {
// The word is NOT in the profile one either - so enable add option
haveAddOption = true;
} else {
// However the word is in the profile one - so enable remove option
haveRemoveOption = true;
}
if (haveAddOption) {
action_addWord->setEnabled(true);
connect(action_addWord, &QAction::triggered, this, &TCommandLine::slot_addWord);
}
if (haveRemoveOption) {
action_removeWord->setEnabled(true);
connect(action_removeWord, &QAction::triggered, this, &TCommandLine::slot_removeWord);
}
}
}
mSystemDictionarySuggestionsCount = Hunspell_suggest(handle_system, &mpSystemSuggestionsList, encodedText.constData());
}
if (handle_profile) {
mUserDictionarySuggestionsCount = Hunspell_suggest(handle_profile, &mpUserSuggestionsList, utf8Text.constData());
} else {
mUserDictionarySuggestionsCount = 0;
}
if (mSystemDictionarySuggestionsCount) {
for (int i = 0; i < mSystemDictionarySuggestionsCount; ++i) {
auto pA = new QAction(codec->toUnicode(mpSystemSuggestionsList[i]));
#if defined(Q_OS_FREEBSD)
// Adding the text afterwards as user data as well as in the
// constructor is to fix a bug(?) in FreeBSD that
// automagically adds a '&' somewhere in the text to be a
// shortcut - but doesn't show it and forgets to remove
// it when asked for the text later:
pA->setData(codec->toUnicode(mpSystemSuggestionsList[i]));
#endif
connect(pA, &QAction::triggered, this, &TCommandLine::slot_popupMenu);
spellings_system << pA;
}
} else {
/*:
Used when the command spelling checker using the selected system dictionary has
no words to suggest.
*/
auto pA = new QAction(tr("no suggestions (system)"));
pA->setEnabled(false);
spellings_system << pA;
}
if (handle_profile) {
if (mUserDictionarySuggestionsCount) {
for (int i = 0; i < mUserDictionarySuggestionsCount; ++i) {
auto pA = new QAction(codec->toUnicode(mpUserSuggestionsList[i]));
#if defined(Q_OS_FREEBSD)
// Adding the text afterwards as user data as well as in the
// constructor is to fix a bug(?) in FreeBSD that
// automagically adds a '&' somewhere in the text to be a
// shortcut - but doesn't show it and forgets to remove
// it when asked for the text later:
pA->setData(codec->toUnicode(mpUserSuggestionsList[i]));
#endif
connect(pA, &QAction::triggered, this, &TCommandLine::slot_popupMenu);
spellings_profile << pA;
}
} else {
QAction* pA = nullptr;
auto mainConsole = mpConsole->mpHost->mpConsole;
if (mainConsole->isUsingSharedDictionary()) {
/*:
Used when the command spelling checker using the dictionary shared between
profile has no words to suggest.
*/
pA = new QAction(tr("no suggestions (shared)"));
} else {
/*:
Used when the command spelling checker using the profile's own dictionary has
no words to suggest.
*/
pA = new QAction(tr("no suggestions (profile)"));
}
pA->setEnabled(false);
spellings_profile << pA;
}
}
/*
* Build up the extra context menu items from the BOTTOM up, so that
* the top of the context menu looks like:
*
* profile dictionary suggestions
* --------- separator_aboveDictionarySeparatorLine
* \/ System dictionary suggestions /\ Profile <== Text
* --------- separator_aboveSystemDictionarySuggestions
* system dictionary suggestions
* --------- separator_aboveAddAndRemove
* Add word action
* Remove word action
* --------- separator_aboveStandardMenu
*
* The insertAction[s](...)/(Separator(...)) insert their things
* second argument (or generated by themself) before the first (or
* only) argument given.
*/
auto separator_aboveStandardMenu = popup->insertSeparator(popup->actions().first());
if (handle_profile) {
popup->insertAction(separator_aboveStandardMenu, action_removeWord);
popup->insertAction(action_removeWord, action_addWord);
auto separator_aboveAddAndRemove = popup->insertSeparator(action_addWord);
popup->insertActions(separator_aboveAddAndRemove, spellings_system);
auto separator_aboveSystemDictionarySuggestions = popup->insertSeparator(spellings_system.first());
popup->insertAction(separator_aboveSystemDictionarySuggestions, action_dictionarySeparatorLine);
auto separator_aboveDictionarySeparatorLine = popup->insertSeparator(action_dictionarySeparatorLine);
popup->insertActions(separator_aboveDictionarySeparatorLine, spellings_profile);
} else {
popup->insertActions(separator_aboveStandardMenu, spellings_system);
}
}
void TCommandLine::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::RightButton) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto popup = createStandardContextMenu(event->globalPos());
#else
auto popup = createStandardContextMenu(event->globalPosition().toPoint());
#endif
if (mpHost->mEnableSpellCheck) {
fillSpellCheckList(event, popup);
// else the word is in the dictionary - in either case show the context
// menu - either the one with the prefixed spellings, or the standard one
}
popup->addSeparator();
foreach(auto label, contextMenuItems.keys()) {
auto eventName = contextMenuItems.value(label);
auto action = new QAction(label, this);
connect(action, &QAction::triggered, this, [=]() {
TEvent mudletEvent = {};
mudletEvent.mArgumentList << eventName;
mudletEvent.mArgumentTypeList << ARGUMENT_TYPE_STRING;
mpHost->raiseEvent(mudletEvent);
});
popup->addAction(action);
}
mPopupPosition = event->pos();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
popup->popup(event->globalPos());
#else
popup->popup(event->globalPosition().toPoint());
#endif
// The use of accept here is supposed to prevents this event from
// reaching any parent widget - like the TConsole containing this
// TCommandLine...
event->accept();
}
// Process any other possible mousePressEvent - which is default context
// menu handling - and which accepts the event:
QPlainTextEdit::mousePressEvent(event);
mudlet::self()->activateProfile(mpHost);
}
void TCommandLine::mouseReleaseEvent(QMouseEvent* event)
{
// Process any other possible mouseReleaseEvent - which is default context
// menu handling - and which accepts the event:
QPlainTextEdit::mouseReleaseEvent(event);
mudlet::self()->activateProfile(mpHost);
}
void TCommandLine::enterCommand(QKeyEvent* event)
{
Q_UNUSED(event)
mTabCompletionCount = -1;
mAutoCompletionCount = -1;
mTabCompletionTyped.clear();
mLastCompletion.clear();
mUserKeptOnTyping = false;
QStringList commandList = toPlainText().split(QChar::LineFeed);
for (int i = 0; i < commandList.size(); ++i) {
if (mType != MainCommandLine && mActionFunction) {
mpHost->getLuaInterpreter()->callCmdLineAction(mActionFunction, commandList.at(i));
} else {
mpHost->send(commandList.at(i));
}
// send command to your MiniConsole
if (mType == ConsoleCommandLine && !mActionFunction && mpHost->mPrintCommand){
// This usage of commandList modifies the content!!!
mpConsole->printCommand(commandList[i]);
}
}
if (!toPlainText().isEmpty()) {
if (mpHost->mAutoClearCommandLineAfterSend) {
mHistoryBuffer = 0;
} else {
mHistoryBuffer = 1;
}
mHistoryList.removeAll(toPlainText());
if (!mHistoryList.isEmpty()) {
mHistoryList[0] = toPlainText();
} else {
mHistoryList.push_front(toPlainText());
}
mHistoryList.push_front(QString());
}
if (mpHost->mAutoClearCommandLineAfterSend) {
#if defined (Q_OS_MACOS)
// clearing the input line on macOS 11.6 makes VoiceOver announce the removed text,
// essentially re-announcing everything we've typed. This workaround fixes this behaviour
// and does not seem to negatively affect other platforms
hide();
#endif
clear();
#if defined (Q_OS_MACOS)
show();
#endif
} else {
selectAll();
}
adjustHeight();
}
// TAB completion mode gets turned on by the tab key.
// This mode tries to find suitable matches for the letters being typed by the user
// in the output buffer of data being sent by the MUD. This helps the user
// to quickly type complicated names by only having to type the first letters.
// You can cycle through all possible matches of the currently typed letters
// with by repeatedly pressing tab or shift+tab. ESC-key brings you back into regular mode
void TCommandLine::handleTabCompletion(bool direction)
{
if ((mTabCompletionCount < 0) || (mUserKeptOnTyping)) {
mTabCompletionTyped = toPlainText();
if (mTabCompletionTyped.isEmpty()) {
return;
}
mUserKeptOnTyping = false;
mTabCompletionCount = -1;
}
int amount = mpHost->mpConsole->buffer.size();
if (amount > 500) {