1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
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/chart/ChartLegendExpansion.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>
39 
40 using namespace ::com::sun::star;
41 using ::com::sun::star::uno::Reference;
42 
43 //.............................................................................
44 namespace chart
45 {
46 //.............................................................................
47 
48 
49 Reference< chart2::XLegend > LegendHelper::showLegend( const Reference< frame::XModel >& xModel
50                                                     , const uno::Reference< uno::XComponentContext >& xContext )
51 {
52     uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, xContext, true );
53     uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
54     if( xProp.is())
55     {
56         xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_True) );
57 
58         chart2::RelativePosition aRelativePosition;
59         if( !(xProp->getPropertyValue( C2U( "RelativePosition" )) >>=  aRelativePosition) )
60         {
61             chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
62             if( !(xProp->getPropertyValue( C2U( "AnchorPosition" )) >>= ePos ) )
63                 xProp->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( ePos ));
64 
65             ::com::sun::star::chart::ChartLegendExpansion eExpansion =
66                     ( ePos == chart2::LegendPosition_LINE_END ||
67                       ePos == chart2::LegendPosition_LINE_START )
68                     ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
69                     : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
70             if( !(xProp->getPropertyValue( C2U( "Expansion" )) >>= eExpansion ) )
71                 xProp->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eExpansion ));
72 
73             xProp->setPropertyValue( C2U( "RelativePosition" ), uno::Any());
74         }
75 
76     }
77     return xLegend;
78 }
79 
80 void LegendHelper::hideLegend( const Reference< frame::XModel >& xModel )
81 {
82     uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( xModel, 0, false );
83     uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
84     if( xProp.is())
85     {
86         xProp->setPropertyValue( C2U("Show"), uno::makeAny(sal_False) );
87     }
88 }
89 
90 uno::Reference< chart2::XLegend > LegendHelper::getLegend(
91       const uno::Reference< frame::XModel >& xModel
92     , const uno::Reference< uno::XComponentContext >& xContext
93     , bool bCreate )
94 {
95     uno::Reference< chart2::XLegend > xResult;
96 
97     uno::Reference< chart2::XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
98     if( xChartDoc.is())
99     {
100         try
101         {
102             uno::Reference< chart2::XDiagram > xDia( xChartDoc->getFirstDiagram());
103             if( xDia.is() )
104             {
105                 xResult.set( xDia->getLegend() );
106                 if( bCreate && !xResult.is() && xContext.is() )
107                 {
108                     xResult.set( xContext->getServiceManager()->createInstanceWithContext(
109                         C2U( "com.sun.star.chart2.Legend" ), xContext ), uno::UNO_QUERY );
110                     xDia->setLegend( xResult );
111                 }
112             }
113             else if(bCreate)
114             {
115                 DBG_ERROR("need diagram for creation of legend");
116             }
117         }
118         catch( uno::Exception & ex )
119         {
120             ASSERT_EXCEPTION( ex );
121         }
122     }
123 
124     return xResult;
125 }
126 
127 bool LegendHelper::hasLegend( const uno::Reference< chart2::XDiagram > & xDiagram )
128 {
129     bool bReturn = false;
130     if( xDiagram.is())
131     {
132         uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
133         if( xLegendProp.is())
134             xLegendProp->getPropertyValue( C2U("Show")) >>= bReturn;
135     }
136 
137     return bReturn;
138 }
139 
140 //.............................................................................
141 } //namespace chart
142 //.............................................................................
143 
144