Qt 4.8
qgraphicssvgitem.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSvg module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "qgraphicssvgitem.h"
42 
43 #ifndef QT_NO_GRAPHICSSVGITEM
44 
45 #include "qpainter.h"
46 #include "qstyleoption.h"
47 #include "qsvgrenderer.h"
48 #include "qdebug.h"
49 
50 #include "private/qobject_p.h"
51 #include "private/qgraphicsitem_p.h"
52 
54 
56 {
57 public:
59 
61  : renderer(0), shared(false)
62  {
63  }
64 
66  {
68  q->setParentItem(parent);
69  renderer = new QSvgRenderer(q);
70  QObject::connect(renderer, SIGNAL(repaintNeeded()),
71  q, SLOT(_q_repaintItem()));
72  q->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
73  q->setMaximumCacheSize(QSize(1024, 768));
74  }
75 
77  {
78  q_func()->update();
79  }
80 
81  inline void updateDefaultSize()
82  {
83  QRectF bounds;
84  if (elemId.isEmpty()) {
85  bounds = QRectF(QPointF(0, 0), renderer->defaultSize());
86  } else {
87  bounds = renderer->boundsOnElement(elemId);
88  }
89  if (boundingRect.size() != bounds.size()) {
90  q_func()->prepareGeometryChange();
91  boundingRect.setSize(bounds.size());
92  }
93  }
94 
97  bool shared;
99 };
100 
145 {
147  d->init(parent);
148 }
149 
156 {
158  d->init(parent);
159  d->renderer->load(fileName);
160  d->updateDefaultSize();
161 }
162 
167 {
168  return d_func()->renderer;
169 }
170 
171 
176 {
177  Q_D(const QGraphicsSvgItem);
178  return d->boundingRect;
179 }
180 
192  QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option)
193 {
194  const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1));
195  if (qFuzzyIsNull(qMax(murect.width(), murect.height())))
196  return;
197 
198  const QRectF mbrect = painter->transform().mapRect(item->boundingRect());
199  if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
200  return;
201 
202  qreal itemPenWidth;
203  switch (item->type()) {
205  itemPenWidth = static_cast<QGraphicsEllipseItem *>(item)->pen().widthF();
206  break;
208  itemPenWidth = static_cast<QGraphicsPathItem *>(item)->pen().widthF();
209  break;
211  itemPenWidth = static_cast<QGraphicsPolygonItem *>(item)->pen().widthF();
212  break;
214  itemPenWidth = static_cast<QGraphicsRectItem *>(item)->pen().widthF();
215  break;
217  itemPenWidth = static_cast<QGraphicsSimpleTextItem *>(item)->pen().widthF();
218  break;
220  itemPenWidth = static_cast<QGraphicsLineItem *>(item)->pen().widthF();
221  break;
222  default:
223  itemPenWidth = 1.0;
224  }
225  const qreal pad = itemPenWidth / 2;
226 
227  const qreal penWidth = 0; // cosmetic pen
228 
229  const QColor fgcolor = option->palette.windowText().color();
230  const QColor bgcolor( // ensure good contrast against fgcolor
231  fgcolor.red() > 127 ? 0 : 255,
232  fgcolor.green() > 127 ? 0 : 255,
233  fgcolor.blue() > 127 ? 0 : 255);
234 
235  painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine));
236  painter->setBrush(Qt::NoBrush);
237  painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
238 
239  painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine));
240  painter->setBrush(Qt::NoBrush);
241  painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
242 }
243 
248  QWidget *widget)
249 {
250 // Q_UNUSED(option);
251  Q_UNUSED(widget);
252 
254  if (!d->renderer->isValid())
255  return;
256 
257  if (d->elemId.isEmpty())
258  d->renderer->render(painter, d->boundingRect);
259  else
260  d->renderer->render(painter, d->elemId, d->boundingRect);
261 
262  if (option->state & QStyle::State_Selected)
263  qt_graphicsItem_highlightSelected(this, painter, option);
264 }
265 
270 {
271  return Type;
272 }
273 
300 {
302  update();
303 }
304 
319 {
321 }
322 
337 {
339  d->elemId = id;
340  d->updateDefaultSize();
341  update();
342 }
343 
350 {
351  Q_D(const QGraphicsSvgItem);
352  return d->elemId;
353 }
354 
363 {
365  if (!d->shared)
366  delete d->renderer;
367 
368  d->renderer = renderer;
369  d->shared = true;
370 
371  d->updateDefaultSize();
372 
373  update();
374 }
375 
386 {
388 }
389 
399 {
400  return cacheMode() != QGraphicsItem::NoCache;
401 }
402 
404 
405 #include "moc_qgraphicssvgitem.cpp"
406 
407 #endif // QT_NO_GRAPHICSSVGITEM
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
void setMaximumCacheSize(const QSize &size)
Sets the maximum device coordinate cache size of the item to size.
QGraphicsItem * parent
QGraphicsSvgItem(QGraphicsItem *parentItem=0)
Constructs a new SVG item with the given parent.
QSvgRenderer * renderer() const
Returns the currently use QSvgRenderer.
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
const QTransform & transform() const
Returns the world transformation matrix.
Definition: qpainter.cpp:9558
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
QPointer< QWidget > widget
QScopedPointer< QGraphicsItemPrivate > d_ptr
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
virtual QRectF boundingRect() const =0
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
virtual QRectF boundingRect() const
Returns the bounding rectangle of this item.
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
The QString class provides a Unicode character string.
Definition: qstring.h:83
void update(const QRectF &rect=QRectF())
Schedules a redraw of the area covered by rect in this item.
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
#define Q_D(Class)
Definition: qglobal.h:2482
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QRect mapRect(const QRect &) const
Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate ...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setSharedRenderer(QSvgRenderer *renderer)
Sets renderer to be a shared QSvgRenderer on the item.
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void init(QGraphicsItem *parent)
void setCacheMode(CacheMode mode, const QSize &cacheSize=QSize())
Sets the item&#39;s cache mode to mode.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void setElementId(const QString &id)
Sets the XML ID of the element to id.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
virtual int type() const
Returns the type of an item as an int.
virtual int type() const
Reimplemented Function
QSize defaultSize() const
Returns the default size of the document contents.
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition: qrect.h:781
QVariant extra(Extra type) const
QSize toSize() const
Returns the variant as a QSize if the variant has type() Size ; otherwise returns an invalid QSize...
Definition: qvariant.cpp:2432
void setSize(const QSizeF &s)
Sets the size of the rectangle to the given size.
Definition: qrect.h:790
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
const QBrush & windowText() const
Returns the window text (general foreground) brush of the current color group.
Definition: qpalette.h:124
void setCachingEnabled(bool)
Use QGraphicsItem::setCacheMode() instead.
The QGraphicsSimpleTextItem class provides a simple text path item that you can add to a QGraphicsSce...
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
The QSvgRenderer class is used to draw the contents of SVG files onto paint devices.
Definition: qsvgrenderer.h:64
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented Function
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void setBrush(const QBrush &brush)
Sets the painter&#39;s brush to the given brush.
Definition: qpainter.cpp:4171
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
void setExtra(Extra type, const QVariant &value)
QSizeF size() const
Returns the size of the rectangle.
Definition: qrect.h:713
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition: qpainter.h:650
bool isCachingEnabled() const
Use QGraphicsItem::cacheMode() instead.
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QGraphicsSvgItem class is a QGraphicsItem that can be used to render the contents of SVG files...
QSize maximumCacheSize() const
QString elementId() const
The QGraphicsObject class provides a base class for all graphics items that require signals...
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
Definition: qstyleoption.h:867
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
static QString fileName(const QString &fileUrl)
static void qt_graphicsItem_highlightSelected(QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option)
Highlights item as selected.
CacheMode cacheMode() const
Returns the cache mode for this item.
QRectF boundsOnElement(const QString &id) const
Returns bounding rectangle of the item with the given id.
The QGraphicsPathItem class provides a path item that you can add to a QGraphicsScene.