LCOV - code coverage report
Current view: top level - xmloff/source/chart - SchXMLLegendContext.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 72 78 92.3 %
Date: 2014-11-03 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          18 :     LegendAttributeTokenMap(): SvXMLTokenMap( aLegendAttributeTokenMap ) {}
      74          18 :     virtual ~LegendAttributeTokenMap() {}
      75             : };
      76             : 
      77             : //a LegendAttributeTokenMap Singleton
      78             : struct theLegendAttributeTokenMap : public rtl::Static< LegendAttributeTokenMap, theLegendAttributeTokenMap > {};
      79             : 
      80             : }//end anonymous namespace
      81             : 
      82         188 : SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport, const OUString& rLocalName ) :
      83             :     SvXMLImportContext( rImport, XML_NAMESPACE_CHART, rLocalName ),
      84         188 :     mrImportHelper( rImpHelper )
      85             : {
      86         188 : }
      87             : 
      88         188 : void SchXMLLegendContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
      89             : {
      90         188 :     uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
      91         188 :     if( !xDoc.is() )
      92           0 :         return;
      93             : 
      94             :     // turn on legend
      95         376 :     uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
      96         188 :     if( xDocProp.is() )
      97             :     {
      98             :         try
      99             :         {
     100         188 :             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         376 :     uno::Reference< drawing::XShape > xLegendShape( xDoc->getLegend(), uno::UNO_QUERY );
     109         376 :     uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
     110         188 :     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         188 :     sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
     118         188 :     const SvXMLTokenMap& rAttrTokenMap = theLegendAttributeTokenMap::get();
     119             : 
     120         188 :     awt::Point aLegendPos;
     121         188 :     bool bHasXPosition=false;
     122         188 :     bool bHasYPosition=false;
     123         188 :     awt::Size aLegendSize;
     124         188 :     bool bHasWidth=false;
     125         188 :     bool bHasHeight=false;
     126         188 :     chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
     127         188 :     bool bHasExpansion=false;
     128             : 
     129         376 :     OUString sAutoStyleName;
     130         376 :     uno::Any aAny;
     131             : 
     132        1132 :     for( sal_Int16 i = 0; i < nAttrCount; i++ )
     133             :     {
     134         944 :         OUString sAttrName = xAttrList->getNameByIndex( i );
     135        1888 :         OUString aLocalName;
     136        1888 :         OUString aValue = xAttrList->getValueByIndex( i );
     137         944 :         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
     138             : 
     139         944 :         switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
     140             :         {
     141             :             case XML_TOK_LEGEND_POSITION:
     142             :                 {
     143             :                     try
     144             :                     {
     145         188 :                         if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() ) )
     146         188 :                             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         188 :                 break;
     154             : 
     155             :             case XML_TOK_LEGEND_X:
     156         188 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     157         188 :                         aLegendPos.X, aValue );
     158         188 :                 bHasXPosition = true;
     159         188 :                 break;
     160             :             case XML_TOK_LEGEND_Y:
     161         188 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     162         188 :                         aLegendPos.Y, aValue );
     163         188 :                 bHasYPosition = true;
     164         188 :                 break;
     165             :             case XML_TOK_LEGEND_STYLE_NAME:
     166         188 :                 sAutoStyleName = aValue;
     167         188 :                 break;
     168             :             case XML_TOK_LEGEND_EXPANSION:
     169         180 :                 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() );
     170         180 :                 bHasExpansion = (aAny>>=nLegendExpansion);
     171         180 :                 break;
     172             :             case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO:
     173           4 :                 break;
     174             :             case XML_TOK_LEGEND_WIDTH:
     175             :             case XML_TOK_LEGEND_WIDTH_EXT:
     176           4 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     177           4 :                         aLegendSize.Width, aValue );
     178           4 :                 bHasWidth = true;
     179           4 :                 break;
     180             :             case XML_TOK_LEGEND_HEIGHT:
     181             :             case XML_TOK_LEGEND_HEIGHT_EXT:
     182           4 :                 GetImport().GetMM100UnitConverter().convertMeasureToCore(
     183           4 :                         aLegendSize.Height, aValue );
     184           4 :                 bHasHeight = true;
     185           4 :                 break;
     186             :             default:
     187           0 :                 break;
     188             :         }
     189         944 :     }
     190             : 
     191         188 :     if( bHasXPosition && bHasYPosition )
     192         188 :         xLegendShape->setPosition( aLegendPos );
     193             : 
     194         188 :     if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
     195           0 :         xLegendProps->setPropertyValue("Expansion", uno::makeAny(nLegendExpansion) );
     196         188 :     else if( bHasHeight && bHasWidth )
     197           4 :         xLegendShape->setSize( aLegendSize );
     198             : 
     199             :     // the fill style has the default "none" in XML, but "solid" in the model.
     200         188 :     xLegendProps->setPropertyValue("FillStyle", uno::makeAny( drawing::FillStyle_NONE ));
     201             : 
     202             :     // set auto-styles for Legend
     203         188 :     const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
     204         188 :     if( pStylesCtxt )
     205             :     {
     206             :         const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
     207         188 :             SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
     208             : 
     209         188 :         if( pStyle && pStyle->ISA( XMLPropStyleContext ))
     210         188 :             const_cast<XMLPropStyleContext*>( static_cast<const XMLPropStyleContext*>( pStyle ) )->FillPropertySet( xLegendProps );
     211         188 :     }
     212             : }
     213             : 
     214         376 : SchXMLLegendContext::~SchXMLLegendContext()
     215             : {
     216         376 : }
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10