merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / LegendHelper.cxx
blob4e778db1a68ceb92e5d21b84c019b757a7ca83a1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "LegendHelper.hxx"
31 #include "macros.hxx"
32 #include <com/sun/star/chart2/LegendExpansion.hpp>
33 #include <com/sun/star/chart2/LegendPosition.hpp>
34 #include <com/sun/star/chart2/RelativePosition.hpp>
35 #include <com/sun/star/chart2/XChartDocument.hpp>
36 #include <com/sun/star/chart2/XLegend.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <tools/debug.hxx>
40 using namespace ::com::sun::star;
41 using ::com::sun::star::uno::Reference;
43 //.............................................................................
44 namespace chart
46 //.............................................................................
49 //static
50 Reference< chart2::XLegend > LegendHelper::showLegend( const Reference< frame::XModel >& xModel
51 , const uno::Reference< uno::XComponentContext >& xContext )
53 uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, xContext, true );
54 uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
55 if( xProp.is())
57 xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_True) );
59 chart2::RelativePosition aRelativePosition;
60 if( !(xProp->getPropertyValue( C2U( "RelativePosition" )) >>= aRelativePosition) )
62 chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
63 if( !(xProp->getPropertyValue( C2U( "AnchorPosition" )) >>= ePos ) )
64 xProp->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( ePos ));
66 chart2::LegendExpansion eExpansion =
67 ( ePos == chart2::LegendPosition_LINE_END ||
68 ePos == chart2::LegendPosition_LINE_START )
69 ? chart2::LegendExpansion_HIGH
70 : chart2::LegendExpansion_WIDE;
71 if( !(xProp->getPropertyValue( C2U( "Expansion" )) >>= eExpansion ) )
72 xProp->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExpansion ));
74 xProp->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
78 return xLegend;
81 //static
82 void LegendHelper::hideLegend( const Reference< frame::XModel >& xModel )
84 uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, 0, false );
85 uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
86 if( xProp.is())
88 xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_False) );
92 // static
93 uno::Reference< chart2::XLegend > LegendHelper::getLegend(
94 const uno::Reference< frame::XModel >& xModel
95 , const uno::Reference< uno::XComponentContext >& xContext
96 , bool bCreate )
98 uno::Reference< chart2::XLegend > xResult;
100 uno::Reference< chart2::XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
101 if( xChartDoc.is())
105 uno::Reference< chart2::XDiagram > xDia( xChartDoc->getFirstDiagram());
106 if( xDia.is() )
108 xResult.set( xDia->getLegend() );
109 if( bCreate && !xResult.is() && xContext.is() )
111 xResult.set( xContext->getServiceManager()->createInstanceWithContext(
112 C2U( "com.sun.star.chart2.Legend" ), xContext ), uno::UNO_QUERY );
113 xDia->setLegend( xResult );
116 else if(bCreate)
118 DBG_ERROR("need diagram for creation of legend");
121 catch( uno::Exception & ex )
123 ASSERT_EXCEPTION( ex );
127 return xResult;
130 // static
131 bool LegendHelper::hasLegend( const uno::Reference< chart2::XDiagram > & xDiagram )
133 bool bReturn = false;
134 if( xDiagram.is())
136 uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
137 if( xLegendProp.is())
138 xLegendProp->getPropertyValue( C2U("Show")) >>= bReturn;
141 return bReturn;
144 //.............................................................................
145 } //namespace chart
146 //.............................................................................