xref: /AOO41X/main/chart2/source/tools/LegendHelper.cxx (revision cde9e8dc2218e857da4894ecba5c903312256674)
1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "LegendHelper.hxx"
27cdf0e10cSrcweir #include "macros.hxx"
28cdf0e10cSrcweir #include <com/sun/star/chart/ChartLegendExpansion.hpp>
29cdf0e10cSrcweir #include <com/sun/star/chart2/LegendPosition.hpp>
30cdf0e10cSrcweir #include <com/sun/star/chart2/RelativePosition.hpp>
31cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp>
32cdf0e10cSrcweir #include <com/sun/star/chart2/XLegend.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <tools/debug.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //.............................................................................
40cdf0e10cSrcweir namespace chart
41cdf0e10cSrcweir {
42cdf0e10cSrcweir //.............................................................................
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
showLegend(const Reference<frame::XModel> & xModel,const uno::Reference<uno::XComponentContext> & xContext)45cdf0e10cSrcweir Reference< chart2::XLegend > LegendHelper::showLegend( const Reference< frame::XModel >& xModel
46cdf0e10cSrcweir                                                     , const uno::Reference< uno::XComponentContext >& xContext )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, xContext, true );
49cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
50cdf0e10cSrcweir     if( xProp.is())
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_True) );
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         chart2::RelativePosition aRelativePosition;
55cdf0e10cSrcweir         if( !(xProp->getPropertyValue( C2U( "RelativePosition" )) >>=  aRelativePosition) )
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
58cdf0e10cSrcweir             if( !(xProp->getPropertyValue( C2U( "AnchorPosition" )) >>= ePos ) )
59cdf0e10cSrcweir                 xProp->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( ePos ));
60cdf0e10cSrcweir 
61cdf0e10cSrcweir             ::com::sun::star::chart::ChartLegendExpansion eExpansion =
62cdf0e10cSrcweir                     ( ePos == chart2::LegendPosition_LINE_END ||
63cdf0e10cSrcweir                       ePos == chart2::LegendPosition_LINE_START )
64cdf0e10cSrcweir                     ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
65cdf0e10cSrcweir                     : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
66cdf0e10cSrcweir             if( !(xProp->getPropertyValue( C2U( "Expansion" )) >>= eExpansion ) )
67cdf0e10cSrcweir                 xProp->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExpansion ));
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             xProp->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir     return xLegend;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
hideLegend(const Reference<frame::XModel> & xModel)76cdf0e10cSrcweir void LegendHelper::hideLegend( const Reference< frame::XModel >& xModel )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, 0, false );
79cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
80cdf0e10cSrcweir     if( xProp.is())
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_False) );
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
getLegend(const uno::Reference<frame::XModel> & xModel,const uno::Reference<uno::XComponentContext> & xContext,bool bCreate)86cdf0e10cSrcweir uno::Reference< chart2::XLegend > LegendHelper::getLegend(
87cdf0e10cSrcweir       const uno::Reference< frame::XModel >& xModel
88cdf0e10cSrcweir     , const uno::Reference< uno::XComponentContext >& xContext
89cdf0e10cSrcweir     , bool bCreate )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     uno::Reference< chart2::XLegend > xResult;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     uno::Reference< chart2::XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
94cdf0e10cSrcweir     if( xChartDoc.is())
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         try
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             uno::Reference< chart2::XDiagram > xDia( xChartDoc->getFirstDiagram());
99cdf0e10cSrcweir             if( xDia.is() )
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 xResult.set( xDia->getLegend() );
102cdf0e10cSrcweir                 if( bCreate && !xResult.is() && xContext.is() )
103cdf0e10cSrcweir                 {
104cdf0e10cSrcweir                     xResult.set( xContext->getServiceManager()->createInstanceWithContext(
105cdf0e10cSrcweir                         C2U( "com.sun.star.chart2.Legend" ), xContext ), uno::UNO_QUERY );
106cdf0e10cSrcweir                     xDia->setLegend( xResult );
107cdf0e10cSrcweir                 }
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir             else if(bCreate)
110cdf0e10cSrcweir             {
111cdf0e10cSrcweir                 DBG_ERROR("need diagram for creation of legend");
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         catch( uno::Exception & ex )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             ASSERT_EXCEPTION( ex );
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     return xResult;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
hasLegend(const uno::Reference<chart2::XDiagram> & xDiagram)123cdf0e10cSrcweir bool LegendHelper::hasLegend( const uno::Reference< chart2::XDiagram > & xDiagram )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     bool bReturn = false;
126cdf0e10cSrcweir     if( xDiagram.is())
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
129cdf0e10cSrcweir         if( xLegendProp.is())
130cdf0e10cSrcweir             xLegendProp->getPropertyValue( C2U("Show")) >>= bReturn;
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     return bReturn;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir //.............................................................................
137cdf0e10cSrcweir } //namespace chart
138cdf0e10cSrcweir //.............................................................................
139cdf0e10cSrcweir 
140