bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / chart / SchXMLLegendContext.cxx
blob80e829d98d7d2638498b5988d0165cca1eb0998c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "SchXMLLegendContext.hxx"
21 #include "SchXMLEnumConverter.hxx"
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>
29 #include <com/sun/star/chart/ChartLegendPosition.hpp>
30 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
31 #include <com/sun/star/drawing/FillStyle.hpp>
33 using namespace ::xmloff::token;
34 using namespace com::sun::star;
36 using com::sun::star::uno::Reference;
38 namespace
41 enum LegendAttributeTokens
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
55 const SvXMLTokenMapEntry aLegendAttributeTokenMap[] =
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
70 class LegendAttributeTokenMap : public SvXMLTokenMap
72 public:
73 LegendAttributeTokenMap(): SvXMLTokenMap( aLegendAttributeTokenMap ) {}
74 virtual ~LegendAttributeTokenMap() {}
77 //a LegendAttributeTokenMap Singleton
78 struct theLegendAttributeTokenMap : public rtl::Static< LegendAttributeTokenMap, theLegendAttributeTokenMap > {};
80 }//end anonymous namespace
82 SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport, const OUString& rLocalName ) :
83 SvXMLImportContext( rImport, XML_NAMESPACE_CHART, rLocalName ),
84 mrImportHelper( rImpHelper )
88 void SchXMLLegendContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
90 uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
91 if( !xDoc.is() )
92 return;
94 // turn on legend
95 uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
96 if( xDocProp.is() )
98 try
100 xDocProp->setPropertyValue("HasLegend", uno::makeAny( sal_True ) );
102 catch(const beans::UnknownPropertyException&)
104 SAL_INFO("xmloff.chart", "Property HasLegend not found" );
108 uno::Reference< drawing::XShape > xLegendShape( xDoc->getLegend(), uno::UNO_QUERY );
109 uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
110 if( !xLegendShape.is() || !xLegendProps.is() )
112 SAL_INFO("xmloff.chart", "legend could not be created" );
113 return;
116 // parse attributes
117 sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
118 const SvXMLTokenMap& rAttrTokenMap = theLegendAttributeTokenMap::get();
120 awt::Point aLegendPos;
121 bool bHasXPosition=false;
122 bool bHasYPosition=false;
123 awt::Size aLegendSize;
124 bool bHasWidth=false;
125 bool bHasHeight=false;
126 chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
127 bool bHasExpansion=false;
129 OUString sAutoStyleName;
130 uno::Any aAny;
132 for( sal_Int16 i = 0; i < nAttrCount; i++ )
134 OUString sAttrName = xAttrList->getNameByIndex( i );
135 OUString aLocalName;
136 OUString aValue = xAttrList->getValueByIndex( i );
137 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
139 switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
141 case XML_TOK_LEGEND_POSITION:
145 if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() ) )
146 xLegendProps->setPropertyValue("Alignment", aAny );
148 catch(const beans::UnknownPropertyException&)
150 SAL_INFO("xmloff.chart", "Property Alignment (legend) not found" );
153 break;
155 case XML_TOK_LEGEND_X:
156 GetImport().GetMM100UnitConverter().convertMeasureToCore(
157 aLegendPos.X, aValue );
158 bHasXPosition = true;
159 break;
160 case XML_TOK_LEGEND_Y:
161 GetImport().GetMM100UnitConverter().convertMeasureToCore(
162 aLegendPos.Y, aValue );
163 bHasYPosition = true;
164 break;
165 case XML_TOK_LEGEND_STYLE_NAME:
166 sAutoStyleName = aValue;
167 break;
168 case XML_TOK_LEGEND_EXPANSION:
169 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() );
170 bHasExpansion = (aAny>>=nLegendExpansion);
171 break;
172 case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO:
173 break;
174 case XML_TOK_LEGEND_WIDTH:
175 case XML_TOK_LEGEND_WIDTH_EXT:
176 GetImport().GetMM100UnitConverter().convertMeasureToCore(
177 aLegendSize.Width, aValue );
178 bHasWidth = true;
179 break;
180 case XML_TOK_LEGEND_HEIGHT:
181 case XML_TOK_LEGEND_HEIGHT_EXT:
182 GetImport().GetMM100UnitConverter().convertMeasureToCore(
183 aLegendSize.Height, aValue );
184 bHasHeight = true;
185 break;
186 default:
187 break;
191 if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
192 xLegendProps->setPropertyValue("Expansion", uno::makeAny(nLegendExpansion) );
193 else if( bHasHeight && bHasWidth )
194 xLegendShape->setSize( aLegendSize );
196 if( bHasXPosition && bHasYPosition )
197 xLegendShape->setPosition( aLegendPos );
199 // the fill style has the default "none" in XML, but "solid" in the model.
200 xLegendProps->setPropertyValue("FillStyle", uno::makeAny( drawing::FillStyle_NONE ));
202 // set auto-styles for Legend
203 const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
204 if( pStylesCtxt )
206 const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
207 SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
209 if( pStyle && pStyle->ISA( XMLPropStyleContext ))
210 const_cast<XMLPropStyleContext*>( static_cast<const XMLPropStyleContext*>( pStyle ) )->FillPropertySet( xLegendProps );
214 SchXMLLegendContext::~SchXMLLegendContext()
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */