LCOV - code coverage report
Current view: top level - libreoffice/oox/source/drawingml/chart - converterbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 131 0.8 %
Date: 2012-12-17 Functions: 2 35 5.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 "oox/drawingml/chart/converterbase.hxx"
      21             : 
      22             : #include <com/sun/star/chart/XAxisXSupplier.hpp>
      23             : #include <com/sun/star/chart/XAxisYSupplier.hpp>
      24             : #include <com/sun/star/chart/XAxisZSupplier.hpp>
      25             : #include <com/sun/star/chart/XChartDocument.hpp>
      26             : #include <com/sun/star/chart/XSecondAxisTitleSupplier.hpp>
      27             : #include <com/sun/star/chart2/RelativePosition.hpp>
      28             : #include <com/sun/star/chart2/RelativeSize.hpp>
      29             : #include <com/sun/star/drawing/FillStyle.hpp>
      30             : #include <com/sun/star/drawing/LineStyle.hpp>
      31             : #include <com/sun/star/frame/XModel.hpp>
      32             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      33             : #include "basegfx/numeric/ftools.hxx"
      34             : #include "oox/core/xmlfilterbase.hxx"
      35             : #include "oox/drawingml/theme.hxx"
      36             : 
      37             : namespace oox {
      38             : namespace drawingml {
      39             : namespace chart {
      40             : 
      41             : // ============================================================================
      42             : 
      43             : namespace cssc = ::com::sun::star::chart;
      44             : 
      45             : using namespace ::com::sun::star;
      46             : using namespace ::com::sun::star::chart2;
      47             : using namespace ::com::sun::star::drawing;
      48             : using namespace ::com::sun::star::frame;
      49             : using namespace ::com::sun::star::lang;
      50             : using namespace ::com::sun::star::uno;
      51             : 
      52             : using ::oox::core::XmlFilterBase;
      53             : 
      54             : // ============================================================================
      55             : 
      56             : namespace {
      57             : 
      58             : struct TitleKey : public ::std::pair< ObjectType, ::std::pair< sal_Int32, sal_Int32 > >
      59             : {
      60           0 :     inline explicit     TitleKey( ObjectType eObjType, sal_Int32 nMainIdx = -1, sal_Int32 nSubIdx = -1 )
      61           0 :                             { first = eObjType; second.first = nMainIdx; second.second = nSubIdx; }
      62             : };
      63             : 
      64             : // ----------------------------------------------------------------------------
      65             : 
      66             : /** A helper structure to store all data related to title objects. Needed for
      67             :     the conversion of manual title positions that needs the old Chart1 API.
      68             :  */
      69           0 : struct TitleLayoutInfo
      70             : {
      71             :     typedef Reference< XShape > (*GetShapeFunc)( const Reference< cssc::XChartDocument >& );
      72             : 
      73             :     Reference< XTitle > mxTitle;        /// The API title object.
      74             :     ModelRef< LayoutModel > mxLayout;   /// The layout model, if existing.
      75             :     GetShapeFunc        mpGetShape;     /// Helper function to receive the title shape.
      76             : 
      77           0 :     inline explicit     TitleLayoutInfo() : mpGetShape( 0 ) {}
      78             : 
      79             :     void                convertTitlePos(
      80             :                             ConverterRoot& rRoot,
      81             :                             const Reference< cssc::XChartDocument >& rxChart1Doc );
      82             : };
      83             : 
      84           0 : void TitleLayoutInfo::convertTitlePos( ConverterRoot& rRoot, const Reference< cssc::XChartDocument >& rxChart1Doc )
      85             : {
      86           0 :     if( mxTitle.is() && mpGetShape ) try
      87             :     {
      88             :         // try to get the title shape
      89           0 :         Reference< XShape > xTitleShape( mpGetShape( rxChart1Doc ), UNO_SET_THROW );
      90             :         // get title rotation angle, needed for correction of position of top-left edge
      91           0 :         double fAngle = 0.0;
      92           0 :         PropertySet aTitleProp( mxTitle );
      93           0 :         aTitleProp.getProperty( fAngle, PROP_TextRotation );
      94             :         // convert the position
      95           0 :         LayoutModel& rLayout = mxLayout.getOrCreate();
      96           0 :         LayoutConverter aLayoutConv( rRoot, rLayout );
      97           0 :         aLayoutConv.convertFromModel( xTitleShape, fAngle );
      98             :     }
      99           0 :     catch( Exception& )
     100             :     {
     101             :     }
     102           0 : }
     103             : 
     104             : // ----------------------------------------------------------------------------
     105             : 
     106             : /*  The following local functions implement getting the XShape interface of all
     107             :     supported title objects (chart and axes). This needs some effort due to the
     108             :     design of the old Chart1 API used to access these objects. */
     109             : 
     110             : /** A code fragment that returns a shape object from the passed shape supplier
     111             :     using the specified interface function. Checks a boolean property first. */
     112             : #define OOX_FRAGMENT_GETTITLESHAPE( shape_supplier, supplier_func, property_name ) \
     113             :     PropertySet aPropSet( shape_supplier ); \
     114             :     if( shape_supplier.is() && aPropSet.getBoolProperty( PROP_##property_name ) ) \
     115             :         return shape_supplier->supplier_func(); \
     116             :     return Reference< XShape >(); \
     117             : 
     118             : /** Implements a function returning the drawing shape of an axis title, if
     119             :     existing, using the specified API interface and its function. */
     120             : #define OOX_DEFINEFUNC_GETAXISTITLESHAPE( func_name, interface_type, supplier_func, property_name ) \
     121             : Reference< XShape > func_name( const Reference< cssc::XChartDocument >& rxChart1Doc ) \
     122             : { \
     123             :     Reference< cssc::interface_type > xAxisSupp( rxChart1Doc->getDiagram(), UNO_QUERY ); \
     124             :     OOX_FRAGMENT_GETTITLESHAPE( xAxisSupp, supplier_func, property_name ) \
     125             : }
     126             : 
     127             : /** Returns the drawing shape of the main title, if existing. */
     128           0 : Reference< XShape > lclGetMainTitleShape( const Reference< cssc::XChartDocument >& rxChart1Doc )
     129             : {
     130           0 :     OOX_FRAGMENT_GETTITLESHAPE( rxChart1Doc, getTitle, HasMainTitle )
     131             : }
     132             : 
     133           0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetXAxisTitleShape, XAxisXSupplier, getXAxisTitle, HasXAxisTitle )
     134           0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetYAxisTitleShape, XAxisYSupplier, getYAxisTitle, HasYAxisTitle )
     135           0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetZAxisTitleShape, XAxisZSupplier, getZAxisTitle, HasZAxisTitle )
     136           0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetSecXAxisTitleShape, XSecondAxisTitleSupplier, getSecondXAxisTitle, HasSecondaryXAxisTitle )
     137           0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetSecYAxisTitleShape, XSecondAxisTitleSupplier, getSecondYAxisTitle, HasSecondaryYAxisTitle )
     138             : 
     139             : #undef OOX_DEFINEFUNC_GETAXISTITLESHAPE
     140             : #undef OOX_IMPLEMENT_GETTITLESHAPE
     141             : 
     142             : } // namespace
     143             : 
     144             : // ============================================================================
     145             : 
     146             : struct ConverterData
     147             : {
     148             :     typedef ::std::map< TitleKey, TitleLayoutInfo > TitleMap;
     149             : 
     150             :     ObjectFormatter     maFormatter;
     151             :     TitleMap            maTitles;
     152             :     XmlFilterBase&      mrFilter;
     153             :     ChartConverter&     mrConverter;
     154             :     Reference< XChartDocument > mxDoc;
     155             :     awt::Size                maSize;
     156             : 
     157             :     explicit            ConverterData(
     158             :                             XmlFilterBase& rFilter,
     159             :                             ChartConverter& rChartConverter,
     160             :                             const ChartSpaceModel& rChartModel,
     161             :                             const Reference< XChartDocument >& rxChartDoc,
     162             :                             const awt::Size& rChartSize );
     163             :                         ~ConverterData();
     164             : };
     165             : 
     166             : // ----------------------------------------------------------------------------
     167             : 
     168           0 : ConverterData::ConverterData(
     169             :         XmlFilterBase& rFilter,
     170             :         ChartConverter& rChartConverter,
     171             :         const ChartSpaceModel& rChartModel,
     172             :         const Reference< XChartDocument >& rxChartDoc,
     173             :         const awt::Size& rChartSize ) :
     174             :     maFormatter( rFilter, rxChartDoc, rChartModel ),
     175             :     mrFilter( rFilter ),
     176             :     mrConverter( rChartConverter ),
     177             :     mxDoc( rxChartDoc ),
     178           0 :     maSize( rChartSize )
     179             : {
     180             :     OSL_ENSURE( mxDoc.is(), "ConverterData::ConverterData - missing chart document" );
     181             :     // lock the model to suppress internal updates during conversion
     182             :     try
     183             :     {
     184           0 :         Reference< XModel > xModel( mxDoc, UNO_QUERY_THROW );
     185           0 :         xModel->lockControllers();
     186             :     }
     187           0 :     catch( Exception& )
     188             :     {
     189             :     }
     190             : 
     191             :     // prepare conversion of title positions
     192           0 :     maTitles[ TitleKey( OBJECTTYPE_CHARTTITLE ) ].mpGetShape = lclGetMainTitleShape;
     193           0 :     maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_X_AXIS ) ].mpGetShape = lclGetXAxisTitleShape;
     194           0 :     maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_Y_AXIS ) ].mpGetShape = lclGetYAxisTitleShape;
     195           0 :     maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_Z_AXIS ) ].mpGetShape = lclGetZAxisTitleShape;
     196           0 :     maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_SECN_AXESSET, API_X_AXIS ) ].mpGetShape = lclGetSecXAxisTitleShape;
     197           0 :     maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_SECN_AXESSET, API_Y_AXIS ) ].mpGetShape = lclGetSecYAxisTitleShape;
     198           0 : }
     199             : 
     200           0 : ConverterData::~ConverterData()
     201             : {
     202             :     // unlock the model
     203             :     try
     204             :     {
     205           0 :         Reference< XModel > xModel( mxDoc, UNO_QUERY_THROW );
     206           0 :         xModel->unlockControllers();
     207             :     }
     208           0 :     catch( Exception& )
     209             :     {
     210             :     }
     211           0 : }
     212             : 
     213             : // ============================================================================
     214             : 
     215           0 : ConverterRoot::ConverterRoot(
     216             :         XmlFilterBase& rFilter,
     217             :         ChartConverter& rChartConverter,
     218             :         const ChartSpaceModel& rChartModel,
     219             :         const Reference< XChartDocument >& rxChartDoc,
     220             :         const awt::Size& rChartSize ) :
     221           0 :     mxData( new ConverterData( rFilter, rChartConverter, rChartModel, rxChartDoc, rChartSize ) )
     222             : {
     223           0 : }
     224             : 
     225           0 : ConverterRoot::~ConverterRoot()
     226             : {
     227           0 : }
     228             : 
     229           0 : Reference< XInterface > ConverterRoot::createInstance( const OUString& rServiceName ) const
     230             : {
     231           0 :     Reference< XInterface > xInt;
     232             :     try
     233             :     {
     234           0 :         xInt = mxData->mrFilter.getServiceFactory()->createInstance( rServiceName );
     235             :     }
     236           0 :     catch( Exception& )
     237             :     {
     238             :     }
     239             :     OSL_ENSURE( xInt.is(), "ConverterRoot::createInstance - cannot create instance" );
     240           0 :     return xInt;
     241             : }
     242             : 
     243           0 : XmlFilterBase& ConverterRoot::getFilter() const
     244             : {
     245           0 :     return mxData->mrFilter;
     246             : }
     247             : 
     248           0 : ChartConverter* ConverterRoot::getChartConverter() const
     249             : {
     250           0 :     return &mxData->mrConverter;
     251             : }
     252             : 
     253           0 : Reference< XChartDocument > ConverterRoot::getChartDocument() const
     254             : {
     255           0 :     return mxData->mxDoc;
     256             : }
     257             : 
     258           0 : const awt::Size& ConverterRoot::getChartSize() const
     259             : {
     260           0 :     return mxData->maSize;
     261             : }
     262             : 
     263           0 : ObjectFormatter& ConverterRoot::getFormatter() const
     264             : {
     265           0 :     return mxData->maFormatter;
     266             : }
     267             : 
     268           0 : void ConverterRoot::registerTitleLayout( const Reference< XTitle >& rxTitle,
     269             :         const ModelRef< LayoutModel >& rxLayout, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
     270             : {
     271             :     OSL_ENSURE( rxTitle.is(), "ConverterRoot::registerTitleLayout - missing title object" );
     272           0 :     TitleLayoutInfo& rTitleInfo = mxData->maTitles[ TitleKey( eObjType, nMainIdx, nSubIdx ) ];
     273             :     OSL_ENSURE( rTitleInfo.mpGetShape, "ConverterRoot::registerTitleLayout - invalid title key" );
     274           0 :     rTitleInfo.mxTitle = rxTitle;
     275           0 :     rTitleInfo.mxLayout = rxLayout;
     276           0 : }
     277             : 
     278           0 : void ConverterRoot::convertTitlePositions()
     279             : {
     280             :     try
     281             :     {
     282           0 :         Reference< cssc::XChartDocument > xChart1Doc( mxData->mxDoc, UNO_QUERY_THROW );
     283           0 :         for( ConverterData::TitleMap::iterator aIt = mxData->maTitles.begin(), aEnd = mxData->maTitles.end(); aIt != aEnd; ++aIt )
     284           0 :             aIt->second.convertTitlePos( *this, xChart1Doc );
     285             :     }
     286           0 :     catch( Exception& )
     287             :     {
     288             :     }
     289           0 : }
     290             : 
     291             : // ============================================================================
     292             : 
     293             : namespace {
     294             : 
     295             : /** Returns a position value in the chart area in 1/100 mm. */
     296           0 : sal_Int32 lclCalcPosition( sal_Int32 nChartSize, double fPos, sal_Int32 nPosMode )
     297             : {
     298           0 :     switch( nPosMode )
     299             :     {
     300             :         case XML_edge:      // absolute start position as factor of chart size
     301           0 :             return getLimitedValue< sal_Int32, double >( nChartSize * fPos + 0.5, 0, nChartSize );
     302             :         case XML_factor:    // position relative to object default position
     303             :             OSL_FAIL( "lclCalcPosition - relative positioning not supported" );
     304           0 :             return -1;
     305             :     };
     306             : 
     307             :     OSL_FAIL( "lclCalcPosition - unknown positioning mode" );
     308           0 :     return -1;
     309             : }
     310             : 
     311             : /** Returns a size value in the chart area in 1/100 mm. */
     312           0 : sal_Int32 lclCalcSize( sal_Int32 nPos, sal_Int32 nChartSize, double fSize, sal_Int32 nSizeMode )
     313             : {
     314           0 :     sal_Int32 nValue = getLimitedValue< sal_Int32, double >( nChartSize * fSize + 0.5, 0, nChartSize );
     315           0 :     switch( nSizeMode )
     316             :     {
     317             :         case XML_factor:    // passed value is width/height
     318           0 :             return nValue;
     319             :         case XML_edge:      // passed value is right/bottom position
     320           0 :             return nValue - nPos + 1;
     321             :     };
     322             : 
     323             :     OSL_FAIL( "lclCalcSize - unknown size mode" );
     324           0 :     return -1;
     325             : }
     326             : 
     327             : /** Returns a relative size value in the chart area. */
     328           0 : double lclCalcRelSize( double fPos, double fSize, sal_Int32 nSizeMode )
     329             : {
     330           0 :     switch( nSizeMode )
     331             :     {
     332             :         case XML_factor:    // passed value is width/height
     333           0 :         break;
     334             :         case XML_edge:      // passed value is right/bottom position
     335           0 :             fSize -= fPos;
     336           0 :         break;
     337             :         default:
     338             :             OSL_ENSURE( false, "lclCalcRelSize - unknown size mode" );
     339           0 :             fSize = 0.0;
     340             :     };
     341           0 :     return getLimitedValue< double, double >( fSize, 0.0, 1.0 - fPos );
     342             : }
     343             : 
     344             : } // namespace
     345             : 
     346             : // ----------------------------------------------------------------------------
     347             : 
     348           0 : LayoutConverter::LayoutConverter( const ConverterRoot& rParent, LayoutModel& rModel ) :
     349           0 :     ConverterBase< LayoutModel >( rParent, rModel )
     350             : {
     351           0 : }
     352             : 
     353           0 : LayoutConverter::~LayoutConverter()
     354             : {
     355           0 : }
     356             : 
     357           0 : bool LayoutConverter::calcAbsRectangle( awt::Rectangle& orRect ) const
     358             : {
     359           0 :     if( !mrModel.mbAutoLayout )
     360             :     {
     361           0 :         const awt::Size& rChartSize = getChartSize();
     362           0 :         orRect.X = lclCalcPosition( rChartSize.Width,  mrModel.mfX, mrModel.mnXMode );
     363           0 :         orRect.Y = lclCalcPosition( rChartSize.Height, mrModel.mfY, mrModel.mnYMode );
     364           0 :         if( (orRect.X >= 0) && (orRect.Y >= 0) )
     365             :         {
     366           0 :             orRect.Width  = lclCalcSize( orRect.X, rChartSize.Width,  mrModel.mfW, mrModel.mnWMode );
     367           0 :             orRect.Height = lclCalcSize( orRect.Y, rChartSize.Height, mrModel.mfH, mrModel.mnHMode );
     368           0 :             return (orRect.Width > 0) && (orRect.Height > 0);
     369             :         }
     370             :     }
     371           0 :     return false;
     372             : }
     373             : 
     374           0 : bool LayoutConverter::convertFromModel( PropertySet& rPropSet )
     375             : {
     376           0 :     if( !mrModel.mbAutoLayout &&
     377             :         (mrModel.mnXMode == XML_edge) && (mrModel.mfX >= 0.0) &&
     378             :         (mrModel.mnYMode == XML_edge) && (mrModel.mfY >= 0.0) )
     379             :     {
     380             :         RelativePosition aPos(
     381           0 :             getLimitedValue< double, double >( mrModel.mfX, 0.0, 1.0 ),
     382           0 :             getLimitedValue< double, double >( mrModel.mfY, 0.0, 1.0 ),
     383           0 :             Alignment_TOP_LEFT );
     384           0 :         rPropSet.setProperty( PROP_RelativePosition, aPos );
     385             : 
     386             :         RelativeSize aSize(
     387           0 :             lclCalcRelSize( aPos.Primary, mrModel.mfW, mrModel.mnWMode ),
     388           0 :             lclCalcRelSize( aPos.Secondary, mrModel.mfH, mrModel.mnHMode ) );
     389           0 :         if( (aSize.Primary > 0.0) && (aSize.Secondary > 0.0) )
     390             :         {
     391           0 :             rPropSet.setProperty( PROP_RelativeSize, aSize );
     392           0 :             return true;
     393             :         }
     394             :     }
     395           0 :     return false;
     396             : }
     397             : 
     398           0 : bool LayoutConverter::convertFromModel( const Reference< XShape >& rxShape, double fRotationAngle )
     399             : {
     400           0 :     if( !mrModel.mbAutoLayout )
     401             :     {
     402           0 :         const awt::Size& rChartSize = getChartSize();
     403             :         awt::Point aShapePos(
     404           0 :             lclCalcPosition( rChartSize.Width,  mrModel.mfX, mrModel.mnXMode ),
     405           0 :             lclCalcPosition( rChartSize.Height, mrModel.mfY, mrModel.mnYMode ) );
     406           0 :         if( (aShapePos.X >= 0) && (aShapePos.Y >= 0) )
     407             :         {
     408             :             // the call to XShape.getSize() may recalc the chart view
     409           0 :             awt::Size aShapeSize = rxShape->getSize();
     410             :             // rotated shapes need special handling...
     411           0 :             double fSin = fabs( sin( fRotationAngle * F_PI180 ) );
     412             :             // add part of height to X direction, if title is rotated down
     413           0 :             if( fRotationAngle > 180.0 )
     414           0 :                 aShapePos.X += static_cast< sal_Int32 >( fSin * aShapeSize.Height + 0.5 );
     415             :             // add part of width to Y direction, if title is rotated up
     416           0 :             else if( fRotationAngle > 0.0 )
     417           0 :                 aShapePos.Y += static_cast< sal_Int32 >( fSin * aShapeSize.Width + 0.5 );
     418             :             // set the resulting position at the shape
     419           0 :             rxShape->setPosition( aShapePos );
     420           0 :             return true;
     421             :         }
     422             :     }
     423           0 :     return false;
     424             : }
     425             : 
     426             : // ============================================================================
     427             : 
     428             : } // namespace chart
     429             : } // namespace drawingml
     430         174 : } // namespace oox
     431             : 
     432             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10