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

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