1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtSvg module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qsvgrenderer.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_SVGRENDERER
|
---|
45 |
|
---|
46 | #include "qsvgtinydocument_p.h"
|
---|
47 |
|
---|
48 | #include "qbytearray.h"
|
---|
49 | #include "qtimer.h"
|
---|
50 | #include "qdebug.h"
|
---|
51 | #include "private/qobject_p.h"
|
---|
52 |
|
---|
53 |
|
---|
54 | QT_BEGIN_NAMESPACE
|
---|
55 |
|
---|
56 | /*!
|
---|
57 | \class QSvgRenderer
|
---|
58 | \ingroup multimedia
|
---|
59 |
|
---|
60 | \brief The QSvgRenderer class is used to draw the contents of SVG files onto paint devices.
|
---|
61 | \since 4.1
|
---|
62 | \reentrant
|
---|
63 |
|
---|
64 | Using QSvgRenderer, Scalable Vector Graphics (SVG) can be rendered onto any QPaintDevice
|
---|
65 | subclass, including QWidget, QImage, and QGLWidget.
|
---|
66 |
|
---|
67 | QSvgRenderer provides an API that supports basic features of SVG rendering, such as loading
|
---|
68 | and rendering of static drawings, and more interactive features like animation. Since the
|
---|
69 | rendering is performed using QPainter, SVG drawings can be rendered on any subclass of
|
---|
70 | QPaintDevice.
|
---|
71 |
|
---|
72 | SVG drawings are either loaded when an QSvgRenderer is constructed, or loaded later
|
---|
73 | using the load() functions. Data is either supplied directly as serialized XML, or
|
---|
74 | indirectly using a file name. If a valid file has been loaded, either when the renderer
|
---|
75 | is constructed or at some later time, isValid() returns true; otherwise it returns false.
|
---|
76 | QSvgRenderer provides the render() slot to render the current document, or the current
|
---|
77 | frame of an animated document, using a given painter.
|
---|
78 |
|
---|
79 | The defaultSize() function provides information about the amount of space that is required
|
---|
80 | to render the currently loaded SVG file. This is useful for paint devices, such as QWidget,
|
---|
81 | that often need to supply a size hint to their parent layout.
|
---|
82 | The default size of a drawing may differ from its visible area, found using the \l viewBox
|
---|
83 | property.
|
---|
84 |
|
---|
85 | Animated SVG drawings are supported, and can be controlled with a simple collection of
|
---|
86 | functions and properties:
|
---|
87 |
|
---|
88 | \list
|
---|
89 | \o The animated() function indicates whether a drawing contains animation information.
|
---|
90 | \omit
|
---|
91 | \o The animationDuration() function provides the duration in milliseconds of the
|
---|
92 | animation, without taking any looping into account.
|
---|
93 | \o The \l currentFrame property contains the current frame of the animation.
|
---|
94 | \endomit
|
---|
95 | \o The \l framesPerSecond property contains the rate at which the animation plays.
|
---|
96 | \endlist
|
---|
97 |
|
---|
98 | Finally, the QSvgRenderer class provides the repaintNeeded() signal which is emitted
|
---|
99 | whenever the rendering of the document needs to be updated.
|
---|
100 |
|
---|
101 | \sa QSvgWidget, {QtSvg Module}, {SVG Viewer Example}, QPicture
|
---|
102 | */
|
---|
103 |
|
---|
104 | class QSvgRendererPrivate : public QObjectPrivate
|
---|
105 | {
|
---|
106 | Q_DECLARE_PUBLIC(QSvgRenderer)
|
---|
107 | public:
|
---|
108 | explicit QSvgRendererPrivate()
|
---|
109 | : QObjectPrivate(),
|
---|
110 | render(0), timer(0),
|
---|
111 | fps(30)
|
---|
112 | {}
|
---|
113 | ~QSvgRendererPrivate()
|
---|
114 | {
|
---|
115 | delete render;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void callRepaintNeeded(QSvgRenderer *const q);
|
---|
119 |
|
---|
120 | QSvgTinyDocument *render;
|
---|
121 | QTimer *timer;
|
---|
122 | int fps;
|
---|
123 | };
|
---|
124 |
|
---|
125 | /*!
|
---|
126 | Constructs a new renderer with the given \a parent.
|
---|
127 | */
|
---|
128 | QSvgRenderer::QSvgRenderer(QObject *parent)
|
---|
129 | : QObject(*(new QSvgRendererPrivate), parent)
|
---|
130 | {
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*!
|
---|
134 | Constructs a new renderer with the given \a parent and loads the contents of the
|
---|
135 | SVG file with the specified \a filename.
|
---|
136 | */
|
---|
137 | QSvgRenderer::QSvgRenderer(const QString &filename, QObject *parent)
|
---|
138 | : QObject(*new QSvgRendererPrivate, parent)
|
---|
139 | {
|
---|
140 | load(filename);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*!
|
---|
144 | Constructs a new renderer with the given \a parent and loads the SVG data
|
---|
145 | from the byte array specified by \a contents.
|
---|
146 | */
|
---|
147 | QSvgRenderer::QSvgRenderer(const QByteArray &contents, QObject *parent)
|
---|
148 | : QObject(*new QSvgRendererPrivate, parent)
|
---|
149 | {
|
---|
150 | load(contents);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | \since 4.5
|
---|
155 |
|
---|
156 | Constructs a new renderer with the given \a parent and loads the SVG data
|
---|
157 | using the stream reader specified by \a contents.
|
---|
158 | */
|
---|
159 | QSvgRenderer::QSvgRenderer(QXmlStreamReader *contents, QObject *parent)
|
---|
160 | : QObject(*new QSvgRendererPrivate, parent)
|
---|
161 | {
|
---|
162 | load(contents);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /*!
|
---|
166 | Destroys the renderer.
|
---|
167 | */
|
---|
168 | QSvgRenderer::~QSvgRenderer()
|
---|
169 | {
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | /*!
|
---|
174 | Returns true if there is a valid current document; otherwise returns false.
|
---|
175 | */
|
---|
176 | bool QSvgRenderer::isValid() const
|
---|
177 | {
|
---|
178 | Q_D(const QSvgRenderer);
|
---|
179 | return d->render;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*!
|
---|
183 | Returns the default size of the document contents.
|
---|
184 | */
|
---|
185 | QSize QSvgRenderer::defaultSize() const
|
---|
186 | {
|
---|
187 | Q_D(const QSvgRenderer);
|
---|
188 | if (d->render)
|
---|
189 | return d->render->size();
|
---|
190 | else
|
---|
191 | return QSize();
|
---|
192 | }
|
---|
193 |
|
---|
194 | /*!
|
---|
195 | Returns viewBoxF().toRect().
|
---|
196 |
|
---|
197 | \sa viewBoxF()
|
---|
198 | */
|
---|
199 | QRect QSvgRenderer::viewBox() const
|
---|
200 | {
|
---|
201 | Q_D(const QSvgRenderer);
|
---|
202 | if (d->render)
|
---|
203 | return d->render->viewBox().toRect();
|
---|
204 | else
|
---|
205 | return QRect();
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | \property QSvgRenderer::viewBox
|
---|
210 | \brief the rectangle specifying the visible area of the document in logical coordinates
|
---|
211 | \since 4.2
|
---|
212 | */
|
---|
213 | void QSvgRenderer::setViewBox(const QRect &viewbox)
|
---|
214 | {
|
---|
215 | Q_D(QSvgRenderer);
|
---|
216 | if (d->render)
|
---|
217 | d->render->setViewBox(viewbox);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /*!
|
---|
221 | Returns true if the current document contains animated elements; otherwise
|
---|
222 | returns false.
|
---|
223 |
|
---|
224 | \sa framesPerSecond()
|
---|
225 | */
|
---|
226 | bool QSvgRenderer::animated() const
|
---|
227 | {
|
---|
228 | Q_D(const QSvgRenderer);
|
---|
229 | if (d->render)
|
---|
230 | return d->render->animated();
|
---|
231 | else
|
---|
232 | return false;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*!
|
---|
236 | \property QSvgRenderer::framesPerSecond
|
---|
237 | \brief the number of frames per second to be shown
|
---|
238 |
|
---|
239 | The number of frames per second is 0 if the current document is not animated.
|
---|
240 |
|
---|
241 | \sa animated()
|
---|
242 | */
|
---|
243 | int QSvgRenderer::framesPerSecond() const
|
---|
244 | {
|
---|
245 | Q_D(const QSvgRenderer);
|
---|
246 | return d->fps;
|
---|
247 | }
|
---|
248 |
|
---|
249 | void QSvgRenderer::setFramesPerSecond(int num)
|
---|
250 | {
|
---|
251 | Q_D(QSvgRenderer);
|
---|
252 | if (num < 0) {
|
---|
253 | qWarning("QSvgRenderer::setFramesPerSecond: Cannot set negative value %d", num);
|
---|
254 | return;
|
---|
255 | }
|
---|
256 | d->fps = num;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*!
|
---|
260 | \property QSvgRenderer::currentFrame
|
---|
261 | \brief the current frame of the document's animation, or 0 if the document is not animated
|
---|
262 | \internal
|
---|
263 |
|
---|
|
---|