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
qgraphicssvgitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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
6
7#if !defined(QT_NO_GRAPHICSVIEW)
8
9#include "qpainter.h"
10#include "qstyleoption.h"
11#include "qsvgrenderer.h"
12#include "qdebug.h"
13
14#include <QtCore/private/qobject_p.h>
15#include <QtWidgets/private/qgraphicsitem_p.h>
16
18
20{
21public:
22 Q_DECLARE_PUBLIC(QGraphicsSvgItem)
23
25
26 void init(QGraphicsItem *parent)
27 {
28 Q_Q(QGraphicsSvgItem);
29 q->setParentItem(parent);
30 renderer = new QSvgRenderer(q);
31 QObject::connect(renderer, SIGNAL(repaintNeeded()),
32 q, SLOT(_q_repaintItem()));
33 q->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
34 q->setMaximumCacheSize(QSize(1024, 768));
35 }
36
38 {
39 q_func()->update();
40 }
41
42 inline void updateDefaultSize()
43 {
44 QRectF bounds;
45 if (elemId.isEmpty()) {
46 bounds = QRectF(QPointF(0, 0), renderer->defaultSize());
47 } else {
48 bounds = renderer->boundsOnElement(elemId);
49 }
50 if (boundingRect.size() != bounds.size()) {
51 q_func()->prepareGeometryChange();
52 boundingRect.setSize(bounds.size());
53 }
54 }
55
56 QSvgRenderer *renderer{nullptr};
58 bool shared{false};
60};
61
62/*!
63 \class QGraphicsSvgItem
64 \inmodule QtSvgWidgets
65 \ingroup graphicsview-api
66 \brief The QGraphicsSvgItem class is a QGraphicsItem that can be used to render
67 the contents of SVG files.
68
69 \since 4.2
70
71 QGraphicsSvgItem provides a way of rendering SVG files onto QGraphicsView.
72 QGraphicsSvgItem can be created by passing the SVG file to be rendered to
73 its constructor or by explicit setting a shared QSvgRenderer on it.
74
75 Note that setting QSvgRenderer on a QGraphicsSvgItem doesn't make the item take
76 ownership of the renderer, therefore if using setSharedRenderer() method one has
77 to make sure that the lifetime of the QSvgRenderer object will be at least as long
78 as that of the QGraphicsSvgItem.
79
80 QGraphicsSvgItem provides a way of rendering only parts of the SVG files via
81 the setElementId. If setElementId() method is called, only the SVG element
82 (and its children) with the passed id will be renderer. This provides a convenient
83 way of selectively rendering large SVG files that contain a number of discrete
84 elements. For example the following code renders only jokers from a SVG file
85 containing a whole card deck:
86
87 \snippet src_svg_qgraphicssvgitem.cpp 0
88
89 Size of the item can be set via direct manipulation of the items
90 transformation matrix.
91
92 By default the SVG rendering is cached using QGraphicsItem::DeviceCoordinateCache
93 mode to speedup the display of items. Caching can be disabled by passing
94 QGraphicsItem::NoCache to the QGraphicsItem::setCacheMode() method.
95
96 \sa QSvgWidget, {Qt SVG C++ Classes}, QGraphicsItem, QGraphicsView
97*/
98
99/*!
100 Constructs a new SVG item with the given \a parent.
101*/
102QGraphicsSvgItem::QGraphicsSvgItem(QGraphicsItem *parent)
103 : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0)
104{
105 Q_D(QGraphicsSvgItem);
106 d->init(parent);
107}
108
109/*!
110 Constructs a new item with the given \a parent and loads the contents of the
111 SVG file with the specified \a fileName.
112*/
113QGraphicsSvgItem::QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent)
114 : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0)
115{
116 Q_D(QGraphicsSvgItem);
117 d->init(parent);
118 d->renderer->load(fileName);
119 d->updateDefaultSize();
120}
121
122QGraphicsSvgItem::~QGraphicsSvgItem()
123{
124 // must be empty until Qt 7 (used to be inline)
125}
126
127/*!
128 Returns the currently use QSvgRenderer.
129*/
130QSvgRenderer *QGraphicsSvgItem::renderer() const
131{
132 return d_func()->renderer;
133}
134
135
136/*!
137 Returns the bounding rectangle of this item.
138*/
139QRectF QGraphicsSvgItem::boundingRect() const
140{
141 Q_D(const QGraphicsSvgItem);
142 return d->boundingRect;
143}
144
145// from qgraphicsitem.cpp
146void Q_WIDGETS_EXPORT qt_graphicsItem_highlightSelected(QGraphicsItem *item, QPainter *painter,
147 const QStyleOptionGraphicsItem *option);
148
149/*!
150 \reimp
151*/
152void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
153 QWidget *widget)
154{
155// Q_UNUSED(option);
156 Q_UNUSED(widget);
157
158 Q_D(QGraphicsSvgItem);
159 if (!d->renderer->isValid())
160 return;
161
162 if (d->elemId.isEmpty())
163 d->renderer->render(painter, d->boundingRect);
164 else
165 d->renderer->render(painter, d->elemId, d->boundingRect);
166
167 if (option->state & QStyle::State_Selected)
168 qt_graphicsItem_highlightSelected(this, painter, option);
169}
170
171/*!
172 \reimp
173*/
174int QGraphicsSvgItem::type() const
175{
176 return Type;
177}
178
179/*!
180 \property QGraphicsSvgItem::maximumCacheSize
181 \since 4.6
182
183 This property holds the maximum size of the device coordinate cache
184 for this item.
185 */
186
187/*!
188 Sets the maximum device coordinate cache size of the item to \a size.
189 If the item is cached using QGraphicsItem::DeviceCoordinateCache mode,
190 caching is bypassed if the extension of the item in device coordinates
191 is larger than \a size.
192
193 The cache corresponds to the QPixmap which is used to cache the
194 results of the rendering.
195 Use QPixmapCache::setCacheLimit() to set limitations on the whole cache
196 and use setMaximumCacheSize() when setting cache size for individual
197 items.
198
199 \sa QGraphicsItem::cacheMode()
200*/
201void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size)
202{
203 QGraphicsItem::d_ptr->setExtra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize, size);
204 update();
205}
206
207/*!
208 Returns the current maximum size of the device coordinate cache for this item.
209 If the item is cached using QGraphicsItem::DeviceCoordinateCache mode,
210 caching is bypassed if the extension of the item in device coordinates
211 is larger than the maximum size.
212
213 The default maximum cache size is 1024x768.
214 QPixmapCache::cacheLimit() gives the
215 cumulative bounds of the whole cache, whereas maximumCacheSize() refers
216 to a maximum cache size for this particular item.
217
218 \sa QGraphicsItem::cacheMode()
219*/
220QSize QGraphicsSvgItem::maximumCacheSize() const
221{
222 return QGraphicsItem::d_ptr->extra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize).toSize();
223}
224
225/*!
226 \property QGraphicsSvgItem::elementId
227 \since 4.6
228
229 This property holds the element's XML ID.
230 */
231
232/*!
233 Sets the XML ID of the element to \a id.
234*/
235void QGraphicsSvgItem::setElementId(const QString &id)
236{
237 Q_D(QGraphicsSvgItem);
238 d->elemId = id;
239 d->updateDefaultSize();
240 update();
241}
242
243/*!
244 Returns the XML ID the element that is currently
245 being rendered. Returns an empty string if the whole
246 file is being rendered.
247*/
248QString QGraphicsSvgItem::elementId() const
249{
250 Q_D(const QGraphicsSvgItem);
251 return d->elemId;
252}
253
254/*!
255 Sets \a renderer to be a shared QSvgRenderer on the item. By
256 using this method one can share the same QSvgRenderer on a number
257 of items. This means that the SVG file will be parsed only once.
258 QSvgRenderer passed to this method has to exist for as long as
259 this item is used.
260*/
261void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer)
262{
263 Q_D(QGraphicsSvgItem);
264 if (!d->shared)
265 delete d->renderer;
266
267 d->renderer = renderer;
268 d->shared = true;
269
270 d->updateDefaultSize();
271
272 update();
273}
274
275/*!
276 \deprecated
277
278 Use QGraphicsItem::setCacheMode() instead. Passing true to this function is equivalent
279 to QGraphicsItem::setCacheMode(QGraphicsItem::DeviceCoordinateCache).
280*/
281void QGraphicsSvgItem::setCachingEnabled(bool caching)
282{
283 setCacheMode(caching ? QGraphicsItem::DeviceCoordinateCache : QGraphicsItem::NoCache);
284}
285
286/*!
287 \deprecated
288
289 Use QGraphicsItem::cacheMode() instead.
290*/
291bool QGraphicsSvgItem::isCachingEnabled() const
292{
293 return cacheMode() != QGraphicsItem::NoCache;
294}
295
296QT_END_NAMESPACE
297
298#include "moc_qgraphicssvgitem.cpp"
299
300#endif // QT_NO_GRAPHICSVIEW
void init(QGraphicsItem *parent)
Combined button and popup list for selecting options.