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 QTEXTOBJECT_H
5#define QTEXTOBJECT_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtCore/qobject.h>
9#include <QtGui/qtextformat.h>
10#include <QtGui/qtextlayout.h>
11#include <QtGui/qglyphrun.h>
12
13QT_BEGIN_NAMESPACE
14
15
16class QTextObjectPrivate;
17class QTextDocument;
18class QTextDocumentPrivate;
19class QTextCursor;
20class QTextBlock;
21class QTextFragment;
22class QTextList;
23
24class Q_GUI_EXPORT QTextObject : public QObject
25{
26 Q_OBJECT
27
28protected:
29 explicit QTextObject(QTextDocument *doc);
30 ~QTextObject();
31
32 void setFormat(const QTextFormat &format);
33
34public:
35 QTextFormat format() const;
36 int formatIndex() const;
37
38 QTextDocument *document() const;
39
40 int objectIndex() const;
41
42protected:
43 QTextObject(QTextObjectPrivate &p, QTextDocument *doc);
44
45private:
46 Q_DECLARE_PRIVATE(QTextObject)
47 Q_DISABLE_COPY(QTextObject)
48 friend class QTextDocumentPrivate;
49};
50
51class QTextBlockGroupPrivate;
52class Q_GUI_EXPORT QTextBlockGroup : public QTextObject
53{
54 Q_OBJECT
55
56protected:
57 explicit QTextBlockGroup(QTextDocument *doc);
58 ~QTextBlockGroup();
59
60 virtual void blockInserted(const QTextBlock &block);
61 virtual void blockRemoved(const QTextBlock &block);
62 virtual void blockFormatChanged(const QTextBlock &block);
63
64 QList<QTextBlock> blockList() const;
65
66protected:
67 QTextBlockGroup(QTextBlockGroupPrivate &p, QTextDocument *doc);
68private:
69 Q_DECLARE_PRIVATE(QTextBlockGroup)
70 Q_DISABLE_COPY(QTextBlockGroup)
71 friend class QTextDocumentPrivate;
72};
73
74class Q_GUI_EXPORT QTextFrameLayoutData {
75public:
76 virtual ~QTextFrameLayoutData();
77};
78
79class QTextFramePrivate;
80class Q_GUI_EXPORT QTextFrame : public QTextObject
81{
82 Q_OBJECT
83
84public:
85 explicit QTextFrame(QTextDocument *doc);
86 ~QTextFrame();
87
88 inline void setFrameFormat(const QTextFrameFormat &format);
89 QTextFrameFormat frameFormat() const { return QTextObject::format().toFrameFormat(); }
90
91 QTextCursor firstCursorPosition() const;
92 QTextCursor lastCursorPosition() const;
93 int firstPosition() const;
94 int lastPosition() const;
95
96 QTextFrameLayoutData *layoutData() const;
97 void setLayoutData(QTextFrameLayoutData *data);
98
99 QList<QTextFrame *> childFrames() const;
100 QTextFrame *parentFrame() const;
101
102 class iterator {
103 QTextFrame *f = nullptr;
104 int b = 0;
105 int e = 0;
106 QTextFrame *cf = nullptr;
107 int cb = 0;
108
109 friend class QTextFrame;
110 friend class QTextTableCell;
111 friend class QTextDocumentLayoutPrivate;
112 inline iterator(QTextFrame *frame, int block, int begin, int end)
113 : f(frame), b(begin), e(end), cb(block)
114 {}
115 public:
116 constexpr iterator() noexcept = default;
117 QTextFrame *parentFrame() const { return f; }
118
119 QTextFrame *currentFrame() const { return cf; }
120 Q_GUI_EXPORT QTextBlock currentBlock() const;
121
122 bool atEnd() const { return !cf && cb == e; }
123
124 inline bool operator==(const iterator &o) const { return f == o.f && cf == o.cf && cb == o.cb; }
125 inline bool operator!=(const iterator &o) const { return f != o.f || cf != o.cf || cb != o.cb; }
126 Q_GUI_EXPORT iterator &operator++();
127 inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
128 Q_GUI_EXPORT iterator &operator--();
129 inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
130 };
131
132 friend class iterator;
133 // more Qt
134 typedef iterator Iterator;
135
136 iterator begin() const;
137 iterator end() const;
138
139protected:
140 QTextFrame(QTextFramePrivate &p, QTextDocument *doc);
141private:
142 friend class QTextDocumentPrivate;
143 Q_DECLARE_PRIVATE(QTextFrame)
144 Q_DISABLE_COPY(QTextFrame)
145};
146Q_DECLARE_TYPEINFO(QTextFrame::iterator, Q_RELOCATABLE_TYPE);
147
148inline void QTextFrame::setFrameFormat(const QTextFrameFormat &aformat)
149{ QTextObject::setFormat(aformat); }
150
151class Q_GUI_EXPORT QTextBlockUserData {
152public:
153 virtual ~QTextBlockUserData();
154};
155
156class Q_GUI_EXPORT QTextBlock
157{
158 friend class QSyntaxHighlighter;
159public:
160 inline QTextBlock(QTextDocumentPrivate *priv, int b) : p(priv), n(b) {}
161 inline QTextBlock() : p(nullptr), n(0) {}
162 inline QTextBlock(const QTextBlock &o) : p(o.p), n(o.n) {}
163 inline QTextBlock &operator=(const QTextBlock &o) { p = o.p; n = o.n; return *this; }
164
165 bool isValid() const;
166
167 inline bool operator==(const QTextBlock &o) const { return p == o.p && n == o.n; }
168 inline bool operator!=(const QTextBlock &o) const { return p != o.p || n != o.n; }
169 inline bool operator<(const QTextBlock &o) const { return position() < o.position(); }
170
171 int position() const;
172 int length() const;
173 bool contains(int position) const;
174
175 QTextLayout *layout() const;
176 void clearLayout();
177 QTextBlockFormat blockFormat() const;
178 int blockFormatIndex() const;
179 QTextCharFormat charFormat() const;
180 int charFormatIndex() const;
181
182 Qt::LayoutDirection textDirection() const;
183
184 QString text() const;
185
186 QList<QTextLayout::FormatRange> textFormats() const;
187
188 const QTextDocument *document() const;
189
190 QTextList *textList() const;
191
192 QTextBlockUserData *userData() const;
193 void setUserData(QTextBlockUserData *data);
194
195 int userState() const;
196 void setUserState(int state);
197
198 int revision() const;
199 void setRevision(int rev);
200
201 bool isVisible() const;
202 void setVisible(bool visible);
203
204 int blockNumber() const;
205 int firstLineNumber() const;
206
207 void setLineCount(int count);
208 int lineCount() const;
209
210 class iterator {
211 const QTextDocumentPrivate *p = nullptr;
212 int b = 0;
213 int e = 0;
214 int n = 0;
215 friend class QTextBlock;
216 iterator(const QTextDocumentPrivate *priv, int begin, int end, int f)
217 : p(priv), b(begin), e(end), n(f) {}
218 public:
219 constexpr iterator() = default;
220
221 Q_GUI_EXPORT QTextFragment fragment() const;
222
223 bool atEnd() const { return n == e; }
224
225 inline bool operator==(const iterator &o) const { return p == o.p && n == o.n; }
226 inline bool operator!=(const iterator &o) const { return p != o.p || n != o.n; }
227 Q_GUI_EXPORT iterator &operator++();
228 inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
229 Q_GUI_EXPORT iterator &operator--();
230 inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
231 };
232
233 // more Qt
234 typedef iterator Iterator;
235
236 iterator begin() const;
237 iterator end() const;
238
239 QTextBlock next() const;
240 QTextBlock previous() const;
241
242 inline int fragmentIndex() const { return n; }
243
244private:
245 QTextDocumentPrivate *p;
246 int n;
247 friend class QTextDocumentPrivate;
248 friend class QTextLayout;
249};
250
251Q_DECLARE_TYPEINFO(QTextBlock, Q_RELOCATABLE_TYPE);
252Q_DECLARE_TYPEINFO(QTextBlock::iterator, Q_RELOCATABLE_TYPE);
253
254
255class Q_GUI_EXPORT QTextFragment
256{
257public:
258 inline QTextFragment(const QTextDocumentPrivate *priv, int f, int fe) : p(priv), n(f), ne(fe) {}
259 inline QTextFragment() : p(nullptr), n(0), ne(0) {}
260 inline QTextFragment(const QTextFragment &o) : p(o.p), n(o.n), ne(o.ne) {}
261 inline QTextFragment &operator=(const QTextFragment &o) { p = o.p; n = o.n; ne = o.ne; return *this; }
262
263 inline bool isValid() const { return p && n; }
264
265 inline bool operator==(const QTextFragment &o) const { return p == o.p && n == o.n; }
266 inline bool operator!=(const QTextFragment &o) const { return p != o.p || n != o.n; }
267 inline bool operator<(const QTextFragment &o) const { return position() < o.position(); }
268
269 int position() const;
270 int length() const;
271 bool contains(int position) const;
272
273 QTextCharFormat charFormat() const;
274 int charFormatIndex() const;
275 QString text() const;
276
277#if !defined(QT_NO_RAWFONT)
278 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
279#endif
280
281private:
282 const QTextDocumentPrivate *p;
283 int n;
284 int ne;
285};
286
287Q_DECLARE_TYPEINFO(QTextFragment, Q_RELOCATABLE_TYPE);
288
289QT_END_NAMESPACE
290
291#endif // QTEXTOBJECT_H
292

source code of qtbase/src/gui/text/qtextobject.h