Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsvgrenderer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsvgrenderer.h"
5
6#ifndef QT_NO_SVGRENDERER
7
9
10#include "qbytearray.h"
11#include "qtimer.h"
12#include "qtransform.h"
13#include "qdebug.h"
14#include "private/qobject_p.h"
15
16
18
69{
70 Q_DECLARE_PUBLIC(QSvgRenderer)
71public:
74 render(0), timer(0),
75 fps(30)
76 {}
78 {
79 delete render;
80 }
81
82 static void callRepaintNeeded(QSvgRenderer *const q);
83
86 int fps;
87};
88
94{
95}
96
103{
104 load(filename);
105}
106
113{
114 load(contents);
115}
116
125{
126 load(contents);
127}
128
133{
134
135}
136
141{
142 Q_D(const QSvgRenderer);
143 return d->render;
144}
145
150{
151 Q_D(const QSvgRenderer);
152 if (d->render)
153 return d->render->size();
154 else
155 return QSize();
156}
157
164{
165 Q_D(const QSvgRenderer);
166 if (d->render)
167 return d->render->viewBox().toRect();
168 else
169 return QRect();
170}
171
177void QSvgRenderer::setViewBox(const QRect &viewbox)
178{
179 Q_D(QSvgRenderer);
180 if (d->render)
181 d->render->setViewBox(viewbox);
182}
183
191{
192 Q_D(const QSvgRenderer);
193 if (d->render)
194 return d->render->animated();
195 else
196 return false;
197}
198
208{
209 Q_D(const QSvgRenderer);
210 return d->fps;
211}
212
214{
215 Q_D(QSvgRenderer);
216 if (num < 0) {
217 qWarning("QSvgRenderer::setFramesPerSecond: Cannot set negative value %d", num);
218 return;
219 }
220 d->fps = num;
221}
222
240{
241 Q_D(const QSvgRenderer);
242 if (d->render && d->render->preserveAspectRatio())
243 return Qt::KeepAspectRatio;
245}
246
248{
249 Q_D(QSvgRenderer);
250 if (d->render) {
252 d->render->setPreserveAspectRatio(true);
253 else if (mode == Qt::IgnoreAspectRatio)
254 d->render->setPreserveAspectRatio(false);
255 }
256}
257
270{
271 Q_D(const QSvgRenderer);
272 return d->render->currentFrame();
273}
274
279{
280 Q_D(QSvgRenderer);
281 d->render->setCurrentFrame(frame);
282}
283
293{
294 Q_D(const QSvgRenderer);
295 return d->render->animationDuration();
296}
297
306{
307 emit q->repaintNeeded();
308}
309
310template<typename TInputType>
311static bool loadDocument(QSvgRenderer *const q,
312 QSvgRendererPrivate *const d,
313 const TInputType &in)
314{
315 delete d->render;
316 d->render = QSvgTinyDocument::load(in);
317 if (d->render && !d->render->size().isValid()) {
318 delete d->render;
319 d->render = nullptr;
320 }
321 if (d->render && d->render->animated() && d->fps > 0) {
322 if (!d->timer)
323 d->timer = new QTimer(q);
324 else
325 d->timer->stop();
326 q->connect(d->timer, SIGNAL(timeout()),
327 q, SIGNAL(repaintNeeded()));
328 d->timer->start(1000/d->fps);
329 } else if (d->timer) {
330 d->timer->stop();
331 }
332
333 //force first update
335
336 return d->render;
337}
338
343bool QSvgRenderer::load(const QString &filename)
344{
345 Q_D(QSvgRenderer);
346 return loadDocument(this, d, filename);
347}
348
354{
355 Q_D(QSvgRenderer);
356 return loadDocument(this, d, contents);
357}
358
368bool QSvgRenderer::load(QXmlStreamReader *contents)
369{
370 Q_D(QSvgRenderer);
371 return loadDocument(this, d, contents);
372}
373
379{
380 Q_D(QSvgRenderer);
381 if (d->render) {
382 d->render->draw(painter);
383 }
384}
385
399 const QRectF &bounds)
400{
401 Q_D(QSvgRenderer);
402 if (d->render) {
403 d->render->draw(painter, elementId, bounds);
404 }
405}
406
414{
415 Q_D(QSvgRenderer);
416 if (d->render) {
417 d->render->draw(painter, bounds);
418 }
419}
420
422{
423 Q_D(const QSvgRenderer);
424 if (d->render)
425 return d->render->viewBox();
426 else
427 return QRect();
428}
429
431{
432 Q_D(QSvgRenderer);
433 if (d->render)
434 d->render->setViewBox(viewbox);
435}
436
447{
448 Q_D(const QSvgRenderer);
449 QRectF bounds;
450 if (d->render)
451 bounds = d->render->boundsOnElement(id);
452 return bounds;
453}
454
455
470{
471 Q_D(const QSvgRenderer);
472 bool exists = false;
473 if (d->render)
474 exists = d->render->elementExists(id);
475 return exists;
476}
477
492{
493 Q_D(const QSvgRenderer);
494 QTransform trans;
495 if (d->render)
496 trans = d->render->transformForElement(id);
497 return trans;
498}
499
501
502#include "moc_qsvgrenderer.cpp"
503
504#endif // QT_NO_SVGRENDERER
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qobject.h:90
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore\reentrant
Definition qrect.h:483
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QSvgTinyDocument * render
static void callRepaintNeeded(QSvgRenderer *const q)
\inmodule QtSvg
QTransform transformForElement(const QString &id) const
void render(QPainter *p)
Renders the current document, or the current frame of an animated document, using the given painter.
QRectF boundsOnElement(const QString &id) const
QSize defaultSize() const
Returns the default size of the document contents.
bool animated() const
Returns true if the current document contains animated elements; otherwise returns false.
~QSvgRenderer()
Destroys the renderer.
bool isValid() const
Returns true if there is a valid current document; otherwise returns false.
QSvgRenderer(QObject *parent=nullptr)
Constructs a new renderer with the given parent.
int currentFrame
the current frame of the document's animation, or 0 if the document is not animated
void setFramesPerSecond(int num)
int framesPerSecond
the number of frames per second to be shown
void setAspectRatioMode(Qt::AspectRatioMode mode)
Qt::AspectRatioMode aspectRatioMode
how rendering adheres to the SVG view box aspect ratio
void setViewBox(const QRect &viewbox)
bool load(const QString &filename)
Loads the SVG file specified by filename, returning true if the content was successfully parsed; othe...
int animationDuration() const
bool elementExists(const QString &id) const
QRectF viewBoxF() const
void setCurrentFrame(int)
QRectF viewBox
the rectangle specifying the visible area of the document in logical coordinates
static QSvgTinyDocument * load(const QString &file)
\inmodule QtCore
Definition qtimer.h:20
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
Combined button and popup list for selecting options.
AspectRatioMode
@ KeepAspectRatio
@ IgnoreAspectRatio
#define qWarning
Definition qlogging.h:162
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLenum mode
GLbitfield GLuint64 timeout
[4]
GLuint in
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint num
static bool loadDocument(QSvgRenderer *const q, QSvgRendererPrivate *const d, const TInputType &in)
#define emit
QPainter painter(this)
[7]
QFrame frame
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent