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
125/*!
126 Returns the currently use QSvgRenderer.
127*/
128QSvgRenderer *QGraphicsSvgItem::renderer() const
129{
130 return d_func()->renderer;
131}
132
133
134/*!
135 Returns the bounding rectangle of this item.
136*/
137QRectF QGraphicsSvgItem::boundingRect() const
138{
139 Q_D(const QGraphicsSvgItem);
140 return d->boundingRect;
141}
142
143// from qgraphicsitem.cpp
144void Q_WIDGETS_EXPORT qt_graphicsItem_highlightSelected(QGraphicsItem *item, QPainter *painter,
145 const QStyleOptionGraphicsItem *option);
146
147/*!
148 \reimp
149*/
150void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
151 QWidget *widget)
152{
153// Q_UNUSED(option);
154 Q_UNUSED(widget);
155
156 Q_D(QGraphicsSvgItem);
157 if (!d->renderer->isValid())
158 return;
159
160 if (d->elemId.isEmpty())
161 d->renderer->render(painter, d->boundingRect);
162 else
163 d->renderer->render(painter, d->elemId, d->boundingRect);
164
165 if (option->state & QStyle::State_Selected)
166 qt_graphicsItem_highlightSelected(this, painter, option);
167}
168
169/*!
170 \reimp
171*/
172int QGraphicsSvgItem::type() const
173{
174 return Type;
175}
176
177/*!
178 \property QGraphicsSvgItem::maximumCacheSize
179 \since 4.6
180
181 This property holds the maximum size of the device coordinate cache
182 for this item.
183 */
184
185/*!
186 Sets the maximum device coordinate cache size of the item to \a size.
187 If the item is cached using QGraphicsItem::DeviceCoordinateCache mode,
188 caching is bypassed if the extension of the item in device coordinates
189 is larger than \a size.
190
191 The cache corresponds to the QPixmap which is used to cache the
192 results of the rendering.
193 Use QPixmapCache::setCacheLimit() to set limitations on the whole cache
194 and use setMaximumCacheSize() when setting cache size for individual
195 items.
196
197 \sa QGraphicsItem::cacheMode()
198*/
199void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size)
200{
201 QGraphicsItem::d_ptr->setExtra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize, size);
202 update();
203}
204
205/*!
206 Returns the current maximum size of the device coordinate cache for this item.
207 If the item is cached using QGraphicsItem::DeviceCoordinateCache mode,
208 caching is bypassed if the extension of the item in device coordinates
209 is larger than the maximum size.
210
211 The default maximum cache size is 1024x768.
212 QPixmapCache::cacheLimit() gives the
213 cumulative bounds of the whole cache, whereas maximumCacheSize() refers
214 to a maximum cache size for this particular item.
215
216 \sa QGraphicsItem::cacheMode()
217*/
218QSize QGraphicsSvgItem::maximumCacheSize() const
219{
220 return QGraphicsItem::d_ptr->extra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize).toSize();
221}
222
223/*!
224 \property QGraphicsSvgItem::elementId
225 \since 4.6
226
227 This property holds the element's XML ID.
228 */
229
230/*!
231 Sets the XML ID of the element to \a id.
232*/
233void QGraphicsSvgItem::setElementId(const QString &id)
234{
235 Q_D(QGraphicsSvgItem);
236 d->elemId = id;
237 d->updateDefaultSize();
238 update();
239}
240
241/*!
242 Returns the XML ID the element that is currently
243 being rendered. Returns an empty string if the whole
244 file is being rendered.
245*/
246QString QGraphicsSvgItem::elementId() const
247{
248 Q_D(const QGraphicsSvgItem);
249 return d->elemId;
250}
251
252/*!
253 Sets \a renderer to be a shared QSvgRenderer on the item. By
254 using this method one can share the same QSvgRenderer on a number
255 of items. This means that the SVG file will be parsed only once.
256 QSvgRenderer passed to this method has to exist for as long as
257 this item is used.
258*/
259void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer)
260{
261 Q_D(QGraphicsSvgItem);
262 if (!d->shared)
263 delete d->renderer;
264
265 d->renderer = renderer;
266 d->shared = true;
267
268 d->updateDefaultSize();
269
270 update();
271}
272
273/*!
274 \deprecated
275
276 Use QGraphicsItem::setCacheMode() instead. Passing true to this function is equivalent
277 to QGraphicsItem::setCacheMode(QGraphicsItem::DeviceCoordinateCache).
278*/
279void QGraphicsSvgItem::setCachingEnabled(bool caching)
280{
281 setCacheMode(caching ? QGraphicsItem::DeviceCoordinateCache : QGraphicsItem::NoCache);
282}
283
284/*!
285 \deprecated
286
287 Use QGraphicsItem::cacheMode() instead.
288*/
289bool QGraphicsSvgItem::isCachingEnabled() const
290{
291 return cacheMode() != QGraphicsItem::NoCache;
292}
293
294QT_END_NAMESPACE
295
296#include "moc_qgraphicssvgitem.cpp"
297
298#endif // QT_NO_GRAPHICSVIEW
Combined button and popup list for selecting options.