Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsvgnode.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 "qsvgnode_p.h"
6
7#include <QLoggingCategory>
8
9#include "qdebug.h"
10#include "qstack.h"
11
12#include <QtGui/private/qoutlinemapper_p.h>
13
14#if !defined(QT_SVG_SIZE_LIMIT)
15# define QT_SVG_SIZE_LIMIT QT_RASTER_COORD_LIMIT
16#endif
17
19
21
23 : m_parent(parent),
24 m_visible(true),
25 m_displayMode(BlockMode)
26{
27}
28
30{
31
32}
33
35{
36 const QSvgNode *n = this;
37 while (n) {
38 if (n == parent)
39 return true;
40 n = n->m_parent;
41 }
42 return false;
43}
44
46{
47 //qDebug()<<"appending "<<prop->type()<< " ("<< id <<") "<<"to "<<this<<this->type();
49 switch (prop->type()) {
51 m_style.quality = static_cast<QSvgQualityStyle*>(prop);
52 break;
54 m_style.fill = static_cast<QSvgFillStyle*>(prop);
55 break;
57 m_style.viewportFill = static_cast<QSvgViewportFillStyle*>(prop);
58 break;
60 m_style.font = static_cast<QSvgFontStyle*>(prop);
61 break;
63 m_style.stroke = static_cast<QSvgStrokeStyle*>(prop);
64 break;
66 m_style.solidColor = static_cast<QSvgSolidColorStyle*>(prop);
67 doc = document();
68 if (doc && !id.isEmpty())
70 break;
72 m_style.gradient = static_cast<QSvgGradientStyle*>(prop);
73 doc = document();
74 if (doc && !id.isEmpty())
76 break;
78 m_style.transform = static_cast<QSvgTransformStyle*>(prop);
79 break;
81 m_style.animateColor = static_cast<QSvgAnimateColor*>(prop);
82 break;
85 static_cast<QSvgAnimateTransform*>(prop));
86 break;
88 m_style.opacity = static_cast<QSvgOpacityStyle*>(prop);
89 break;
91 m_style.compop = static_cast<QSvgCompOpStyle*>(prop);
92 break;
93 default:
94 qDebug("QSvgNode: Trying to append unknown property!");
95 break;
96 }
97}
98
100{
101 m_style.apply(p, this, states);
102}
103
105{
107}
108
110{
111 const QSvgNode *node = this;
112 while (node) {
113 switch (type) {
115 if (node->m_style.quality)
116 return node->m_style.quality;
117 break;
119 if (node->m_style.fill)
120 return node->m_style.fill;
121 break;
124 return node->m_style.viewportFill;
125 break;
127 if (node->m_style.font)
128 return node->m_style.font;
129 break;
131 if (node->m_style.stroke)
132 return node->m_style.stroke;
133 break;
135 if (node->m_style.solidColor)
136 return node->m_style.solidColor;
137 break;
139 if (node->m_style.gradient)
140 return node->m_style.gradient;
141 break;
143 if (node->m_style.transform)
144 return node->m_style.transform;
145 break;
147 if (node->m_style.animateColor)
148 return node->m_style.animateColor;
149 break;
151 if (!node->m_style.animateTransforms.isEmpty())
152 return node->m_style.animateTransforms.first();
153 break;
155 if (node->m_style.opacity)
156 return node->m_style.opacity;
157 break;
159 if (node->m_style.compop)
160 return node->m_style.compop;
161 break;
162 default:
163 break;
164 }
165 node = node->parent();
166 }
167
168 return 0;
169}
170
172{
173 QString rid = id;
174 if (rid.startsWith(QLatin1Char('#')))
175 rid.remove(0, 1);
176 QSvgTinyDocument *doc = document();
177 return doc ? doc->namedStyle(rid) : 0;
178}
179
181{
182 return bounds(p, states);
183}
184
186{
187 return QRectF(0, 0, 0, 0);
188}
189
191{
192 if (!m_cachedBounds.isEmpty())
193 return m_cachedBounds;
194
195 QImage dummy(1, 1, QImage::Format_RGB32);
196 QPainter p(&dummy);
198
200 pen.setMiterLimit(4);
201 p.setPen(pen);
202
203 QStack<QSvgNode*> parentApplyStack;
204 QSvgNode *parent = m_parent;
205 while (parent) {
206 parentApplyStack.push(parent);
207 parent = parent->parent();
208 }
209
210 for (int i = parentApplyStack.size() - 1; i >= 0; --i)
211 parentApplyStack[i]->applyStyle(&p, states);
212
213 p.setWorldTransform(QTransform());
214
215 m_cachedBounds = transformedBounds(&p, states);
216 return m_cachedBounds;
217}
218
220{
221 QSvgTinyDocument *doc = nullptr;
222 QSvgNode *node = const_cast<QSvgNode*>(this);
223 while (node && node->type() != QSvgNode::DOC) {
224 node = node->parent();
225 }
226 doc = static_cast<QSvgTinyDocument*>(node);
227
228 return doc;
229}
230
232{
233 m_requiredFeatures = lst;
234}
235
237{
238 return m_requiredFeatures;
239}
240
242{
243 m_requiredExtensions = lst;
244}
245
247{
248 return m_requiredExtensions;
249}
250
252{
253 m_requiredLanguages = lst;
254}
255
257{
258 return m_requiredLanguages;
259}
260
262{
263 m_requiredFormats = lst;
264}
265
267{
268 return m_requiredFormats;
269}
270
272{
273 m_requiredFonts = lst;
274}
275
277{
278 return m_requiredFonts;
279}
280
281void QSvgNode::setVisible(bool visible)
282{
283 //propagate visibility change of true to the parent
284 //not propagating false is just a small performance
285 //degradation since we'll iterate over children without
286 //drawing any of them
287 if (m_parent && visible && !m_parent->isVisible())
288 m_parent->setVisible(true);
289
290 m_visible = visible;
291}
292
294{
298 return rect;
299}
300
302{
303 m_id = i;
304}
305
307{
308 m_class = str;
309}
310
312{
313 m_displayMode = mode;
314}
315
317{
318 return m_displayMode;
319}
320
322{
323 const QPen &pen = p->pen();
324 if (pen.style() == Qt::NoPen || pen.brush().style() == Qt::NoBrush || pen.isCosmetic())
325 return 0;
326 return pen.widthF();
327}
328
330{
331 static bool alwaysDraw = qEnvironmentVariableIntValue("QT_SVG_DISABLE_SIZE_LIMIT");
332 if (alwaysDraw)
333 return true;
334
335 QRectF brect = fastBounds(p, states);
336 if (brect.width() <= QT_SVG_SIZE_LIMIT && brect.height() <= QT_SVG_SIZE_LIMIT) {
337 return true;
338 } else {
339 qCWarning(lcSvgDraw) << "Shape of type" << type() << "ignored because it will take too long to rasterize (bounding rect=" << brect << ")."
340 << "Set QT_SVG_DISABLE_SIZE_LIMIT=1 to disable this check.";
341 return false;
342 }
343}
344
Qt::BrushStyle style() const
Returns the brush style.
Definition qbrush.h:120
\inmodule QtGui
Definition qimage.h:37
@ Format_RGB32
Definition qimage.h:46
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
T & first()
Definition qlist.h:628
void append(parameter_type t)
Definition qlist.h:441
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtGui
Definition qpen.h:25
qreal widthF() const
Returns the pen width with floating point precision.
Definition qpen.cpp:598
bool isCosmetic() const
Returns true if the pen is cosmetic; otherwise returns false.
Definition qpen.cpp:783
void setMiterLimit(qreal limit)
Sets the miter limit of this pen to the given limit.
Definition qpen.cpp:570
QBrush brush() const
Returns the brush used to fill strokes generated with this pen.
Definition qpen.cpp:741
Qt::PenStyle style() const
Returns the pen style.
Definition qpen.cpp:385
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr bool isEmpty() const noexcept
Returns true if the rectangle is empty, otherwise returns false.
Definition qrect.h:647
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:718
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:715
\inmodule QtCore
Definition qstack.h:13
void push(const T &t)
Adds element t to the top of the stack.
Definition qstack.h:17
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5299
QString & remove(qsizetype i, qsizetype len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition qstring.cpp:3435
void setRequiredFeatures(const QStringList &lst)
Definition qsvgnode.cpp:231
void setXmlClass(const QString &str)
Definition qsvgnode.cpp:306
void revertStyle(QPainter *p, QSvgExtraStates &states) const
Definition qsvgnode.cpp:104
static qreal strokeWidth(QPainter *p)
Definition qsvgnode.cpp:321
bool isDescendantOf(const QSvgNode *parent) const
Definition qsvgnode.cpp:34
QSvgStyleProperty * styleProperty(QSvgStyleProperty::Type type) const
Definition qsvgnode.cpp:109
void setRequiredExtensions(const QStringList &lst)
Definition qsvgnode.cpp:241
virtual QRectF bounds(QPainter *p, QSvgExtraStates &states) const
Definition qsvgnode.cpp:185
void setRequiredFonts(const QStringList &lst)
Definition qsvgnode.cpp:271
const QStringList & requiredLanguages() const
Definition qsvgnode.cpp:256
void setNodeId(const QString &i)
Definition qsvgnode.cpp:301
QSvgNode * parent() const
Definition qsvgnode_p.h:148
void applyStyle(QPainter *p, QSvgExtraStates &states) const
Definition qsvgnode.cpp:99
const QStringList & requiredFonts() const
Definition qsvgnode.cpp:276
const QStringList & requiredFormats() const
Definition qsvgnode.cpp:266
DisplayMode displayMode() const
Definition qsvgnode.cpp:316
void appendStyleProperty(QSvgStyleProperty *prop, const QString &id)
Definition qsvgnode.cpp:45
QSvgStyle m_style
Definition qsvgnode_p.h:125
void setDisplayMode(DisplayMode display)
Definition qsvgnode.cpp:311
bool shouldDrawNode(QPainter *p, QSvgExtraStates &states) const
Definition qsvgnode.cpp:329
virtual ~QSvgNode()
Definition qsvgnode.cpp:29
virtual QRectF fastBounds(QPainter *p, QSvgExtraStates &states) const
Definition qsvgnode.cpp:180
QRectF transformedBounds() const
Definition qsvgnode.cpp:190
bool isVisible() const
Definition qsvgnode_p.h:153
const QStringList & requiredFeatures() const
Definition qsvgnode.cpp:236
void setRequiredLanguages(const QStringList &lst)
Definition qsvgnode.cpp:251
QSvgTinyDocument * document() const
Definition qsvgnode.cpp:219
virtual Type type() const =0
void setVisible(bool visible)
Definition qsvgnode.cpp:281
const QStringList & requiredExtensions() const
Definition qsvgnode.cpp:246
void setRequiredFormats(const QStringList &lst)
Definition qsvgnode.cpp:261
virtual Type type() const =0
void apply(QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
QSvgRefCounter< QSvgSolidColorStyle > solidColor
QSvgRefCounter< QSvgOpacityStyle > opacity
QSvgRefCounter< QSvgViewportFillStyle > viewportFill
QList< QSvgRefCounter< QSvgAnimateTransform > > animateTransforms
QSvgRefCounter< QSvgFontStyle > font
QSvgRefCounter< QSvgTransformStyle > transform
QSvgRefCounter< QSvgStrokeStyle > stroke
QSvgRefCounter< QSvgAnimateColor > animateColor
QSvgRefCounter< QSvgQualityStyle > quality
QSvgRefCounter< QSvgFillStyle > fill
QSvgRefCounter< QSvgCompOpStyle > compop
QSvgRefCounter< QSvgGradientStyle > gradient
void revert(QPainter *p, QSvgExtraStates &states)
QSvgFillStyleProperty * namedStyle(const QString &id) const
void addNamedStyle(const QString &id, QSvgFillStyleProperty *style)
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QString str
[2]
rect
[4]
Combined button and popup list for selecting options.
@ SolidLine
@ NoPen
@ SvgMiterJoin
@ NoBrush
@ FlatCap
#define qDebug
[1]
Definition qlogging.h:160
#define qCWarning(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLenum mode
GLenum GLuint id
[7]
GLenum type
GLfloat n
GLfloat GLfloat p
[1]
GLuint * states
#define QT_SVG_SIZE_LIMIT
Definition qsvgnode.cpp:15
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
double qreal
Definition qtypes.h:92
\inmodule QtCore \reentrant
Definition qchar.h:17
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent