1/****************************************************************************
2**
3** Copyright (C) 2019 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 QTEXTDOCUMENT_H
41#define QTEXTDOCUMENT_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qobject.h>
45#include <QtCore/qsize.h>
46#include <QtCore/qrect.h>
47#include <QtCore/qvariant.h>
48#include <QtGui/qfont.h>
49#include <QtCore/qurl.h>
50
51QT_BEGIN_NAMESPACE
52
53
54class QTextFormatCollection;
55class QTextListFormat;
56class QRect;
57class QPainter;
58class QPagedPaintDevice;
59class QAbstractTextDocumentLayout;
60class QPoint;
61class QTextObject;
62class QTextFormat;
63class QTextFrame;
64class QTextBlock;
65class QTextCodec;
66class QVariant;
67class QRectF;
68class QTextOption;
69class QTextCursor;
70
71template<typename T> class QVector;
72
73namespace Qt
74{
75 Q_GUI_EXPORT bool mightBeRichText(const QString&);
76 Q_GUI_EXPORT QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode = WhiteSpacePre);
77
78#if QT_CONFIG(textcodec) || defined(Q_CLANG_QDOC)
79 Q_GUI_EXPORT QTextCodec *codecForHtml(const QByteArray &ba);
80#endif
81}
82
83class Q_GUI_EXPORT QAbstractUndoItem
84{
85public:
86 virtual ~QAbstractUndoItem() = 0;
87 virtual void undo() = 0;
88 virtual void redo() = 0;
89};
90
91inline QAbstractUndoItem::~QAbstractUndoItem()
92{
93}
94
95class QTextDocumentPrivate;
96
97class Q_GUI_EXPORT QTextDocument : public QObject
98{
99 Q_OBJECT
100
101 Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
102 Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
103 Q_PROPERTY(QSizeF pageSize READ pageSize WRITE setPageSize)
104 Q_PROPERTY(QFont defaultFont READ defaultFont WRITE setDefaultFont)
105 Q_PROPERTY(bool useDesignMetrics READ useDesignMetrics WRITE setUseDesignMetrics)
106 Q_PROPERTY(QSizeF size READ size)
107 Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth)
108 Q_PROPERTY(int blockCount READ blockCount)
109 Q_PROPERTY(qreal indentWidth READ indentWidth WRITE setIndentWidth)
110#ifndef QT_NO_CSSPARSER
111 Q_PROPERTY(QString defaultStyleSheet READ defaultStyleSheet WRITE setDefaultStyleSheet)
112#endif
113 Q_PROPERTY(int maximumBlockCount READ maximumBlockCount WRITE setMaximumBlockCount)
114 Q_PROPERTY(qreal documentMargin READ documentMargin WRITE setDocumentMargin)
115 QDOC_PROPERTY(QTextOption defaultTextOption READ defaultTextOption WRITE setDefaultTextOption)
116 Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged)
117
118public:
119 explicit QTextDocument(QObject *parent = nullptr);
120 explicit QTextDocument(const QString &text, QObject *parent = nullptr);
121 ~QTextDocument();
122
123 QTextDocument *clone(QObject *parent = nullptr) const;
124
125 bool isEmpty() const;
126 virtual void clear();
127
128 void setUndoRedoEnabled(bool enable);
129 bool isUndoRedoEnabled() const;
130
131 bool isUndoAvailable() const;
132 bool isRedoAvailable() const;
133
134 int availableUndoSteps() const;
135 int availableRedoSteps() const;
136
137 int revision() const;
138
139 void setDocumentLayout(QAbstractTextDocumentLayout *layout);
140 QAbstractTextDocumentLayout *documentLayout() const;
141
142 enum MetaInformation {
143 DocumentTitle,
144 DocumentUrl
145 };
146 void setMetaInformation(MetaInformation info, const QString &);
147 QString metaInformation(MetaInformation info) const;
148
149#ifndef QT_NO_TEXTHTMLPARSER
150 QString toHtml(const QByteArray &encoding = QByteArray()) const;
151 void setHtml(const QString &html);
152#endif
153
154#if QT_CONFIG(textmarkdownwriter) || QT_CONFIG(textmarkdownreader)
155 // Must be in sync with QTextMarkdownImporter::Features, should be in sync with #define MD_FLAG_* in md4c
156 enum MarkdownFeature {
157 MarkdownNoHTML = 0x0020 | 0x0040,
158 MarkdownDialectCommonMark = 0,
159 MarkdownDialectGitHub = 0x0004 | 0x0008 | 0x0400 | 0x0100 | 0x0200 | 0x0800
160 };
161 Q_DECLARE_FLAGS(MarkdownFeatures, MarkdownFeature)
162 Q_FLAG(MarkdownFeatures)
163#endif
164
165#if QT_CONFIG(textmarkdownwriter)
166 QString toMarkdown(MarkdownFeatures features = MarkdownDialectGitHub) const;
167#endif
168
169#if QT_CONFIG(textmarkdownreader)
170 void setMarkdown(const QString &markdown, MarkdownFeatures features = MarkdownDialectGitHub);
171#endif
172
173 QString toRawText() const;
174 QString toPlainText() const;
175 void setPlainText(const QString &text);
176
177 QChar characterAt(int pos) const;
178
179 enum FindFlag
180 {
181 FindBackward = 0x00001,
182 FindCaseSensitively = 0x00002,
183 FindWholeWords = 0x00004
184 };
185 Q_DECLARE_FLAGS(FindFlags, FindFlag)
186
187 QTextCursor find(const QString &subString, int from = 0, FindFlags options = FindFlags()) const;
188 QTextCursor find(const QString &subString, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
189
190#ifndef QT_NO_REGEXP
191 QTextCursor find(const QRegExp &expr, int from = 0, FindFlags options = FindFlags()) const;
192 QTextCursor find(const QRegExp &expr, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
193#endif
194
195#if QT_CONFIG(regularexpression)
196 QTextCursor find(const QRegularExpression &expr, int from = 0, FindFlags options = FindFlags()) const;
197 QTextCursor find(const QRegularExpression &expr, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
198#endif
199
200 QTextFrame *frameAt(int pos) const;
201 QTextFrame *rootFrame() const;
202
203 QTextObject *object(int objectIndex) const;
204 QTextObject *objectForFormat(const QTextFormat &) const;
205
206 QTextBlock findBlock(int pos) const;
207 QTextBlock findBlockByNumber(int blockNumber) const;
208 QTextBlock findBlockByLineNumber(int blockNumber) const;
209 QTextBlock begin() const;
210 QTextBlock end() const;
211
212 QTextBlock firstBlock() const;
213 QTextBlock lastBlock() const;
214
215 void setPageSize(const QSizeF &size);
216 QSizeF pageSize() const;
217
218 void setDefaultFont(const QFont &font);
219 QFont defaultFont() const;
220
221 int pageCount() const;
222
223 bool isModified() const;
224
225 void print(QPagedPaintDevice *printer) const;
226
227 enum ResourceType {
228 UnknownResource = 0,
229 HtmlResource = 1,
230 ImageResource = 2,
231 StyleSheetResource = 3,
232 MarkdownResource = 4,
233
234 UserResource = 100
235 };
236 Q_ENUM(ResourceType)
237
238 QVariant resource(int type, const QUrl &name) const;
239 void addResource(int type, const QUrl &name, const QVariant &resource);
240
241 QVector<QTextFormat> allFormats() const;
242
243 void markContentsDirty(int from, int length);
244
245 void setUseDesignMetrics(bool b);
246 bool useDesignMetrics() const;
247
248 void drawContents(QPainter *painter, const QRectF &rect = QRectF());
249
250 void setTextWidth(qreal width);
251 qreal textWidth() const;
252
253 qreal idealWidth() const;
254
255 qreal indentWidth() const;
256 void setIndentWidth(qreal width);
257
258 qreal documentMargin() const;
259 void setDocumentMargin(qreal margin);
260
261 void adjustSize();
262 QSizeF size() const;
263
264 int blockCount() const;
265 int lineCount() const;
266 int characterCount() const;
267
268#ifndef QT_NO_CSSPARSER
269 void setDefaultStyleSheet(const QString &sheet);
270 QString defaultStyleSheet() const;
271#endif
272
273 void undo(QTextCursor *cursor);
274 void redo(QTextCursor *cursor);
275
276 enum Stacks {
277 UndoStack = 0x01,
278 RedoStack = 0x02,
279 UndoAndRedoStacks = UndoStack | RedoStack
280 };
281 void clearUndoRedoStacks(Stacks historyToClear = UndoAndRedoStacks);
282
283 int maximumBlockCount() const;
284 void setMaximumBlockCount(int maximum);
285
286 QTextOption defaultTextOption() const;
287 void setDefaultTextOption(const QTextOption &option);
288
289 QUrl baseUrl() const;
290 void setBaseUrl(const QUrl &url);
291
292 Qt::CursorMoveStyle defaultCursorMoveStyle() const;
293 void setDefaultCursorMoveStyle(Qt::CursorMoveStyle style);
294
295Q_SIGNALS:
296 void contentsChange(int from, int charsRemoved, int charsAdded);
297 void contentsChanged();
298 void undoAvailable(bool);
299 void redoAvailable(bool);
300 void undoCommandAdded();
301 void modificationChanged(bool m);
302 void cursorPositionChanged(const QTextCursor &cursor);
303 void blockCountChanged(int newBlockCount);
304 void baseUrlChanged(const QUrl &url);
305 void documentLayoutChanged();
306
307public Q_SLOTS:
308 void undo();
309 void redo();
310 void appendUndoItem(QAbstractUndoItem *);
311 void setModified(bool m = true);
312
313protected:
314 virtual QTextObject *createObject(const QTextFormat &f);
315 Q_INVOKABLE virtual QVariant loadResource(int type, const QUrl &name);
316
317 QTextDocument(QTextDocumentPrivate &dd, QObject *parent);
318public:
319 QTextDocumentPrivate *docHandle() const;
320private:
321 Q_DISABLE_COPY(QTextDocument)
322 Q_DECLARE_PRIVATE(QTextDocument)
323 friend class QTextObjectPrivate;
324};
325
326Q_DECLARE_OPERATORS_FOR_FLAGS(QTextDocument::FindFlags)
327
328QT_END_NAMESPACE
329
330#endif // QTEXTDOCUMENT_H
331