Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
81 {
82 delete render;
83 }
84
86 {
87 if (animationEnabled && render && render->animated() && fps > 0) {
89 timer->start(1000 / fps);
90 } else if (timer) {
91 timer->stop();
92 }
93 }
94
96 {
97 Q_Q(QSvgRenderer);
98 if (!timer) {
99 timer = new QTimer(q);
101 }
102 }
103
104 static void callRepaintNeeded(QSvgRenderer *const q);
105
106 static QtSvg::Options defaultOptions()
107 {
108 static bool envOk = false;
109 static QtSvg::Options envOpts = QtSvg::Options::fromInt(
110 qEnvironmentVariableIntValue("QT_SVG_DEFAULT_OPTIONS", &envOk));
111 return envOk ? envOpts : appDefaultOptions;
112 }
113
116 int fps;
117 QtSvg::Options options;
118 static QtSvg::Options appDefaultOptions;
119 bool animationEnabled = true;
120};
121
123
128 : QObject(*(new QSvgRendererPrivate), parent)
129{
130}
131
137 : QObject(*new QSvgRendererPrivate, parent)
138{
139 load(filename);
140}
141
151
158QSvgRenderer::QSvgRenderer(QXmlStreamReader *contents, QObject *parent)
159 : QObject(*new QSvgRendererPrivate, parent)
160{
161 load(contents);
162}
163
171
176{
177 Q_D(const QSvgRenderer);
178 return d->render;
179}
180
185{
186 Q_D(const QSvgRenderer);
187 if (d->render)
188 return d->render->size();
189 else
190 return QSize();
191}
192
199{
200 Q_D(const QSvgRenderer);
201 if (d->render)
202 return d->render->viewBox().toRect();
203 else
204 return QRect();
205}
206
212void QSvgRenderer::setViewBox(const QRect &viewbox)
213{
214 Q_D(QSvgRenderer);
215 if (d->render)
216 d->render->setViewBox(viewbox);
217}
218
226{
227 Q_D(const QSvgRenderer);
228 if (d->render)
229 return d->render->animated();
230 else
231 return false;
232}
233
250{
251 Q_D(const QSvgRenderer);
252 return d->animationEnabled;
253}
254
256{
257 Q_D(QSvgRenderer);
258 d->animationEnabled = enable;
259 d->startOrStopTimer();
260}
261
271{
272 Q_D(const QSvgRenderer);
273 return d->fps;
274}
275
277{
278 Q_D(QSvgRenderer);
279 if (num < 0) {
280 qWarning("QSvgRenderer::setFramesPerSecond: Cannot set negative value %d", num);
281 return;
282 }
283 d->fps = num;
284 d->startOrStopTimer();
285}
286
304{
305 Q_D(const QSvgRenderer);
306 if (d->render && d->render->preserveAspectRatio())
307 return Qt::KeepAspectRatio;
309}
310
312{
313 Q_D(QSvgRenderer);
314 if (d->render) {
316 d->render->setPreserveAspectRatio(true);
317 else if (mode == Qt::IgnoreAspectRatio)
318 d->render->setPreserveAspectRatio(false);
319 }
320}
321
334QtSvg::Options QSvgRenderer::options() const
335{
336 Q_D(const QSvgRenderer);
337 return d->options;
338}
339
340void QSvgRenderer::setOptions(QtSvg::Options flags)
341{
342 Q_D(QSvgRenderer);
343 d->options = flags;
344}
345
357
370{
371 Q_D(const QSvgRenderer);
372 return d->render->currentFrame();
373}
374
379{
380 Q_D(QSvgRenderer);
381 d->render->setCurrentFrame(frame);
382}
383
393{
394 Q_D(const QSvgRenderer);
395 return d->render->animationDuration();
396}
397
406{
407 emit q->repaintNeeded();
408}
409
410template<typename TInputType>
411static bool loadDocument(QSvgRenderer *const q,
412 QSvgRendererPrivate *const d,
413 const TInputType &in)
414{
415 delete d->render;
416 d->render = QSvgTinyDocument::load(in, d->options);
417 if (d->render && !d->render->size().isValid()) {
418 delete d->render;
419 d->render = nullptr;
420 }
421 d->startOrStopTimer();
422
423 //force first update
425
426 return d->render;
427}
428
433bool QSvgRenderer::load(const QString &filename)
434{
435 Q_D(QSvgRenderer);
436 return loadDocument(this, d, filename);
437}
438
444{
445 Q_D(QSvgRenderer);
446 return loadDocument(this, d, contents);
447}
448
458bool QSvgRenderer::load(QXmlStreamReader *contents)
459{
460 Q_D(QSvgRenderer);
461 return loadDocument(this, d, contents);
462}
463
469{
470 Q_D(QSvgRenderer);
471 if (d->render) {
472 d->render->draw(painter);
473 }
474}
475
489 const QRectF &bounds)
490{
491 Q_D(QSvgRenderer);
492 if (d->render) {
493 d->render->draw(painter, elementId, bounds);
494 }
495}
496
504{
505 Q_D(QSvgRenderer);
506 if (d->render) {
507 d->render->draw(painter, bounds);
508 }
509}
510
512{
513 Q_D(const QSvgRenderer);
514 if (d->render)
515 return d->render->viewBox();
516 else
517 return QRect();
518}
519
521{
522 Q_D(QSvgRenderer);
523 if (d->render)
524 d->render->setViewBox(viewbox);
525}
526
537{
538 Q_D(const QSvgRenderer);
539 QRectF bounds;
540 if (d->render)
541 bounds = d->render->boundsOnElement(id);
542 return bounds;
543}
544
545
560{
561 Q_D(const QSvgRenderer);
562 bool exists = false;
563 if (d->render)
564 exists = d->render->elementExists(id);
565 return exists;
566}
567
582{
583 Q_D(const QSvgRenderer);
584 QTransform trans;
585 if (d->render)
586 trans = d->render->transformForElement(id);
587 return trans;
588}
589
591
592#include "moc_qsvgrenderer.cpp"
593
594#endif // QT_NO_SVGRENDERER
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qobject.h:103
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QSvgTinyDocument * render
static QtSvg::Options defaultOptions()
static QtSvg::Options appDefaultOptions
static void callRepaintNeeded(QSvgRenderer *const q)
QtSvg::Options options
\inmodule QtSvg
QTransform transformForElement(const QString &id) const
void setOptions(QtSvg::Options flags)
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.
static void setDefaultOptions(QtSvg::Options flags)
Sets the option flags that renderers will be created with to flags.
~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
QtSvg::Options options
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
bool isAnimationEnabled() const
void setCurrentFrame(int)
void repaintNeeded()
This signal is emitted whenever the rendering of the document needs to be updated,...
QRectF viewBox
the rectangle specifying the visible area of the document in logical coordinates
void setAnimationEnabled(bool enable)
static QSvgTinyDocument * load(const QString &file, QtSvg::Options options={})
\inmodule QtCore
Definition qtimer.h:20
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:241
void stop()
Stops the timer.
Definition qtimer.cpp:267
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
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:166
GLenum mode
GLbitfield flags
GLboolean enable
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)
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
#define emit
QPainter painter(this)
[7]
QFrame frame
[0]