xref: /AOO41X/main/xmloff/source/chart/SchXMLLegendContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir #include "SchXMLLegendContext.hxx"
31*cdf0e10cSrcweir #include "SchXMLEnumConverter.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <xmloff/xmlnmspe.hxx>
34*cdf0e10cSrcweir #include <xmloff/xmlement.hxx>
35*cdf0e10cSrcweir #include <xmloff/prstylei.hxx>
36*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
37*cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <tools/debug.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/chart/ChartLegendPosition.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/chart/ChartLegendExpansion.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/drawing/FillStyle.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::xmloff::token;
46*cdf0e10cSrcweir using namespace com::sun::star;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using rtl::OUString;
49*cdf0e10cSrcweir using com::sun::star::uno::Reference;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //----------------------------------------
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir namespace
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir enum LegendAttributeTokens
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     XML_TOK_LEGEND_POSITION,
59*cdf0e10cSrcweir     XML_TOK_LEGEND_X,
60*cdf0e10cSrcweir     XML_TOK_LEGEND_Y,
61*cdf0e10cSrcweir     XML_TOK_LEGEND_STYLE_NAME,
62*cdf0e10cSrcweir     XML_TOK_LEGEND_EXPANSION,
63*cdf0e10cSrcweir     XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO,
64*cdf0e10cSrcweir     XML_TOK_LEGEND_WIDTH,
65*cdf0e10cSrcweir     XML_TOK_LEGEND_WIDTH_EXT,
66*cdf0e10cSrcweir     XML_TOK_LEGEND_HEIGHT,
67*cdf0e10cSrcweir     XML_TOK_LEGEND_HEIGHT_EXT
68*cdf0e10cSrcweir };
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir SvXMLTokenMapEntry aLegendAttributeTokenMap[] =
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     { XML_NAMESPACE_CHART,      XML_LEGEND_POSITION,    XML_TOK_LEGEND_POSITION     },
73*cdf0e10cSrcweir     { XML_NAMESPACE_SVG,        XML_X,                  XML_TOK_LEGEND_X            },
74*cdf0e10cSrcweir     { XML_NAMESPACE_SVG,        XML_Y,                  XML_TOK_LEGEND_Y            },
75*cdf0e10cSrcweir     { XML_NAMESPACE_CHART,      XML_STYLE_NAME,         XML_TOK_LEGEND_STYLE_NAME   },
76*cdf0e10cSrcweir     { XML_NAMESPACE_STYLE,      XML_LEGEND_EXPANSION,   XML_TOK_LEGEND_EXPANSION    },
77*cdf0e10cSrcweir     { XML_NAMESPACE_STYLE,      XML_LEGEND_EXPANSION_ASPECT_RATIO,   XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO    },
78*cdf0e10cSrcweir     { XML_NAMESPACE_SVG,        XML_WIDTH,              XML_TOK_LEGEND_WIDTH        },
79*cdf0e10cSrcweir     { XML_NAMESPACE_CHART_EXT,  XML_WIDTH,              XML_TOK_LEGEND_WIDTH_EXT    },
80*cdf0e10cSrcweir     { XML_NAMESPACE_SVG,        XML_HEIGHT,             XML_TOK_LEGEND_HEIGHT       },
81*cdf0e10cSrcweir     { XML_NAMESPACE_CHART_EXT,  XML_HEIGHT,             XML_TOK_LEGEND_HEIGHT_EXT   },
82*cdf0e10cSrcweir     XML_TOKEN_MAP_END
83*cdf0e10cSrcweir };
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir class LegendAttributeTokenMap : public SvXMLTokenMap
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir public:
88*cdf0e10cSrcweir     LegendAttributeTokenMap(): SvXMLTokenMap( aLegendAttributeTokenMap ) {}
89*cdf0e10cSrcweir     virtual ~LegendAttributeTokenMap() {}
90*cdf0e10cSrcweir };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir //a LegendAttributeTokenMap Singleton
93*cdf0e10cSrcweir struct theLegendAttributeTokenMap : public rtl::Static< LegendAttributeTokenMap, theLegendAttributeTokenMap > {};
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir }//end anonymous namespace
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir //----------------------------------------
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport, const rtl::OUString& rLocalName ) :
100*cdf0e10cSrcweir     SvXMLImportContext( rImport, XML_NAMESPACE_CHART, rLocalName ),
101*cdf0e10cSrcweir     mrImportHelper( rImpHelper )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void SchXMLLegendContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
108*cdf0e10cSrcweir     if( !xDoc.is() )
109*cdf0e10cSrcweir         return;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     // turn on legend
112*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
113*cdf0e10cSrcweir     if( xDocProp.is() )
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         try
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir             xDocProp->setPropertyValue( rtl::OUString::createFromAscii( "HasLegend" ), uno::makeAny( sal_True ) );
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         catch( beans::UnknownPropertyException )
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             DBG_ERROR( "Property HasLegend not found" );
122*cdf0e10cSrcweir         }
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     uno::Reference< drawing::XShape > xLegendShape( xDoc->getLegend(), uno::UNO_QUERY );
126*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
127*cdf0e10cSrcweir     if( !xLegendShape.is() || !xLegendProps.is() )
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         DBG_ERROR( "legend could not be created" );
130*cdf0e10cSrcweir         return;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     // parse attributes
134*cdf0e10cSrcweir     sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
135*cdf0e10cSrcweir     const SvXMLTokenMap& rAttrTokenMap = theLegendAttributeTokenMap::get();
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     awt::Point aLegendPos;
138*cdf0e10cSrcweir     bool bHasXPosition=false;
139*cdf0e10cSrcweir     bool bHasYPosition=false;
140*cdf0e10cSrcweir     awt::Size aLegendSize;
141*cdf0e10cSrcweir     bool bHasWidth=false;
142*cdf0e10cSrcweir     bool bHasHeight=false;
143*cdf0e10cSrcweir     chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
144*cdf0e10cSrcweir     bool bHasExpansion=false;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     rtl::OUString sAutoStyleName;
147*cdf0e10cSrcweir     uno::Any aAny;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     for( sal_Int16 i = 0; i < nAttrCount; i++ )
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         rtl::OUString sAttrName = xAttrList->getNameByIndex( i );
152*cdf0e10cSrcweir         rtl::OUString aLocalName;
153*cdf0e10cSrcweir         rtl::OUString aValue = xAttrList->getValueByIndex( i );
154*cdf0e10cSrcweir         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
157*cdf0e10cSrcweir         {
158*cdf0e10cSrcweir             case XML_TOK_LEGEND_POSITION:
159*cdf0e10cSrcweir                 {
160*cdf0e10cSrcweir                     try
161*cdf0e10cSrcweir                     {
162*cdf0e10cSrcweir                         if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() ) )
163*cdf0e10cSrcweir                             xLegendProps->setPropertyValue( rtl::OUString::createFromAscii( "Alignment" ), aAny );
164*cdf0e10cSrcweir                     }
165*cdf0e10cSrcweir                     catch( beans::UnknownPropertyException )
166*cdf0e10cSrcweir                     {
167*cdf0e10cSrcweir                         DBG_ERROR( "Property Alignment (legend) not found" );
168*cdf0e10cSrcweir                     }
169*cdf0e10cSrcweir                 }
170*cdf0e10cSrcweir                 break;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             case XML_TOK_LEGEND_X:
173*cdf0e10cSrcweir                 GetImport().GetMM100UnitConverter().convertMeasure( aLegendPos.X, aValue );
174*cdf0e10cSrcweir                 bHasXPosition = true;
175*cdf0e10cSrcweir                 break;
176*cdf0e10cSrcweir             case XML_TOK_LEGEND_Y:
177*cdf0e10cSrcweir                 GetImport().GetMM100UnitConverter().convertMeasure( aLegendPos.Y, aValue );
178*cdf0e10cSrcweir                 bHasYPosition = true;
179*cdf0e10cSrcweir                 break;
180*cdf0e10cSrcweir             case XML_TOK_LEGEND_STYLE_NAME:
181*cdf0e10cSrcweir                 sAutoStyleName = aValue;
182*cdf0e10cSrcweir                 break;
183*cdf0e10cSrcweir             case XML_TOK_LEGEND_EXPANSION:
184*cdf0e10cSrcweir                 SchXMLEnumConverter::getLegendPositionConverter().importXML( aValue, aAny, GetImport().GetMM100UnitConverter() );
185*cdf0e10cSrcweir                 bHasExpansion = (aAny>>=nLegendExpansion);
186*cdf0e10cSrcweir                 break;
187*cdf0e10cSrcweir             case XML_TOK_LEGEND_EXPANSION_ASPECT_RATIO:
188*cdf0e10cSrcweir                 break;
189*cdf0e10cSrcweir             case XML_TOK_LEGEND_WIDTH:
190*cdf0e10cSrcweir             case XML_TOK_LEGEND_WIDTH_EXT:
191*cdf0e10cSrcweir                 GetImport().GetMM100UnitConverter().convertMeasure( aLegendSize.Width, aValue );
192*cdf0e10cSrcweir                 bHasWidth = true;
193*cdf0e10cSrcweir                 break;
194*cdf0e10cSrcweir             case XML_TOK_LEGEND_HEIGHT:
195*cdf0e10cSrcweir             case XML_TOK_LEGEND_HEIGHT_EXT:
196*cdf0e10cSrcweir                 GetImport().GetMM100UnitConverter().convertMeasure( aLegendSize.Height, aValue );
197*cdf0e10cSrcweir                 bHasHeight = true;
198*cdf0e10cSrcweir                 break;
199*cdf0e10cSrcweir             default:
200*cdf0e10cSrcweir                 break;
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     if( bHasXPosition && bHasYPosition )
205*cdf0e10cSrcweir         xLegendShape->setPosition( aLegendPos );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
208*cdf0e10cSrcweir         xLegendProps->setPropertyValue( rtl::OUString::createFromAscii( "Expansion" ), uno::makeAny(nLegendExpansion) );
209*cdf0e10cSrcweir     else if( bHasHeight && bHasWidth )
210*cdf0e10cSrcweir         xLegendShape->setSize( aLegendSize );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     // the fill style has the default "none" in XML, but "solid" in the model.
213*cdf0e10cSrcweir     xLegendProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FillStyle" )), uno::makeAny( drawing::FillStyle_NONE ));
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // set auto-styles for Legend
216*cdf0e10cSrcweir     const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
217*cdf0e10cSrcweir     if( pStylesCtxt )
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
220*cdf0e10cSrcweir             mrImportHelper.GetChartFamilyID(), sAutoStyleName );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         if( pStyle && pStyle->ISA( XMLPropStyleContext ))
223*cdf0e10cSrcweir             (( XMLPropStyleContext* )pStyle )->FillPropertySet( xLegendProps );
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir SchXMLLegendContext::~SchXMLLegendContext()
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir }
230