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 
51 QT_BEGIN_NAMESPACE
52 
53 
54 class QTextFormatCollection;
55 class QTextListFormat;
56 class QRect;
57 class QPainter;
58 class QPagedPaintDevice;
59 class QAbstractTextDocumentLayout;
60 class QPoint;
61 class QTextObject;
62 class QTextFormat;
63 class QTextFrame;
64 class QTextBlock;
65 class QTextCodec;
66 class QVariant;
67 class QRectF;
68 class QTextOption;
69 class QTextCursor;
70 
71 template<typename T> class QVector;
72 
73 namespace 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 
83 class Q_GUI_EXPORT QAbstractUndoItem
84 {
85 public:
86     virtual ~QAbstractUndoItem() = 0;
87     virtual void undo() = 0;
88     virtual void redo() = 0;
89 };
90 
~QAbstractUndoItem()91 inline QAbstractUndoItem::~QAbstractUndoItem()
92 {
93 }
94 
95 class QTextDocumentPrivate;
96 
97 class 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 
118 public:
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     enum MarkdownFeature {
156         MarkdownNoHTML = 0x0020 | 0x0040,
157         MarkdownDialectCommonMark = 0,
158         MarkdownDialectGitHub = 0x0004 | 0x0008 | 0x0400 | 0x0100 | 0x0200 | 0x0800
159     };
160     Q_DECLARE_FLAGS(MarkdownFeatures, MarkdownFeature)
161     Q_FLAG(MarkdownFeatures)
162 #endif
163 
164 #if QT_CONFIG(textmarkdownwriter)
165     QString toMarkdown(MarkdownFeatures features = MarkdownDialectGitHub) const;
166 #endif
167 
168 #if QT_CONFIG(textmarkdownreader)
169     void setMarkdown(const QString &markdown, MarkdownFeatures features = MarkdownDialectGitHub);
170 #endif
171 
172     QString toRawText() const;
173     QString toPlainText() const;
174     void setPlainText(const QString &text);
175 
176     QChar characterAt(int pos) const;
177 
178     enum FindFlag
179     {
180         FindBackward        = 0x00001,
181         FindCaseSensitively = 0x00002,
182         FindWholeWords      = 0x00004
183     };
184     Q_DECLARE_FLAGS(FindFlags, FindFlag)
185 
186     QTextCursor find(const QString &subString, int from = 0, FindFlags options = FindFlags()) const;
187     QTextCursor find(const QString &subString, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
188 
189 #ifndef QT_NO_REGEXP
190     QTextCursor find(const QRegExp &expr, int from = 0, FindFlags options = FindFlags()) const;
191     QTextCursor find(const QRegExp &expr, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
192 #endif
193 
194 #if QT_CONFIG(regularexpression)
195     QTextCursor find(const QRegularExpression &expr, int from = 0, FindFlags options = FindFlags()) const;
196     QTextCursor find(const QRegularExpression &expr, const QTextCursor &cursor, FindFlags options = FindFlags()) const;
197 #endif
198 
199     QTextFrame *frameAt(int pos) const;
200     QTextFrame *rootFrame() const;
201 
202     QTextObject *object(int objectIndex) const;
203     QTextObject *objectForFormat(const QTextFormat &) const;
204 
205     QTextBlock findBlock(int pos) const;
206     QTextBlock findBlockByNumber(int blockNumber) const;
207     QTextBlock findBlockByLineNumber(int blockNumber) const;
208     QTextBlock begin() const;
209     QTextBlock end() const;
210 
211     QTextBlock firstBlock() const;
212     QTextBlock lastBlock() const;
213 
214     void setPageSize(const QSizeF &size);
215     QSizeF pageSize() const;
216 
217     void setDefaultFont(const QFont &font);
218     QFont defaultFont() 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     QVector<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 
296 Q_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 
308 public Q_SLOTS:
309     void undo();
310     void redo();
311     void appendUndoItem(QAbstractUndoItem *);
312     void setModified(bool m = true);
313 
314 protected:
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);
319 public:
320     QTextDocumentPrivate *docHandle() const;
321 private:
322     Q_DISABLE_COPY(QTextDocument)
323     Q_DECLARE_PRIVATE(QTextDocument)
324     friend class QTextObjectPrivate;
325 };
326 
327 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextDocument::FindFlags)
328 
329 QT_END_NAMESPACE
330 
331 #endif // QTEXTDOCUMENT_H
332