LCOV - code coverage report
Current view: top level - xmloff/source/chart - SchXMLLegendContext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 72 78 92.3 %
Date: 2015-06-13 12:38:46 Functions: 6 7 85.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 "SchXMLLegendContext.hxx"
      21             : #include "SchXMLEnumConverter.hxx"
      22             : 
      23             : #include <xmloff/xmlnmspe.hxx>
      24             : #include <xmloff/xmlement.hxx>
      25             : #include <xmloff/prstylei.hxx>
      26             : #include <xmloff/nmspmap.hxx>
      27             : #include <xmloff/xmluconv.hxx>
      28             : 
      29             : #include <com/sun/star/chart/ChartLegendPosition.hpp>
      30             : #include <com/sun/star/chart/ChartLegendExpansion.hpp>
      31             : #include <com/sun/star/drawing/FillStyle.hpp>
      32             : 
      33             : using namespace ::xmloff::token;
      34             : using namespace com::sun::star;
      35             : 
      36             : using com::sun::star::uno::Reference;
      37             : 
      38             : namespace
      39             : {
      40             : 
      41             : enum LegendAttributeTokens
      42             : {
      43             :     XML_TOK_LEGEND_POSITION,
      44             :     XML_TOK_LEGEND_X,
      45             :     XML_TOK_LEGEND_Y,
      46             :     XML_TOK_LEGEND_STYLE_NAME,
      47             :     XML_TOK_LEGEND_EXPANSION,
      48             :     XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO,
      49             :     XML_TOK_LEGEND_WIDTH,
      50             :     XML_TOK_LEGEND_WIDTH_EXT,
      51             :     XML_TOK_LEGEND_HEIGHT,
      52             :     XML_TOK_LEGEND_HEIGHT_EXT
      53             : };
      54             : 
      55             : const SvXMLTokenMapEntry aLegendAttributeTokenMap[] =
      56             : {
      57             :     { XML_NAMESPACE_CHART,      XML_LEGEND_POSITION,    XML_TOK_LEGEND_POSITION     },
      58             :     { XML_NAMESPACE_SVG,        XML_X,                  XML_TOK_LEGEND_X            },
      59             :     { XML_NAMESPACE_SVG,        XML_Y,                  XML_TOK_LEGEND_Y            },
      60             :     { XML_NAMESPACE_CHART,      XML_STYLE_NAME,         XML_TOK_LEGEND_STYLE_NAME   },
      61             :     { XML_NAMESPACE_STYLE,      XML_LEGEND_EXPANSION,   XML_TOK_LEGEND_EXPANSION    },
      62             :     { XML_NAMESPACE_STYLE,      XML_LEGEND_EXPANSION_ASPECT_RATIO,   XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO    },
      63             :     { XML_NAMESPACE_SVG,        XML_WIDTH,              XML_TOK_LEGEND_WIDTH        },
      64             :     { XML_NAMESPACE_CHART_EXT,  XML_WIDTH,              XML_TOK_LEGEND_WIDTH_EXT    },
      65             :     { XML_NAMESPACE_SVG,        XML_HEIGHT,             XML_TOK_LEGEND_HEIGHT       },
      66             :     { XML_NAMESPACE_CHART_EXT,  XML_HEIGHT,             XML_TOK_LEGEND_HEIGHT_EXT   },
      67             :     XML_TOKEN_MAP_END
      68             : };
      69             : 
      70             : class LegendAttributeTokenMap : public SvXMLTokenMap
      71             : {
      72             : public:
      73           8 :     LegendAttributeTokenMap(): SvXMLTokenMap( aLegendAttributeTokenMap ) {}
      74           8 :     virtual ~LegendAttributeTokenMap() {}
      75             : };
      76             : 
      77             : //a LegendAttributeTokenMap Singleton
      78             : struct theLegendAttributeTokenMap : public rtl::Static< LegendAttributeTokenMap, theLegendAttributeTokenMap > {};
      79             : 
      80             : }//end anonymous namespace
      81             : 
      82          61 : SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport, const OUString& rLocalName ) :
      83             :     SvXMLImportContext( rImport, XML_NAMESPACE_CHART, rLocalName ),
      84          61 :     mrImportHelper( rImpHelper )
      85             : {
      86          61 : }
      87             : 
      88          61 : void SchXMLLegendContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
      89             : {
      90          61 :     uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
      91          61 :     if( !xDoc.is() )
      92           0 :         return;
      93             : 
      94             :     // turn on legend
      95         122 :     uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
      96          61 :     if( xDocProp.is() )
      97             :     {
      98             :         try
      99             :         {
     100          61 :             xDocProp->setPropertyValue("HasLegend", uno::makeAny( sal_True ) );
     101             :         }
     102           0 :         catch(const beans::UnknownPropertyException&)
     103             :         {
     104             :             SAL_INFO("xmloff.chart", "Property HasLegend not found" );
     105             :         }
     106             :     }
     107             : 
     108         122 :     uno::Reference< drawing::XShape > xLegendShape( xDoc->getLegend(), uno::UNO_QUERY );
     109         122 :     uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
     110          61 :     if( !xLegendShape.is() || !xLegendProps.is() )
     111             :     {
     112             :         SAL_INFO("xmloff.chart", "legend could not be created" );
     113           0 :         return;
     114             :     }
     115             : 
     116             :     // parse attributes
     117          61 :     sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
     118          61 :     const SvXMLTokenMap& rAttrTokenMap = theLegendAttributeTokenMap::get();
     119             : 
     120          61 :     awt::Point aLegendPos;
     121          61 :     bool bHasXPosition=false;
     122          61 :     bool bHasYPosition=false;
     123          61 :     awt::Size aLegendSize;
     124          61 :     bool bHasWidth=false;
     125          61 :     bool bHasHeight=false;
     126          61 :     chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
     127          61 :     bool bHasExpansion=false;
     128             : 
     129         122 :     OUString sAutoStyleName;
     130         122 :     uno::Any aAny;
     131             : 
     132         367 :     for( sal_Int16 i = 0; i < nAttrCount; i++ )
     133             :     {
     134         306 :         OUString sAttrName = xAttrList->getNameByIndex( i );
     135         612 :         OUString aLocalName;
     136         612 :         OUString aValue = xAttrList->getValueByIndex( i );
     137         306 :         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
     138             : 
     139         306 :         switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
     140             :         {
     141             :             case XML_TOK_LEGEND_POSITION:
     142             :                 {
     143             :                     try
     144             :                     {
     145          60 :                         if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() ) )
     146          60 :                             xLegendProps->setPropertyValue("Alignment", aAny );
     147             :                     }
     148           0 :                     catch(const beans::UnknownPropertyException&)
     149             :                     {
     150             :                         SAL_INFO("xmloff.chart", "Property Alignment (legend) not found" );
     151             :                     }
     152             :                 }
     153          60 :                 break;
     154             : 
     155             :             case XML_TOK_LEGEND_X:
     156          61 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     157          61 :                         aLegendPos.X, aValue );
     158          61 :                 bHasXPosition = true;
     159          61 :                 break;
     160             :             case XML_TOK_LEGEND_Y:
     161          61 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     162          61 :                         aLegendPos.Y, aValue );
     163          61 :                 bHasYPosition = true;
     164          61 :                 break;
     165             :             case XML_TOK_LEGEND_STYLE_NAME:
     166          61 :                 sAutoStyleName = aValue;
     167          61 :                 break;
     168             :             case XML_TOK_LEGEND_EXPANSION:
     169          57 :                 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() );
     170          57 :                 bHasExpansion = (aAny>>=nLegendExpansion);
     171          57 :                 break;
     172             :             case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO:
     173           2 :                 break;
     174             :             case XML_TOK_LEGEND_WIDTH:
     175             :             case XML_TOK_LEGEND_WIDTH_EXT:
     176           2 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     177           2 :                         aLegendSize.Width, aValue );
     178           2 :                 bHasWidth = true;
     179           2 :                 break;
     180             :             case XML_TOK_LEGEND_HEIGHT:
     181             :             case XML_TOK_LEGEND_HEIGHT_EXT:
     182           2 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     183           2 :                         aLegendSize.Height, aValue );
     184           2 :                 bHasHeight = true;
     185           2 :                 break;
     186             :             default:
     187           0 :                 break;
     188             :         }
     189         306 :     }
     190             : 
     191          61 :     if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
     192           0 :         xLegendProps->setPropertyValue("Expansion", uno::makeAny(nLegendExpansion) );
     193          61 :     else if( bHasHeight && bHasWidth )
     194           2 :         xLegendShape->setSize( aLegendSize );
     195             : 
     196          61 :     if( bHasXPosition && bHasYPosition )
     197          61 :         xLegendShape->setPosition( aLegendPos );
     198             : 
     199             :     // the fill style has the default "none" in XML, but "solid" in the model.
     200          61 :     xLegendProps->setPropertyValue("FillStyle", uno::makeAny( drawing::FillStyle_NONE ));
     201             : 
     202             :     // set auto-styles for Legend
     203          61 :     const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
     204          61 :     if( pStylesCtxt )
     205             :     {
     206             :         const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
     207          61 :             SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
     208             : 
     209          61 :         if( pStyle && pStyle->ISA( XMLPropStyleContext ))
     210          61 :             const_cast<XMLPropStyleContext*>( static_cast<const XMLPropStyleContext*>( pStyle ) )->FillPropertySet( xLegendProps );
     211          61 :     }
     212             : }
     213             : 
     214         122 : SchXMLLegendContext::~SchXMLLegendContext()
     215             : {
     216         122 : }
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11