src/svg/qsvgnode.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
--- a/src/svg/qsvgnode.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/svg/qsvgnode.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -45,6 +45,7 @@
 #ifndef QT_NO_SVG
 
 #include "qdebug.h"
+#include "qstack.h"
 
 QT_BEGIN_NAMESPACE
 
@@ -114,12 +115,12 @@
     }
 }
 
-void QSvgNode::applyStyle(QPainter *p, QSvgExtraStates &states)
+void QSvgNode::applyStyle(QPainter *p, QSvgExtraStates &states) const
 {
-    m_style.apply(p, bounds(), this, states);
+    m_style.apply(p, this, states);
 }
 
-void QSvgNode::revertStyle(QPainter *p, QSvgExtraStates &states)
+void QSvgNode::revertStyle(QPainter *p, QSvgExtraStates &states) const
 {
     m_style.revert(p, states);
 }
@@ -195,11 +196,40 @@
     return doc ? doc->namedStyle(rid) : 0;
 }
 
-QRectF QSvgNode::bounds() const
+QRectF QSvgNode::bounds(QPainter *, QSvgExtraStates &) const
 {
     return QRectF(0, 0, 0, 0);
 }
 
+QRectF QSvgNode::transformedBounds() const
+{
+    if (!m_cachedBounds.isEmpty())
+        return m_cachedBounds;
+
+    QImage dummy(1, 1, QImage::Format_RGB32);
+    QPainter p(&dummy);
+    QSvgExtraStates states;
+
+    QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
+    pen.setMiterLimit(4);
+    p.setPen(pen);
+
+    QStack<QSvgNode*> parentApplyStack;
+    QSvgNode *parent = m_parent;
+    while (parent) {
+        parentApplyStack.push(parent);
+        parent = parent->parent();
+    }
+
+    for (int i = parentApplyStack.size() - 1; i >= 0; --i)
+        parentApplyStack[i]->applyStyle(&p, states);
+    
+    p.setWorldTransform(QTransform());
+
+    m_cachedBounds = transformedBounds(&p, states);
+    return m_cachedBounds;
+}
+
 QSvgTinyDocument * QSvgNode::document() const
 {
     QSvgTinyDocument *doc = 0;
@@ -274,19 +304,11 @@
     m_visible = visible;
 }
 
-QRectF QSvgNode::transformedBounds(const QTransform &transform) const
+QRectF QSvgNode::transformedBounds(QPainter *p, QSvgExtraStates &states) const
 {
-    QTransform t = transform;
-
-    QSvgTransformStyle *transStyle = m_style.transform;
-    if (transStyle) {
-        t = transStyle->qtransform() * t;
-    }
-
-    QRectF rect = bounds();
-
-    rect = t.mapRect(rect);
-
+    applyStyle(p, states);
+    QRectF rect = bounds(p, states);
+    revertStyle(p, states);
     return rect;
 }
 
@@ -310,15 +332,12 @@
     return m_displayMode;
 }
 
-qreal QSvgNode::strokeWidth() const
+qreal QSvgNode::strokeWidth(QPainter *p)
 {
-    QSvgStrokeStyle *stroke = static_cast<QSvgStrokeStyle*>(
-        styleProperty(QSvgStyleProperty::STROKE));
-    if (!stroke)
+    QPen pen = p->pen();
+    if (pen.style() == Qt::NoPen || pen.brush().style() == Qt::NoBrush || pen.isCosmetic())
         return 0;
-    if (stroke->stroke().brush().style() == Qt::NoBrush)
-        return 0;
-    return stroke->width();
+    return pen.widthF();
 }
 
 QT_END_NAMESPACE