LCOV - code coverage report
Current view: top level - oox/source/drawingml/chart - chartdrawingfragment.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 63 93 67.7 %
Date: 2014-11-03 Functions: 11 12 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "drawingml/chart/chartdrawingfragment.hxx"
      21             : 
      22             : #include "oox/core/xmlfilterbase.hxx"
      23             : #include "oox/drawingml/connectorshapecontext.hxx"
      24             : #include "oox/drawingml/graphicshapecontext.hxx"
      25             : #include "oox/drawingml/shapecontext.hxx"
      26             : #include "oox/drawingml/shapegroupcontext.hxx"
      27             : 
      28             : namespace oox {
      29             : namespace drawingml {
      30             : namespace chart {
      31             : 
      32             : using namespace ::com::sun::star;
      33             : using namespace ::com::sun::star::drawing;
      34             : using namespace ::com::sun::star::uno;
      35             : using namespace ::oox::core;
      36             : 
      37           2 : ShapeAnchor::ShapeAnchor( bool bRelSize ) :
      38           2 :     mbRelSize( bRelSize )
      39             : {
      40           2 : }
      41             : 
      42           0 : void ShapeAnchor::importExt( const AttributeList& rAttribs )
      43             : {
      44             :     OSL_ENSURE( !mbRelSize, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" );
      45           0 :     maSize.Width = rAttribs.getHyper( XML_cx, 0 );
      46           0 :     maSize.Height = rAttribs.getHyper( XML_cy, 0 );
      47           0 : }
      48             : 
      49           8 : void ShapeAnchor::setPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue )
      50             : {
      51           8 :     AnchorPosModel* pAnchorPos = 0;
      52           8 :     switch( nParentContext )
      53             :     {
      54             :         case CDR_TOKEN( from ):
      55           4 :             pAnchorPos = &maFrom;
      56           4 :         break;
      57             :         case CDR_TOKEN( to ):
      58             :             OSL_ENSURE( mbRelSize, "ShapeAnchor::setPos - unexpected 'cdr:to' element" );
      59           4 :             pAnchorPos = &maTo;
      60           4 :         break;
      61             :         default:
      62             :             OSL_FAIL( "ShapeAnchor::setPos - unexpected parent element" );
      63             :     }
      64           8 :     if( pAnchorPos ) switch( nElement )
      65             :     {
      66           4 :         case CDR_TOKEN( x ):    pAnchorPos->mfX = rValue.toDouble();    break;
      67           4 :         case CDR_TOKEN( y ):    pAnchorPos->mfY = rValue.toDouble();    break;
      68             :         default:    OSL_FAIL( "ShapeAnchor::setPos - unexpected element" );
      69             :     }
      70           8 : }
      71             : 
      72           2 : EmuRectangle ShapeAnchor::calcAnchorRectEmu( const EmuRectangle& rChartRect ) const
      73             : {
      74           2 :     EmuRectangle aAnchorRect( -1, -1, -1, -1 );
      75             : 
      76             :     OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" );
      77             :     OSL_ENSURE( mbRelSize ? maTo.isValid() : maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" );
      78           2 :     if( maFrom.isValid() && (mbRelSize ? maTo.isValid() : maSize.isValid()) )
      79             :     {
      80             :         // calculate shape position
      81           2 :         aAnchorRect.X = static_cast< sal_Int64 >( maFrom.mfX * rChartRect.Width + 0.5 );
      82           2 :         aAnchorRect.Y = static_cast< sal_Int64 >( maFrom.mfY * rChartRect.Height + 0.5 );
      83             : 
      84             :         // calculate shape size
      85           2 :         if( mbRelSize )
      86             :         {
      87           2 :             aAnchorRect.Width = static_cast< sal_Int64 >( maTo.mfX * rChartRect.Width + 0.5 ) - aAnchorRect.X;
      88           2 :             if( aAnchorRect.Width < 0 )
      89             :             {
      90           2 :                 aAnchorRect.X += aAnchorRect.Width;
      91           2 :                 aAnchorRect.Width *= -1;
      92             :             }
      93           2 :             aAnchorRect.Height = static_cast< sal_Int64 >( maTo.mfY * rChartRect.Height + 0.5 ) - aAnchorRect.Y;
      94           2 :             if( aAnchorRect.Height < 0 )
      95             :             {
      96           2 :                 aAnchorRect.Y += aAnchorRect.Height;
      97           2 :                 aAnchorRect.Height *= -1;
      98             :             }
      99             :         }
     100             :         else
     101             :         {
     102           0 :             aAnchorRect.setSize( maSize );
     103             :         }
     104             :     }
     105             : 
     106           2 :     return aAnchorRect;
     107             : }
     108             : 
     109           2 : ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase& rFilter,
     110             :         const OUString& rFragmentPath, const Reference< XShapes >& rxDrawPage,
     111             :         const awt::Size& rChartSize, const awt::Point& rShapesOffset, bool bOleSupport ) :
     112             :     FragmentHandler2( rFilter, rFragmentPath ),
     113             :     mxDrawPage( rxDrawPage ),
     114           2 :     mbOleSupport( bOleSupport )
     115             : {
     116           2 :     maChartRectEmu.X = convertHmmToEmu( rShapesOffset.X );
     117           2 :     maChartRectEmu.Y = convertHmmToEmu( rShapesOffset.Y );
     118           2 :     maChartRectEmu.Width = convertHmmToEmu( rChartSize.Width );
     119           2 :     maChartRectEmu.Height = convertHmmToEmu( rChartSize.Height );
     120           2 : }
     121             : 
     122           4 : ChartDrawingFragment::~ChartDrawingFragment()
     123             : {
     124           4 : }
     125             : 
     126          18 : ContextHandlerRef ChartDrawingFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
     127             : {
     128          18 :     switch( getCurrentElement() )
     129             :     {
     130             :         case XML_ROOT_CONTEXT:
     131           2 :             if( nElement == C_TOKEN( userShapes ) ) return this;
     132           0 :         break;
     133             : 
     134             :         case C_TOKEN( userShapes ):
     135           2 :             switch( nElement )
     136             :             {
     137             :                 case CDR_TOKEN( absSizeAnchor ):
     138           0 :                     mxAnchor.reset( new ShapeAnchor( false ) );
     139           0 :                     return this;
     140             :                 case CDR_TOKEN( relSizeAnchor ):
     141           2 :                     mxAnchor.reset( new ShapeAnchor( true ) );
     142           2 :                     return this;
     143             :             }
     144           0 :         break;
     145             : 
     146             :         case CDR_TOKEN( absSizeAnchor ):
     147             :         case CDR_TOKEN( relSizeAnchor ):
     148           6 :             switch( nElement )
     149             :             {
     150             :                 case CDR_TOKEN( sp ):
     151           2 :                     mxShape.reset( new Shape( "com.sun.star.drawing.CustomShape" ) );
     152           2 :                     return new ShapeContext( *this, ShapePtr(), mxShape );
     153             :                 case CDR_TOKEN( cxnSp ):
     154           0 :                     mxShape.reset( new Shape( "com.sun.star.drawing.ConnectorShape" ) );
     155           0 :                     return new ConnectorShapeContext( *this, ShapePtr(), mxShape );
     156             :                 case CDR_TOKEN( pic ):
     157           0 :                     mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
     158           0 :                     return new GraphicShapeContext( *this, ShapePtr(), mxShape );
     159             :                 case CDR_TOKEN( graphicFrame ):
     160           0 :                     if( !mbOleSupport )
     161           0 :                         return 0;
     162           0 :                     mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
     163           0 :                     return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape, true );
     164             :                 case CDR_TOKEN( grpSp ):
     165           0 :                     mxShape.reset( new Shape( "com.sun.star.drawing.GroupShape" ) );
     166           0 :                     return new ShapeGroupContext( *this, ShapePtr(), mxShape );
     167             : 
     168             :                 case CDR_TOKEN( from ):
     169             :                 case CDR_TOKEN( to ):
     170           4 :                     return this;
     171             : 
     172             :                 case CDR_TOKEN( ext ):
     173           0 :                     if( mxAnchor.get() ) mxAnchor->importExt( rAttribs );
     174           0 :                     return 0;
     175             :             }
     176           0 :         break;
     177             : 
     178             :         case CDR_TOKEN( from ):
     179             :         case CDR_TOKEN( to ):
     180           8 :             switch( nElement )
     181             :             {
     182             :                 case CDR_TOKEN( x ):
     183             :                 case CDR_TOKEN( y ):
     184           8 :                     return this;        // collect value in onEndElement()
     185             :             }
     186           0 :         break;
     187             :     }
     188           0 :     return 0;
     189             : }
     190             : 
     191           8 : void ChartDrawingFragment::onCharacters( const OUString& rChars )
     192             : {
     193           8 :     if( isCurrentElement( CDR_TOKEN( x ), CDR_TOKEN( y ) ) && mxAnchor.get() )
     194           8 :         mxAnchor->setPos( getCurrentElement(), getParentElement(), rChars );
     195           8 : }
     196             : 
     197          16 : void ChartDrawingFragment::onEndElement()
     198             : {
     199          16 :     if( isCurrentElement( CDR_TOKEN( absSizeAnchor ), CDR_TOKEN( relSizeAnchor ) ) )
     200             :     {
     201           2 :         if( mxDrawPage.is() && mxShape.get() && mxAnchor.get() )
     202             :         {
     203           2 :             EmuRectangle aShapeRectEmu = mxAnchor->calcAnchorRectEmu( maChartRectEmu );
     204           2 :             if( (aShapeRectEmu.X >= 0) && (aShapeRectEmu.Y >= 0) && (aShapeRectEmu.Width >= 0) && (aShapeRectEmu.Height >= 0) )
     205             :             {
     206             :                 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
     207             :                 awt::Rectangle aShapeRectEmu32(
     208           0 :                     getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.X, 0, SAL_MAX_INT32 ),
     209           0 :                     getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Y, 0, SAL_MAX_INT32 ),
     210           0 :                     getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Width, 0, SAL_MAX_INT32 ),
     211           0 :                     getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Height, 0, SAL_MAX_INT32 ) );
     212           0 :                 basegfx::B2DHomMatrix aMatrix;
     213           0 :                 mxShape->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage, aMatrix, mxShape->getFillProperties(), &aShapeRectEmu32 );
     214             :             }
     215             :         }
     216           2 :         mxShape.reset();
     217           2 :         mxAnchor.reset();
     218             :     }
     219          16 : }
     220             : 
     221             : } // namespace chart
     222             : } // namespace drawingml
     223         408 : } // namespace oox
     224             : 
     225             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10