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