1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
41 enum LegendAttributeTokens
43 XML_TOK_LEGEND_POSITION
,
46 XML_TOK_LEGEND_STYLE_NAME
,
47 XML_TOK_LEGEND_EXPANSION
,
48 XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO
,
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
},
70 class LegendAttributeTokenMap
: public SvXMLTokenMap
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();
95 uno::Reference
< beans::XPropertySet
> xDocProp( xDoc
, uno::UNO_QUERY
);
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" );
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
;
132 for( sal_Int16 i
= 0; i
< nAttrCount
; i
++ )
134 OUString sAttrName
= xAttrList
->getNameByIndex( i
);
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" );
155 case XML_TOK_LEGEND_X
:
156 GetImport().GetMM100UnitConverter().convertMeasureToCore(
157 aLegendPos
.X
, aValue
);
158 bHasXPosition
= true;
160 case XML_TOK_LEGEND_Y
:
161 GetImport().GetMM100UnitConverter().convertMeasureToCore(
162 aLegendPos
.Y
, aValue
);
163 bHasYPosition
= true;
165 case XML_TOK_LEGEND_STYLE_NAME
:
166 sAutoStyleName
= aValue
;
168 case XML_TOK_LEGEND_EXPANSION
:
169 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue
, aAny
, GetImport().GetMM100UnitConverter() );
170 bHasExpansion
= (aAny
>>=nLegendExpansion
);
172 case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO
:
174 case XML_TOK_LEGEND_WIDTH
:
175 case XML_TOK_LEGEND_WIDTH_EXT
:
176 GetImport().GetMM100UnitConverter().convertMeasureToCore(
177 aLegendSize
.Width
, aValue
);
180 case XML_TOK_LEGEND_HEIGHT
:
181 case XML_TOK_LEGEND_HEIGHT_EXT
:
182 GetImport().GetMM100UnitConverter().convertMeasureToCore(
183 aLegendSize
.Height
, aValue
);
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();
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: */