Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / chart / SchXMLLegendContext.cxx
blob04d06c067c539652faeb1953c2823c458e460c21
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 namespace
39 enum LegendAttributeTokens
41 XML_TOK_LEGEND_POSITION,
42 XML_TOK_LEGEND_X,
43 XML_TOK_LEGEND_Y,
44 XML_TOK_LEGEND_STYLE_NAME,
45 XML_TOK_LEGEND_EXPANSION,
46 XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO,
47 XML_TOK_LEGEND_WIDTH,
48 XML_TOK_LEGEND_WIDTH_EXT,
49 XML_TOK_LEGEND_HEIGHT,
50 XML_TOK_LEGEND_HEIGHT_EXT
53 const SvXMLTokenMapEntry aLegendAttributeTokenMap[] =
55 { XML_NAMESPACE_CHART, XML_LEGEND_POSITION, XML_TOK_LEGEND_POSITION },
56 { XML_NAMESPACE_SVG, XML_X, XML_TOK_LEGEND_X },
57 { XML_NAMESPACE_SVG, XML_Y, XML_TOK_LEGEND_Y },
58 { XML_NAMESPACE_CHART, XML_STYLE_NAME, XML_TOK_LEGEND_STYLE_NAME },
59 { XML_NAMESPACE_STYLE, XML_LEGEND_EXPANSION, XML_TOK_LEGEND_EXPANSION },
60 { XML_NAMESPACE_STYLE, XML_LEGEND_EXPANSION_ASPECT_RATIO, XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO },
61 { XML_NAMESPACE_SVG, XML_WIDTH, XML_TOK_LEGEND_WIDTH },
62 { XML_NAMESPACE_CHART_EXT, XML_WIDTH, XML_TOK_LEGEND_WIDTH_EXT },
63 { XML_NAMESPACE_SVG, XML_HEIGHT, XML_TOK_LEGEND_HEIGHT },
64 { XML_NAMESPACE_CHART_EXT, XML_HEIGHT, XML_TOK_LEGEND_HEIGHT_EXT },
65 XML_TOKEN_MAP_END
68 class LegendAttributeTokenMap : public SvXMLTokenMap
70 public:
71 LegendAttributeTokenMap(): SvXMLTokenMap( aLegendAttributeTokenMap ) {}
72 virtual ~LegendAttributeTokenMap() {}
75 //a LegendAttributeTokenMap Singleton
76 struct theLegendAttributeTokenMap : public rtl::Static< LegendAttributeTokenMap, theLegendAttributeTokenMap > {};
78 }//end anonymous namespace
80 SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport, const OUString& rLocalName ) :
81 SvXMLImportContext( rImport, XML_NAMESPACE_CHART, rLocalName ),
82 mrImportHelper( rImpHelper )
86 void SchXMLLegendContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
88 uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
89 if( !xDoc.is() )
90 return;
92 // turn on legend
93 uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
94 if( xDocProp.is() )
96 try
98 xDocProp->setPropertyValue("HasLegend", uno::makeAny( true ) );
100 catch(const beans::UnknownPropertyException&)
102 SAL_INFO("xmloff.chart", "Property HasLegend not found" );
106 uno::Reference< drawing::XShape > xLegendShape( xDoc->getLegend(), uno::UNO_QUERY );
107 uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
108 if( !xLegendShape.is() || !xLegendProps.is() )
110 SAL_INFO("xmloff.chart", "legend could not be created" );
111 return;
114 // parse attributes
115 sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
116 const SvXMLTokenMap& rAttrTokenMap = theLegendAttributeTokenMap::get();
118 awt::Point aLegendPos;
119 bool bHasXPosition=false;
120 bool bHasYPosition=false;
121 awt::Size aLegendSize;
122 bool bHasWidth=false;
123 bool bHasHeight=false;
124 chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
125 bool bHasExpansion=false;
127 OUString sAutoStyleName;
128 uno::Any aAny;
130 for( sal_Int16 i = 0; i < nAttrCount; i++ )
132 OUString sAttrName = xAttrList->getNameByIndex( i );
133 OUString aLocalName;
134 OUString aValue = xAttrList->getValueByIndex( i );
135 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
137 switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
139 case XML_TOK_LEGEND_POSITION:
143 if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() ) )
144 xLegendProps->setPropertyValue("Alignment", aAny );
146 catch(const beans::UnknownPropertyException&)
148 SAL_INFO("xmloff.chart", "Property Alignment (legend) not found" );
151 break;
153 case XML_TOK_LEGEND_X:
154 GetImport().GetMM100UnitConverter().convertMeasureToCore(
155 aLegendPos.X, aValue );
156 bHasXPosition = true;
157 break;
158 case XML_TOK_LEGEND_Y:
159 GetImport().GetMM100UnitConverter().convertMeasureToCore(
160 aLegendPos.Y, aValue );
161 bHasYPosition = true;
162 break;
163 case XML_TOK_LEGEND_STYLE_NAME:
164 sAutoStyleName = aValue;
165 break;
166 case XML_TOK_LEGEND_EXPANSION:
167 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() );
168 bHasExpansion = (aAny>>=nLegendExpansion);
169 break;
170 case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO:
171 break;
172 case XML_TOK_LEGEND_WIDTH:
173 case XML_TOK_LEGEND_WIDTH_EXT:
174 GetImport().GetMM100UnitConverter().convertMeasureToCore(
175 aLegendSize.Width, aValue );
176 bHasWidth = true;
177 break;
178 case XML_TOK_LEGEND_HEIGHT:
179 case XML_TOK_LEGEND_HEIGHT_EXT:
180 GetImport().GetMM100UnitConverter().convertMeasureToCore(
181 aLegendSize.Height, aValue );
182 bHasHeight = true;
183 break;
184 default:
185 break;
189 if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
190 xLegendProps->setPropertyValue("Expansion", uno::makeAny(nLegendExpansion) );
191 else if( bHasHeight && bHasWidth )
192 xLegendShape->setSize( aLegendSize );
194 if( bHasXPosition && bHasYPosition )
195 xLegendShape->setPosition( aLegendPos );
197 // the fill style has the default "none" in XML, but "solid" in the model.
198 xLegendProps->setPropertyValue("FillStyle", uno::makeAny( drawing::FillStyle_NONE ));
200 // set auto-styles for Legend
201 const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
202 if( pStylesCtxt )
204 const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
205 SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
207 if( pStyle && dynamic_cast< const XMLPropStyleContext*>(pStyle) != nullptr)
208 const_cast<XMLPropStyleContext*>( static_cast<const XMLPropStyleContext*>( pStyle ) )->FillPropertySet( xLegendProps );
212 SchXMLLegendContext::~SchXMLLegendContext()
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */