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

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