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