-
Notifications
You must be signed in to change notification settings - Fork 257
/
TerminalDisplay.cpp
3479 lines (2972 loc) · 112 KB
/
TerminalDisplay.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
/*
This file is part of Konsole, a terminal emulator for KDE.
Copyright 2006-2008 by Robert Knight <[email protected]>
Copyright 1997,1998 by Lars Doelle <[email protected]>
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "TerminalDisplay.h"
// Qt
#include <QAbstractButton>
#include <QApplication>
#include <QBoxLayout>
#include <QClipboard>
#include <QKeyEvent>
#include <QEvent>
#include <QTime>
#include <QFile>
#include <QGridLayout>
#include <QLabel>
#include <QLayout>
#include <QMessageBox>
#include <QPainter>
#include <QPixmap>
#include <QRegularExpression>
#include <QScrollBar>
#include <QStyle>
#include <QTimer>
#include <QtDebug>
#include <QUrl>
#include <QMimeData>
#include <QDrag>
// KDE
//#include <kshell.h>
//#include <KColorScheme>
//#include <KCursor>
//#include <kdebug.h>
//#include <KLocale>
//#include <KMenu>
//#include <KNotification>
//#include <KGlobalSettings>
//#include <KShortcut>
//#include <KIO/NetAccess>
// Konsole
//#include <config-apps.h>
#include "Filter.h"
#include "konsole_wcwidth.h"
#include "ScreenWindow.h"
#include "TerminalCharacterDecoder.h"
using namespace Konsole;
#ifndef loc
#define loc(X,Y) ((Y)*_columns+(X))
#endif
#define yMouseScroll 1
#define REPCHAR "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefgjijklmnopqrstuvwxyz" \
"0123456789./+@"
const ColorEntry Konsole::base_color_table[TABLE_COLORS] =
// The following are almost IBM standard color codes, with some slight
// gamma correction for the dim colors to compensate for bright X screens.
// It contains the 8 ansiterm/xterm colors in 2 intensities.
{
// Fixme: could add faint colors here, also.
// normal
ColorEntry(QColor(0x00,0x00,0x00), false), ColorEntry( QColor(0xB2,0xB2,0xB2), true), // Dfore, Dback
ColorEntry(QColor(0x00,0x00,0x00), false), ColorEntry( QColor(0xB2,0x18,0x18), false), // Black, Red
ColorEntry(QColor(0x18,0xB2,0x18), false), ColorEntry( QColor(0xB2,0x68,0x18), false), // Green, Yellow
ColorEntry(QColor(0x18,0x18,0xB2), false), ColorEntry( QColor(0xB2,0x18,0xB2), false), // Blue, Magenta
ColorEntry(QColor(0x18,0xB2,0xB2), false), ColorEntry( QColor(0xB2,0xB2,0xB2), false), // Cyan, White
// intensiv
ColorEntry(QColor(0x00,0x00,0x00), false), ColorEntry( QColor(0xFF,0xFF,0xFF), true),
ColorEntry(QColor(0x68,0x68,0x68), false), ColorEntry( QColor(0xFF,0x54,0x54), false),
ColorEntry(QColor(0x54,0xFF,0x54), false), ColorEntry( QColor(0xFF,0xFF,0x54), false),
ColorEntry(QColor(0x54,0x54,0xFF), false), ColorEntry( QColor(0xFF,0x54,0xFF), false),
ColorEntry(QColor(0x54,0xFF,0xFF), false), ColorEntry( QColor(0xFF,0xFF,0xFF), false)
};
// scroll increment used when dragging selection at top/bottom of window.
// static
bool TerminalDisplay::_antialiasText = true;
bool TerminalDisplay::HAVE_TRANSPARENCY = true;
// we use this to force QPainter to display text in LTR mode
// more information can be found in: http://unicode.org/reports/tr9/
const QChar LTR_OVERRIDE_CHAR( 0x202D );
/* ------------------------------------------------------------------------- */
/* */
/* Colors */
/* */
/* ------------------------------------------------------------------------- */
/* Note that we use ANSI color order (bgr), while IBMPC color order is (rgb)
Code 0 1 2 3 4 5 6 7
----------- ------- ------- ------- ------- ------- ------- ------- -------
ANSI (bgr) Black Red Green Yellow Blue Magenta Cyan White
IBMPC (rgb) Black Blue Green Cyan Red Magenta Yellow White
*/
ScreenWindow* TerminalDisplay::screenWindow() const
{
return _screenWindow;
}
void TerminalDisplay::setScreenWindow(ScreenWindow* window)
{
// disconnect existing screen window if any
if ( _screenWindow )
{
disconnect( _screenWindow , nullptr , this , nullptr );
}
_screenWindow = window;
if ( window )
{
// TODO: Determine if this is an issue.
//#warning "The order here is not specified - does it matter whether updateImage or updateLineProperties comes first?"
connect( _screenWindow , SIGNAL(outputChanged()) , this , SLOT(updateLineProperties()) );
connect( _screenWindow , SIGNAL(outputChanged()) , this , SLOT(updateImage()) );
connect( _screenWindow , SIGNAL(outputChanged()) , this , SLOT(updateFilters()) );
connect( _screenWindow , SIGNAL(scrolled(int)) , this , SLOT(updateFilters()) );
connect( _screenWindow , &ScreenWindow::scrollToEnd , this , &TerminalDisplay::scrollToEnd );
window->setWindowLines(_lines);
}
}
const ColorEntry* TerminalDisplay::colorTable() const
{
return _colorTable;
}
void TerminalDisplay::setBackgroundColor(const QColor& color)
{
_colorTable[DEFAULT_BACK_COLOR].color = color;
QPalette p = palette();
p.setColor( backgroundRole(), color );
setPalette( p );
// Avoid propagating the palette change to the scroll bar
_scrollBar->setPalette( QApplication::palette() );
update();
}
void TerminalDisplay::setForegroundColor(const QColor& color)
{
_colorTable[DEFAULT_FORE_COLOR].color = color;
update();
}
void TerminalDisplay::setColorTable(const ColorEntry table[])
{
for (int i = 0; i < TABLE_COLORS; i++)
_colorTable[i] = table[i];
setBackgroundColor(_colorTable[DEFAULT_BACK_COLOR].color);
}
/* ------------------------------------------------------------------------- */
/* */
/* Font */
/* */
/* ------------------------------------------------------------------------- */
/*
The VT100 has 32 special graphical characters. The usual vt100 extended
xterm fonts have these at 0x00..0x1f.
QT's iso mapping leaves 0x00..0x7f without any changes. But the graphicals
come in here as proper unicode characters.
We treat non-iso10646 fonts as VT100 extended and do the required mapping
from unicode to 0x00..0x1f. The remaining translation is then left to the
QCodec.
*/
bool TerminalDisplay::isLineChar(Character c) const {
return _drawLineChars && c.isLineChar();
}
bool TerminalDisplay::isLineCharString(const std::wstring& string) const {
return string.length() > 0 && _drawLineChars && (string[0] & 0xFF80) == 0x2500;
}
// assert for i in [0..31] : vt100extended(vt100_graphics[i]) == i.
unsigned short Konsole::vt100_graphics[32] =
{ // 0/8 1/9 2/10 3/11 4/12 5/13 6/14 7/15
0x0020, 0x25C6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0,
0x00b1, 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c,
0xF800, 0xF801, 0x2500, 0xF803, 0xF804, 0x251c, 0x2524, 0x2534,
0x252c, 0x2502, 0x2264, 0x2265, 0x03C0, 0x2260, 0x00A3, 0x00b7
};
void TerminalDisplay::fontChange(const QFont&)
{
QFontMetrics fm(font());
_fontHeight = fm.height() + _lineSpacing;
// waba TerminalDisplay 1.123:
// "Base character width on widest ASCII character. This prevents too wide
// characters in the presence of double wide (e.g. Japanese) characters."
// Get the width from representative normal width characters
_fontWidth = qRound(static_cast<double>(fm.horizontalAdvance(QLatin1String(REPCHAR)))/static_cast<double>(qstrlen(REPCHAR)));
_fixedFont = true;
int fw = fm.horizontalAdvance(QLatin1Char(REPCHAR[0]));
for(unsigned int i=1; i< qstrlen(REPCHAR); i++)
{
if (fw != fm.horizontalAdvance(QLatin1Char(REPCHAR[i])))
{
_fixedFont = false;
break;
}
}
_fixedFont_original = _fixedFont;
if (_fontWidth < 1)
_fontWidth=1;
_fontAscent = fm.ascent();
emit changedFontMetricSignal( _fontHeight, _fontWidth );
propagateSize();
// We will run paint event testing procedure.
// Although this operation will destroy the original content,
// the content will be drawn again after the test.
_drawTextTestFlag = true;
update();
}
void TerminalDisplay::calDrawTextAdditionHeight(QPainter& painter)
{
QRect test_rect, feedback_rect;
test_rect.setRect(1, 1, _fontWidth * 4, _fontHeight);
painter.save();
painter.setOpacity(0);
painter.drawText(test_rect, Qt::AlignBottom, LTR_OVERRIDE_CHAR + QLatin1String("Mq"), &feedback_rect);
painter.restore();
_drawTextAdditionHeight = qMax(0, (feedback_rect.height() - _fontHeight) / 2);
_drawTextTestFlag = false;
}
void TerminalDisplay::setVTFont(const QFont& f)
{
QFont font = f;
// Check if font is not fixed pitch and print a warning
if ( !QFontInfo(font).fixedPitch() )
{
qDebug() << "Using a variable-width font in the terminal. This may cause performance degradation and display/alignment errors.";
}
// hint that text should be drawn without anti-aliasing.
// depending on the user's font configuration, this may not be respected
if (!_antialiasText)
font.setStyleStrategy( QFont::NoAntialias );
// experimental optimization. Konsole assumes that the terminal is using a
// mono-spaced font, in which case kerning information should have no effect.
// Disabling kerning saves some computation when rendering text.
font.setKerning(false);
// QFont::ForceIntegerMetrics has been removed.
// Set full hinting instead to ensure the letters are aligned properly.
font.setHintingPreference(QFont::PreferFullHinting);
// "Draw intense colors in bold font" feature needs to use different font weights. StyleName
// property, when set, doesn't allow weight changes. Since all properties (weight, stretch,
// italic, etc) are stored in QFont independently, in almost all cases styleName is not needed.
font.setStyleName(QString());
QWidget::setFont(font);
fontChange(font);
}
void TerminalDisplay::setFont(const QFont &)
{
// ignore font change request if not coming from konsole itself
}
/* ------------------------------------------------------------------------- */
/* */
/* Constructor / Destructor */
/* */
/* ------------------------------------------------------------------------- */
TerminalDisplay::TerminalDisplay(QWidget *parent)
:QWidget(parent)
,_screenWindow(nullptr)
,_allowBell(true)
,_gridLayout(nullptr)
,_fontHeight(1)
,_fontWidth(1)
,_fontAscent(1)
,_boldIntense(true)
,_lines(1)
,_columns(1)
,_usedLines(1)
,_usedColumns(1)
,_contentHeight(1)
,_contentWidth(1)
,_image(nullptr)
,_randomSeed(0)
,_resizing(false)
,_terminalSizeHint(false)
,_terminalSizeStartup(true)
,_bidiEnabled(true)
,_mouseMarks(false)
,_disabledBracketedPasteMode(false)
,_actSel(0)
,_wordSelectionMode(false)
,_lineSelectionMode(false)
,_preserveLineBreaks(false)
,_columnSelectionMode(false)
,_scrollbarLocation(QTermWidget::NoScrollBar)
,_wordCharacters(QLatin1String(":@-./_~"))
,_bellMode(SystemBeepBell)
,_blinking(false)
,_hasBlinker(false)
,_cursorBlinking(false)
,_hasBlinkingCursor(false)
,_allowBlinkingText(true)
,_ctrlDrag(false)
,_tripleClickMode(SelectWholeLine)
,_isFixedSize(false)
,_possibleTripleClick(false)
,_resizeWidget(nullptr)
,_resizeTimer(nullptr)
,_flowControlWarningEnabled(false)
,_outputSuspendedLabel(nullptr)
,_lineSpacing(0)
,_colorsInverted(false)
,_opacity(static_cast<qreal>(1))
,_backgroundMode(None)
,_filterChain(new TerminalImageFilterChain())
,_cursorShape(Emulation::KeyboardCursorShape::BlockCursor)
,mMotionAfterPasting(NoMoveScreenWindow)
,_leftBaseMargin(1)
,_topBaseMargin(1)
,_drawLineChars(true)
{
// variables for draw text
_drawTextAdditionHeight = 0;
_drawTextTestFlag = false;
// terminal applications are not designed with Right-To-Left in mind,
// so the layout is forced to Left-To-Right
setLayoutDirection(Qt::LeftToRight);
// The offsets are not yet calculated.
// Do not calculate these too often to be more smoothly when resizing
// konsole in opaque mode.
_topMargin = _topBaseMargin;
_leftMargin = _leftBaseMargin;
// create scroll bar for scrolling output up and down
// set the scroll bar's slider to occupy the whole area of the scroll bar initially
_scrollBar = new QScrollBar(this);
// since the contrast with the terminal background may not be enough,
// the scrollbar should be auto-filled if not transient
if (!_scrollBar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, _scrollBar))
_scrollBar->setAutoFillBackground(true);
setScroll(0,0);
_scrollBar->setCursor( Qt::ArrowCursor );
connect(_scrollBar, SIGNAL(valueChanged(int)), this,
SLOT(scrollBarPositionChanged(int)));
// qtermwidget: we have to hide it here due the _scrollbarLocation==NoScrollBar
// check in TerminalDisplay::setScrollBarPosition(ScrollBarPosition position)
_scrollBar->hide();
// setup timers for blinking cursor and text
_blinkTimer = new QTimer(this);
connect(_blinkTimer, SIGNAL(timeout()), this, SLOT(blinkEvent()));
_blinkCursorTimer = new QTimer(this);
connect(_blinkCursorTimer, SIGNAL(timeout()), this, SLOT(blinkCursorEvent()));
// KCursor::setAutoHideCursor( this, true );
setUsesMouse(true);
setBracketedPasteMode(false);
setColorTable(base_color_table);
setMouseTracking(true);
// Enable drag and drop
setAcceptDrops(true); // attempt
dragInfo.state = diNone;
setFocusPolicy( Qt::WheelFocus );
// enable input method support
setAttribute(Qt::WA_InputMethodEnabled, true);
// this is an important optimization, it tells Qt
// that TerminalDisplay will handle repainting its entire area.
setAttribute(Qt::WA_OpaquePaintEvent);
_gridLayout = new QGridLayout(this);
_gridLayout->setContentsMargins(0, 0, 0, 0);
setLayout( _gridLayout );
new AutoScrollHandler(this);
}
TerminalDisplay::~TerminalDisplay()
{
disconnect(_blinkTimer);
disconnect(_blinkCursorTimer);
qApp->removeEventFilter( this );
delete[] _image;
delete _gridLayout;
delete _outputSuspendedLabel;
delete _filterChain;
}
/* ------------------------------------------------------------------------- */
/* */
/* Display Operations */
/* */
/* ------------------------------------------------------------------------- */
/**
A table for emulating the simple (single width) unicode drawing chars.
It represents the 250x - 257x glyphs. If it's zero, we can't use it.
if it's not, it's encoded as follows: imagine a 5x5 grid where the points are numbered
0 to 24 left to top, top to bottom. Each point is represented by the corresponding bit.
Then, the pixels basically have the following interpretation:
_|||_
-...-
-...-
-...-
_|||_
where _ = none
| = vertical line.
- = horizontal line.
*/
enum LineEncode
{
TopL = (1<<1),
TopC = (1<<2),
TopR = (1<<3),
LeftT = (1<<5),
Int11 = (1<<6),
Int12 = (1<<7),
Int13 = (1<<8),
RightT = (1<<9),
LeftC = (1<<10),
Int21 = (1<<11),
Int22 = (1<<12),
Int23 = (1<<13),
RightC = (1<<14),
LeftB = (1<<15),
Int31 = (1<<16),
Int32 = (1<<17),
Int33 = (1<<18),
RightB = (1<<19),
BotL = (1<<21),
BotC = (1<<22),
BotR = (1<<23)
};
#include "LineFont.h"
static void drawLineChar(QPainter& paint, int x, int y, int w, int h, uint8_t code)
{
//Calculate cell midpoints, end points.
int cx = x + w/2;
int cy = y + h/2;
int ex = x + w - 1;
int ey = y + h - 1;
quint32 toDraw = LineChars[code];
//Top _lines:
if (toDraw & TopL)
paint.drawLine(cx-1, y, cx-1, cy-2);
if (toDraw & TopC)
paint.drawLine(cx, y, cx, cy-2);
if (toDraw & TopR)
paint.drawLine(cx+1, y, cx+1, cy-2);
//Bot _lines:
if (toDraw & BotL)
paint.drawLine(cx-1, cy+2, cx-1, ey);
if (toDraw & BotC)
paint.drawLine(cx, cy+2, cx, ey);
if (toDraw & BotR)
paint.drawLine(cx+1, cy+2, cx+1, ey);
//Left _lines:
if (toDraw & LeftT)
paint.drawLine(x, cy-1, cx-2, cy-1);
if (toDraw & LeftC)
paint.drawLine(x, cy, cx-2, cy);
if (toDraw & LeftB)
paint.drawLine(x, cy+1, cx-2, cy+1);
//Right _lines:
if (toDraw & RightT)
paint.drawLine(cx+2, cy-1, ex, cy-1);
if (toDraw & RightC)
paint.drawLine(cx+2, cy, ex, cy);
if (toDraw & RightB)
paint.drawLine(cx+2, cy+1, ex, cy+1);
//Intersection points.
if (toDraw & Int11)
paint.drawPoint(cx-1, cy-1);
if (toDraw & Int12)
paint.drawPoint(cx, cy-1);
if (toDraw & Int13)
paint.drawPoint(cx+1, cy-1);
if (toDraw & Int21)
paint.drawPoint(cx-1, cy);
if (toDraw & Int22)
paint.drawPoint(cx, cy);
if (toDraw & Int23)
paint.drawPoint(cx+1, cy);
if (toDraw & Int31)
paint.drawPoint(cx-1, cy+1);
if (toDraw & Int32)
paint.drawPoint(cx, cy+1);
if (toDraw & Int33)
paint.drawPoint(cx+1, cy+1);
}
static void drawOtherChar(QPainter& paint, int x, int y, int w, int h, uchar code)
{
//Calculate cell midpoints, end points.
const int cx = x + w / 2;
const int cy = y + h / 2;
const int ex = x + w - 1;
const int ey = y + h - 1;
// Double dashes
if (0x4C <= code && code <= 0x4F) {
const int xHalfGap = qMax(w / 15, 1);
const int yHalfGap = qMax(h / 15, 1);
switch (code) {
case 0x4D: // BOX DRAWINGS HEAVY DOUBLE DASH HORIZONTAL
paint.drawLine(x, cy - 1, cx - xHalfGap - 1, cy - 1);
paint.drawLine(x, cy + 1, cx - xHalfGap - 1, cy + 1);
paint.drawLine(cx + xHalfGap, cy - 1, ex, cy - 1);
paint.drawLine(cx + xHalfGap, cy + 1, ex, cy + 1);
/* Falls through. */
case 0x4C: // BOX DRAWINGS LIGHT DOUBLE DASH HORIZONTAL
paint.drawLine(x, cy, cx - xHalfGap - 1, cy);
paint.drawLine(cx + xHalfGap, cy, ex, cy);
break;
case 0x4F: // BOX DRAWINGS HEAVY DOUBLE DASH VERTICAL
paint.drawLine(cx - 1, y, cx - 1, cy - yHalfGap - 1);
paint.drawLine(cx + 1, y, cx + 1, cy - yHalfGap - 1);
paint.drawLine(cx - 1, cy + yHalfGap, cx - 1, ey);
paint.drawLine(cx + 1, cy + yHalfGap, cx + 1, ey);
/* Falls through. */
case 0x4E: // BOX DRAWINGS LIGHT DOUBLE DASH VERTICAL
paint.drawLine(cx, y, cx, cy - yHalfGap - 1);
paint.drawLine(cx, cy + yHalfGap, cx, ey);
break;
}
}
// Rounded corner characters
else if (0x6D <= code && code <= 0x70) {
const int r = w * 3 / 8;
const int d = 2 * r;
switch (code) {
case 0x6D: // BOX DRAWINGS LIGHT ARC DOWN AND RIGHT
paint.drawLine(cx, cy + r, cx, ey);
paint.drawLine(cx + r, cy, ex, cy);
paint.drawArc(cx, cy, d, d, 90 * 16, 90 * 16);
break;
case 0x6E: // BOX DRAWINGS LIGHT ARC DOWN AND LEFT
paint.drawLine(cx, cy + r, cx, ey);
paint.drawLine(x, cy, cx - r, cy);
paint.drawArc(cx - d, cy, d, d, 0 * 16, 90 * 16);
break;
case 0x6F: // BOX DRAWINGS LIGHT ARC UP AND LEFT
paint.drawLine(cx, y, cx, cy - r);
paint.drawLine(x, cy, cx - r, cy);
paint.drawArc(cx - d, cy - d, d, d, 270 * 16, 90 * 16);
break;
case 0x70: // BOX DRAWINGS LIGHT ARC UP AND RIGHT
paint.drawLine(cx, y, cx, cy - r);
paint.drawLine(cx + r, cy, ex, cy);
paint.drawArc(cx, cy - d, d, d, 180 * 16, 90 * 16);
break;
}
}
// Diagonals
else if (0x71 <= code && code <= 0x73) {
switch (code) {
case 0x71: // BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT
paint.drawLine(ex, y, x, ey);
break;
case 0x72: // BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT
paint.drawLine(x, y, ex, ey);
break;
case 0x73: // BOX DRAWINGS LIGHT DIAGONAL CROSS
paint.drawLine(ex, y, x, ey);
paint.drawLine(x, y, ex, ey);
break;
}
}
}
void TerminalDisplay::drawLineCharString( QPainter& painter, int x, int y, const std::wstring& str,
const Character* attributes) const
{
const QPen& currentPen = painter.pen();
if ( (attributes->rendition & RE_BOLD) && _boldIntense )
{
QPen boldPen(currentPen);
boldPen.setWidth(3);
painter.setPen( boldPen );
}
for (size_t i=0 ; i < str.length(); i++)
{
uint8_t code = static_cast<uint8_t>(str[i] & 0xffU);
if (LineChars[code])
drawLineChar(painter, x + (_fontWidth*i), y, _fontWidth, _fontHeight, code);
else
drawOtherChar(painter, x + (_fontWidth * i), y, _fontWidth, _fontHeight, code);
}
painter.setPen( currentPen );
}
void TerminalDisplay::setKeyboardCursorShape(QTermWidget::KeyboardCursorShape shape)
{
_cursorShape = shape;
updateCursor();
}
QTermWidget::KeyboardCursorShape TerminalDisplay::keyboardCursorShape() const
{
return _cursorShape;
}
void TerminalDisplay::setKeyboardCursorColor(bool useForegroundColor, const QColor& color)
{
if (useForegroundColor)
_cursorColor = QColor(); // an invalid color means that
// the foreground color of the
// current character should
// be used
else
_cursorColor = color;
}
QColor TerminalDisplay::keyboardCursorColor() const
{
return _cursorColor;
}
void TerminalDisplay::setOpacity(qreal opacity)
{
_opacity = qBound(static_cast<qreal>(0), opacity, static_cast<qreal>(1));
}
void TerminalDisplay::setBackgroundImage(const QString& backgroundImage)
{
if (!backgroundImage.isEmpty())
{
_backgroundImage.load(backgroundImage);
setAttribute(Qt::WA_OpaquePaintEvent, false);
}
else
{
_backgroundImage = QPixmap();
setAttribute(Qt::WA_OpaquePaintEvent, true);
}
}
void TerminalDisplay::setBackgroundMode(BackgroundMode mode)
{
_backgroundMode = mode;
}
void TerminalDisplay::drawBackground(QPainter& painter, const QRect& rect, const QColor& backgroundColor, bool useOpacitySetting )
{
// The whole widget rectangle is filled by the background color from
// the color scheme set in setColorTable(), while the scrollbar is
// left to the widget style for a consistent look.
if ( useOpacitySetting )
{
if (_backgroundImage.isNull()) {
QColor color(backgroundColor);
color.setAlphaF(_opacity);
painter.save();
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(rect, color);
painter.restore();
}
}
else
painter.fillRect(rect, backgroundColor);
}
void TerminalDisplay::drawCursor(QPainter& painter,
const QRect& rect,
const QColor& foregroundColor,
const QColor& /*backgroundColor*/,
bool& invertCharacterColor)
{
QRectF cursorRect = rect;
cursorRect.setHeight(_fontHeight - _lineSpacing - 1);
if (!_cursorBlinking)
{
if ( _cursorColor.isValid() )
painter.setPen(_cursorColor);
else
painter.setPen(foregroundColor);
if ( _cursorShape == Emulation::KeyboardCursorShape::BlockCursor )
{
// draw the cursor outline, adjusting the area so that
// it is draw entirely inside 'rect'
float penWidth = qMax(1,painter.pen().width());
painter.drawRect(cursorRect.adjusted(penWidth/2,
penWidth/2,
- penWidth/2,
- penWidth/2));
if ( hasFocus() )
{
painter.fillRect(cursorRect, _cursorColor.isValid() ? _cursorColor : foregroundColor);
if ( !_cursorColor.isValid() )
{
// invert the colour used to draw the text to ensure that the character at
// the cursor position is readable
invertCharacterColor = true;
}
}
}
else if ( _cursorShape == Emulation::KeyboardCursorShape::UnderlineCursor )
painter.drawLine(cursorRect.left(),
cursorRect.bottom(),
cursorRect.right(),
cursorRect.bottom());
else if ( _cursorShape == Emulation::KeyboardCursorShape::IBeamCursor )
painter.drawLine(cursorRect.left(),
cursorRect.top(),
cursorRect.left(),
cursorRect.bottom());
}
}
void TerminalDisplay::drawCharacters(QPainter& painter,
const QRect& rect,
const std::wstring& text,
const Character* style,
bool invertCharacterColor,
bool tooWide)
{
// don't draw text which is currently blinking
if ( _blinking && (style->rendition & RE_BLINK) )
return;
// don't draw concealed characters
if (style->rendition & RE_CONCEAL)
return;
// setup bold and underline
bool useBold = ((style->rendition & RE_BOLD) && _boldIntense) || font().bold();
const bool useUnderline = style->rendition & RE_UNDERLINE || font().underline();
const bool useItalic = style->rendition & RE_ITALIC || font().italic();
const bool useStrikeOut = style->rendition & RE_STRIKEOUT || font().strikeOut();
const bool useOverline = style->rendition & RE_OVERLINE || font().overline();
QFont font = painter.font();
if ( font.bold() != useBold
|| font.underline() != useUnderline
|| font.italic() != useItalic
|| font.strikeOut() != useStrikeOut
|| font.overline() != useOverline) {
font.setBold(useBold);
font.setUnderline(useUnderline);
font.setItalic(useItalic);
font.setStrikeOut(useStrikeOut);
font.setOverline(useOverline);
painter.setFont(font);
}
// setup pen
const CharacterColor& textColor = ( invertCharacterColor ? style->backgroundColor : style->foregroundColor );
const QColor color = textColor.color(_colorTable);
QPen pen = painter.pen();
if ( pen.color() != color )
{
pen.setColor(color);
painter.setPen(color);
}
// draw text
if ( isLineCharString(text) )
drawLineCharString(painter,rect.x(),rect.y(),text,style);
else
{
// Force using LTR as the document layout for the terminal area, because
// there is no use cases for RTL emulator and RTL terminal application.
//
// This still allows RTL characters to be rendered in the RTL way.
painter.setLayoutDirection(Qt::LeftToRight);
if (_bidiEnabled) {
if (tooWide)
{
QRect drawRect(rect.topLeft(), rect.size());
drawRect.setHeight(rect.height() + _drawTextAdditionHeight);
painter.drawText(drawRect, Qt::AlignBottom, QString::fromStdWString(text));
}
else
{
painter.drawText(rect.x(), rect.y() + _fontAscent + _lineSpacing,
QString::fromStdWString(text));
}
}
else
{
QRect drawRect(rect.topLeft(), rect.size());
drawRect.setHeight(rect.height() + _drawTextAdditionHeight);
painter.drawText(drawRect, Qt::AlignBottom, LTR_OVERRIDE_CHAR + QString::fromStdWString(text));
}
}
}
void TerminalDisplay::drawTextFragment(QPainter& painter ,
const QRect& rect,
const std::wstring& text,
const Character* style,
bool tooWide)
{
painter.save();
// setup painter
const QColor foregroundColor = style->foregroundColor.color(_colorTable);
const QColor backgroundColor = style->backgroundColor.color(_colorTable);
// draw background if different from the display's background color
if ( backgroundColor != palette().window().color() )
drawBackground(painter,rect,backgroundColor,
false /* do not use transparency */);
// draw cursor shape if the current character is the cursor
// this may alter the foreground and background colors
bool invertCharacterColor = false;
if ( style->rendition & RE_CURSOR )
drawCursor(painter,rect,foregroundColor,backgroundColor,invertCharacterColor);
// draw text
drawCharacters(painter,rect,text,style,invertCharacterColor, tooWide);
painter.restore();
}
void TerminalDisplay::setRandomSeed(uint randomSeed) { _randomSeed = randomSeed; }
uint TerminalDisplay::randomSeed() const { return _randomSeed; }
#if 0
/*!
Set XIM Position
*/
void TerminalDisplay::setCursorPos(const int curx, const int cury)
{
QPoint tL = contentsRect().topLeft();
int tLx = tL.x();
int tLy = tL.y();
int xpos, ypos;
ypos = _topMargin + tLy + _fontHeight*(cury-1) + _fontAscent;
xpos = _leftMargin + tLx + _fontWidth*curx;
//setMicroFocusHint(xpos, ypos, 0, _fontHeight); //### ???
// fprintf(stderr, "x/y = %d/%d\txpos/ypos = %d/%d\n", curx, cury, xpos, ypos);
_cursorLine = cury;
_cursorCol = curx;
}
#endif
// scrolls the image by 'lines', down if lines > 0 or up otherwise.
//
// the terminal emulation keeps track of the scrolling of the character
// image as it receives input, and when the view is updated, it calls scrollImage()
// with the final scroll amount. this improves performance because scrolling the
// display is much cheaper than re-rendering all the text for the
// part of the image which has moved up or down.
// Instead only new lines have to be drawn
void TerminalDisplay::scrollImage(int lines , const QRect& screenWindowRegion)
{
// if the flow control warning is enabled this will interfere with the
// scrolling optimizations and cause artifacts. the simple solution here
// is to just disable the optimization whilst it is visible
if ( _outputSuspendedLabel && _outputSuspendedLabel->isVisible() )
return;
// constrain the region to the display
// the bottom of the region is capped to the number of lines in the display's
// internal image - 2, so that the height of 'region' is strictly less
// than the height of the internal image.
QRect region = screenWindowRegion;
region.setBottom( qMin(region.bottom(),this->_lines-2) );
// return if there is nothing to do
if ( lines == 0
|| _image == nullptr
|| !region.isValid()
|| (region.top() + abs(lines)) >= region.bottom()
|| this->_lines <= region.height() ) return;
// hide terminal size label to prevent it being scrolled
if (_resizeWidget && _resizeWidget->isVisible())
_resizeWidget->hide();
// Note: With Qt 4.4 the left edge of the scrolled area must be at 0
// to get the correct (newly exposed) part of the widget repainted.
//
// The right edge must be before the left edge of the scroll bar to
// avoid triggering a repaint of the entire widget, the distance is
// given by SCROLLBAR_CONTENT_GAP
//
// Set the QT_FLUSH_PAINT environment variable to '1' before starting the
// application to monitor repainting.
//
int scrollBarWidth = _scrollBar->isHidden() ? 0 :
_scrollBar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, _scrollBar) ?
0 : _scrollBar->width();
const int SCROLLBAR_CONTENT_GAP = scrollBarWidth == 0 ? 0 : 1;
QRect scrollRect;
if ( _scrollbarLocation == QTermWidget::ScrollBarLeft )
{
scrollRect.setLeft(scrollBarWidth+SCROLLBAR_CONTENT_GAP);
scrollRect.setRight(width());
}
else
{
scrollRect.setLeft(0);
scrollRect.setRight(width() - scrollBarWidth - SCROLLBAR_CONTENT_GAP);
}
void* firstCharPos = &_image[ region.top() * this->_columns ];
void* lastCharPos = &_image[ (region.top() + abs(lines)) * this->_columns ];
int top = _topMargin + (region.top() * _fontHeight);
int linesToMove = region.height() - abs(lines);
int bytesToMove = linesToMove *
this->_columns *
sizeof(Character);
Q_ASSERT( linesToMove > 0 );
Q_ASSERT( bytesToMove > 0 );