1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
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 a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example richtext/textobject
|
---|
30 | \title Text Object Example
|
---|
31 |
|
---|
32 | The Text Object example shows how to insert an SVG file into a
|
---|
33 | QTextDocument.
|
---|
34 |
|
---|
35 | \image textobject-example.png
|
---|
36 |
|
---|
37 | A QTextDocument consists of a hierarchy of elements, such as text blocks and
|
---|
38 | frames. A text object describes the structure or format of one or more of these
|
---|
39 | elements. For instance, images imported from HTML are implemented using text
|
---|
40 | objects. Text objects are used by the document's
|
---|
41 | \l{QAbstractTextDocumentLayout}{layout} to lay out and render (paint) the
|
---|
42 | document. Each object knows how to paint the elements they govern, and
|
---|
43 | calculates their size.
|
---|
44 |
|
---|
45 | To be able to insert an SVG image into a text document, we create
|
---|
46 | a text object, and implement painting for that object. This object
|
---|
47 | can then be \l{QTextCharFormat::setObjectType()}{set} on a
|
---|
48 | QTextCharFormat. We also register the text object with the layout
|
---|
49 | of the document, enabling it to draw \l{QTextCharFormat}s governed
|
---|
50 | by our text object. We can summarize the procedure with the
|
---|
51 | following steps:
|
---|
52 |
|
---|
53 | \list
|
---|
54 | \o Implement the text object.
|
---|
55 | \o Register the text object with the layout of the text
|
---|
56 | document.
|
---|
57 | \o Set the text object on a QTextCharFormat.
|
---|
58 | \o Insert a QChar::ObjectReplacementCharacter with that
|
---|
59 | text char format into the document.
|
---|
60 | \endlist
|
---|
61 |
|
---|
62 | The example consists of the following classes:
|
---|
63 |
|
---|
64 | \list
|
---|
65 | \o \c{SvgTextObject} implements the text object.
|
---|
66 | \o \c{Window} shows a QTextEdit into which SVG images can be
|
---|
67 | inserted.
|
---|
68 | \endlist
|
---|
69 |
|
---|
70 | \section1 SvgTextObject Class Definition
|
---|
71 |
|
---|
72 | Let's take a look at the header file of \c {SvgTextObject}:
|
---|
73 |
|
---|
74 | \snippet examples/richtext/textobject/svgtextobject.h 0
|
---|
75 |
|
---|
76 | A text object is a QObject that implements QTextObjectInterface.
|
---|
77 | Note that the first class inherited must be QObject, and that
|
---|
78 | you must use Q_INTERFACES to let Qt know that your class
|
---|
79 | implements QTextObjectInterface.
|
---|
80 |
|
---|
81 | The document layout keeps a collection of text objects stored as
|
---|
82 | \l{QObject}s, each of which has an associated object type. The
|
---|
83 | layout casts the QObject for the associated object type into the
|
---|
84 | QTextObjectInterface.
|
---|
85 |
|
---|
86 | The \l{QTextObjectInterface::}{intrinsicSize()} and
|
---|
87 | \l{QTextObjectInterface::}{drawObject()} functions are then used
|
---|
88 | to calculate the size of the text object and draw it.
|
---|
89 |
|
---|
90 | \section1 SvgTextObject Class Implementation
|
---|
91 |
|
---|
92 | We start of by taking a look at the
|
---|
93 | \l{QTextObjectInterface::}{intrinsicSize()} function:
|
---|
94 |
|
---|
95 | \snippet examples/richtext/textobject/svgtextobject.cpp 0
|
---|
96 |
|
---|
97 | \c intrinsicSize() is called by the layout to calculate the size
|
---|
98 | of the text object. Notice that we have drawn the SVG image on a
|
---|
99 | QImage. This is because SVG rendering is quite expensive. The
|
---|
100 | example would lag seriously for large images if we drew them
|
---|
101 | with a QSvgRenderer each time.
|
---|
102 |
|
---|
103 | \snippet examples/richtext/textobject/svgtextobject.cpp 1
|
---|
104 |
|
---|
105 | In \c drawObject(), we paint the SVG image using the QPainter
|
---|
106 | provided by the layout.
|
---|
107 |
|
---|
108 | \section1 Window Class Definition
|
---|
109 |
|
---|
110 | The \c Window class is a self-contained window that has a
|
---|
111 | QTextEdit in which SVG images can be inserted.
|
---|
112 |
|
---|
113 | \snippet examples/richtext/textobject/window.h 0
|
---|
114 |
|
---|
115 | The \c insertTextObject() slot inserts an SVG image at the current
|
---|
116 | cursor position, while \c setupTextObject() creates and registers
|
---|
117 | the SvgTextObject with the layout of the text edit's document.
|
---|
118 |
|
---|
119 | The constructor simply calls \c setupTextObject() and \c
|
---|
120 | setupGui(), which creates and lays out the widgets of the \c
|
---|
121 | Window.
|
---|
122 |
|
---|
123 | \section1 Window Class Implementation
|
---|
124 |
|
---|
125 | We will now take a closer look at the functions that are relevant
|
---|
126 | to our text object, starting with the \c setupTextObject()
|
---|
127 | function.
|
---|
128 |
|
---|
129 | \snippet examples/richtext/textobject/window.cpp 3
|
---|
130 |
|
---|
131 | \c {SvgTextFormat}'s value is the number of our object type. It is
|
---|
132 | used to identify object types by the document layout.
|
---|
133 |
|
---|
134 | Note that we only create one SvgTextObject instance; it will be
|
---|
135 | used for all QTextCharFormat's with the \c SvgTextFormat object
|
---|
136 | type.
|
---|
137 |
|
---|
138 | Let's move on to the \c insertTextObject() function:
|
---|
139 |
|
---|
140 | \snippet examples/richtext/textobject/window.cpp 1
|
---|
141 |
|
---|
142 | First, the \c .svg file is opened and its contents are read
|
---|
143 | into the \c svgData array.
|
---|
144 |
|
---|
145 | \snippet examples/richtext/textobject/window.cpp 2
|
---|
146 |
|
---|
147 | To speed things up, we buffer the SVG image in a QImage. We use
|
---|
148 | \l{QTextFormat::}{setProperty()} to store the QImage in the in the
|
---|
149 | QTextCharFormat. We can retrieve it later with
|
---|
150 | \l{QTextCharFormat::}{property()}.
|
---|
151 |
|
---|
152 | We insert the char format in the standard way - using a
|
---|
153 | QTextCursor. Notice that we use the special QChar
|
---|
154 | \l{QChar::}{ObjectReplacementCharacter}.
|
---|
155 | */
|
---|
156 |
|
---|