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