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