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
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#pragma once
#include "terminal_global.h"
#include "terminalsurface.h"
#include <QAbstractScrollArea>
#include <QAction>
#include <QTextLayout>
#include <QTimer>
#include <chrono>
#include <memory>
#include <optional>
namespace TerminalSolution {
class SurfaceIntegration;
class TerminalViewPrivate;
struct SearchHit
{
int start{-1};
int end{-1};
bool operator!=(const SearchHit &other) const
{
return start != other.start || end != other.end;
}
bool operator==(const SearchHit &other) const { return !operator!=(other); }
};
QString TERMINAL_EXPORT defaultFontFamily();
int TERMINAL_EXPORT defaultFontSize();
class TERMINAL_EXPORT TerminalView : public QAbstractScrollArea
{
friend class CellIterator;
Q_OBJECT
public:
enum class WidgetColorIdx {
Foreground = ColorIndex::Foreground,
Background = ColorIndex::Background,
Selection,
FindMatch,
};
TerminalView(QWidget *parent = nullptr);
~TerminalView() override;
void setAllowBlinkingCursor(bool allow);
bool allowBlinkingCursor() const;
void setFont(const QFont &font);
void enableMouseTracking(bool enable);
void copyToClipboard();
void pasteFromClipboard();
void paste(const QString &text);
void copyLinkToClipboard();
struct Selection
{
int start;
int end;
bool final{false};
bool operator!=(const Selection &other) const
{
return start != other.start || end != other.end || final != other.final;
}
bool operator==(const Selection &other) const { return !operator!=(other); }
};
std::optional<Selection> selection() const;
void clearSelection();
void selectAll();
void zoomIn();
void zoomOut();
void moveCursorWordLeft();
void moveCursorWordRight();
void clearContents();
void setSurfaceIntegration(SurfaceIntegration *surfaceIntegration);
void setColors(const std::array<QColor, 20> &colors);
void setPasswordMode(bool passwordMode);
struct Link
{
QString text;
int targetLine = 0;
int targetColumn = 0;
};
struct LinkSelection : public Selection
{
Link link;
bool operator!=(const LinkSelection &other) const
{
return link.text != other.link.text || link.targetLine != other.link.targetLine
|| link.targetColumn != other.link.targetColumn || Selection::operator!=(other);
}
};
virtual qint64 writeToPty(const QByteArray &data)
{
Q_UNUSED(data)
return 0;
}
void writeToTerminal(const QByteArray &data, bool forceFlush);
void restart();
virtual const QList<SearchHit> &searchHits() const
{
static QList<SearchHit> noHits;
return noHits;
}
virtual bool resizePty(QSize newSize)
{
Q_UNUSED(newSize)
return true;
}
virtual void setClipboard(const QString &text) { Q_UNUSED(text) }
virtual std::optional<Link> toLink(const QString &text)
{
Q_UNUSED(text)
return std::nullopt;
}
virtual void selectionChanged(const std::optional<Selection> &newSelection)
{
Q_UNUSED(newSelection)
}
virtual void linkActivated(const Link &link) { Q_UNUSED(link) }
virtual void contextMenuRequested(const QPoint &pos) { Q_UNUSED(pos) }
virtual void surfaceChanged(){};
TerminalSurface *surface() const;
protected:
void paintEvent(QPaintEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void inputMethodEvent(QInputMethodEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
bool event(QEvent *event) override;
protected:
void setupSurface();
int paintCell(QPainter &p,
const QRectF &cellRect,
QPoint gridPos,
const TerminalCell &cell,
QFont &f,
QList<SearchHit>::const_iterator &searchIt) const;
void paintCells(QPainter &painter, QPaintEvent *event) const;
void paintCursor(QPainter &painter) const;
void paintPreedit(QPainter &painter) const;
bool paintFindMatches(QPainter &painter,
QList<SearchHit>::const_iterator &searchIt,
const QRectF &cellRect,
const QPoint gridPos) const;
bool paintSelection(QPainter &painter, const QRectF &cellRect, const QPoint gridPos) const;
void paintDebugSelection(QPainter &painter, const Selection &selection) const;
qreal topMargin() const;
QPoint viewportToGlobal(QPoint p) const;
QPoint globalToViewport(QPoint p) const;
QPoint globalToGrid(QPointF p) const;
QPointF gridToGlobal(QPoint p, bool bottom = false, bool right = false) const;
QRect gridToViewport(QRect rect) const;
QPoint toGridPos(QMouseEvent *event) const;
void updateViewport();
void updateViewportRect(const QRect &rect);
int textLineFromPixel(int y) const;
std::optional<int> textPosFromPoint(const QTextLayout &textLayout, QPoint p) const;
std::optional<QTextLayout::FormatRange> selectionToFormatRange(Selection selection,
const QTextLayout &layout,
int rowOffset) const;
bool checkLinkAt(const QPoint &pos);
struct TextAndOffsets
{
int start;
int end;
std::u32string text;
};
TextAndOffsets textAt(const QPoint &pos) const;
void applySizeChange();
void updateScrollBars();
void flushVTerm(bool force);
bool setSelection(const std::optional<Selection> &selection, bool scroll = true);
QString textFromSelection() const;
void configBlinkTimer();
QColor toQColor(std::variant<int, QColor> color) const;
private:
void scheduleViewportUpdate();
signals:
void cleared();
private:
std::unique_ptr<TerminalViewPrivate> d;
};
} // namespace TerminalSolution
|