src/svg/qsvgnode.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtSvg module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 QSvgNode::QSvgNode(QSvgNode *parent)
       
    52     : m_parent(parent),
       
    53       m_visible(true),
       
    54       m_displayMode(BlockMode)
       
    55 {
       
    56 }
       
    57 
       
    58 QSvgNode::~QSvgNode()
       
    59 {
       
    60 
       
    61 }
       
    62 
       
    63 void QSvgNode::appendStyleProperty(QSvgStyleProperty *prop, const QString &id)
       
    64 {
       
    65     //qDebug()<<"appending "<<prop->type()<< " ("<< id <<") "<<"to "<<this<<this->type();
       
    66     QSvgTinyDocument *doc;
       
    67     switch (prop->type()) {
       
    68     case QSvgStyleProperty::QUALITY:
       
    69         m_style.quality = static_cast<QSvgQualityStyle*>(prop);
       
    70         break;
       
    71     case QSvgStyleProperty::FILL:
       
    72         m_style.fill = static_cast<QSvgFillStyle*>(prop);
       
    73         break;
       
    74     case QSvgStyleProperty::VIEWPORT_FILL:
       
    75         m_style.viewportFill = static_cast<QSvgViewportFillStyle*>(prop);
       
    76         break;
       
    77     case QSvgStyleProperty::FONT:
       
    78         m_style.font = static_cast<QSvgFontStyle*>(prop);
       
    79         break;
       
    80     case QSvgStyleProperty::STROKE:
       
    81         m_style.stroke = static_cast<QSvgStrokeStyle*>(prop);
       
    82         break;
       
    83     case QSvgStyleProperty::SOLID_COLOR:
       
    84         m_style.solidColor = static_cast<QSvgSolidColorStyle*>(prop);
       
    85         doc = document();
       
    86         if (doc && !id.isEmpty())
       
    87             doc->addNamedStyle(id, m_style.solidColor);
       
    88         break;
       
    89     case QSvgStyleProperty::GRADIENT:
       
    90         m_style.gradient = static_cast<QSvgGradientStyle*>(prop);
       
    91         doc = document();
       
    92         if (doc && !id.isEmpty())
       
    93             doc->addNamedStyle(id, m_style.gradient);
       
    94         break;
       
    95     case QSvgStyleProperty::TRANSFORM:
       
    96         m_style.transform = static_cast<QSvgTransformStyle*>(prop);
       
    97         break;
       
    98     case QSvgStyleProperty::ANIMATE_COLOR:
       
    99         m_style.animateColor = static_cast<QSvgAnimateColor*>(prop);
       
   100         break;
       
   101     case QSvgStyleProperty::ANIMATE_TRANSFORM:
       
   102         m_style.animateTransforms.append(
       
   103             static_cast<QSvgAnimateTransform*>(prop));
       
   104         break;
       
   105     case QSvgStyleProperty::OPACITY:
       
   106         m_style.opacity = static_cast<QSvgOpacityStyle*>(prop);
       
   107         break;
       
   108     case QSvgStyleProperty::COMP_OP:
       
   109         m_style.compop = static_cast<QSvgCompOpStyle*>(prop);
       
   110         break;
       
   111     default:
       
   112         qDebug("QSvgNode: Trying to append unknown property!");
       
   113         break;
       
   114     }
       
   115 }
       
   116 
       
   117 void QSvgNode::applyStyle(QPainter *p, QSvgExtraStates &states)
       
   118 {
       
   119     m_style.apply(p, bounds(), this, states);
       
   120 }
       
   121 
       
   122 void QSvgNode::revertStyle(QPainter *p, QSvgExtraStates &states)
       
   123 {
       
   124     m_style.revert(p, states);
       
   125 }
       
   126 
       
   127 QSvgStyleProperty * QSvgNode::styleProperty(QSvgStyleProperty::Type type) const
       
   128 {
       
   129     const QSvgNode *node = this;
       
   130     while (node) {
       
   131         switch (type) {
       
   132         case QSvgStyleProperty::QUALITY:
       
   133             if (node->m_style.quality)
       
   134                 return node->m_style.quality;
       
   135             break;
       
   136         case QSvgStyleProperty::FILL:
       
   137             if (node->m_style.fill)
       
   138                 return node->m_style.fill;
       
   139             break;
       
   140         case QSvgStyleProperty::VIEWPORT_FILL:
       
   141             if (m_style.viewportFill)
       
   142                 return node->m_style.viewportFill;
       
   143             break;
       
   144         case QSvgStyleProperty::FONT:
       
   145             if (node->m_style.font)
       
   146                 return node->m_style.font;
       
   147             break;
       
   148         case QSvgStyleProperty::STROKE:
       
   149             if (node->m_style.stroke)
       
   150                 return node->m_style.stroke;
       
   151             break;
       
   152         case QSvgStyleProperty::SOLID_COLOR:
       
   153             if (node->m_style.solidColor)
       
   154                 return node->m_style.solidColor;
       
   155             break;
       
   156         case QSvgStyleProperty::GRADIENT:
       
   157             if (node->m_style.gradient)
       
   158                 return node->m_style.gradient;
       
   159             break;
       
   160         case QSvgStyleProperty::TRANSFORM:
       
   161             if (node->m_style.transform)
       
   162                 return node->m_style.transform;
       
   163             break;
       
   164         case QSvgStyleProperty::ANIMATE_COLOR:
       
   165             if (node->m_style.animateColor)
       
   166                 return node->m_style.animateColor;
       
   167             break;
       
   168         case QSvgStyleProperty::ANIMATE_TRANSFORM:
       
   169             if (!node->m_style.animateTransforms.isEmpty())
       
   170                 return node->m_style.animateTransforms.first();
       
   171             break;
       
   172         case QSvgStyleProperty::OPACITY:
       
   173             if (node->m_style.opacity)
       
   174                 return node->m_style.opacity;
       
   175             break;
       
   176         case QSvgStyleProperty::COMP_OP:
       
   177             if (node->m_style.compop)
       
   178                 return node->m_style.compop;
       
   179             break;
       
   180         default:
       
   181             break;
       
   182         }
       
   183         node = node->parent();
       
   184     }
       
   185 
       
   186     return 0;
       
   187 }
       
   188 
       
   189 QSvgFillStyleProperty * QSvgNode::styleProperty(const QString &id) const
       
   190 {
       
   191     QString rid = id;
       
   192     if (rid.startsWith(QLatin1Char('#')))
       
   193         rid.remove(0, 1);
       
   194     QSvgTinyDocument *doc = document();
       
   195     return doc ? doc->namedStyle(rid) : 0;
       
   196 }
       
   197 
       
   198 QRectF QSvgNode::bounds() const
       
   199 {
       
   200     return QRectF(0, 0, 0, 0);
       
   201 }
       
   202 
       
   203 QSvgTinyDocument * QSvgNode::document() const
       
   204 {
       
   205     QSvgTinyDocument *doc = 0;
       
   206     QSvgNode *node = const_cast<QSvgNode*>(this);
       
   207     while (node && node->type() != QSvgNode::DOC) {
       
   208         node = node->parent();
       
   209     }
       
   210     doc = static_cast<QSvgTinyDocument*>(node);
       
   211 
       
   212     return doc;
       
   213 }
       
   214 
       
   215 void QSvgNode::setRequiredFeatures(const QStringList &lst)
       
   216 {
       
   217     m_requiredFeatures = lst;
       
   218 }
       
   219 
       
   220 const QStringList & QSvgNode::requiredFeatures() const
       
   221 {
       
   222     return m_requiredFeatures;
       
   223 }
       
   224 
       
   225 void QSvgNode::setRequiredExtensions(const QStringList &lst)
       
   226 {
       
   227     m_requiredExtensions = lst;
       
   228 }
       
   229 
       
   230 const QStringList & QSvgNode::requiredExtensions() const
       
   231 {
       
   232     return m_requiredExtensions;
       
   233 }
       
   234 
       
   235 void QSvgNode::setRequiredLanguages(const QStringList &lst)
       
   236 {
       
   237     m_requiredLanguages = lst;
       
   238 }
       
   239 
       
   240 const QStringList & QSvgNode::requiredLanguages() const
       
   241 {
       
   242     return m_requiredLanguages;
       
   243 }
       
   244 
       
   245 void QSvgNode::setRequiredFormats(const QStringList &lst)
       
   246 {
       
   247     m_requiredFormats = lst;
       
   248 }
       
   249 
       
   250 const QStringList & QSvgNode::requiredFormats() const
       
   251 {
       
   252     return m_requiredFormats;
       
   253 }
       
   254 
       
   255 void QSvgNode::setRequiredFonts(const QStringList &lst)
       
   256 {
       
   257     m_requiredFonts = lst;
       
   258 }
       
   259 
       
   260 const QStringList & QSvgNode::requiredFonts() const
       
   261 {
       
   262     return m_requiredFonts;
       
   263 }
       
   264 
       
   265 void QSvgNode::setVisible(bool visible)
       
   266 {
       
   267     //propagate visibility change of true to the parent
       
   268     //not propagating false is just a small performance
       
   269     //degradation since we'll iterate over children without
       
   270     //drawing any of them
       
   271     if (m_parent && visible && !m_parent->isVisible())
       
   272         m_parent->setVisible(true);
       
   273 
       
   274     m_visible = visible;
       
   275 }
       
   276 
       
   277 QRectF QSvgNode::transformedBounds(const QTransform &transform) const
       
   278 {
       
   279     QTransform t = transform;
       
   280 
       
   281     QSvgTransformStyle *transStyle = m_style.transform;
       
   282     if (transStyle) {
       
   283         t = transStyle->qtransform() * t;
       
   284     }
       
   285 
       
   286     QRectF rect = bounds();
       
   287 
       
   288     rect = t.mapRect(rect);
       
   289 
       
   290     return rect;
       
   291 }
       
   292 
       
   293 void QSvgNode::setNodeId(const QString &i)
       
   294 {
       
   295     m_id = i;
       
   296 }
       
   297 
       
   298 void QSvgNode::setXmlClass(const QString &str)
       
   299 {
       
   300     m_class = str;
       
   301 }
       
   302 
       
   303 void QSvgNode::setDisplayMode(DisplayMode mode)
       
   304 {
       
   305     m_displayMode = mode;
       
   306 }
       
   307 
       
   308 QSvgNode::DisplayMode QSvgNode::displayMode() const
       
   309 {
       
   310     return m_displayMode;
       
   311 }
       
   312 
       
   313 qreal QSvgNode::strokeWidth() const
       
   314 {
       
   315     QSvgStrokeStyle *stroke = static_cast<QSvgStrokeStyle*>(
       
   316         styleProperty(QSvgStyleProperty::STROKE));
       
   317     if (!stroke)
       
   318         return 0;
       
   319     if (stroke->stroke().brush().style() == Qt::NoBrush)
       
   320         return 0;
       
   321     return stroke->width();
       
   322 }
       
   323 
       
   324 QT_END_NAMESPACE
       
   325 
       
   326 #endif // QT_NO_SVG