Qt 4.8
qsvgrenderer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSvg 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
55 
108 {
110 public:
112  : QObjectPrivate(),
113  render(0), timer(0),
114  fps(30)
115  {}
117  {
118  delete render;
119  }
120 
121  static void callRepaintNeeded(QSvgRenderer *const q);
122 
125  int fps;
126 };
127 
132  : QObject(*(new QSvgRendererPrivate), parent)
133 {
134 }
135 
141  : QObject(*new QSvgRendererPrivate, parent)
142 {
143  load(filename);
144 }
145 
151  : QObject(*new QSvgRendererPrivate, parent)
152 {
153  load(contents);
154 }
155 
166  : QObject(*new QSvgRendererPrivate, parent)
167 {
168  load(contents);
169 }
170 
175 {
176 
177 }
178 
183 {
184  Q_D(const QSvgRenderer);
185  return d->render;
186 }
187 
192 {
193  Q_D(const QSvgRenderer);
194  if (d->render)
195  return d->render->size();
196  else
197  return QSize();
198 }
199 
206 {
207  Q_D(const QSvgRenderer);
208  if (d->render)
209  return d->render->viewBox().toRect();
210  else
211  return QRect();
212 }
213 
222 void QSvgRenderer::setViewBox(const QRect &viewbox)
223 {
224  Q_D(QSvgRenderer);
225  if (d->render)
226  d->render->setViewBox(viewbox);
227 }
228 
236 {
237  Q_D(const QSvgRenderer);
238  if (d->render)
239  return d->render->animated();
240  else
241  return false;
242 }
243 
256 {
257  Q_D(const QSvgRenderer);
258  return d->fps;
259 }
260 
262 {
263  Q_D(QSvgRenderer);
264  if (num < 0) {
265  qWarning("QSvgRenderer::setFramesPerSecond: Cannot set negative value %d", num);
266  return;
267  }
268  d->fps = num;
269 }
270 
285 int QSvgRenderer::currentFrame() const
286 {
287  Q_D(const QSvgRenderer);
288  return d->render->currentFrame();
289 }
290 
295 {
296  Q_D(QSvgRenderer);
297  d->render->setCurrentFrame(frame);
298 }
299 
312 {
313  Q_D(const QSvgRenderer);
314  return d->render->animationDuration();
315 }
316 
325 {
326  q->repaintNeeded();
327 }
328 
329 template<typename TInputType>
330 static bool loadDocument(QSvgRenderer *const q,
331  QSvgRendererPrivate *const d,
332  const TInputType &in)
333 {
334  delete d->render;
336  if (d->render && d->render->animated() && d->fps > 0) {
337  if (!d->timer)
338  d->timer = new QTimer(q);
339  else
340  d->timer->stop();
341  q->connect(d->timer, SIGNAL(timeout()),
342  q, SIGNAL(repaintNeeded()));
343  d->timer->start(1000/d->fps);
344  } else if (d->timer) {
345  d->timer->stop();
346  }
347 
348  //force first update
350 
351  return d->render;
352 }
353 
358 bool QSvgRenderer::load(const QString &filename)
359 {
360  Q_D(QSvgRenderer);
361  return loadDocument(this, d, filename);
362 }
363 
368 bool QSvgRenderer::load(const QByteArray &contents)
369 {
370  Q_D(QSvgRenderer);
371  return loadDocument(this, d, contents);
372 }
373 
384 {
385  Q_D(QSvgRenderer);
386  return loadDocument(this, d, contents);
387 }
388 
394 {
395  Q_D(QSvgRenderer);
396  if (d->render) {
397  d->render->draw(painter);
398  }
399 }
400 
416 void QSvgRenderer::render(QPainter *painter, const QString &elementId,
417  const QRectF &bounds)
418 {
419  Q_D(QSvgRenderer);
420  if (d->render) {
421  d->render->draw(painter, elementId, bounds);
422  }
423 }
424 
431 void QSvgRenderer::render(QPainter *painter, const QRectF &bounds)
432 {
433  Q_D(QSvgRenderer);
434  if (d->render) {
435  d->render->draw(painter, bounds);
436  }
437 }
438 
440 {
441  Q_D(const QSvgRenderer);
442  if (d->render)
443  return d->render->viewBox();
444  else
445  return QRect();
446 }
447 
448 void QSvgRenderer::setViewBox(const QRectF &viewbox)
449 {
450  Q_D(QSvgRenderer);
451  if (d->render)
452  d->render->setViewBox(viewbox);
453 }
454 
468 {
469  Q_D(const QSvgRenderer);
470  QRectF bounds;
471  if (d->render)
472  bounds = d->render->boundsOnElement(id);
473  return bounds;
474 }
475 
476 
494 {
495  Q_D(const QSvgRenderer);
496  bool exists = false;
497  if (d->render)
498  exists = d->render->elementExists(id);
499  return exists;
500 }
501 
519 {
520  Q_D(const QSvgRenderer);
521  QMatrix mat;
522  if (d->render)
523  mat = d->render->matrixForElement(id);
524  return mat;
525 }
526 
528 
529 #include "moc_qsvgrenderer.cpp"
530 
531 #endif // QT_NO_SVGRENDERER
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
QSvgTinyDocument * render
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int framesPerSecond() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
int currentFrame() const
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QRect viewBox() const
#define Q_D(Class)
Definition: qglobal.h:2482
static void callRepaintNeeded(QSvgRenderer *const q)
bool animated() const
Returns true if the current document contains animated elements; otherwise returns false...
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QRectF viewBoxF() const
int animationDuration() const
Returns the number of frames in the animation, or 0 if the current document is not animated...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isValid() const
Returns true if there is a valid current document; otherwise returns false.
void setCurrentFrame(int)
Q_CORE_EXPORT void qWarning(const char *,...)
QSize defaultSize() const
Returns the default size of the document contents.
void setViewBox(const QRect &viewbox)
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QSvgRenderer class is used to draw the contents of SVG files onto paint devices.
Definition: qsvgrenderer.h:64
static bool loadDocument(QSvgRenderer *const q, QSvgRendererPrivate *const d, const TInputType &in)
void setFramesPerSecond(int num)
QMatrix matrixForElement(const QString &id) const
Returns the transformation matrix for the element with the given id.
QSvgRenderer(QObject *parent=0)
Constructs a new renderer with the given parent.
void repaintNeeded()
This signal is emitted whenever the rendering of the document needs to be updated, usually for the purposes of animation.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void render(QPainter *p)
Renders the current document, or the current frame of an animated document, using the given painter...
~QSvgRenderer()
Destroys the renderer.
bool load(const QString &filename)
Loads the SVG file specified by filename, returning true if the content was successfully parsed; othe...
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QObject * parent
Definition: qobject.h:92
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QXmlStreamReader class provides a fast parser for reading well-formed XML via a simple streaming ...
Definition: qxmlstream.h:290
The QTimer class provides repetitive and single-shot timers.
Definition: qtimer.h:56
void stop()
Stops the timer.
Definition: qtimer.cpp:284
static QSvgTinyDocument * load(const QString &file)
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition: qtimer.cpp:249
bool elementExists(const QString &id) const
Returns true if the element with the given id exists in the currently parsed SVG file and is a render...
QRectF boundsOnElement(const QString &id) const
Returns bounding rectangle of the item with the given id.