source: trunk/src/gui/text/qabstracttextdocumentlayout.cpp@ 858

Last change on this file since 858 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 20.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qabstracttextdocumentlayout.h>
43#include <qtextformat.h>
44#include "qtextdocument_p.h"
45#include "qtextengine_p.h"
46
47#include "qabstracttextdocumentlayout_p.h"
48
49QT_BEGIN_NAMESPACE
50
51/*!
52 \class QAbstractTextDocumentLayout
53 \reentrant
54
55 \brief The QAbstractTextDocumentLayout class is an abstract base
56 class used to implement custom layouts for QTextDocuments.
57
58 \ingroup richtext-processing
59
60 The standard layout provided by Qt can handle simple word processing
61 including inline images, lists and tables.
62
63 Some applications, e.g., a word processor or a DTP application might need
64 more features than the ones provided by Qt's layout engine, in which case
65 you can subclass QAbstractTextDocumentLayout to provide custom layout
66 behavior for your text documents.
67
68 An instance of the QAbstractTextDocumentLayout subclass can be installed
69 on a QTextDocument object with the
70 \l{QTextDocument::}{setDocumentLayout()} function.
71
72 You can insert custom objects into a QTextDocument; see the
73 QTextObjectInterface class description for details.
74
75 \sa QTextObjectInterface
76*/
77
78/*!
79 \class QTextObjectInterface
80 \brief The QTextObjectInterface class allows drawing of
81 custom text objects in \l{QTextDocument}s.
82 \since 4.5
83
84 A text object describes the structure of one or more elements in a
85 text document; for instance, images imported from HTML are
86 implemented using text objects. A text object knows how to lay out
87 and draw its elements when a document is being rendered.
88
89 Qt allows custom text objects to be inserted into a document by
90 registering a custom \l{QTextCharFormat::objectType()}{object
91 type} with QTextCharFormat. A QTextObjectInterface must also be
92 implemented for this type and be
93 \l{QAbstractTextDocumentLayout::registerHandler()}{registered}
94 with the QAbstractTextDocumentLayout of the document. When the
95 object type is encountered while rendering a QTextDocument, the
96 intrinsicSize() and drawObject() functions of the interface are
97 called.
98
99 The following list explains the required steps of inserting a
100 custom text object into a document:
101
102 \list
103 \o Choose an \a objectType. The \a objectType is an integer with a
104 value greater or equal to QTextFormat::UserObject.
105 \o Create a QTextCharFormat object and set the object type to the
106 chosen type using the setObjectType() function.
107 \o Implement the QTextObjectInterface class.
108 \o Call QAbstractTextDocumentLayout::registerHandler() with an instance of your
109 QTextObjectInterface subclass to register your object type.
110 \o Insert QChar::ObjectReplacementCharacter with the aforementioned
111 QTextCharFormat of the chosen object type into the document.
112 As mentioned, the functions of QTextObjectInterface
113 \l{QTextObjectInterface::}{intrinsicSize()} and
114 \l{QTextObjectInterface::}{drawObject()} will then be called with the
115 QTextFormat as parameter whenever the replacement character is
116 encountered.
117 \endlist
118
119 A class implementing a text object needs to inherit both QObject
120 and QTextObjectInterface. QObject must be the first class
121 inherited. For instance:
122
123 \snippet examples/richtext/textobject/svgtextobject.h 1
124
125 The data of a text object is usually stored in the QTextCharFormat
126 using QTextCharFormat::setProperty(), and then retrieved with
127 QTextCharFormat::property().
128
129 \warning Copy and Paste operations ignore custom text objects.
130
131 \sa {Text Object Example}, QTextCharFormat, QTextLayout
132*/
133
134/*!
135 \fn QTextObjectInterface::~QTextObjectInterface()
136
137 Destroys this QTextObjectInterface.
138*/
139
140/*!
141 \fn virtual QSizeF QTextObjectInterface::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0
142
143 The intrinsicSize() function returns the size of the text object
144 represented by \a format in the given document (\a doc) at the
145 given position (\a posInDocument).
146
147 The size calculated will be used for subsequent calls to
148 drawObject() for this \a format.
149
150 \sa drawObject()
151*/
152
153/*!
154 \fn virtual void QTextObjectInterface::drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0
155
156 Draws this text object using the specified \a painter.
157
158 The size of the rectangle, \a rect, to draw in is the size
159 previously calculated by intrinsicSize(). The rectangles position
160 is relative to the \a painter.
161
162 You also get the document (\a doc) and the position (\a
163 posInDocument) of the \a format in that document.
164
165 \sa intrinsicSize()
166*/
167
168/*!
169 \fn void QAbstractTextDocumentLayout::update(const QRectF &rect)
170
171 This signal is emitted when the rectangle \a rect has been updated.
172
173 Subclasses of QAbstractTextDocumentLayout should emit this signal when
174 the layout of the contents change in order to repaint.
175*/
176
177/*!
178 \fn void QAbstractTextDocumentLayout::updateBlock(const QTextBlock &block)
179 \since 4.4
180
181 This signal is emitted when the specified \a block has been updated.
182
183 Subclasses of QAbstractTextDocumentLayout should emit this signal when
184 the layout of \a block has changed in order to repaint.
185*/
186
187/*!
188 \fn void QAbstractTextDocumentLayout::documentSizeChanged(const QSizeF &newSize)
189
190 This signal is emitted when the size of the document layout changes to
191 \a newSize.
192
193 Subclasses of QAbstractTextDocumentLayout should emit this signal when the
194 document's entire layout size changes. This signal is useful for widgets
195 that display text documents since it enables them to update their scroll
196 bars correctly.
197
198 \sa documentSize()
199*/
200
201/*!
202 \fn void QAbstractTextDocumentLayout::pageCountChanged(int newPages)
203
204 This signal is emitted when the number of pages in the layout changes;
205 \a newPages is the updated page count.
206
207 Subclasses of QAbstractTextDocumentLayout should emit this signal when
208 the number of pages in the layout has changed. Changes to the page count
209 are caused by changes to the layout or the document content itself.
210
211 \sa pageCount()
212*/
213
214/*!
215 \fn int QAbstractTextDocumentLayout::pageCount() const
216
217 Returns the number of pages contained in the layout.
218
219 \sa pageCountChanged()
220*/
221
222/*!
223 \fn QSizeF QAbstractTextDocumentLayout::documentSize() const
224
225 Returns the total size of the document's layout.
226
227 This information can be used by display widgets to update their scroll bars
228 correctly.
229
230 \sa documentSizeChanged(), QTextDocument::pageSize
231*/
232
233/*!
234 \fn void QAbstractTextDocumentLayout::draw(QPainter *painter, const PaintContext &context)
235
236 Draws the layout with the given \a painter using the given \a context.
237*/
238
239/*!
240 \fn int QAbstractTextDocumentLayout::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
241
242 Returns the cursor postion for the given \a point with the specified
243 \a accuracy. Returns -1 if no valid cursor position was found.
244*/
245
246/*!