1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTEXTOBJECT_H
41#define QTEXTOBJECT_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qobject.h>
45#include <QtGui/qtextformat.h>
46#include <QtGui/qtextlayout.h>
47#include <QtGui/qglyphrun.h>
48
49QT_BEGIN_NAMESPACE
50
51
52class QTextObjectPrivate;
53class QTextDocument;
54class QTextDocumentPrivate;
55class QTextCursor;
56class QTextBlock;
57class QTextFragment;
58class QTextList;
59
60class Q_GUI_EXPORT QTextObject : public QObject
61{
62 Q_OBJECT
63
64protected:
65 explicit QTextObject(QTextDocument *doc);
66 ~QTextObject();
67
68 void setFormat(const QTextFormat &format);
69
70public:
71 QTextFormat format() const;
72 int formatIndex() const;
73
74 QTextDocument *document() const;
75
76 int objectIndex() const;
77
78protected:
79 QTextObject(QTextObjectPrivate &p, QTextDocument *doc);
80
81private:
82 Q_DECLARE_PRIVATE(QTextObject)
83 Q_DISABLE_COPY(QTextObject)
84 friend class QTextDocumentPrivate;
85};
86
87class QTextBlockGroupPrivate;
88class Q_GUI_EXPORT QTextBlockGroup : public QTextObject
89{
90 Q_OBJECT
91
92protected:
93 explicit QTextBlockGroup(QTextDocument *doc);
94 ~QTextBlockGroup();
95
96 virtual void blockInserted(const QTextBlock &block);
97 virtual void blockRemoved(const QTextBlock &block);
98 virtual void blockFormatChanged(const QTextBlock &block);
99
100 QList<QTextBlock> blockList() const;
101
102protected:
103 QTextBlockGroup(QTextBlockGroupPrivate &p, QTextDocument *doc);
104private:
105 Q_DECLARE_PRIVATE(QTextBlockGroup)
106 Q_DISABLE_COPY(QTextBlockGroup)
107 friend class QTextDocumentPrivate;
108};
109
110class Q_GUI_EXPORT QTextFrameLayoutData {
111public:
112 virtual ~QTextFrameLayoutData();
113};
114
115class QTextFramePrivate;
116class Q_GUI_EXPORT QTextFrame : public QTextObject
117{
118 Q_OBJECT
119
120public:
121 explicit QTextFrame(QTextDocument *doc);
122 ~QTextFrame();
123
124 inline void setFrameFormat(const QTextFrameFormat &format);
125 QTextFrameFormat frameFormat() const { return QTextObject::format().toFrameFormat(); }
126
127 QTextCursor firstCursorPosition() const;
128 QTextCursor lastCursorPosition() const;
129 int firstPosition() const;
130 int lastPosition() const;
131
132 QTextFrameLayoutData *layoutData() const;
133 void setLayoutData(QTextFrameLayoutData *data);
134
135 QList<QTextFrame *> childFrames() const;
136 QTextFrame *parentFrame() const;
137
138 class Q_GUI_EXPORT iterator {
139 QTextFrame *f = nullptr;
140 int b = 0;
141 int e = 0;
142 QTextFrame *cf = nullptr;
143 int cb = 0;
144
145 friend class QTextFrame;
146 friend class QTextTableCell;
147 friend class QTextDocumentLayoutPrivate;
148 iterator(QTextFrame *frame, int block, int begin, int end);
149 public:
150 constexpr iterator() noexcept = default;
151 QTextFrame *parentFrame() const { return f; }
152
153 QTextFrame *currentFrame() const;
154 QTextBlock currentBlock() const;
155
156 bool atEnd() const { return !cf && cb == e; }
157
158 inline bool operator==(const iterator &o) const { return f == o.f && cf == o.cf && cb == o.cb; }
159 inline bool operator!=(const iterator &o) const { return f != o.f || cf != o.cf || cb != o.cb; }
160 iterator &operator++();
161 inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
162 iterator &operator--();
163 inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
164 };
165
166 friend class iterator;
167 // more Qt
168 typedef iterator Iterator;
169
170 iterator begin() const;
171 iterator end() const;
172
173protected:
174 QTextFrame(QTextFramePrivate &p, QTextDocument *doc);
175private:
176 friend class QTextDocumentPrivate;
177 Q_DECLARE_PRIVATE(QTextFrame)
178 Q_DISABLE_COPY(QTextFrame)
179};
180Q_DECLARE_TYPEINFO(QTextFrame::iterator, Q_MOVABLE_TYPE);
181
182inline void QTextFrame::setFrameFormat(const QTextFrameFormat &aformat)
183{ QTextObject::setFormat(aformat); }
184
185class Q_GUI_EXPORT QTextBlockUserData {
186public:
187 virtual ~QTextBlockUserData();
188};
189
190class Q_GUI_EXPORT QTextBlock
191{
192 friend class QSyntaxHighlighter;
193public:
194 inline QTextBlock(QTextDocumentPrivate *priv, int b) : p(priv), n(b) {}
195 inline QTextBlock() : p(nullptr), n(0) {}
196 inline QTextBlock(const QTextBlock &o) : p(o.p), n(o.n) {}
197 inline QTextBlock &operator=(const QTextBlock &o) { p = o.p; n = o.n; return *this; }
198
199 bool isValid() const;
200
201 inline bool operator==(const QTextBlock &o) const { return p == o.p && n == o.n; }
202 inline bool operator!=(const QTextBlock &o) const { return p != o.p || n != o.n; }
203 inline bool operator<(const QTextBlock &o) const { return position() < o.position(); }
204
205 int position() const;
206 int length() const;
207 bool contains(int position) const;
208
209 QTextLayout *layout() const;
210 void clearLayout();
211 QTextBlockFormat blockFormat() const;
212 int blockFormatIndex() const;
213 QTextCharFormat charFormat() const;
214 int charFormatIndex() const;
215
216 Qt::LayoutDirection textDirection() const;
217
218 QString text() const;
219
220 QList<QTextLayout::FormatRange> textFormats() const;
221
222 const QTextDocument *document() const;
223
224 QTextList *textList() const;
225
226 QTextBlockUserData *userData() const;
227 void setUserData(QTextBlockUserData *data);
228
229 int userState() const;
230 void setUserState(int state);
231
232 int revision() const;
233 void setRevision(int rev);
234
235 bool isVisible() const;
236 void setVisible(bool visible);
237
238 int blockNumber() const;
239 int firstLineNumber() const;
240
241 void setLineCount(int count);
242 int lineCount() const;
243
244 class Q_GUI_EXPORT iterator {
245 const QTextDocumentPrivate *p;
246 int b;
247 int e;
248 int n;
249 friend class QTextBlock;
250 iterator(const QTextDocumentPrivate *priv, int begin, int end, int f) : p(priv), b(begin), e(end), n(f) {}
251 public:
252 iterator() : p(nullptr), b(0), e(0), n(0) {}
253
254 QTextFragment fragment() const;
255
256 bool atEnd() const { return n == e; }
257
258 inline bool operator==(const iterator &o) const { return p == o.p && n == o.n; }
259 inline bool operator!=(const iterator &o) const { return p != o.p || n != o.n; }
260 iterator &operator++();
261 inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
262 iterator &operator--();
263 inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
264 };
265
266 // more Qt
267 typedef iterator Iterator;
268
269 iterator begin() const;
270 iterator end() const;
271
272 QTextBlock next() const;
273 QTextBlock previous() const;
274
275 inline int fragmentIndex() const { return n; }
276
277private:
278 QTextDocumentPrivate *p;
279 int n;
280 friend class QTextDocumentPrivate;
281 friend class QTextLayout;
282};
283
284Q_DECLARE_TYPEINFO(QTextBlock, Q_MOVABLE_TYPE);
285Q_DECLARE_TYPEINFO(QTextBlock::iterator, Q_MOVABLE_TYPE);
286
287
288class Q_GUI_EXPORT QTextFragment
289{
290public:
291 inline QTextFragment(const QTextDocumentPrivate *priv, int f, int fe) : p(priv), n(f), ne(fe) {}
292 inline QTextFragment() : p(nullptr), n(0), ne(0) {}
293 inline QTextFragment(const QTextFragment &o) : p(o.p), n(o.n), ne(o.ne) {}
294 inline QTextFragment &operator=(const QTextFragment &o) { p = o.p; n = o.n; ne = o.ne; return *this; }
295
296 inline bool isValid() const { return p && n; }
297
298 inline bool operator==(const QTextFragment &o) const { return p == o.p && n == o.n; }
299 inline bool operator!=(const QTextFragment &o) const { return p != o.p || n != o.n; }
300 inline bool operator<(const QTextFragment &o) const { return position() < o.position(); }
301
302 int position() const;
303 int length() const;
304 bool contains(int position) const;
305
306 QTextCharFormat charFormat() const;
307 int charFormatIndex() const;
308 QString text() const;
309
310#if !defined(QT_NO_RAWFONT)
311 QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
312#endif
313
314private:
315 const QTextDocumentPrivate *p;
316 int n;
317 int ne;
318};
319
320Q_DECLARE_TYPEINFO(QTextFragment, Q_MOVABLE_TYPE);
321
322QT_END_NAMESPACE
323
324#endif // QTEXTOBJECT_H
325