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_P_H
5#define QQUICKTEXTEDIT_P_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 "qquicktextedit_p.h"
19#include "qquickimplicitsizeitem_p_p.h"
20#include "qquicktextutil_p.h"
21
22#include <QtQml/qqml.h>
23#include <QtCore/qlist.h>
24#include <private/qlazilyallocated_p.h>
25
26#include <limits>
27
28QT_BEGIN_NAMESPACE
29class QTextLayout;
30class QQuickTextDocumentWithImageResources;
31class QQuickTextControl;
32class QQuickTextNode;
33class QQuickTextNodeEngine;
34
35class Q_QUICK_PRIVATE_EXPORT QQuickTextEditPrivate : public QQuickImplicitSizeItemPrivate
36{
37public:
38 Q_DECLARE_PUBLIC(QQuickTextEdit)
39
40 typedef QQuickTextEdit Public;
41
42 struct Node {
43 explicit Node(int startPos = std::numeric_limits<int>::max(),
44 QQuickTextNode *node = nullptr)
45 : m_startPos(startPos), m_node(node), m_dirty(false) { }
46 QQuickTextNode* textNode() const { return m_node; }
47 void moveStartPos(int delta) { Q_ASSERT(m_startPos + delta > 0); m_startPos += delta; }
48 int startPos() const { return m_startPos; }
49 void setDirty() { m_dirty = true; }
50 bool dirty() const { return m_dirty; }
51
52 private:
53 int m_startPos;
54 QQuickTextNode* m_node;
55 bool m_dirty;
56
57#ifndef QT_NO_DEBUG_STREAM
58 friend QDebug Q_QUICK_PRIVATE_EXPORT operator<<(QDebug, const Node &);
59#endif
60 };
61 typedef QList<Node>::iterator TextNodeIterator;
62
63 struct ExtraData {
64 ExtraData();
65
66 qreal padding;
67 qreal topPadding;
68 qreal leftPadding;
69 qreal rightPadding;
70 qreal bottomPadding;
71 bool explicitTopPadding : 1;
72 bool explicitLeftPadding : 1;
73 bool explicitRightPadding : 1;
74 bool explicitBottomPadding : 1;
75 bool implicitResize : 1;
76 };
77 QLazilyAllocated<ExtraData> extra;
78
79
80 QQuickTextEditPrivate()
81 : color(QRgb(0xFF000000)), selectionColor(QRgb(0xFF000080)), selectedTextColor(QRgb(0xFFFFFFFF))
82 , textMargin(0.0), xoff(0), yoff(0)
83 , font(sourceFont), cursorComponent(nullptr), cursorItem(nullptr), document(nullptr), control(nullptr)
84 , quickDocument(nullptr), lastSelectionStart(0), lastSelectionEnd(0), lineCount(0)
85 , hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop)
86 , format(QQuickTextEdit::PlainText), wrapMode(QQuickTextEdit::NoWrap)
87 , renderType(QQuickTextUtil::textRenderType<QQuickTextEdit>())
88 , contentDirection(Qt::LayoutDirectionAuto)
89 , mouseSelectionMode(QQuickTextEdit::SelectCharacters)
90#if QT_CONFIG(im)
91 , inputMethodHints(Qt::ImhNone)
92#endif
93 , updateType(UpdatePaintNode)
94 , dirty(false), richText(false), cursorVisible(false), cursorPending(false)
95 , focusOnPress(true), persistentSelection(false), requireImplicitWidth(false)
96 , selectByMouse(true), canPaste(false), canPasteValid(false), hAlignImplicit(true)
97 , textCached(true), inLayout(false), selectByKeyboard(false), selectByKeyboardSet(false)
98 , hadSelection(false), markdownText(false), inResize(false)
99 {
100 }
101
102 static QQuickTextEditPrivate *get(QQuickTextEdit *item) {
103 return static_cast<QQuickTextEditPrivate *>(QObjectPrivate::get(o: item)); }
104
105 void init();
106
107 void resetInputMethod();
108 void updateDefaultTextOption();
109 void relayoutDocument();
110 bool determineHorizontalAlignment();
111 bool setHAlign(QQuickTextEdit::HAlignment, bool forceAlign = false);
112 void mirrorChange() override;
113 bool transformChanged(QQuickItem *transformedItem) override;
114 qreal getImplicitWidth() const override;
115 Qt::LayoutDirection textDirection(const QString &text) const;
116 bool isLinkHoveredConnected();
117
118#if QT_CONFIG(cursor)
119 void updateMouseCursorShape();
120#endif
121
122 void setNativeCursorEnabled(bool) {}
123 void handleFocusEvent(QFocusEvent *event);
124 void addCurrentTextNodeToRoot(QQuickTextNodeEngine *, QSGTransformNode *, QQuickTextNode*, TextNodeIterator&, int startPos);
125 QQuickTextNode* createTextNode();
126
127#if QT_CONFIG(im)
128 Qt::InputMethodHints effectiveInputMethodHints() const;
129#endif
130
131 inline qreal padding() const { return extra.isAllocated() ? extra->padding : 0.0; }
132 void setTopPadding(qreal value, bool reset = false);
133 void setLeftPadding(qreal value, bool reset = false);
134 void setRightPadding(qreal value, bool reset = false);
135 void setBottomPadding(qreal value, bool reset = false);
136
137 bool isImplicitResizeEnabled() const;
138 void setImplicitResizeEnabled(bool enabled);
139
140 QColor color;
141 QColor selectionColor;
142 QColor selectedTextColor;
143
144 QSizeF contentSize;
145
146 qreal textMargin;
147 qreal xoff;
148 qreal yoff;
149
150 QString text;
151 QUrl baseUrl;
152 QFont sourceFont;
153 QFont font;
154
155 QQmlComponent* cursorComponent;
156 QQuickItem* cursorItem;
157 QQuickTextDocumentWithImageResources *document;
158 QQuickTextControl *control;
159 QQuickTextDocument *quickDocument;
160 QList<Node> textNodeMap;
161
162 int lastSelectionStart;
163 int lastSelectionEnd;
164 int lineCount;
165 int firstBlockInViewport = -1; // only for the autotest; can be wrong after scrolling sometimes
166 int firstBlockPastViewport = -1; // only for the autotest
167 QRectF renderedRegion;
168
169 enum UpdateType {
170 UpdateNone,
171 UpdateOnlyPreprocess,
172 UpdatePaintNode,
173 UpdateAll
174 };
175
176 QQuickTextEdit::HAlignment hAlign;
177 QQuickTextEdit::VAlignment vAlign;
178 QQuickTextEdit::TextFormat format;
179 QQuickTextEdit::WrapMode wrapMode;
180 QQuickTextEdit::RenderType renderType;
181 Qt::LayoutDirection contentDirection;
182 QQuickTextEdit::SelectionMode mouseSelectionMode;
183#if QT_CONFIG(im)
184 Qt::InputMethodHints inputMethodHints;
185#endif
186 UpdateType updateType;
187
188 bool dirty : 1;
189 bool richText : 1;
190 bool cursorVisible : 1;
191 bool cursorPending : 1;
192 bool focusOnPress : 1;
193 bool persistentSelection : 1;
194 bool requireImplicitWidth:1;
195 bool selectByMouse:1;
196 bool canPaste:1;
197 bool canPasteValid:1;
198 bool hAlignImplicit:1;
199 bool textCached:1;
200 bool inLayout:1;
201 bool selectByKeyboard:1;
202 bool selectByKeyboardSet:1;
203 bool hadSelection : 1;
204 bool markdownText : 1;
205 bool inResize : 1;
206
207 static const int largeTextSizeThreshold;
208};
209
210#ifndef QT_NO_DEBUG_STREAM
211QDebug Q_QUICK_PRIVATE_EXPORT operator<<(QDebug debug, const QQuickTextEditPrivate::Node &);
212#endif
213
214QT_END_NAMESPACE
215
216#endif // QQUICKTEXTEDIT_P_P_H
217

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