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 QABSTRACTTEXTDOCUMENTLAYOUT_H
41 #define QABSTRACTTEXTDOCUMENTLAYOUT_H
42 
43 #include <QtGui/qtguiglobal.h>
44 #include <QtCore/qobject.h>
45 #include <QtGui/qtextlayout.h>
46 #include <QtGui/qtextdocument.h>
47 #include <QtGui/qtextcursor.h>
48 #include <QtGui/qpalette.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 
53 class QAbstractTextDocumentLayoutPrivate;
54 class QTextBlock;
55 class QTextObjectInterface;
56 class QTextFrame;
57 
58 class Q_GUI_EXPORT QAbstractTextDocumentLayout : public QObject
59 {
60     Q_OBJECT
61     Q_DECLARE_PRIVATE(QAbstractTextDocumentLayout)
62 
63 public:
64     explicit QAbstractTextDocumentLayout(QTextDocument *doc);
65     ~QAbstractTextDocumentLayout();
66 
67     struct Selection
68     {
69         QTextCursor cursor;
70         QTextCharFormat format;
71     };
72 
73     struct PaintContext
74     {
PaintContextPaintContext75         PaintContext()
76             : cursorPosition(-1)
77             {}
78         int cursorPosition;
79         QPalette palette;
80         QRectF clip;
81         QVector<Selection> selections;
82     };
83 
84     virtual void draw(QPainter *painter, const PaintContext &context) = 0;
85     virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const = 0;
86 
87     QString anchorAt(const QPointF& pos) const;
88     QString imageAt(const QPointF &pos) const;
89     QTextFormat formatAt(const QPointF &pos) const;
90     QTextBlock blockWithMarkerAt(const QPointF &pos) const;
91 
92     virtual int pageCount() const = 0;
93     virtual QSizeF documentSize() const = 0;
94 
95     virtual QRectF frameBoundingRect(QTextFrame *frame) const = 0;
96     virtual QRectF blockBoundingRect(const QTextBlock &block) const = 0;
97 
98     void setPaintDevice(QPaintDevice *device);
99     QPaintDevice *paintDevice() const;
100 
101     QTextDocument *document() const;
102 
103     void registerHandler(int objectType, QObject *component);
104     void unregisterHandler(int objectType, QObject *component = nullptr);
105     QTextObjectInterface *handlerForObject(int objectType) const;
106 
107 Q_SIGNALS:
108     void update(const QRectF & = QRectF(0., 0., 1000000000., 1000000000.));
109     void updateBlock(const QTextBlock &block);
110     void documentSizeChanged(const QSizeF &newSize);
111     void pageCountChanged(int newPages);
112 
113 protected:
114     QAbstractTextDocumentLayout(QAbstractTextDocumentLayoutPrivate &, QTextDocument *);
115 
116     virtual void documentChanged(int from, int charsRemoved, int charsAdded) = 0;
117 
118     virtual void resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format);
119     virtual void positionInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format);
120     virtual void drawInlineObject(QPainter *painter, const QRectF &rect, QTextInlineObject object, int posInDocument, const QTextFormat &format);
121 
122     int formatIndex(int pos);
123     QTextCharFormat format(int pos);
124 
125 private:
126     friend class QWidgetTextControl;
127     friend class QTextDocument;
128     friend class QTextDocumentPrivate;
129     friend class QTextEngine;
130     friend class QTextLayout;
131     friend class QTextLine;
132     Q_PRIVATE_SLOT(d_func(), void _q_handlerDestroyed(QObject *obj))
133     Q_PRIVATE_SLOT(d_func(), int _q_dynamicPageCountSlot())
134     Q_PRIVATE_SLOT(d_func(), QSizeF _q_dynamicDocumentSizeSlot())
135 };
136 Q_DECLARE_TYPEINFO(QAbstractTextDocumentLayout::Selection,    Q_RELOCATABLE_TYPE);
137 Q_DECLARE_TYPEINFO(QAbstractTextDocumentLayout::PaintContext, Q_RELOCATABLE_TYPE);
138 
139 class Q_GUI_EXPORT QTextObjectInterface
140 {
141 public:
142     virtual ~QTextObjectInterface();
143     virtual QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0;
144     virtual void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0;
145 };
146 
147 #ifndef Q_CLANG_QDOC
148 Q_DECLARE_INTERFACE(QTextObjectInterface, "org.qt-project.Qt.QTextObjectInterface")
149 #endif
150 
151 QT_END_NAMESPACE
152 
153 #endif // QABSTRACTTEXTDOCUMENTLAYOUT_H
154