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 
49 QT_BEGIN_NAMESPACE
50 
51 
52 class QTextObjectPrivate;
53 class QTextDocument;
54 class QTextDocumentPrivate;
55 class QTextCursor;
56 class QTextBlock;
57 class QTextFragment;
58 class QTextList;
59 
60 class Q_GUI_EXPORT QTextObject : public QObject
61 {
62     Q_OBJECT
63 
64 protected:
65     explicit QTextObject(QTextDocument *doc);
66     ~QTextObject();
67 
68     void setFormat(const QTextFormat &format);
69 
70 public:
71     QTextFormat format() const;
72     int formatIndex() const;
73 
74     QTextDocument *document() const;
75 
76     int objectIndex() const;
77 
78     QTextDocumentPrivate *docHandle() const;
79 
80 protected:
81     QTextObject(QTextObjectPrivate &p, QTextDocument *doc);
82 
83 private:
84     Q_DECLARE_PRIVATE(QTextObject)
85     Q_DISABLE_COPY(QTextObject)
86     friend class QTextDocumentPrivate;
87 };
88 
89 class QTextBlockGroupPrivate;
90 class Q_GUI_EXPORT QTextBlockGroup : public QTextObject
91 {
92     Q_OBJECT
93 
94 protected:
95     explicit QTextBlockGroup(QTextDocument *doc);
96     ~QTextBlockGroup();
97 
98     virtual void blockInserted(const QTextBlock &block);
99     virtual void blockRemoved(const QTextBlock &block);
100     virtual void blockFormatChanged(const QTextBlock &block);
101 
102     QList<QTextBlock> blockList() const;
103 
104 protected:
105     QTextBlockGroup(QTextBlockGroupPrivate &p, QTextDocument *doc);
106 private:
107     Q_DECLARE_PRIVATE(QTextBlockGroup)
108     Q_DISABLE_COPY(QTextBlockGroup)
109     friend class QTextDocumentPrivate;
110 };
111 
112 class Q_GUI_EXPORT QTextFrameLayoutData {
113 public:
114     virtual ~QTextFrameLayoutData();
115 };
116 
117 class QTextFramePrivate;
118 class Q_GUI_EXPORT QTextFrame : public QTextObject
119 {
120     Q_OBJECT
121 
122 public:
123     explicit QTextFrame(QTextDocument *doc);
124     ~QTextFrame();
125 
126     inline void setFrameFormat(const QTextFrameFormat &format);
frameFormat()127     QTextFrameFormat frameFormat() const { return QTextObject::format().toFrameFormat(); }
128 
129     QTextCursor firstCursorPosition() const;
130     QTextCursor lastCursorPosition() const;
131     int firstPosition() const;
132     int lastPosition() const;
133 
134     QTextFrameLayoutData *layoutData() const;
135     void setLayoutData(QTextFrameLayoutData *data);
136 
137     QList<QTextFrame *> childFrames() const;
138     QTextFrame *parentFrame() const;
139 
140     class Q_GUI_EXPORT iterator {
141         QTextFrame *f;
142         int b;
143         int e;
144         QTextFrame *cf;
145         int cb;
146 
147         friend class QTextFrame;
148         friend class QTextTableCell;
149         friend class QTextDocumentLayoutPrivate;
150         iterator(QTextFrame *frame, int block, int begin, int end);
151     public:
152         iterator(); // ### Qt 6: inline
153 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
154         iterator(const iterator &o) noexcept; // = default
155         iterator &operator=(const iterator &o) noexcept; // = default
iterator(iterator && other)156         iterator(iterator &&other) noexcept // = default
157         { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(iterator)); }
158         iterator &operator=(iterator &&other) noexcept // = default
159         { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(iterator)); return *this; }
160 #endif
161 
parentFrame()162         QTextFrame *parentFrame() const { return f; }
163 
164         QTextFrame *currentFrame() const;
165         QTextBlock currentBlock() const;
166 
atEnd()167         bool atEnd() const { return !cf && cb == e; }
168 
169         inline bool operator==(const iterator &o) const { return f == o.f && cf == o.cf && cb == o.cb; }
170         inline bool operator!=(const iterator &o) const { return f != o.f || cf != o.cf || cb != o.cb; }
171         iterator &operator++();
172         inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
173         iterator &operator--();
174         inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
175     };
176 
177     friend class iterator;
178     // more Qt
179     typedef iterator Iterator;
180 
181     iterator begin() const;
182     iterator end() const;
183 
184 protected:
185     QTextFrame(QTextFramePrivate &p, QTextDocument *doc);
186 private:
187     friend class QTextDocumentPrivate;
188     Q_DECLARE_PRIVATE(QTextFrame)
189     Q_DISABLE_COPY(QTextFrame)
190 };
191 Q_DECLARE_TYPEINFO(QTextFrame::iterator, Q_MOVABLE_TYPE);
192 
setFrameFormat(const QTextFrameFormat & aformat)193 inline void QTextFrame::setFrameFormat(const QTextFrameFormat &aformat)
194 { QTextObject::setFormat(aformat); }
195 
196 class Q_GUI_EXPORT QTextBlockUserData {
197 public:
198     virtual ~QTextBlockUserData();
199 };
200 
201 class Q_GUI_EXPORT QTextBlock
202 {
203     friend class QSyntaxHighlighter;
204 public:
QTextBlock(QTextDocumentPrivate * priv,int b)205     inline QTextBlock(QTextDocumentPrivate *priv, int b) : p(priv), n(b) {}
QTextBlock()206     inline QTextBlock() : p(nullptr), n(0) {}
QTextBlock(const QTextBlock & o)207     inline QTextBlock(const QTextBlock &o) : p(o.p), n(o.n) {}
208     inline QTextBlock &operator=(const QTextBlock &o) { p = o.p; n = o.n; return *this; }
209 
210     bool isValid() const;
211 
212     inline bool operator==(const QTextBlock &o) const { return p == o.p && n == o.n; }
213     inline bool operator!=(const QTextBlock &o) const { return p != o.p || n != o.n; }
214     inline bool operator<(const QTextBlock &o) const { return position() < o.position(); }
215 
216     int position() const;
217     int length() const;
218     bool contains(int position) const;
219 
220     QTextLayout *layout() const;
221     void clearLayout();
222     QTextBlockFormat blockFormat() const;
223     int blockFormatIndex() const;
224     QTextCharFormat charFormat() const;
225     int charFormatIndex() const;
226 
227     Qt::LayoutDirection textDirection() const;
228 
229     QString text() const;
230 
231     QVector<QTextLayout::FormatRange> textFormats() const;
232 
233     const QTextDocument *document() const;
234 
235     QTextList *textList() const;
236 
237     QTextBlockUserData *userData() const;
238     void setUserData(QTextBlockUserData *data);
239 
240     int userState() const;
241     void setUserState(int state);
242 
243     int revision() const;
244     void setRevision(int rev);
245 
246     bool isVisible() const;
247     void setVisible(bool visible);
248 
249     int blockNumber() const;
250     int firstLineNumber() const;
251 
252     void setLineCount(int count);
253     int lineCount() const;
254 
255     class Q_GUI_EXPORT iterator {
256         const QTextDocumentPrivate *p;
257         int b;
258         int e;
259         int n;
260         friend class QTextBlock;
iterator(const QTextDocumentPrivate * priv,int begin,int end,int f)261         iterator(const QTextDocumentPrivate *priv, int begin, int end, int f) : p(priv), b(begin), e(end), n(f) {}
262     public:
iterator()263         iterator() : p(nullptr), b(0), e(0), n(0) {}
264 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
iterator(const iterator & o)265         iterator(const iterator &o) : p(o.p), b(o.b), e(o.e), n(o.n) {}
266         iterator &operator=(const iterator &o) = default;
267 #endif
268 
269         QTextFragment fragment() const;
270 
atEnd()271         bool atEnd() const { return n == e; }
272 
273         inline bool operator==(const iterator &o) const { return p == o.p && n == o.n; }
274         inline bool operator!=(const iterator &o) const { return p != o.p || n != o.n; }
275         iterator &operator++();
276         inline iterator operator++(int) { iterator tmp = *this; operator++(); return tmp; }
277         iterator &operator--();
278         inline iterator operator--(int) { iterator tmp = *this; operator--(); return tmp; }
279     };
280 
281     // more Qt
282     typedef iterator Iterator;
283 
284     iterator begin() const;
285     iterator end() const;
286 
287     QTextBlock next() const;
288     QTextBlock previous() const;
289 
docHandle()290     inline QTextDocumentPrivate *docHandle() const { return p; }
fragmentIndex()291     inline int fragmentIndex() const { return n; }
292 
293 private:
294     QTextDocumentPrivate *p;
295     int n;
296     friend class QTextDocumentPrivate;
297     friend class QTextLayout;
298 };
299 
300 Q_DECLARE_TYPEINFO(QTextBlock, Q_MOVABLE_TYPE);
301 Q_DECLARE_TYPEINFO(QTextBlock::iterator, Q_MOVABLE_TYPE);
302 
303 
304 class Q_GUI_EXPORT QTextFragment
305 {
306 public:
QTextFragment(const QTextDocumentPrivate * priv,int f,int fe)307     inline QTextFragment(const QTextDocumentPrivate *priv, int f, int fe) : p(priv), n(f), ne(fe) {}
QTextFragment()308     inline QTextFragment() : p(nullptr), n(0), ne(0) {}
QTextFragment(const QTextFragment & o)309     inline QTextFragment(const QTextFragment &o) : p(o.p), n(o.n), ne(o.ne) {}
310     inline QTextFragment &operator=(const QTextFragment &o) { p = o.p; n = o.n; ne = o.ne; return *this; }
311 
isValid()312     inline bool isValid() const { return p && n; }
313 
314     inline bool operator==(const QTextFragment &o) const { return p == o.p && n == o.n; }
315     inline bool operator!=(const QTextFragment &o) const { return p != o.p || n != o.n; }
316     inline bool operator<(const QTextFragment &o) const { return position() < o.position(); }
317 
318     int position() const;
319     int length() const;
320     bool contains(int position) const;
321 
322     QTextCharFormat charFormat() const;
323     int charFormatIndex() const;
324     QString text() const;
325 
326 #if !defined(QT_NO_RAWFONT)
327     QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const;
328 #endif
329 
330 private:
331     const QTextDocumentPrivate *p;
332     int n;
333     int ne;
334 };
335 
336 Q_DECLARE_TYPEINFO(QTextFragment, Q_MOVABLE_TYPE);
337 
338 QT_END_NAMESPACE
339 
340 #endif // QTEXTOBJECT_H
341