Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvgtinydocument_p.h
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#ifndef QSVGTINYDOCUMENT_P_H
5#define QSVGTINYDOCUMENT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
19#include "qtsvgglobal_p.h"
20
21#include "QtCore/qrect.h"
22#include "QtCore/qlist.h"
23#include "QtCore/qhash.h"
24#include "QtCore/qdatetime.h"
25#include "QtCore/qxmlstream.h"
26#include "QtCore/qscopedvaluerollback.h"
27#include "QtCore/qsharedpointer.h"
28#include "qsvgstyle_p.h"
29#include "qsvgfont_p.h"
30#include "private/qsvganimator_p.h"
31
33
34class QPainter;
35class QByteArray;
36class QSvgFont;
37class QTransform;
38
39class Q_SVG_EXPORT QSvgTinyDocument : public QSvgStructureNode
40{
41public:
42 static QSvgTinyDocument *load(const QString &file, QtSvg::Options options = {},
43 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
44 static QSvgTinyDocument *load(const QByteArray &contents, QtSvg::Options options = {},
45 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
46 static QSvgTinyDocument *load(QXmlStreamReader *contents, QtSvg::Options options = {},
47 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
48 static bool isLikelySvg(QIODevice *device, bool *isCompressed = nullptr);
49public:
50 QSvgTinyDocument(QtSvg::Options options, QtSvg::AnimatorType type);
51 ~QSvgTinyDocument();
52 Type type() const override;
53
54 inline QSize size() const;
55 void setWidth(int len, bool percent);
56 void setHeight(int len, bool percent);
57 inline int width() const;
58 inline int height() const;
59 inline bool widthPercent() const;
60 inline bool heightPercent() const;
61
62 inline bool preserveAspectRatio() const;
63 void setPreserveAspectRatio(bool on);
64
65 inline QRectF viewBox() const;
66 void setViewBox(const QRectF &rect);
67 bool isCalculatingImplicitViewBox() { return m_calculatingImplicitViewBox; }
68
69 QtSvg::Options options() const;
70
71 void drawCommand(QPainter *, QSvgExtraStates &) override;
72
73 void draw(QPainter *p);
74 void draw(QPainter *p, const QRectF &bounds);
75 void draw(QPainter *p, const QString &id,
76 const QRectF &bounds=QRectF());
77
78 QTransform transformForElement(const QString &id) const;
79 QRectF boundsOnElement(const QString &id) const;
80 bool elementExists(const QString &id) const;
81
82 void addSvgFont(QSvgFont *);
83 QSvgFont *svgFont(const QString &family) const;
84 void addNamedNode(const QString &id, QSvgNode *node);
85 QSvgNode *namedNode(const QString &id) const;
86 void addNamedStyle(const QString &id, QSvgPaintStyleProperty *style);
87 QSvgPaintStyleProperty *namedStyle(const QString &id) const;
88
89 void restartAnimation();
90 inline qint64 currentElapsed() const;
91 bool animated() const;
92 void setAnimated(bool a);
93 inline int animationDuration() const;
94 int currentFrame() const;
95 void setCurrentFrame(int);
96 void setFramesPerSecond(int num);
97
98 QSharedPointer<QSvgAbstractAnimator> animator() const;
99
100private:
101 void mapSourceToTarget(QPainter *p, const QRectF &targetRect, const QRectF &sourceRect = QRectF());
102private:
103 QSize m_size;
104 bool m_widthPercent;
105 bool m_heightPercent;
106
107 mutable bool m_calculatingImplicitViewBox = false;
108 mutable bool m_implicitViewBox = true;
109 mutable QRectF m_viewBox;
110 bool m_preserveAspectRatio = false;
111
112 QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts;
113 QHash<QString, QSvgNode *> m_namedNodes;
114 QHash<QString, QSvgRefCounter<QSvgPaintStyleProperty> > m_namedStyles;
115
116 bool m_animated;
117 int m_fps;
118
119 QSvgExtraStates m_states;
120
121 const QtSvg::Options m_options;
122 QSharedPointer<QSvgAbstractAnimator> m_animator;
123};
124
125Q_SVG_EXPORT QDebug operator<<(QDebug debug, const QSvgTinyDocument &doc);
126
127inline QSize QSvgTinyDocument::size() const
128{
129 if (m_size.isEmpty())
130 return viewBox().size().toSize();
131 if (m_widthPercent || m_heightPercent) {
132 const int width = m_widthPercent ? qRound(0.01 * m_size.width() * viewBox().size().width()) : m_size.width();
133 const int height = m_heightPercent ? qRound(0.01 * m_size.height() * viewBox().size().height()) : m_size.height();
134 return QSize(width, height);
135 }
136 return m_size;
137}
138
139inline int QSvgTinyDocument::width() const
140{
141 return size().width();
142}
143
144inline int QSvgTinyDocument::height() const
145{
146 return size().height();
147}
148
149inline bool QSvgTinyDocument::widthPercent() const
150{
151 return m_widthPercent;
152}
153
154inline bool QSvgTinyDocument::heightPercent() const
155{
156 return m_heightPercent;
157}
158
159inline QRectF QSvgTinyDocument::viewBox() const
160{
161 if (m_viewBox.isNull()) {
162 QScopedValueRollback<bool> guard(m_calculatingImplicitViewBox, true);
163 m_viewBox = bounds();
164 m_implicitViewBox = true;
165 }
166
167 return m_viewBox;
168}
169
170inline bool QSvgTinyDocument::preserveAspectRatio() const
171{
172 return m_preserveAspectRatio;
173}
174
175inline qint64 QSvgTinyDocument::currentElapsed() const
176{
177 return m_animator->currentElapsed();
178}
179
180inline int QSvgTinyDocument::animationDuration() const
181{
182 return m_animator->animationDuration();
183}
184
185QT_END_NAMESPACE
186
187#endif // QSVGTINYDOCUMENT_P_H
friend class QPainter
QRectF decoratedInternalBounds(QPainter *p, QSvgExtraStates &states) const override
QSvgNode * link() const
void setLink(QSvgNode *link)
void drawCommand(QPainter *p, QSvgExtraStates &states) override
bool isResolved() const
QRectF internalBounds(QPainter *p, QSvgExtraStates &states) const override
bool isRecursing() const
QPointF start() const
const QString & linkId() const
QSvgUse(const QPointF &start, QSvgNode *parent, const QString &linkId)
QSvgUse(const QPointF &start, QSvgNode *parent, QSvgNode *link)
Type type() const override
void drawCommand(QPainter *, QSvgExtraStates &) override
Type type() const override
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2568