Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qabstracttextdocumentlayout.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include <qabstracttextdocumentlayout.h> | - | ||||||||||||||||||
35 | #include <qtextformat.h> | - | ||||||||||||||||||
36 | #include "qtextdocument_p.h" | - | ||||||||||||||||||
37 | #include "qtextengine_p.h" | - | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | #include "qabstracttextdocumentlayout_p.h" | - | ||||||||||||||||||
40 | - | |||||||||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
42 | - | |||||||||||||||||||
43 | QAbstractTextDocumentLayoutPrivate::~QAbstractTextDocumentLayoutPrivate() | - | ||||||||||||||||||
44 | { | - | ||||||||||||||||||
45 | } | - | ||||||||||||||||||
46 | - | |||||||||||||||||||
47 | QTextObjectInterface::~QTextObjectInterface() | - | ||||||||||||||||||
48 | { | - | ||||||||||||||||||
49 | // must be empty until ### Qt 6 | - | ||||||||||||||||||
50 | } | - | ||||||||||||||||||
51 | - | |||||||||||||||||||
52 | /*! | - | ||||||||||||||||||
53 | \class QAbstractTextDocumentLayout | - | ||||||||||||||||||
54 | \reentrant | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | \brief The QAbstractTextDocumentLayout class is an abstract base | - | ||||||||||||||||||
57 | class used to implement custom layouts for QTextDocuments. | - | ||||||||||||||||||
58 | \inmodule QtGui | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | \ingroup richtext-processing | - | ||||||||||||||||||
61 | - | |||||||||||||||||||
62 | The standard layout provided by Qt can handle simple word processing | - | ||||||||||||||||||
63 | including inline images, lists and tables. | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | Some applications, e.g., a word processor or a DTP application might need | - | ||||||||||||||||||
66 | more features than the ones provided by Qt's layout engine, in which case | - | ||||||||||||||||||
67 | you can subclass QAbstractTextDocumentLayout to provide custom layout | - | ||||||||||||||||||
68 | behavior for your text documents. | - | ||||||||||||||||||
69 | - | |||||||||||||||||||
70 | An instance of the QAbstractTextDocumentLayout subclass can be installed | - | ||||||||||||||||||
71 | on a QTextDocument object with the | - | ||||||||||||||||||
72 | \l{QTextDocument::}{setDocumentLayout()} function. | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | You can insert custom objects into a QTextDocument; see the | - | ||||||||||||||||||
75 | QTextObjectInterface class description for details. | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | \sa QTextObjectInterface | - | ||||||||||||||||||
78 | */ | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | /*! | - | ||||||||||||||||||
81 | \class QTextObjectInterface | - | ||||||||||||||||||
82 | \brief The QTextObjectInterface class allows drawing of | - | ||||||||||||||||||
83 | custom text objects in \l{QTextDocument}s. | - | ||||||||||||||||||
84 | \since 4.5 | - | ||||||||||||||||||
85 | \inmodule QtGui | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | A text object describes the structure of one or more elements in a | - | ||||||||||||||||||
88 | text document; for instance, images imported from HTML are | - | ||||||||||||||||||
89 | implemented using text objects. A text object knows how to lay out | - | ||||||||||||||||||
90 | and draw its elements when a document is being rendered. | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | Qt allows custom text objects to be inserted into a document by | - | ||||||||||||||||||
93 | registering a custom \l{QTextCharFormat::objectType()}{object | - | ||||||||||||||||||
94 | type} with QTextCharFormat. A QTextObjectInterface must also be | - | ||||||||||||||||||
95 | implemented for this type and be | - | ||||||||||||||||||
96 | \l{QAbstractTextDocumentLayout::registerHandler()}{registered} | - | ||||||||||||||||||
97 | with the QAbstractTextDocumentLayout of the document. When the | - | ||||||||||||||||||
98 | object type is encountered while rendering a QTextDocument, the | - | ||||||||||||||||||
99 | intrinsicSize() and drawObject() functions of the interface are | - | ||||||||||||||||||
100 | called. | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | The following list explains the required steps of inserting a | - | ||||||||||||||||||
103 | custom text object into a document: | - | ||||||||||||||||||
104 | - | |||||||||||||||||||
105 | \list | - | ||||||||||||||||||
106 | \li Choose an \a objectType. The \a objectType is an integer with a | - | ||||||||||||||||||
107 | value greater or equal to QTextFormat::UserObject. | - | ||||||||||||||||||
108 | \li Create a QTextCharFormat object and set the object type to the | - | ||||||||||||||||||
109 | chosen type using the setObjectType() function. | - | ||||||||||||||||||
110 | \li Implement the QTextObjectInterface class. | - | ||||||||||||||||||
111 | \li Call QAbstractTextDocumentLayout::registerHandler() with an instance of your | - | ||||||||||||||||||
112 | QTextObjectInterface subclass to register your object type. | - | ||||||||||||||||||
113 | \li Insert QChar::ObjectReplacementCharacter with the aforementioned | - | ||||||||||||||||||
114 | QTextCharFormat of the chosen object type into the document. | - | ||||||||||||||||||
115 | As mentioned, the functions of QTextObjectInterface | - | ||||||||||||||||||
116 | \l{QTextObjectInterface::}{intrinsicSize()} and | - | ||||||||||||||||||
117 | \l{QTextObjectInterface::}{drawObject()} will then be called with the | - | ||||||||||||||||||
118 | QTextFormat as parameter whenever the replacement character is | - | ||||||||||||||||||
119 | encountered. | - | ||||||||||||||||||
120 | \endlist | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | A class implementing a text object needs to inherit both QObject | - | ||||||||||||||||||
123 | and QTextObjectInterface. QObject must be the first class | - | ||||||||||||||||||
124 | inherited. For instance: | - | ||||||||||||||||||
125 | - | |||||||||||||||||||
126 | \snippet qtextobject/textobjectinterface.h 0 | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | The data of a text object is usually stored in the QTextCharFormat | - | ||||||||||||||||||
129 | using QTextCharFormat::setProperty(), and then retrieved with | - | ||||||||||||||||||
130 | QTextCharFormat::property(). | - | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | \warning Copy and Paste operations ignore custom text objects. | - | ||||||||||||||||||
133 | - | |||||||||||||||||||
134 | \sa {Text Object Example}, QTextCharFormat, QTextLayout | - | ||||||||||||||||||
135 | */ | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | /*! | - | ||||||||||||||||||
138 | \fn QTextObjectInterface::~QTextObjectInterface() | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | Destroys this QTextObjectInterface. | - | ||||||||||||||||||
141 | */ | - | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | /*! | - | ||||||||||||||||||
144 | \fn virtual QSizeF QTextObjectInterface::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0 | - | ||||||||||||||||||
145 | - | |||||||||||||||||||
146 | The intrinsicSize() function returns the size of the text object | - | ||||||||||||||||||
147 | represented by \a format in the given document (\a doc) at the | - | ||||||||||||||||||
148 | given position (\a posInDocument). | - | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | The size calculated will be used for subsequent calls to | - | ||||||||||||||||||
151 | drawObject() for this \a format. | - | ||||||||||||||||||
152 | - | |||||||||||||||||||
153 | \sa drawObject() | - | ||||||||||||||||||
154 | */ | - | ||||||||||||||||||
155 | - | |||||||||||||||||||
156 | /*! | - | ||||||||||||||||||
157 | \fn virtual void QTextObjectInterface::drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0 | - | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | Draws this text object using the specified \a painter. | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | The size of the rectangle, \a rect, to draw in is the size | - | ||||||||||||||||||
162 | previously calculated by intrinsicSize(). The rectangles position | - | ||||||||||||||||||
163 | is relative to the \a painter. | - | ||||||||||||||||||
164 | - | |||||||||||||||||||
165 | You also get the document (\a doc) and the position (\a | - | ||||||||||||||||||
166 | posInDocument) of the \a format in that document. | - | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | \sa intrinsicSize() | - | ||||||||||||||||||
169 | */ | - | ||||||||||||||||||
170 | - | |||||||||||||||||||
171 | /*! | - | ||||||||||||||||||
172 | \fn void QAbstractTextDocumentLayout::update(const QRectF &rect) | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | This signal is emitted when the rectangle \a rect has been updated. | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | Subclasses of QAbstractTextDocumentLayout should emit this signal when | - | ||||||||||||||||||
177 | the layout of the contents change in order to repaint. | - | ||||||||||||||||||
178 | */ | - | ||||||||||||||||||
179 | - | |||||||||||||||||||
180 | /*! | - | ||||||||||||||||||
181 | \fn void QAbstractTextDocumentLayout::updateBlock(const QTextBlock &block) | - | ||||||||||||||||||
182 | \since 4.4 | - | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | This signal is emitted when the specified \a block has been updated. | - | ||||||||||||||||||
185 | - | |||||||||||||||||||
186 | Subclasses of QAbstractTextDocumentLayout should emit this signal when | - | ||||||||||||||||||
187 | the layout of \a block has changed in order to repaint. | - | ||||||||||||||||||
188 | */ | - | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | /*! | - | ||||||||||||||||||
191 | \fn void QAbstractTextDocumentLayout::documentSizeChanged(const QSizeF &newSize) | - | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | This signal is emitted when the size of the document layout changes to | - | ||||||||||||||||||
194 | \a newSize. | - | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | Subclasses of QAbstractTextDocumentLayout should emit this signal when the | - | ||||||||||||||||||
197 | document's entire layout size changes. This signal is useful for widgets | - | ||||||||||||||||||
198 | that display text documents since it enables them to update their scroll | - | ||||||||||||||||||
199 | bars correctly. | - | ||||||||||||||||||
200 | - | |||||||||||||||||||
201 | \sa documentSize() | - | ||||||||||||||||||
202 | */ | - | ||||||||||||||||||
203 | - | |||||||||||||||||||
204 | /*! | - | ||||||||||||||||||
205 | \fn void QAbstractTextDocumentLayout::pageCountChanged(int newPages) | - | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | This signal is emitted when the number of pages in the layout changes; | - | ||||||||||||||||||
208 | \a newPages is the updated page count. | - | ||||||||||||||||||
209 | - | |||||||||||||||||||
210 | Subclasses of QAbstractTextDocumentLayout should emit this signal when | - | ||||||||||||||||||
211 | the number of pages in the layout has changed. Changes to the page count | - | ||||||||||||||||||
212 | are caused by changes to the layout or the document content itself. | - | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | \sa pageCount() | - | ||||||||||||||||||
215 | */ | - | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | /*! | - | ||||||||||||||||||
218 | \fn int QAbstractTextDocumentLayout::pageCount() const | - | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | Returns the number of pages contained in the layout. | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | \sa pageCountChanged() | - | ||||||||||||||||||
223 | */ | - | ||||||||||||||||||
224 | - | |||||||||||||||||||
225 | /*! | - | ||||||||||||||||||
226 | \fn QSizeF QAbstractTextDocumentLayout::documentSize() const | - | ||||||||||||||||||
227 | - | |||||||||||||||||||
228 | Returns the total size of the document's layout. | - | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | This information can be used by display widgets to update their scroll bars | - | ||||||||||||||||||
231 | correctly. | - | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | \sa documentSizeChanged(), QTextDocument::pageSize | - | ||||||||||||||||||
234 | */ | - | ||||||||||||||||||
235 | - | |||||||||||||||||||
236 | /*! | - | ||||||||||||||||||
237 | \fn void QAbstractTextDocumentLayout::draw(QPainter *painter, const PaintContext &context) | - | ||||||||||||||||||
238 | - | |||||||||||||||||||
239 | Draws the layout with the given \a painter using the given \a context. | - | ||||||||||||||||||
240 | */ | - | ||||||||||||||||||
241 | - | |||||||||||||||||||
242 | /*! | - | ||||||||||||||||||
243 | \fn int QAbstractTextDocumentLayout::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | Returns the cursor position for the given \a point with the specified | - | ||||||||||||||||||
246 | \a accuracy. Returns -1 if no valid cursor position was found. | - | ||||||||||||||||||
247 | */ | - | ||||||||||||||||||
248 | - | |||||||||||||||||||
249 | /*! | - | ||||||||||||||||||
250 | \fn void QAbstractTextDocumentLayout::documentChanged(int position, int charsRemoved, int charsAdded) | - | ||||||||||||||||||
251 | - | |||||||||||||||||||
252 | This function is called whenever the contents of the document change. A | - | ||||||||||||||||||
253 | change occurs when text is inserted, removed, or a combination of these | - | ||||||||||||||||||
254 | two. The change is specified by \a position, \a charsRemoved, and | - | ||||||||||||||||||
255 | \a charsAdded corresponding to the starting character position of the | - | ||||||||||||||||||
256 | change, the number of characters removed from the document, and the | - | ||||||||||||||||||
257 | number of characters added. | - | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | For example, when inserting the text "Hello" into an empty document, | - | ||||||||||||||||||
260 | \a charsRemoved would be 0 and \a charsAdded would be 5 (the length of | - | ||||||||||||||||||
261 | the string). | - | ||||||||||||||||||
262 | - | |||||||||||||||||||
263 | Replacing text is a combination of removing and inserting. For example, if | - | ||||||||||||||||||
264 | the text "Hello" gets replaced by "Hi", \a charsRemoved would be 5 and | - | ||||||||||||||||||
265 | \a charsAdded would be 2. | - | ||||||||||||||||||
266 | - | |||||||||||||||||||
267 | For subclasses of QAbstractTextDocumentLayout, this is the central function | - | ||||||||||||||||||
268 | where a large portion of the work to lay out and position document contents | - | ||||||||||||||||||
269 | is done. | - | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | For example, in a subclass that only arranges blocks of text, an | - | ||||||||||||||||||
272 | implementation of this function would have to do the following: | - | ||||||||||||||||||
273 | - | |||||||||||||||||||
274 | \list | - | ||||||||||||||||||
275 | \li Determine the list of changed \l{QTextBlock}(s) using the parameters | - | ||||||||||||||||||
276 | provided. | - | ||||||||||||||||||
277 | \li Each QTextBlock object's corresponding QTextLayout object needs to | - | ||||||||||||||||||
278 | be processed. You can access the \l{QTextBlock}'s layout using the | - | ||||||||||||||||||
279 | QTextBlock::layout() function. This processing should take the | - | ||||||||||||||||||
280 | document's page size into consideration. | - | ||||||||||||||||||
281 | \li If the total number of pages changed, the pageCountChanged() signal | - | ||||||||||||||||||
282 | should be emitted. | - | ||||||||||||||||||
283 | \li If the total size changed, the documentSizeChanged() signal should | - | ||||||||||||||||||
284 | be emitted. | - | ||||||||||||||||||
285 | \li The update() signal should be emitted to schedule a repaint of areas | - | ||||||||||||||||||
286 | in the layout that require repainting. | - | ||||||||||||||||||
287 | \endlist | - | ||||||||||||||||||
288 | - | |||||||||||||||||||
289 | \sa QTextLayout | - | ||||||||||||||||||
290 | */ | - | ||||||||||||||||||
291 | - | |||||||||||||||||||
292 | /*! | - | ||||||||||||||||||
293 | \class QAbstractTextDocumentLayout::PaintContext | - | ||||||||||||||||||
294 | \reentrant | - | ||||||||||||||||||
295 | \inmodule QtGui | - | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | \brief The QAbstractTextDocumentLayout::PaintContext class is a convenience | - | ||||||||||||||||||
298 | class defining the parameters used when painting a document's layout. | - | ||||||||||||||||||
299 | - | |||||||||||||||||||
300 | A paint context is used when rendering custom layouts for QTextDocuments | - | ||||||||||||||||||
301 | with the QAbstractTextDocumentLayout::draw() function. It is specified by | - | ||||||||||||||||||
302 | a \l {cursorPosition}{cursor position}, \l {palette}{default text color}, | - | ||||||||||||||||||
303 | \l clip rectangle and a collection of \l selections. | - | ||||||||||||||||||
304 | - | |||||||||||||||||||
305 | \sa QAbstractTextDocumentLayout | - | ||||||||||||||||||
306 | */ | - | ||||||||||||||||||
307 | - | |||||||||||||||||||
308 | /*! | - | ||||||||||||||||||
309 | \fn QAbstractTextDocumentLayout::PaintContext::PaintContext() | - | ||||||||||||||||||
310 | \internal | - | ||||||||||||||||||
311 | */ | - | ||||||||||||||||||
312 | - | |||||||||||||||||||
313 | /*! | - | ||||||||||||||||||
314 | \variable QAbstractTextDocumentLayout::PaintContext::cursorPosition | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | \brief the position within the document, where the cursor line should be | - | ||||||||||||||||||
317 | drawn. | - | ||||||||||||||||||
318 | - | |||||||||||||||||||
319 | The default value is -1. | - | ||||||||||||||||||
320 | */ | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | /*! | - | ||||||||||||||||||
323 | \variable QAbstractTextDocumentLayout::PaintContext::palette | - | ||||||||||||||||||
324 | - | |||||||||||||||||||
325 | \brief the default color that is used for the text, when no color is | - | ||||||||||||||||||
326 | specified. | - | ||||||||||||||||||
327 | - | |||||||||||||||||||
328 | The default value is the application's default palette. | - | ||||||||||||||||||
329 | */ | - | ||||||||||||||||||
330 | - | |||||||||||||||||||
331 | /*! | - | ||||||||||||||||||
332 | \variable QAbstractTextDocumentLayout::PaintContext::clip | - | ||||||||||||||||||
333 | - | |||||||||||||||||||
334 | \brief a hint to the layout specifying the area around paragraphs, frames | - | ||||||||||||||||||
335 | or text require painting. | - | ||||||||||||||||||
336 | - | |||||||||||||||||||
337 | Everything outside of this rectangle does not need to be painted. | - | ||||||||||||||||||
338 | - | |||||||||||||||||||
339 | Specifying a clip rectangle can speed up drawing of large documents | - | ||||||||||||||||||
340 | significantly. Note that the clip rectangle is in document coordinates (not | - | ||||||||||||||||||
341 | in viewport coordinates). It is not a substitute for a clip region set on | - | ||||||||||||||||||
342 | the painter but merely a hint. | - | ||||||||||||||||||
343 | - | |||||||||||||||||||
344 | The default value is a null rectangle indicating everything needs to be | - | ||||||||||||||||||
345 | painted. | - | ||||||||||||||||||
346 | */ | - | ||||||||||||||||||
347 | - | |||||||||||||||||||
348 | /*! | - | ||||||||||||||||||
349 | \variable QAbstractTextDocumentLayout::PaintContext::selections | - | ||||||||||||||||||
350 | - | |||||||||||||||||||
351 | \brief the collection of selections that will be rendered when passing this | - | ||||||||||||||||||
352 | paint context to QAbstractTextDocumentLayout's draw() function. | - | ||||||||||||||||||
353 | - | |||||||||||||||||||
354 | The default value is an empty vector indicating no selection. | - | ||||||||||||||||||
355 | */ | - | ||||||||||||||||||
356 | - | |||||||||||||||||||
357 | /*! | - | ||||||||||||||||||
358 | \class QAbstractTextDocumentLayout::Selection | - | ||||||||||||||||||
359 | \reentrant | - | ||||||||||||||||||
360 | \inmodule QtGui | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | \brief The QAbstractTextDocumentLayout::Selection class is a convenience | - | ||||||||||||||||||
363 | class defining the parameters of a selection. | - | ||||||||||||||||||
364 | - | |||||||||||||||||||
365 | A selection can be used to specify a part of a document that should be | - | ||||||||||||||||||
366 | highlighted when drawing custom layouts for QTextDocuments with the | - | ||||||||||||||||||
367 | QAbstractTextDocumentLayout::draw() function. It is specified using | - | ||||||||||||||||||
368 | \l cursor and a \l format. | - | ||||||||||||||||||
369 | - | |||||||||||||||||||
370 | \sa QAbstractTextDocumentLayout, PaintContext | - | ||||||||||||||||||
371 | */ | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | /*! | - | ||||||||||||||||||
374 | \variable QAbstractTextDocumentLayout::Selection::format | - | ||||||||||||||||||
375 | - | |||||||||||||||||||
376 | \brief the format of the selection | - | ||||||||||||||||||
377 | - | |||||||||||||||||||
378 | The default value is QTextFormat::InvalidFormat. | - | ||||||||||||||||||
379 | */ | - | ||||||||||||||||||
380 | - | |||||||||||||||||||
381 | /*! | - | ||||||||||||||||||
382 | \variable QAbstractTextDocumentLayout::Selection::cursor | - | ||||||||||||||||||
383 | \brief the selection's cursor | - | ||||||||||||||||||
384 | - | |||||||||||||||||||
385 | The default value is a null cursor. | - | ||||||||||||||||||
386 | */ | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | /*! | - | ||||||||||||||||||
389 | Creates a new text document layout for the given \a document. | - | ||||||||||||||||||
390 | */ | - | ||||||||||||||||||
391 | QAbstractTextDocumentLayout::QAbstractTextDocumentLayout(QTextDocument *document) | - | ||||||||||||||||||
392 | : QObject(*new QAbstractTextDocumentLayoutPrivate, document) | - | ||||||||||||||||||
393 | { | - | ||||||||||||||||||
394 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
395 | d->setDocument(document); | - | ||||||||||||||||||
396 | } never executed: end of block | 0 | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | /*! | - | ||||||||||||||||||
399 | \internal | - | ||||||||||||||||||
400 | */ | - | ||||||||||||||||||
401 | QAbstractTextDocumentLayout::QAbstractTextDocumentLayout(QAbstractTextDocumentLayoutPrivate &p, QTextDocument *document) | - | ||||||||||||||||||
402 | :QObject(p, document) | - | ||||||||||||||||||
403 | { | - | ||||||||||||||||||
404 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
405 | d->setDocument(document); | - | ||||||||||||||||||
406 | } never executed: end of block | 0 | ||||||||||||||||||
407 | - | |||||||||||||||||||
408 | /*! | - | ||||||||||||||||||
409 | \internal | - | ||||||||||||||||||
410 | */ | - | ||||||||||||||||||
411 | QAbstractTextDocumentLayout::~QAbstractTextDocumentLayout() | - | ||||||||||||||||||
412 | { | - | ||||||||||||||||||
413 | } | - | ||||||||||||||||||
414 | - | |||||||||||||||||||
415 | /*! | - | ||||||||||||||||||
416 | Registers the given \a component as a handler for items of the given \a objectType. | - | ||||||||||||||||||
417 | - | |||||||||||||||||||
418 | \note registerHandler() has to be called once for each object type. This | - | ||||||||||||||||||
419 | means that there is only one handler for multiple replacement characters | - | ||||||||||||||||||
420 | of the same object type. | - | ||||||||||||||||||
421 | - | |||||||||||||||||||
422 | The text document layout does not take ownership of \c component. | - | ||||||||||||||||||
423 | */ | - | ||||||||||||||||||
424 | void QAbstractTextDocumentLayout::registerHandler(int objectType, QObject *component) | - | ||||||||||||||||||
425 | { | - | ||||||||||||||||||
426 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
427 | - | |||||||||||||||||||
428 | QTextObjectInterface *iface = qobject_cast<QTextObjectInterface *>(component); | - | ||||||||||||||||||
429 | if (!iface)
| 0 | ||||||||||||||||||
430 | return; // ### print error message on terminal? never executed: return; | 0 | ||||||||||||||||||
431 | - | |||||||||||||||||||
432 | connect(component, SIGNAL(destroyed(QObject*)), this, SLOT(_q_handlerDestroyed(QObject*))); | - | ||||||||||||||||||
433 | - | |||||||||||||||||||
434 | QTextObjectHandler h; | - | ||||||||||||||||||
435 | h.iface = iface; | - | ||||||||||||||||||
436 | h.component = component; | - | ||||||||||||||||||
437 | d->handlers.insert(objectType, h); | - | ||||||||||||||||||
438 | } never executed: end of block | 0 | ||||||||||||||||||
439 | - | |||||||||||||||||||
440 | /*! | - | ||||||||||||||||||
441 | \since 5.2 | - | ||||||||||||||||||
442 | - | |||||||||||||||||||
443 | Unregisters the given \a component as a handler for items of the given \a objectType, or | - | ||||||||||||||||||
444 | any handler if the \a component is not specified. | - | ||||||||||||||||||
445 | */ | - | ||||||||||||||||||
446 | void QAbstractTextDocumentLayout::unregisterHandler(int objectType, QObject *component) | - | ||||||||||||||||||
447 | { | - | ||||||||||||||||||
448 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
449 | - | |||||||||||||||||||
450 | HandlerHash::iterator it = d->handlers.find(objectType); | - | ||||||||||||||||||
451 | if (it != d->handlers.end() && (!component || component == it->component)) {
| 0 | ||||||||||||||||||
452 | if (component)
| 0 | ||||||||||||||||||
453 | disconnect(component, SIGNAL(destroyed(QObject*)), this, SLOT(_q_handlerDestroyed(QObject*))); never executed: disconnect(component, qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "453"), this, qFlagLocation("1""_q_handlerDestroyed(QObject*)" "\0" __FILE__ ":" "453")); | 0 | ||||||||||||||||||
454 | d->handlers.erase(it); | - | ||||||||||||||||||
455 | } never executed: end of block | 0 | ||||||||||||||||||
456 | } never executed: end of block | 0 | ||||||||||||||||||
457 | - | |||||||||||||||||||
458 | /*! | - | ||||||||||||||||||
459 | Returns a handler for objects of the given \a objectType. | - | ||||||||||||||||||
460 | */ | - | ||||||||||||||||||
461 | QTextObjectInterface *QAbstractTextDocumentLayout::handlerForObject(int objectType) const | - | ||||||||||||||||||
462 | { | - | ||||||||||||||||||
463 | Q_D(const QAbstractTextDocumentLayout); | - | ||||||||||||||||||
464 | - | |||||||||||||||||||
465 | QTextObjectHandler handler = d->handlers.value(objectType); | - | ||||||||||||||||||
466 | if (!handler.component)
| 0 | ||||||||||||||||||
467 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
468 | - | |||||||||||||||||||
469 | return handler.iface; never executed: return handler.iface; | 0 | ||||||||||||||||||
470 | } | - | ||||||||||||||||||
471 | - | |||||||||||||||||||
472 | /*! | - | ||||||||||||||||||
473 | Sets the size of the inline object \a item corresponding to the text | - | ||||||||||||||||||
474 | \a format. | - | ||||||||||||||||||
475 | - | |||||||||||||||||||
476 | \a posInDocument specifies the position of the object within the document. | - | ||||||||||||||||||
477 | - | |||||||||||||||||||
478 | The default implementation resizes the \a item to the size returned by | - | ||||||||||||||||||
479 | the object handler's intrinsicSize() function. This function is called only | - | ||||||||||||||||||
480 | within Qt. Subclasses can reimplement this function to customize the | - | ||||||||||||||||||
481 | resizing of inline objects. | - | ||||||||||||||||||
482 | */ | - | ||||||||||||||||||
483 | void QAbstractTextDocumentLayout::resizeInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format) | - | ||||||||||||||||||
484 | { | - | ||||||||||||||||||
485 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
486 | - | |||||||||||||||||||
487 | QTextCharFormat f = format.toCharFormat(); | - | ||||||||||||||||||
488 | Q_ASSERT(f.isValid()); | - | ||||||||||||||||||
489 | QTextObjectHandler handler = d->handlers.value(f.objectType()); | - | ||||||||||||||||||
490 | if (!handler.component)
| 0 | ||||||||||||||||||
491 | return; never executed: return; | 0 | ||||||||||||||||||
492 | - | |||||||||||||||||||
493 | QSizeF s = handler.iface->intrinsicSize(document(), posInDocument, format); | - | ||||||||||||||||||
494 | item.setWidth(s.width()); | - | ||||||||||||||||||
495 | item.setAscent(s.height()); | - | ||||||||||||||||||
496 | item.setDescent(0); | - | ||||||||||||||||||
497 | } never executed: end of block | 0 | ||||||||||||||||||
498 | - | |||||||||||||||||||
499 | /*! | - | ||||||||||||||||||
500 | Lays out the inline object \a item using the given text \a format. | - | ||||||||||||||||||
501 | - | |||||||||||||||||||
502 | \a posInDocument specifies the position of the object within the document. | - | ||||||||||||||||||
503 | - | |||||||||||||||||||
504 | The default implementation does nothing. This function is called only | - | ||||||||||||||||||
505 | within Qt. Subclasses can reimplement this function to customize the | - | ||||||||||||||||||
506 | position of inline objects. | - | ||||||||||||||||||
507 | - | |||||||||||||||||||
508 | \sa drawInlineObject() | - | ||||||||||||||||||
509 | */ | - | ||||||||||||||||||
510 | void QAbstractTextDocumentLayout::positionInlineObject(QTextInlineObject item, int posInDocument, const QTextFormat &format) | - | ||||||||||||||||||
511 | { | - | ||||||||||||||||||
512 | Q_UNUSED(item); | - | ||||||||||||||||||
513 | Q_UNUSED(posInDocument); | - | ||||||||||||||||||
514 | Q_UNUSED(format); | - | ||||||||||||||||||
515 | } never executed: end of block | 0 | ||||||||||||||||||
516 | - | |||||||||||||||||||
517 | /*! | - | ||||||||||||||||||
518 | \fn void QAbstractTextDocumentLayout::drawInlineObject(QPainter *painter, const QRectF &rect, QTextInlineObject object, int posInDocument, const QTextFormat &format) | - | ||||||||||||||||||
519 | - | |||||||||||||||||||
520 | This function is called to draw the inline object, \a object, with the | - | ||||||||||||||||||
521 | given \a painter within the rectangle specified by \a rect using the | - | ||||||||||||||||||
522 | specified text \a format. | - | ||||||||||||||||||
523 | - | |||||||||||||||||||
524 | \a posInDocument specifies the position of the object within the document. | - | ||||||||||||||||||
525 | - | |||||||||||||||||||
526 | The default implementation calls drawObject() on the object handlers. This | - | ||||||||||||||||||
527 | function is called only within Qt. Subclasses can reimplement this function | - | ||||||||||||||||||
528 | to customize the drawing of inline objects. | - | ||||||||||||||||||
529 | - | |||||||||||||||||||
530 | \sa draw() | - | ||||||||||||||||||
531 | */ | - | ||||||||||||||||||
532 | void QAbstractTextDocumentLayout::drawInlineObject(QPainter *p, const QRectF &rect, QTextInlineObject item, | - | ||||||||||||||||||
533 | int posInDocument, const QTextFormat &format) | - | ||||||||||||||||||
534 | { | - | ||||||||||||||||||
535 | Q_UNUSED(item); | - | ||||||||||||||||||
536 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
537 | - | |||||||||||||||||||
538 | QTextCharFormat f = format.toCharFormat(); | - | ||||||||||||||||||
539 | Q_ASSERT(f.isValid()); | - | ||||||||||||||||||
540 | QTextObjectHandler handler = d->handlers.value(f.objectType()); | - | ||||||||||||||||||
541 | if (!handler.component)
| 0 | ||||||||||||||||||
542 | return; never executed: return; | 0 | ||||||||||||||||||
543 | - | |||||||||||||||||||
544 | handler.iface->drawObject(p, rect, document(), posInDocument, format); | - | ||||||||||||||||||
545 | } never executed: end of block | 0 | ||||||||||||||||||
546 | - | |||||||||||||||||||
547 | void QAbstractTextDocumentLayoutPrivate::_q_handlerDestroyed(QObject *obj) | - | ||||||||||||||||||
548 | { | - | ||||||||||||||||||
549 | HandlerHash::Iterator it = handlers.begin(); | - | ||||||||||||||||||
550 | while (it != handlers.end())
| 0 | ||||||||||||||||||
551 | if ((*it).component == obj)
| 0 | ||||||||||||||||||
552 | it = handlers.erase(it); never executed: it = handlers.erase(it); | 0 | ||||||||||||||||||
553 | else | - | ||||||||||||||||||
554 | ++it; never executed: ++it; | 0 | ||||||||||||||||||
555 | } never executed: end of block | 0 | ||||||||||||||||||
556 | - | |||||||||||||||||||
557 | /*! | - | ||||||||||||||||||
558 | \internal | - | ||||||||||||||||||
559 | - | |||||||||||||||||||
560 | Returns the index of the format at position \a pos. | - | ||||||||||||||||||
561 | */ | - | ||||||||||||||||||
562 | int QAbstractTextDocumentLayout::formatIndex(int pos) | - | ||||||||||||||||||
563 | { | - | ||||||||||||||||||
564 | QTextDocumentPrivate *pieceTable = qobject_cast<QTextDocument *>(parent())->docHandle(); | - | ||||||||||||||||||
565 | return pieceTable->find(pos).value()->format; never executed: return pieceTable->find(pos).value()->format; | 0 | ||||||||||||||||||
566 | } | - | ||||||||||||||||||
567 | - | |||||||||||||||||||
568 | /*! | - | ||||||||||||||||||
569 | \fn QTextCharFormat QAbstractTextDocumentLayout::format(int position) | - | ||||||||||||||||||
570 | - | |||||||||||||||||||
571 | Returns the character format that is applicable at the given \a position. | - | ||||||||||||||||||
572 | */ | - | ||||||||||||||||||
573 | QTextCharFormat QAbstractTextDocumentLayout::format(int pos) | - | ||||||||||||||||||
574 | { | - | ||||||||||||||||||
575 | QTextDocumentPrivate *pieceTable = qobject_cast<QTextDocument *>(parent())->docHandle(); | - | ||||||||||||||||||
576 | int idx = pieceTable->find(pos).value()->format; | - | ||||||||||||||||||
577 | return pieceTable->formatCollection()->charFormat(idx); never executed: return pieceTable->formatCollection()->charFormat(idx); | 0 | ||||||||||||||||||
578 | } | - | ||||||||||||||||||
579 | - | |||||||||||||||||||
580 | - | |||||||||||||||||||
581 | - | |||||||||||||||||||
582 | /*! | - | ||||||||||||||||||
583 | Returns the text document that this layout is operating on. | - | ||||||||||||||||||
584 | */ | - | ||||||||||||||||||
585 | QTextDocument *QAbstractTextDocumentLayout::document() const | - | ||||||||||||||||||
586 | { | - | ||||||||||||||||||
587 | Q_D(const QAbstractTextDocumentLayout); | - | ||||||||||||||||||
588 | return d->document; never executed: return d->document; | 0 | ||||||||||||||||||
589 | } | - | ||||||||||||||||||
590 | - | |||||||||||||||||||
591 | /*! | - | ||||||||||||||||||
592 | \fn QString QAbstractTextDocumentLayout::anchorAt(const QPointF &position) const | - | ||||||||||||||||||
593 | - | |||||||||||||||||||
594 | Returns the reference of the anchor the given \a position, or an empty | - | ||||||||||||||||||
595 | string if no anchor exists at that point. | - | ||||||||||||||||||
596 | */ | - | ||||||||||||||||||
597 | QString QAbstractTextDocumentLayout::anchorAt(const QPointF& pos) const | - | ||||||||||||||||||
598 | { | - | ||||||||||||||||||
599 | int cursorPos = hitTest(pos, Qt::ExactHit); | - | ||||||||||||||||||
600 | if (cursorPos == -1)
| 0 | ||||||||||||||||||
601 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
602 | - | |||||||||||||||||||
603 | // compensate for preedit in the hit text block | - | ||||||||||||||||||
604 | QTextBlock block = document()->firstBlock(); | - | ||||||||||||||||||
605 | while (block.isValid()) {
| 0 | ||||||||||||||||||
606 | QRectF blockBr = blockBoundingRect(block); | - | ||||||||||||||||||
607 | if (blockBr.contains(pos)) {
| 0 | ||||||||||||||||||
608 | QTextLayout *layout = block.layout(); | - | ||||||||||||||||||
609 | int relativeCursorPos = cursorPos - block.position(); | - | ||||||||||||||||||
610 | const int preeditLength = layout ? layout->preeditAreaText().length() : 0;
| 0 | ||||||||||||||||||
611 | if (preeditLength > 0 && relativeCursorPos > layout->preeditAreaPosition())
| 0 | ||||||||||||||||||
612 | cursorPos -= qMin(cursorPos - layout->preeditAreaPosition(), preeditLength); never executed: cursorPos -= qMin(cursorPos - layout->preeditAreaPosition(), preeditLength); | 0 | ||||||||||||||||||
613 | break; never executed: break; | 0 | ||||||||||||||||||
614 | } | - | ||||||||||||||||||
615 | block = block.next(); | - | ||||||||||||||||||
616 | } never executed: end of block | 0 | ||||||||||||||||||
617 | - | |||||||||||||||||||
618 | QTextDocumentPrivate *pieceTable = qobject_cast<const QTextDocument *>(parent())->docHandle(); | - | ||||||||||||||||||
619 | QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos); | - | ||||||||||||||||||
620 | QTextCharFormat fmt = pieceTable->formatCollection()->charFormat(it->format); | - | ||||||||||||||||||
621 | return fmt.anchorHref(); never executed: return fmt.anchorHref(); | 0 | ||||||||||||||||||
622 | } | - | ||||||||||||||||||
623 | - | |||||||||||||||||||
624 | /*! | - | ||||||||||||||||||
625 | \fn QRectF QAbstractTextDocumentLayout::frameBoundingRect(QTextFrame *frame) const | - | ||||||||||||||||||
626 | - | |||||||||||||||||||
627 | Returns the bounding rectangle of \a frame. | - | ||||||||||||||||||
628 | */ | - | ||||||||||||||||||
629 | - | |||||||||||||||||||
630 | /*! | - | ||||||||||||||||||
631 | \fn QRectF QAbstractTextDocumentLayout::blockBoundingRect(const QTextBlock &block) const | - | ||||||||||||||||||
632 | - | |||||||||||||||||||
633 | Returns the bounding rectangle of \a block. | - | ||||||||||||||||||
634 | */ | - | ||||||||||||||||||
635 | - | |||||||||||||||||||
636 | /*! | - | ||||||||||||||||||
637 | Sets the paint device used for rendering the document's layout to the given | - | ||||||||||||||||||
638 | \a device. | - | ||||||||||||||||||
639 | - | |||||||||||||||||||
640 | \sa paintDevice() | - | ||||||||||||||||||
641 | */ | - | ||||||||||||||||||
642 | void QAbstractTextDocumentLayout::setPaintDevice(QPaintDevice *device) | - | ||||||||||||||||||
643 | { | - | ||||||||||||||||||
644 | Q_D(QAbstractTextDocumentLayout); | - | ||||||||||||||||||
645 | d->paintDevice = device; | - | ||||||||||||||||||
646 | } never executed: end of block | 0 | ||||||||||||||||||
647 | - | |||||||||||||||||||
648 | /*! | - | ||||||||||||||||||
649 | Returns the paint device used to render the document's layout. | - | ||||||||||||||||||
650 | - | |||||||||||||||||||
651 | \sa setPaintDevice() | - | ||||||||||||||||||
652 | */ | - | ||||||||||||||||||
653 | QPaintDevice *QAbstractTextDocumentLayout::paintDevice() const | - | ||||||||||||||||||
654 | { | - | ||||||||||||||||||
655 | Q_D(const QAbstractTextDocumentLayout); | - | ||||||||||||||||||
656 | return d->paintDevice; never executed: return d->paintDevice; | 0 | ||||||||||||||||||
657 | } | - | ||||||||||||||||||
658 | - | |||||||||||||||||||
659 | QT_END_NAMESPACE | - | ||||||||||||||||||
660 | - | |||||||||||||||||||
661 | #include "moc_qabstracttextdocumentlayout.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |