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
qsvgdebug.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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// Qt-Security score:significant reason:default
4
5
7#include <QDebug>
8
10
11static const char *nodeTypeStrings[] = {
12 "DOC",
13 "G",
14 "DEFS",
15 "SWITCH",
16 "ANIMATION",
17 "ARC",
18 "CIRCLE",
19 "ELLIPSE",
20 "IMAGE",
21 "LINE",
22 "PATH",
23 "POLYGON",
24 "POLYLINE",
25 "RECT",
26 "TEXT",
27 "TEXTAREA",
28 "TSPAN",
29 "USE",
30 "VIDEO"
31};
32
33// TODO: something like this is needed in several places. Make a common version.
34static const char *typeName(const QSvgNode *node)
35{
36 constexpr int typeNameCount = sizeof(nodeTypeStrings) / sizeof(const char *);
37 if (node->type() < typeNameCount)
38 return nodeTypeStrings[node->type()];
39 return "UNKNOWN";
40}
41
43{
44public:
45 SvgDebugVisitor(QDebug &stream) : debug(stream) {}
46 void write(const QSvgDocument *doc);
47
48protected:
49 void visitNode(const QSvgNode *) override;
50 bool visitStructureNodeStart(const QSvgStructureNode *node) override;
51 void visitStructureNodeEnd(const QSvgStructureNode *) override;
52 void visitAnimateNode(const QSvgAnimateNode *node) override;
53 void visitEllipseNode(const QSvgEllipse *node) override;
54 void visitImageNode(const QSvgImage *node) override;
55 void visitLineNode(const QSvgLine *node) override;
56 void visitPathNode(const QSvgPath *node) override;
57 void visitPolygonNode(const QSvgPolygon *node) override;
58 void visitPolylineNode(const QSvgPolyline *node) override;
59 void visitRectNode(const QSvgRect *node) override;
60 void visitTextNode(const QSvgText *node) override;
61 void visitUseNode(const QSvgUse *node) override;
62 void visitVideoNode(const QSvgVideo *node) override;
63
64private:
65 const char *indent() { m_indent.fill(' ', m_indentLevel * 2); return m_indent.constData();}
66 void handleBaseNode(const QSvgNode *node);
67 QDebug &debug;
68 int m_indentLevel = 0;
69 QByteArray m_indent;
70 int nodeCounter = 0;
71};
72
73void SvgDebugVisitor::handleBaseNode(const QSvgNode *node)
74{
75 debug << indent() << typeName(node) << "node, ID:" << node->nodeId();
76 nodeCounter++;
77}
78
79void SvgDebugVisitor::visitNode(const QSvgNode *node)
80{
81 handleBaseNode(node);
82 debug << Qt::endl;
83}
84
85bool SvgDebugVisitor::visitStructureNodeStart(const QSvgStructureNode *node)
86{
87 debug << indent() << "START node" << node->nodeId() << "type" << typeName(node) << node->type() << Qt::endl;
88 m_indentLevel++;
89 return true;
90}
91
92void SvgDebugVisitor::visitStructureNodeEnd(const QSvgStructureNode *node)
93{
94 m_indentLevel--;
95 debug << indent() << "END node" << node->nodeId() << Qt::endl;
96}
97
98void SvgDebugVisitor::visitAnimateNode(const QSvgAnimateNode *node)
99{
100 handleBaseNode(node);
101 debug << Qt::endl;
102}
103
104void SvgDebugVisitor::visitEllipseNode(const QSvgEllipse *node)
105{
106 handleBaseNode(node);
107 debug << "rect:" << node->rect() << Qt::endl;
108}
109
110void SvgDebugVisitor::visitImageNode(const QSvgImage *node)
111{
112 handleBaseNode(node);
113 debug << "image:" << node->image() << Qt::endl;
114}
115
116void SvgDebugVisitor::visitLineNode(const QSvgLine *node)
117{
118 handleBaseNode(node);
119 debug << "line:" << node->line() << Qt::endl;
120}
121
122void SvgDebugVisitor::visitPathNode(const QSvgPath *node)
123{
124 handleBaseNode(node);
125 debug << "path:" << node->path().elementCount() << "elements." << Qt::endl;
126}
127
128void SvgDebugVisitor::visitPolygonNode(const QSvgPolygon *node)
129{
130 handleBaseNode(node);
131 debug << "polygon:" << node->polygon().size() << "elements." << Qt::endl;
132}
133
134void SvgDebugVisitor::visitPolylineNode(const QSvgPolyline *node)
135{
136 handleBaseNode(node);
137 debug << "polygon:" << node->polygon().size() << "elements." << Qt::endl;
138}
139
140void SvgDebugVisitor::visitRectNode(const QSvgRect *node)
141{
142 handleBaseNode(node);
143 debug << "rect:" << node->rect() << "radius:" << node->radius() << Qt::endl;
144}
145
146void SvgDebugVisitor::visitTextNode(const QSvgText *node)
147{
148 handleBaseNode(node);
149 QString text;
150 for (const auto *tspan : node->tspans()) {
151 if (!tspan)
152 text += QStringLiteral("\\n");
153 else
154 text += tspan->text();
155 }
156 debug << "text:" << text << Qt::endl;
157}
158
159void SvgDebugVisitor::visitUseNode(const QSvgUse *node)
160{
161 handleBaseNode(node);
162 debug << "link ID:" << node->linkId() << Qt::endl;
163}
164
165void SvgDebugVisitor::visitVideoNode(const QSvgVideo *node)
166{
167 handleBaseNode(node);
168 debug << Qt::endl;
169}
170
171void SvgDebugVisitor::write(const QSvgDocument *doc)
172{
173 debug << "SVG" << doc->size() << "viewBox" << doc->viewBox() << Qt::endl;
174 traverse(doc);
175
176 debug << "END SVG" << nodeCounter << "nodes";
177}
178
179QDebug operator<<(QDebug debug, const QSvgDocument &doc)
180{
181 SvgDebugVisitor visitor(debug);
182 visitor.write(&doc);
183
184 return debug;
185}
186
187QT_END_NAMESPACE
bool visitStructureNodeStart(const QSvgStructureNode *node) override
Definition qsvgdebug.cpp:85
void visitLineNode(const QSvgLine *node) override
void visitStructureNodeEnd(const QSvgStructureNode *) override
Definition qsvgdebug.cpp:92
void visitAnimateNode(const QSvgAnimateNode *node) override
Definition qsvgdebug.cpp:98
void visitPathNode(const QSvgPath *node) override
void write(const QSvgDocument *doc)
void visitEllipseNode(const QSvgEllipse *node) override
void visitRectNode(const QSvgRect *node) override
void visitVideoNode(const QSvgVideo *node) override
void visitTextNode(const QSvgText *node) override
SvgDebugVisitor(QDebug &stream)
Definition qsvgdebug.cpp:45
void visitPolygonNode(const QSvgPolygon *node) override
void visitImageNode(const QSvgImage *node) override
void visitPolylineNode(const QSvgPolyline *node) override
void visitNode(const QSvgNode *) override
Definition qsvgdebug.cpp:79
void visitUseNode(const QSvgUse *node) override
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static const char * typeName(const QSvgNode *node)
Definition qsvgdebug.cpp:34
static QT_BEGIN_NAMESPACE const char * nodeTypeStrings[]
Definition qsvgdebug.cpp:11