Qt 4.8
qsvgnode.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 
42 #include "qsvgnode_p.h"
43 #include "qsvgtinydocument_p.h"
44 
45 #ifndef QT_NO_SVG
46 
47 #include "qdebug.h"
48 #include "qstack.h"
49 
51 
53  : m_parent(parent),
54  m_visible(true),
55  m_displayMode(BlockMode)
56 {
57 }
58 
60 {
61 
62 }
63 
65 {
66  //qDebug()<<"appending "<<prop->type()<< " ("<< id <<") "<<"to "<<this<<this->type();
67  QSvgTinyDocument *doc;
68  switch (prop->type()) {
70  m_style.quality = static_cast<QSvgQualityStyle*>(prop);
71  break;
73  m_style.fill = static_cast<QSvgFillStyle*>(prop);
74  break;
76  m_style.viewportFill = static_cast<QSvgViewportFillStyle*>(prop);
77  break;
79  m_style.font = static_cast<QSvgFontStyle*>(prop);
80  break;
82  m_style.stroke = static_cast<QSvgStrokeStyle*>(prop);
83  break;
85  m_style.solidColor = static_cast<QSvgSolidColorStyle*>(prop);
86  doc = document();
87  if (doc && !id.isEmpty())
89  break;
91  m_style.gradient = static_cast<QSvgGradientStyle*>(prop);
92  doc = document();
93  if (doc && !id.isEmpty())
94  doc->addNamedStyle(id, m_style.gradient);
95  break;
97  m_style.transform = static_cast<QSvgTransformStyle*>(prop);
98  break;
100  m_style.animateColor = static_cast<QSvgAnimateColor*>(prop);
101  break;
104  static_cast<QSvgAnimateTransform*>(prop));
105  break;
107  m_style.opacity = static_cast<QSvgOpacityStyle*>(prop);
108  break;
110  m_style.compop = static_cast<QSvgCompOpStyle*>(prop);
111  break;
112  default:
113  qDebug("QSvgNode: Trying to append unknown property!");
114  break;
115  }
116 }
117 
119 {
120  m_style.apply(p, this, states);
121 }
122 
124 {
125  m_style.revert(p, states);
126 }
127 
129 {
130  const QSvgNode *node = this;
131  while (node) {
132  switch (type) {
134  if (node->m_style.quality)
135  return node->m_style.quality;
136  break;
138  if (node->m_style.fill)
139  return node->m_style.fill;
140  break;
142  if (m_style.viewportFill)
143  return node->m_style.viewportFill;
144  break;
146  if (node->m_style.font)
147  return node->m_style.font;
148  break;
150  if (node->m_style.stroke)
151  return node->m_style.stroke;
152  break;
154  if (node->m_style.solidColor)
155  return node->m_style.solidColor;
156  break;
158  if (node->m_style.gradient)
159  return node->m_style.gradient;
160  break;
162  if (node->m_style.transform)
163  return node->m_style.transform;
164  break;
166  if (node->m_style.animateColor)
167  return node->m_style.animateColor;
168  break;
170  if (!node->m_style.animateTransforms.isEmpty())
171  return node->m_style.animateTransforms.first();
172  break;
174  if (node->m_style.opacity)
175  return node->m_style.opacity;
176  break;
178  if (node->m_style.compop)
179  return node->m_style.compop;
180  break;
181  default:
182  break;
183  }
184  node = node->parent();
185  }
186 
187  return 0;
188 }
189 
191 {
192  QString rid = id;
193  if (rid.startsWith(QLatin1Char('#')))
194  rid.remove(0, 1);
195  QSvgTinyDocument *doc = document();
196  return doc ? doc->namedStyle(rid) : 0;
197 }
198 
200 {
201  return QRectF(0, 0, 0, 0);
202 }
203 
205 {
206  if (!m_cachedBounds.isEmpty())
207  return m_cachedBounds;
208 
209  QImage dummy(1, 1, QImage::Format_RGB32);
210  QPainter p(&dummy);
211  QSvgExtraStates states;
212 
214  pen.setMiterLimit(4);
215  p.setPen(pen);
216 
217  QStack<QSvgNode*> parentApplyStack;
219  while (parent) {
220  parentApplyStack.push(parent);
221  parent = parent->parent();
222  }
223 
224  for (int i = parentApplyStack.size() - 1; i >= 0; --i)
225  parentApplyStack[i]->applyStyle(&p, states);
226 
228 
229  m_cachedBounds = transformedBounds(&p, states);
230  return m_cachedBounds;
231 }
232 
234 {
235  QSvgTinyDocument *doc = 0;
236  QSvgNode *node = const_cast<QSvgNode*>(this);
237  while (node && node->type() != QSvgNode::DOC) {
238  node = node->parent();
239  }
240  doc = static_cast<QSvgTinyDocument*>(node);
241 
242  return doc;
243 }
244 
246 {
247  m_requiredFeatures = lst;
248 }
249 
251 {
252  return m_requiredFeatures;
253 }
254 
256 {
257  m_requiredExtensions = lst;
258 }
259 
261 {
262  return m_requiredExtensions;
263 }
264 
266 {
267  m_requiredLanguages = lst;
268 }
269 
271 {
272  return m_requiredLanguages;
273 }
274 
276 {
277  m_requiredFormats = lst;
278 }
279 
281 {
282  return m_requiredFormats;
283 }
284 
286 {
287  m_requiredFonts = lst;
288 }
289 
291 {
292  return m_requiredFonts;
293 }
294 
295 void QSvgNode::setVisible(bool visible)
296 {
297  //propagate visibility change of true to the parent
298  //not propagating false is just a small performance
299  //degradation since we'll iterate over children without
300  //drawing any of them
301  if (m_parent && visible && !m_parent->isVisible())
302  m_parent->setVisible(true);
303 
304  m_visible = visible;
305 }
306 
308 {
309  applyStyle(p, states);
310  QRectF rect = bounds(p, states);
311  revertStyle(p, states);
312  return rect;
313 }
314 
316 {
317  m_id = i;
318 }
319 
321 {
322  m_class = str;
323 }
324 
326 {
327  m_displayMode = mode;
328 }
329 
331 {
332  return m_displayMode;
333 }
334 
336 {
337  QPen pen = p->pen();
338  if (pen.style() == Qt::NoPen || pen.brush().style() == Qt::NoBrush || pen.isCosmetic())
339  return 0;
340  return pen.widthF();
341 }
342 
344 
345 #endif // QT_NO_SVG
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
virtual Type type() const =0
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void appendStyleProperty(QSvgStyleProperty *prop, const QString &id)
Definition: qsvgnode.cpp:64
QSvgStyle m_style
Definition: qsvgnode_p.h:160
QSvgRefCounter< QSvgGradientStyle > gradient
Definition: qsvgstyle_p.h:777
const QStringList & requiredFormats() const
Definition: qsvgnode.cpp:280
void apply(QPainter *p, const QSvgNode *node, QSvgExtraStates &states)
Definition: qsvgstyle.cpp:524
void setRequiredExtensions(const QStringList &lst)
Definition: qsvgnode.cpp:255
const QStringList & requiredExtensions() const
Definition: qsvgnode.cpp:260
QSvgRefCounter< QSvgViewportFillStyle > viewportFill
Definition: qsvgstyle_p.h:773
Qt::PenStyle style() const
Returns the pen style.
Definition: qpen.cpp:428
QRectF transformedBounds() const
Definition: qsvgnode.cpp:204
void addNamedStyle(const QString &id, QSvgFillStyleProperty *style)
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
QStringList m_requiredFormats
Definition: qsvgnode_p.h:169
void setXmlClass(const QString &str)
Definition: qsvgnode.cpp:320
DisplayMode m_displayMode
Definition: qsvgnode_p.h:177
void applyStyle(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:118
QSvgNode * parent() const
Definition: qsvgnode_p.h:183
The QString class provides a Unicode character string.
Definition: qstring.h:83
QSvgNode(QSvgNode *parent=0)
Definition: qsvgnode.cpp:52
QStringList m_requiredLanguages
Definition: qsvgnode_p.h:168
QList< QSvgRefCounter< QSvgAnimateTransform > > animateTransforms
Definition: qsvgstyle_p.h:780
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
void revert(QPainter *p, QSvgExtraStates &states)
Definition: qsvgstyle.cpp:588
void setRequiredLanguages(const QStringList &lst)
Definition: qsvgnode.cpp:265
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
const QStringList & requiredFeatures() const
Definition: qsvgnode.cpp:250
virtual QRectF bounds(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:199
Q_CORE_EXPORT void qDebug(const char *,...)
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QSvgRefCounter< QSvgOpacityStyle > opacity
Definition: qsvgstyle_p.h:781
bool isVisible() const
Definition: qsvgnode_p.h:188
const QPen & pen() const
Returns the painter&#39;s current pen.
Definition: qpainter.cpp:4152
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QSvgRefCounter< QSvgCompOpStyle > compop
Definition: qsvgstyle_p.h:782
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
static bool isEmpty(const char *str)
QStringList m_requiredFeatures
Definition: qsvgnode_p.h:166
bool isCosmetic() const
Returns true if the pen is cosmetic; otherwise returns false.
Definition: qpen.cpp:840
QSvgRefCounter< QSvgTransformStyle > transform
Definition: qsvgstyle_p.h:778
QSvgNode * m_parent
Definition: qsvgnode_p.h:164
QSvgFillStyleProperty * namedStyle(const QString &id) const
DisplayMode displayMode() const
Definition: qsvgnode.cpp:330
bool m_visible
Definition: qsvgnode_p.h:172
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setRequiredFormats(const QStringList &lst)
Definition: qsvgnode.cpp:275
void setRequiredFeatures(const QStringList &lst)
Definition: qsvgnode.cpp:245
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
QSvgRefCounter< QSvgQualityStyle > quality
Definition: qsvgstyle_p.h:771
QBrush brush() const
Returns the brush used to fill strokes generated with this pen.
Definition: qpen.cpp:797
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182
static qreal strokeWidth(QPainter *p)
Definition: qsvgnode.cpp:335
QSvgRefCounter< QSvgFillStyle > fill
Definition: qsvgstyle_p.h:772
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
QSvgRefCounter< QSvgAnimateColor > animateColor
Definition: qsvgstyle_p.h:779
QString m_class
Definition: qsvgnode_p.h:175
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
const QStringList & requiredFonts() const
Definition: qsvgnode.cpp:290
virtual ~QSvgNode()
Definition: qsvgnode.cpp:59
QSvgRefCounter< QSvgStrokeStyle > stroke
Definition: qsvgstyle_p.h:775
QSvgRefCounter< QSvgFontStyle > font
Definition: qsvgstyle_p.h:774
QSvgStyleProperty * styleProperty(QSvgStyleProperty::Type type) const
Definition: qsvgnode.cpp:128
virtual Type type() const =0
QStringList m_requiredExtensions
Definition: qsvgnode_p.h:167
void setDisplayMode(DisplayMode display)
Definition: qsvgnode.cpp:325
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
qreal widthF() const
Returns the pen width with floating point precision.
Definition: qpen.cpp:645
const QStringList & requiredLanguages() const
Definition: qsvgnode.cpp:270
QSvgRefCounter< QSvgSolidColorStyle > solidColor
Definition: qsvgstyle_p.h:776
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630
QStringList m_requiredFonts
Definition: qsvgnode_p.h:170
void setVisible(bool visible)
Definition: qsvgnode.cpp:295
void setNodeId(const QString &i)
Definition: qsvgnode.cpp:315
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
QSvgTinyDocument * document() const
Definition: qsvgnode.cpp:233
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:658
void setRequiredFonts(const QStringList &lst)
Definition: qsvgnode.cpp:285
void revertStyle(QPainter *p, QSvgExtraStates &states) const
Definition: qsvgnode.cpp:123
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QString m_id
Definition: qsvgnode_p.h:174
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QRectF m_cachedBounds
Definition: qsvgnode_p.h:178
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
void setMiterLimit(qreal limit)
Sets the miter limit of this pen to the given limit.
Definition: qpen.cpp:611