-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqlightterminal.cpp
624 lines (501 loc) · 18.3 KB
/
qlightterminal.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
/*
* Copyright© Florian Plesker <[email protected]>
*/
#include "qlightterminal.h"
#include <QByteArray>
#include <QTextCursor>
#include <QPainter>
#include <QKeyEvent>
#include <QPoint>
#include <QLayout>
#include <QGraphicsAnchorLayout>
#include <QClipboard>
#include <QGuiApplication>
#include <QPointF>
#include <QFontMetricsF>
QLightTerminal::QLightTerminal(QWidget *parent) : QWidget(parent), scrollbar(Qt::Orientation::Vertical),
boxLayout(this), cursorTimer(this), selectionTimer(this),
win{0, 0, 0, 0, 100, 10, 10, 1.25, 10, 8.42, 0, 8} {
// set up terminal
st = new SimpleTerminal();
// setup default style
// Note: font size is not reliable use win.charWidth for length computation
setAttribute(Qt::WA_StyledBackground, true);
this->setFontSize(10, 500);
this->updateStyleSheet();
// set up scrollbar
boxLayout.setSpacing(0);
boxLayout.setContentsMargins(0, 0, 0, 0);
boxLayout.addWidget(&scrollbar);
boxLayout.setAlignment(&scrollbar, Qt::AlignRight);
connect(&scrollbar, &QScrollBar::valueChanged, this, &QLightTerminal::scrollX);
win.viewPortHeight = win.height / win.lineheight;
setupScrollbar();
connect(st, &SimpleTerminal::s_error, this, [this](QString error) { emit s_error("Error from st: " + error); });
connect(st, &SimpleTerminal::s_updateView, this, &QLightTerminal::updateTerminal);
// set up blinking cursor
connect(&cursorTimer, &QTimer::timeout, this, [this]() {
cursorVisible = !cursorVisible;
// cursor position is in float units while the update is in int -> need to rerender full screen to avoid cutoffs
update();
});
cursorTimer.start(750);
// allows for auto scrolling on selection reaching the borders
connect(&selectionTimer, &QTimer::timeout, this, &QLightTerminal::updateSelection);
// debounce resizing
connect(&resizeTimer, &QTimer::timeout, this, &QLightTerminal::resize);
// connect close event of the tty
connect(st, &SimpleTerminal::s_closed, this, &QLightTerminal::close);
}
void QLightTerminal::close() {
setDisabled(true);
closed = true;
scrollbar.setVisible(false);
cursorTimer.stop();
selectionTimer.stop();
update();
emit s_closed();
}
void QLightTerminal::updateTerminal(Term *term) {
cursorVisible = true;
cursorTimer.start(750);
if (st->term.histi != scrollbar.maximum()) {
bool isMax = scrollbar.value() == scrollbar.value();
scrollbar.setMaximum(st->term.histi * win.scrollMultiplier);
// stick to the bottom
if (isMax) {
scrollbar.setValue(scrollbar.maximum());
}
scrollbar.setVisible(scrollbar.maximum() != 0);
}
update();
}
void QLightTerminal::scrollX(int n) {
int scroll = (st->term.scr - (scrollbar.maximum() - scrollbar.value()) / win.scrollMultiplier);
if (scroll < 0) {
st->kscrollup(-scroll);
} else {
st->kscrolldown(scroll);
}
update();
}
void QLightTerminal::setFontSize(int size, int weight) {
QFont mono = QFont("Monospace", size, weight);
mono.setFixedPitch(true);
mono.setStyleHint(QFont::Monospace);
setFont(mono); // font must be monospace
QFontMetricsF metric = QFontMetricsF(mono);
int linespacing = metric.lineSpacing();
this->win.lineheight = linespacing * this->win.lineHeightScale;
this->win.fontSize = size;
auto initialRect = metric.boundingRect("a");
auto improvedRect = metric.boundingRect(initialRect, 0, "a");
this->win.charWith = improvedRect.width();
this->win.charHeight = improvedRect.height();
this->update();
}
void QLightTerminal::setBackground(QColor color) {
this->colors[this->defaultBackground] = color;
this->updateStyleSheet();
}
void QLightTerminal::setLineHeightScale(double scale) {
QFontMetricsF metric = QFontMetricsF(this->font());
this->win.lineheight = metric.lineSpacing() * scale;
this->win.lineHeightScale = scale;
this->update();
}
void QLightTerminal::setPadding(double vertical, double horizontal) {
this->win.hPadding = horizontal;
this->win.vPadding = vertical;
this->update();
}
void QLightTerminal::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setBackgroundMode(Qt::BGMode::OpaqueMode);
if (closed) {
painter.drawText(QPointF(win.hPadding, win.lineheight + win.vPadding), "Terminal is closed.");
return;
}
QFont font;
QString line;
uint32_t fgColor = 0;
uint32_t bgColor = 0;
uint32_t cfgColor = 0; // control for change detection
uint32_t cbgColor = 0; // control for change detection
ushort mode = -1;
double offset;
bool changed = false;
// calculate the view port
int drawOffset = MAX(event->rect().y() - win.vPadding, 0) / win.lineheight; // line index offset of the viewPort
int drawHeight = (event->rect().height()) / win.lineheight; // height of the viewPort in lines
int drawEnd = drawOffset + drawHeight; // last line index of the viewPort
int i = MIN(drawEnd, win.viewPortHeight);
int stop = MAX(i - drawHeight, 0);
double yPos = i * win.lineheight + win.vPadding; // y position of the the lastViewPortLine
int temp;
while (i > stop) {
i--;
offset = win.hPadding;
line = QString();
// same logic as TLine from st-utils
Glyph *tLine = ((i) < st->term.scr ? st->term.hist[((i) + st->term.histi - \
st->term.scr + HISTSIZE + 1) % HISTSIZE] : \
st->term.line[(i) - st->term.scr]);
for (int j = 0; j < st->term.col; j++) {
Glyph g = tLine[j];
if (g.mode == ATTR_WDUMMY)
continue;
if (cfgColor != g.fg) {
fgColor = g.fg;
cfgColor = g.fg;
changed = true;
}
if (cbgColor != g.bg) {
bgColor = g.bg;
cbgColor = g.bg;
changed = true;
}
if (st->selected(j, i)) {
g.mode ^= ATTR_REVERSE;
}
if (mode != g.mode) {
mode = g.mode;
changed = true;
fgColor = cfgColor; // reset color
bgColor = cbgColor;
if ((g.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
painter.setOpacity(0.5);
} else {
painter.setOpacity(1);
}
if (g.mode & ATTR_REVERSE) {
temp = fgColor;
fgColor = bgColor;
bgColor = temp;
}
if (g.mode & ATTR_INVISIBLE)
fgColor = bgColor;
font = painter.font();
bool bold = g.mode & ATTR_BOLD;
if (bold != font.bold()) font.setBold(bold);
bool italic = g.mode & ATTR_ITALIC;
if (italic != font.italic()) font.setItalic(italic);
bool underline = g.mode & ATTR_UNDERLINE;
if (underline != font.underline()) font.setUnderline(underline);
bool strikethrough = g.mode & ATTR_STRUCK;
if (strikethrough != font.strikeOut()) font.setStrikeOut(strikethrough);
}
// draw current line state and change color
if (changed) {
painter.drawText(QPointF(offset, yPos), line);
offset += line.size() * win.charWith;
if (IS_TRUECOL(fgColor)) {
painter.setPen(QColor(RED_FROM_TRUE(fgColor), GREEN_FROM_TRUE(fgColor), BLUE_FROM_TRUE(fgColor)));
} else {
painter.setPen(colors[fgColor]);
}
if (IS_TRUECOL(bgColor)) {
painter.setBackground(
QBrush(QColor(RED_FROM_TRUE(bgColor), GREEN_FROM_TRUE(bgColor), BLUE_FROM_TRUE(bgColor))));
} else {
painter.setBackground(QBrush(colors[bgColor]));
}
line = QString();
changed = false;
painter.setFont(font);
}
if (0xffff < g.u) {
line += QStringView(QChar::fromUcs4(g.u));
} else {
line += QChar(g.u);
}
}
painter.drawText(QPointF(offset, yPos), line);
yPos -= win.lineheight;
}
if (st->term.scr != 0) {
return; // do not draw, cursor is scrolled out of view
}
// draw cursor
// drawn by reversing foreground color and background color
fgColor = st->term.c.attr.bg;
if (IS_TRUECOL(fgColor)) {
painter.setPen(QColor(RED_FROM_TRUE(fgColor), GREEN_FROM_TRUE(fgColor), BLUE_FROM_TRUE(fgColor)));
} else {
painter.setPen(colors[fgColor]);
}
bgColor = st->term.c.attr.fg;
if (IS_TRUECOL(bgColor)) {
painter.setBackground(
QBrush(QColor(RED_FROM_TRUE(bgColor), GREEN_FROM_TRUE(bgColor), BLUE_FROM_TRUE(bgColor))));
} else {
painter.setBackground(QBrush(colors[bgColor]));
}
line = QString();
for (int i = 0; i < st->term.c.x; i++) {
auto rune = st->term.line[st->term.c.y][i].u;
if (0xffff < rune) {
line += QStringView(QChar::fromUcs4(rune));
} else {
line += QChar(rune);
}
}
int cursorOffset = line.size() * win.charWith;
double cursorPosVert = MIN(st->term.c.y + 1, win.viewPortHeight); // line of the cursor
auto cursorPos = QPointF(cursorOffset + win.hPadding, cursorPosVert * win.lineheight + win.vPadding);
if (!cursorVisible) {
return;
}
auto runeAtCursor = st->term.line[st->term.c.y][st->term.c.x].u;
if (0xffff < runeAtCursor) {
painter.drawText(cursorPos, QString(QChar::fromUcs4(runeAtCursor)));
} else {
painter.drawText(cursorPos, QChar(runeAtCursor));
}
/**
* TODO Add later
* if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
colfg.red = fg->color.red / 2;
colfg.green = fg->color.green / 2;
colfg.blue = fg->color.blue / 2;
colfg.alpha = fg->color.alpha;
XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
fg = &revfg;
}
if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
fg = bg;
*/
}
/*
* Override keyEvents and send the input to the shell
*/
void QLightTerminal::keyPressEvent(QKeyEvent *e) {
e->accept();
QString input = e->text();
Qt::KeyboardModifiers mods = e->modifiers();
int key = e->key();
if (key == Qt::Key_Backspace) {
if (mods.testFlag(Qt::KeyboardModifier::AltModifier)) {
st->ttywrite("\033\177", 2, 1);
} else {
st->ttywrite("\177", 1, 1);
}
return;
}
// paste event
if (
key == 86
&& mods & Qt::KeyboardModifier::ShiftModifier
&& mods & Qt::KeyboardModifier::ControlModifier) {
QClipboard *clipboard = QGuiApplication::clipboard();
QString clippedText = clipboard->text();
QByteArray data = clippedText.toLocal8Bit();
st->ttywrite(data.data(), data.size(), 1);
return;
}
// copy event
if (
key == 67
&& mods & Qt::KeyboardModifier::ShiftModifier
&& mods & Qt::KeyboardModifier::ControlModifier) {
QClipboard *clipboard = QGuiApplication::clipboard();
QString clippedText = QString(st->getsel());
clipboard->setText(clippedText);
return;
}
// normal input
if (input != "") {
QByteArray text;
if (input.contains('\n')) {
text = input.left(input.indexOf('\n') + 1).toUtf8();
} else {
text = e->text().toUtf8();
}
st->ttywrite(text, text.size(), 1);
} else {
// special keys
// TODO: Add more short cuts
int end = 24;
if (key > keys[end].key || key < keys[0].key) {
return;
}
for (int i = 0; i < end;) {
int nextKey = keys[i].nextKey;
if (key == keys[i].key) {
for (int j = i; j < i + nextKey; j++) {
if (mods.testFlag(keys[j].mods)) {
st->ttywrite(keys[j].cmd, keys[j].cmd_size, 1);
return;
}
}
}
i += nextKey;
}
}
}
void QLightTerminal::mousePressEvent(QMouseEvent *event) {
setFocus();
mouseDown = true;
lastMousePos = event->pos();
// reset old selection
st->selclear();
update();
// select line if tripple click
if (QDateTime::currentMSecsSinceEpoch() - lastClick < 500) {
lastClick = 0;
QPointF pos = event->position();
int col = (pos.x() - win.hPadding) / win.charWith;
int row = (pos.y() - win.vPadding) / win.lineheight;
st->selstart(col, row, SNAP_LINE);
}
// draw cursor
cursorVisible = true;
update(); // draw cursor
cursorTimer.start(750);
}
void QLightTerminal::mouseReleaseEvent(QMouseEvent *event) {
mouseDown = false;
// close selection if started
if (selectionStarted) {
QPointF pos = event->position();
int col = (pos.x() - win.hPadding) / win.charWith;
int row = (pos.y() - win.vPadding) / win.lineheight;
col = MIN(col, win.viewPortWidth - 1);
row = MIN(row, win.viewPortHeight - 1);
st->selextend(col, row, SEL_REGULAR, 1);
selectionStarted = false;
selectionTimer.stop();
update();
}
};
void QLightTerminal::mouseMoveEvent(QMouseEvent *event) {
// selection handling
if (mouseDown) {
lastMousePos = event->position();
if (!selectionStarted) {
QPointF pos = event->position();
int col = (pos.x() - win.hPadding) / win.charWith;
double row = (pos.y() - win.vPadding) / win.lineheight;
if (row >= this->win.viewPortHeight) {
return;
}
st->selstart(col, row, 0);
selectionTimer.start(100);
selectionStarted = true;
}
}
}
bool QLightTerminal::focusNextPrevChild(bool next) {
// disabled to allow the terminal to consume tab key presses
return false;
}
void QLightTerminal::updateStyleSheet() {
QString stylesheet;
stylesheet += "background-color:" + this->colors[this->defaultBackground].name() + ";";
setStyleSheet(stylesheet);
this->update();
};
void QLightTerminal::updateSelection() {
if (selectionStarted) {
int col = (lastMousePos.x() - win.hPadding) / win.charWith;
double row = (lastMousePos.y() - win.vPadding) / win.lineheight;
if (row < 0.4) {
// scroll up
double scroll = MIN(scrollbar.value() / win.lineheight, (row - 0.4) * -2);
scrollbar.setValue(scrollbar.value() - scroll * win.scrollMultiplier);
}
if (row > win.viewPortHeight - 0.4) {
// scroll down
double scroll = MIN((scrollbar.maximum() - scrollbar.value()) / win.lineheight,
(row - win.viewPortHeight + 0.4) * 2);
scrollbar.setValue(scrollbar.value() + scroll * win.scrollMultiplier);
}
col = MIN(col, win.viewPortWidth - 1);
row = MIN(row, win.viewPortHeight - 1);
st->selextend(col, row, SEL_REGULAR, 0);
update();
}
}
void QLightTerminal::mouseDoubleClickEvent(QMouseEvent *event) {
QPointF pos = event->position();
int col = (pos.x() - win.hPadding) / win.charWith;
int row = (pos.y() - win.vPadding) / win.lineheight;
if (row >= this->win.viewPortHeight || row < 0) {
return;
}
if (col >= this->win.viewPortWidth || col < 0) {
return;
}
st->selclear();
st->selstart(col, row, SNAP_WORD);
lastClick = QDateTime::currentMSecsSinceEpoch();
update();
}
void QLightTerminal::resizeEvent(QResizeEvent *event) {
if (closed) {
return;
}
win.height = event->size().height();
win.width = event->size().width();
if (resizeTimer.isActive()) {
resizeTimer.start(500);
return;
}
resizeTimer.start(500);
event->accept();
}
void QLightTerminal::resize() {
resizeTimer.stop();
int paddingBottom = 4;
int rows = (win.height - win.vPadding * 2 - paddingBottom) / win.lineheight;
int scrollbarPadding = 5;
int cols = (win.width - 2 * win.hPadding - scrollbarPadding) / win.charWith;
cols = MAX(1, cols);
if (cols == win.viewPortWidth && rows == win.viewPortHeight) {
return;
}
win.viewPortWidth = cols;
win.viewPortHeight = rows;
st->tresize(cols, win.viewPortHeight);
st->ttyresize(cols * 8.5, win.viewPortHeight * win.lineheight);
}
void QLightTerminal::wheelEvent(QWheelEvent *event) {
QPoint numPixels = event->pixelDelta();
event->accept();
if (!numPixels.isNull()) {
// TODO: test if this works << needs mouse with finer resolution
scrollbar.setValue(scrollbar.value() - numPixels.y() * 2);
return;
}
QPoint numDegrees = event->angleDelta();
if (!numDegrees.isNull()) {
scrollbar.setValue(scrollbar.value() - numDegrees.y() * 2);
return;
}
}
void QLightTerminal::focusOutEvent(QFocusEvent *event) {
cursorTimer.stop();
cursorVisible = false;
// redraw cursor position
update();
}
void QLightTerminal::setupScrollbar() {
scrollbar.setMaximum(0); // will set in the update Terminal function
scrollbar.setValue(0);
scrollbar.setPageStep(100 * win.scrollMultiplier);
scrollbar.setVisible(false);
scrollbar.setStyleSheet(R"(
QScrollBar::sub-line:vertical, QScrollBar::add-line:vertical {
height: 0;
}
QScrollBar::vertical {
margin: 0;
width: 9px;
padding-right: 1px;
}
QScrollBar::handle {
background: #aaaaaa;
border-radius: 4px;
}
)");
}