Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "LegendHelper.hxx"
21 : #include "macros.hxx"
22 : #include <com/sun/star/chart/ChartLegendExpansion.hpp>
23 : #include <com/sun/star/chart2/LegendPosition.hpp>
24 : #include <com/sun/star/chart2/RelativePosition.hpp>
25 : #include <com/sun/star/chart2/XChartDocument.hpp>
26 : #include <com/sun/star/chart2/XLegend.hpp>
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 :
29 : using namespace ::com::sun::star;
30 : using ::com::sun::star::uno::Reference;
31 :
32 : namespace chart
33 : {
34 :
35 0 : Reference< chart2::XLegend > LegendHelper::showLegend( ChartModel& rModel
36 : , const uno::Reference< uno::XComponentContext >& xContext )
37 : {
38 0 : uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( rModel, xContext, true );
39 0 : uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
40 0 : if( xProp.is())
41 : {
42 0 : xProp->setPropertyValue( "Show", uno::makeAny(sal_True) );
43 :
44 0 : chart2::RelativePosition aRelativePosition;
45 0 : if( !(xProp->getPropertyValue( "RelativePosition") >>= aRelativePosition) )
46 : {
47 0 : chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
48 0 : if( !(xProp->getPropertyValue( "AnchorPosition") >>= ePos ) )
49 0 : xProp->setPropertyValue( "AnchorPosition", uno::makeAny( ePos ));
50 :
51 : ::com::sun::star::chart::ChartLegendExpansion eExpansion =
52 0 : ( ePos == chart2::LegendPosition_LINE_END ||
53 0 : ePos == chart2::LegendPosition_LINE_START )
54 : ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
55 0 : : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
56 0 : if( !(xProp->getPropertyValue( "Expansion") >>= eExpansion ) )
57 0 : xProp->setPropertyValue( "Expansion", uno::makeAny( eExpansion ));
58 :
59 0 : xProp->setPropertyValue( "RelativePosition", uno::Any());
60 : }
61 :
62 : }
63 0 : return xLegend;
64 : }
65 :
66 0 : void LegendHelper::hideLegend( ChartModel& rModel )
67 : {
68 0 : uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( rModel, 0, false );
69 0 : uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
70 0 : if( xProp.is())
71 : {
72 0 : xProp->setPropertyValue( "Show", uno::makeAny(sal_False) );
73 0 : }
74 0 : }
75 :
76 2014 : uno::Reference< chart2::XLegend > LegendHelper::getLegend(
77 : ChartModel& rModel
78 : , const uno::Reference< uno::XComponentContext >& xContext
79 : , bool bCreate )
80 : {
81 2014 : uno::Reference< chart2::XLegend > xResult;
82 :
83 : try
84 : {
85 2014 : uno::Reference< chart2::XDiagram > xDia( rModel.getFirstDiagram());
86 2014 : if( xDia.is() )
87 : {
88 1946 : xResult.set( xDia->getLegend() );
89 1946 : if( bCreate && !xResult.is() && xContext.is() )
90 : {
91 122 : xResult.set( xContext->getServiceManager()->createInstanceWithContext(
92 61 : "com.sun.star.chart2.Legend", xContext ), uno::UNO_QUERY );
93 61 : xDia->setLegend( xResult );
94 : }
95 : }
96 : else if(bCreate)
97 : {
98 : OSL_FAIL("need diagram for creation of legend");
99 2014 : }
100 : }
101 0 : catch( const uno::Exception & ex )
102 : {
103 : ASSERT_EXCEPTION( ex );
104 : }
105 :
106 2014 : return xResult;
107 : }
108 :
109 1624 : bool LegendHelper::hasLegend( const uno::Reference< chart2::XDiagram > & xDiagram )
110 : {
111 1624 : bool bReturn = false;
112 1624 : if( xDiagram.is())
113 : {
114 1608 : uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
115 1608 : if( xLegendProp.is())
116 1608 : xLegendProp->getPropertyValue( "Show") >>= bReturn;
117 : }
118 :
119 1624 : return bReturn;
120 : }
121 :
122 : } //namespace chart
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|