Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#include "qgraphicssvgitem.h"
4
5#if !defined(QT_NO_GRAPHICSVIEW)
6
7#include "qpainter.h"
8#include "qstyleoption.h"
9#include "qsvgrenderer.h"
10#include "qdebug.h"
11
12#include <QtCore/private/qobject_p.h>
13#include <QtWidgets/private/qgraphicsitem_p.h>
14
16
18{
19public:
20 Q_DECLARE_PUBLIC(QGraphicsSvgItem)
21
26
28 {
30 q->setParentItem(parent);
31 renderer = new QSvgRenderer(q);
32 QObject::connect(renderer, SIGNAL(repaintNeeded()),
35 q->setMaximumCacheSize(QSize(1024, 768));
36 }
37
39 {
40 q_func()->update();
41 }
42
43 inline void updateDefaultSize()
44 {
45 QRectF bounds;
46 if (elemId.isEmpty()) {
47 bounds = QRectF(QPointF(0, 0), renderer->defaultSize());
48 } else {
50 }
51 if (boundingRect.size() != bounds.size()) {
52 q_func()->prepareGeometryChange();
53 boundingRect.setSize(bounds.size());
54 }
55 }
56
59 bool shared;
61};
62
109
116{
117 Q_D(QGraphicsSvgItem);
118 d->init(parent);
119 d->renderer->load(fileName);
120 d->updateDefaultSize();
121}
122
127{
128 return d_func()->renderer;
129}
130
131
136{
137 Q_D(const QGraphicsSvgItem);
138 return d->boundingRect;
139}
140
141// from qgraphicsitem.cpp
144
150{
151// Q_UNUSED(option);
153
154 Q_D(QGraphicsSvgItem);
155 if (!d->renderer->isValid())
156 return;
157
158 if (d->elemId.isEmpty())
159 d->renderer->render(painter, d->boundingRect);
160 else
161 d->renderer->render(painter, d->elemId, d->boundingRect);
162
163 if (option->state & QStyle::State_Selected)
165}
166
171{
172 return Type;
173}
174
202
220
232{
233 Q_D(QGraphicsSvgItem);
234 d->elemId = id;
235 d->updateDefaultSize();
236 update();
237}
238
245{
246 Q_D(const QGraphicsSvgItem);
247 return d->elemId;
248}
249
258{
259 Q_D(QGraphicsSvgItem);
260 if (!d->shared)
261 delete d->renderer;
262
263 d->renderer = renderer;
264 d->shared = true;
265
266 d->updateDefaultSize();
267
268 update();
269}
270
281
291
293
294#include "moc_qgraphicssvgitem.cpp"
295
296#endif // QT_NO_GRAPHICSVIEW
QVariant extra(Extra type) const
void setExtra(Extra type, const QVariant &value)
QGraphicsItem * parent
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
QScopedPointer< QGraphicsItemPrivate > d_ptr
void update(const QRectF &rect=QRectF())
Schedules a redraw of the area covered by rect in this item.
CacheMode cacheMode() const
void setCacheMode(CacheMode mode, const QSize &cacheSize=QSize())
The QGraphicsObject class provides a base class for all graphics items that require signals,...
QGraphicsObject * parent
the parent of the item
void init(QGraphicsItem *parent)
\inmodule QtSvgWidgets
void setElementId(const QString &id)
Sets the XML ID of the element to id.
bool isCachingEnabled() const
QSvgRenderer * renderer() const
Returns the currently use QSvgRenderer.
QGraphicsSvgItem(QGraphicsItem *parentItem=nullptr)
Constructs a new SVG item with the given parent.
void setMaximumCacheSize(const QSize &size)
Sets the maximum device coordinate cache size of the item to size.
void setSharedRenderer(QSvgRenderer *renderer)
Sets renderer to be a shared QSvgRenderer on the item.
QRectF boundingRect() const override
Returns the bounding rectangle of this item.
int type() const override
\reimp
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) override
\reimp
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
QRectF boundingRect(const QRectF &rect, int flags, const QString &text)
Returns the bounding rectangle of the text as it will appear when drawn inside the given rectangle wi...
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr void setSize(const QSizeF &s) noexcept
Sets the size of the rectangle to the given finite size.
Definition qrect.h:824
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:735
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
@ State_Selected
Definition qstyle.h:82
\inmodule QtSvg
QRectF boundsOnElement(const QString &id) const
QSize defaultSize() const
Returns the default size of the document contents.
QSize toSize() const
Returns the variant as a QSize if the variant has userType() \l QMetaType::QSize; otherwise returns a...
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QOpenGLWidget * widget
[1]
Combined button and popup list for selecting options.
void Q_WIDGETS_EXPORT qt_graphicsItem_highlightSelected(QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option)
void Q_WIDGETS_EXPORT qt_graphicsItem_highlightSelected(QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint id
[7]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint GLenum option
#define Q_UNUSED(x)
QGraphicsItem * item
QPainter painter(this)
[7]
QSvgRenderer * renderer
[0]