1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQUICKTEXTEDIT_P_H
5#define QQUICKTEXTEDIT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qquickimplicitsizeitem_p.h"
19#include "qquicktextinterface_p.h"
20
21#include <QtGui/qtextoption.h>
22
23QT_BEGIN_NAMESPACE
24
25class QQuickTextDocument;
26class QQuickTextEditPrivate;
27class QTextBlock;
28
29class Q_QUICK_PRIVATE_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem, public QQuickTextInterface
30{
31 Q_OBJECT
32 Q_INTERFACES(QQuickTextInterface)
33
34 Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged FINAL)
35 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged FINAL)
36 Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged FINAL)
37 Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged FINAL)
38 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
39 Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged FINAL)
40 Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged FINAL)
41 Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged FINAL)
42 Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged FINAL)
43 Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged FINAL)
44 Q_PROPERTY(int length READ length NOTIFY textChanged FINAL)
45 Q_PROPERTY(qreal contentWidth READ contentWidth NOTIFY contentSizeChanged FINAL)
46 Q_PROPERTY(qreal contentHeight READ contentHeight NOTIFY contentSizeChanged FINAL)
47 Q_PROPERTY(qreal paintedWidth READ contentWidth NOTIFY contentSizeChanged FINAL) // Compatibility
48 Q_PROPERTY(qreal paintedHeight READ contentHeight NOTIFY contentSizeChanged FINAL)
49 Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY textFormatChanged FINAL)
50 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged FINAL)
51 Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged FINAL)
52 Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged FINAL)
53 Q_PROPERTY(QRectF cursorRectangle READ cursorRectangle NOTIFY cursorRectangleChanged FINAL)
54 Q_PROPERTY(QQmlComponent* cursorDelegate READ cursorDelegate WRITE setCursorDelegate NOTIFY cursorDelegateChanged FINAL)
55 Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode NOTIFY overwriteModeChanged FINAL)
56 Q_PROPERTY(int selectionStart READ selectionStart NOTIFY selectionStartChanged FINAL)
57 Q_PROPERTY(int selectionEnd READ selectionEnd NOTIFY selectionEndChanged FINAL)
58 Q_PROPERTY(QString selectedText READ selectedText NOTIFY selectedTextChanged FINAL)
59 Q_PROPERTY(bool activeFocusOnPress READ focusOnPress WRITE setFocusOnPress NOTIFY activeFocusOnPressChanged FINAL)
60 Q_PROPERTY(bool persistentSelection READ persistentSelection WRITE setPersistentSelection NOTIFY persistentSelectionChanged FINAL)
61 Q_PROPERTY(qreal textMargin READ textMargin WRITE setTextMargin NOTIFY textMarginChanged FINAL)
62 Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints NOTIFY inputMethodHintsChanged FINAL)
63 Q_PROPERTY(bool selectByKeyboard READ selectByKeyboard WRITE setSelectByKeyboard NOTIFY selectByKeyboardChanged REVISION(2, 1) FINAL)
64 Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged FINAL)
65 Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged FINAL)
66 Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged FINAL)
67 Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged FINAL)
68 Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged FINAL)
69 Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged FINAL)
70 Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged FINAL)
71 Q_PROPERTY(RenderType renderType READ renderType WRITE setRenderType NOTIFY renderTypeChanged FINAL)
72 Q_PROPERTY(QQuickTextDocument *textDocument READ textDocument CONSTANT FINAL REVISION(2, 1))
73 Q_PROPERTY(QString hoveredLink READ hoveredLink NOTIFY linkHovered REVISION(2, 2) FINAL)
74 Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged REVISION(2, 6) FINAL)
75 Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY topPaddingChanged REVISION(2, 6) FINAL)
76 Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY leftPaddingChanged REVISION(2, 6) FINAL)
77 Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding NOTIFY rightPaddingChanged REVISION(2, 6) FINAL)
78 Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding NOTIFY bottomPaddingChanged REVISION(2, 6) FINAL)
79 Q_PROPERTY(QString preeditText READ preeditText NOTIFY preeditTextChanged REVISION(2, 7) FINAL)
80 Q_PROPERTY(qreal tabStopDistance READ tabStopDistance WRITE setTabStopDistance NOTIFY tabStopDistanceChanged REVISION(2, 10) FINAL)
81 QML_NAMED_ELEMENT(TextEdit)
82#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
83 QML_ADDED_IN_VERSION(6, 4)
84#endif
85
86public:
87 QQuickTextEdit(QQuickItem *parent=nullptr);
88
89 enum HAlignment {
90 AlignLeft = Qt::AlignLeft,
91 AlignRight = Qt::AlignRight,
92 AlignHCenter = Qt::AlignHCenter,
93 AlignJustify = Qt::AlignJustify
94 };
95 Q_ENUM(HAlignment)
96
97 enum VAlignment {
98 AlignTop = Qt::AlignTop,
99 AlignBottom = Qt::AlignBottom,
100 AlignVCenter = Qt::AlignVCenter
101 };
102 Q_ENUM(VAlignment)
103
104 enum TextFormat {
105 PlainText = Qt::PlainText,
106 RichText = Qt::RichText,
107 AutoText = Qt::AutoText,
108 MarkdownText = Qt::MarkdownText
109 };
110 Q_ENUM(TextFormat)
111
112 enum WrapMode { NoWrap = QTextOption::NoWrap,
113 WordWrap = QTextOption::WordWrap,
114 WrapAnywhere = QTextOption::WrapAnywhere,
115 WrapAtWordBoundaryOrAnywhere = QTextOption::WrapAtWordBoundaryOrAnywhere, // COMPAT
116 Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere
117 };
118 Q_ENUM(WrapMode)
119
120 enum SelectionMode {
121 SelectCharacters,
122 SelectWords
123 };
124 Q_ENUM(SelectionMode)
125
126 enum RenderType { QtRendering,
127 NativeRendering
128 };
129 Q_ENUM(RenderType)
130
131 QString text() const;
132 void setText(const QString &);
133
134 Q_REVISION(2, 7) QString preeditText() const;
135
136 TextFormat textFormat() const;
137 void setTextFormat(TextFormat format);
138
139 QFont font() const;
140 void setFont(const QFont &font);
141
142 QColor color() const;
143 void setColor(const QColor &c);
144
145 QColor selectionColor() const;
146 void setSelectionColor(const QColor &c);
147
148 QColor selectedTextColor() const;
149 void setSelectedTextColor(const QColor &c);
150
151 HAlignment hAlign() const;
152 void setHAlign(HAlignment align);
153 void resetHAlign();
154 HAlignment effectiveHAlign() const;
155
156 VAlignment vAlign() const;
157 void setVAlign(VAlignment align);
158
159 WrapMode wrapMode() const;
160 void setWrapMode(WrapMode w);
161
162 int lineCount() const;
163
164 int length() const;
165
166 bool isCursorVisible() const;
167 void setCursorVisible(bool on);
168
169 int cursorPosition() const;
170 void setCursorPosition(int pos);
171
172 QQmlComponent* cursorDelegate() const;
173 void setCursorDelegate(QQmlComponent*);
174
175 bool overwriteMode() const;
176 void setOverwriteMode(bool overwrite);
177
178 int selectionStart() const;
179 int selectionEnd() const;
180
181 QString selectedText() const;
182
183 bool focusOnPress() const;
184 void setFocusOnPress(bool on);
185
186 bool persistentSelection() const;
187 void setPersistentSelection(bool on);
188
189 qreal textMargin() const;
190 void setTextMargin(qreal margin);
191
192 Qt::InputMethodHints inputMethodHints() const;
193 void setInputMethodHints(Qt::InputMethodHints hints);
194
195 bool selectByKeyboard() const;
196 void setSelectByKeyboard(bool);
197
198 bool selectByMouse() const;
199 void setSelectByMouse(bool);
200
201 SelectionMode mouseSelectionMode() const;
202 void setMouseSelectionMode(SelectionMode mode);
203
204 bool canPaste() const;
205
206 bool canUndo() const;
207 bool canRedo() const;
208
209 void componentComplete() override;
210
211 /* FROM EDIT */
212 void setReadOnly(bool);
213 bool isReadOnly() const;
214
215 QRectF cursorRectangle() const;
216
217#if QT_CONFIG(im)
218 QVariant inputMethodQuery(Qt::InputMethodQuery property) const override;
219 Q_REVISION(2, 4) Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const;
220#endif
221
222 qreal contentWidth() const;
223 qreal contentHeight() const;
224
225 QUrl baseUrl() const;
226 void setBaseUrl(const QUrl &url);
227 void resetBaseUrl();
228
229 Q_INVOKABLE QRectF positionToRectangle(int) const;
230 Q_INVOKABLE int positionAt(qreal x, qreal y) const;
231 Q_INVOKABLE void moveCursorSelection(int pos);
232 Q_INVOKABLE void moveCursorSelection(int pos, SelectionMode mode);
233
234 QRectF boundingRect() const override;
235 QRectF clipRect() const override;
236
237 bool isInputMethodComposing() const;
238
239 RenderType renderType() const;
240 void setRenderType(RenderType renderType);
241
242 Q_INVOKABLE QString getText(int start, int end) const;
243 Q_INVOKABLE QString getFormattedText(int start, int end) const;
244
245 QQuickTextDocument *textDocument();
246
247 QString hoveredLink() const;
248
249 Q_REVISION(2, 3) Q_INVOKABLE QString linkAt(qreal x, qreal y) const;
250
251 qreal padding() const;
252 void setPadding(qreal padding);
253 void resetPadding();
254
255 qreal topPadding() const;
256 void setTopPadding(qreal padding);
257 void resetTopPadding();
258
259 qreal leftPadding() const;
260 void setLeftPadding(qreal padding);
261 void resetLeftPadding();
262
263 qreal rightPadding() const;
264 void setRightPadding(qreal padding);
265 void resetRightPadding();
266
267 qreal bottomPadding() const;
268 void setBottomPadding(qreal padding);
269 void resetBottomPadding();
270
271 int tabStopDistance() const;
272 void setTabStopDistance(qreal distance);
273
274 void invalidate() override;
275
276Q_SIGNALS:
277 void textChanged();
278 Q_REVISION(2, 7) void preeditTextChanged();
279 void contentSizeChanged();
280 void cursorPositionChanged();
281 void cursorRectangleChanged();
282 void selectionStartChanged();
283 void selectionEndChanged();
284 void selectedTextChanged();
285 void colorChanged(const QColor &color);
286 void selectionColorChanged(const QColor &color);
287 void selectedTextColorChanged(const QColor &color);
288 void fontChanged(const QFont &font);
289 void horizontalAlignmentChanged(QQuickTextEdit::HAlignment alignment);
290 void verticalAlignmentChanged(QQuickTextEdit::VAlignment alignment);
291 void wrapModeChanged();
292 void lineCountChanged();
293 void textFormatChanged(QQuickTextEdit::TextFormat textFormat);
294 void readOnlyChanged(bool isReadOnly);
295 void cursorVisibleChanged(bool isCursorVisible);
296 void cursorDelegateChanged();
297 void overwriteModeChanged(bool overwriteMode);
298 void activeFocusOnPressChanged(bool activeFocusOnPressed);
299 void persistentSelectionChanged(bool isPersistentSelection);
300 void textMarginChanged(qreal textMargin);
301 Q_REVISION(2, 1) void selectByKeyboardChanged(bool selectByKeyboard);
302 void selectByMouseChanged(bool selectByMouse);
303 void mouseSelectionModeChanged(QQuickTextEdit::SelectionMode mode);
304 void linkActivated(const QString &link);
305 Q_REVISION(2, 2) void linkHovered(const QString &link);
306 void canPasteChanged();
307 void canUndoChanged();
308 void canRedoChanged();
309 void inputMethodComposingChanged();
310 void effectiveHorizontalAlignmentChanged();
311 void baseUrlChanged();
312 void inputMethodHintsChanged();
313 void renderTypeChanged();
314 Q_REVISION(2, 6) void editingFinished();
315 Q_REVISION(2, 6) void paddingChanged();
316 Q_REVISION(2, 6) void topPaddingChanged();
317 Q_REVISION(2, 6) void leftPaddingChanged();
318 Q_REVISION(2, 6) void rightPaddingChanged();
319 Q_REVISION(2, 6) void bottomPaddingChanged();
320 Q_REVISION(2, 10) void tabStopDistanceChanged(qreal distance);
321
322public Q_SLOTS:
323 void selectAll();
324 void selectWord();
325 void select(int start, int end);
326 void deselect();
327 bool isRightToLeft(int start, int end);
328#if QT_CONFIG(clipboard)
329 void cut();
330 void copy();
331 void paste();
332#endif
333 void undo();
334 void redo();
335 void insert(int position, const QString &text);
336 void remove(int start, int end);
337 Q_REVISION(2, 2) void append(const QString &text);
338 Q_REVISION(2, 7) void clear();
339
340private Q_SLOTS:
341 void q_invalidate();
342 void q_textChanged();
343 void q_contentsChange(int, int, int);
344 void updateSelection();
345 void moveCursorDelegate();
346 void createCursor();
347 void q_canPasteChanged();
348 void updateWholeDocument();
349 void invalidateBlock(const QTextBlock &block);
350 void updateCursor();
351 void q_linkHovered(const QString &link);
352 void q_markerHovered(bool hovered);
353 void q_updateAlignment();
354 void updateSize();
355 void triggerPreprocess();
356
357private:
358 void markDirtyNodesForRange(int start, int end, int charDelta);
359 void updateTotalLines();
360 void invalidateFontCaches();
361
362protected:
363 QQuickTextEdit(QQuickTextEditPrivate &dd, QQuickItem *parent = nullptr);
364#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
365 void setOldSelectionDefault();
366#endif
367
368 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
369
370 bool event(QEvent *) override;
371 void keyPressEvent(QKeyEvent *) override;
372 void keyReleaseEvent(QKeyEvent *) override;
373 void focusInEvent(QFocusEvent *event) override;
374 void focusOutEvent(QFocusEvent *event) override;
375
376 void hoverEnterEvent(QHoverEvent *event) override;
377 void hoverMoveEvent(QHoverEvent *event) override;
378 void hoverLeaveEvent(QHoverEvent *event) override;
379
380 // mouse filter?
381 void mousePressEvent(QMouseEvent *event) override;
382 void mouseReleaseEvent(QMouseEvent *event) override;
383 void mouseDoubleClickEvent(QMouseEvent *event) override;
384 void mouseMoveEvent(QMouseEvent *event) override;
385#if QT_CONFIG(im)
386 void inputMethodEvent(QInputMethodEvent *e) override;
387#endif
388 QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override;
389 void updatePolish() override;
390
391 friend class QQuickTextUtil;
392 friend class QQuickTextDocument;
393
394private:
395 Q_DISABLE_COPY(QQuickTextEdit)
396 Q_DECLARE_PRIVATE(QQuickTextEdit)
397};
398
399#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
400class QQuickPre64TextEdit : public QQuickTextEdit {
401 Q_OBJECT
402 QML_NAMED_ELEMENT(TextEdit)
403 QML_ADDED_IN_VERSION(2, 0)
404 QML_REMOVED_IN_VERSION(6, 4)
405public:
406 QQuickPre64TextEdit(QQuickItem *parent = nullptr);
407};
408#endif
409
410Q_DECLARE_MIXED_ENUM_OPERATORS_SYMMETRIC(int, QQuickTextEdit::HAlignment, QQuickTextEdit::VAlignment)
411
412QT_END_NAMESPACE
413
414QML_DECLARE_TYPE(QQuickTextEdit)
415
416#endif // QQUICKTEXTEDIT_P_H
417

source code of qtdeclarative/src/quick/items/qquicktextedit_p.h