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 <ChartModel.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/uno/XComponentContext.hpp>
26 #include <tools/diagnose_ex.h>
27
28 using namespace ::com::sun::star;
29 using ::com::sun::star::uno::Reference;
30
31 namespace chart
32 {
33
showLegend(ChartModel & rModel,const uno::Reference<uno::XComponentContext> & xContext)34 Reference< chart2::XLegend > LegendHelper::showLegend( ChartModel& rModel
35 , const uno::Reference< uno::XComponentContext >& xContext )
36 {
37 uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( rModel, xContext, true );
38 uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
39 if( xProp.is())
40 {
41 xProp->setPropertyValue( "Show", uno::Any(true) );
42
43 chart2::RelativePosition aRelativePosition;
44 if( !(xProp->getPropertyValue( "RelativePosition") >>= aRelativePosition) )
45 {
46 chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
47 if( !(xProp->getPropertyValue( "AnchorPosition") >>= ePos ) )
48 xProp->setPropertyValue( "AnchorPosition", uno::Any( ePos ));
49
50 css::chart::ChartLegendExpansion eExpansion =
51 ( ePos == chart2::LegendPosition_LINE_END ||
52 ePos == chart2::LegendPosition_LINE_START )
53 ? css::chart::ChartLegendExpansion_HIGH
54 : css::chart::ChartLegendExpansion_WIDE;
55 if( !(xProp->getPropertyValue( "Expansion") >>= eExpansion ) )
56 xProp->setPropertyValue( "Expansion", uno::Any( eExpansion ));
57
58 xProp->setPropertyValue( "RelativePosition", uno::Any());
59 }
60
61 }
62 return xLegend;
63 }
64
hideLegend(ChartModel & rModel)65 void LegendHelper::hideLegend( ChartModel& rModel )
66 {
67 uno::Reference< chart2::XLegend > xLegend = LegendHelper::getLegend( rModel, nullptr );
68 uno::Reference< beans::XPropertySet > xProp( xLegend, uno::UNO_QUERY );
69 if( xProp.is())
70 {
71 xProp->setPropertyValue( "Show", uno::Any(false) );
72 }
73 }
74
getLegend(ChartModel & rModel,const uno::Reference<uno::XComponentContext> & xContext,bool bCreate)75 uno::Reference< chart2::XLegend > LegendHelper::getLegend(
76 ChartModel& rModel
77 , const uno::Reference< uno::XComponentContext >& xContext
78 , bool bCreate )
79 {
80 uno::Reference< chart2::XLegend > xResult;
81
82 try
83 {
84 uno::Reference< chart2::XDiagram > xDia( rModel.getFirstDiagram());
85 if( xDia.is() )
86 {
87 xResult.set( xDia->getLegend() );
88 if( bCreate && !xResult.is() && xContext.is() )
89 {
90 xResult.set( xContext->getServiceManager()->createInstanceWithContext(
91 "com.sun.star.chart2.Legend", xContext ), uno::UNO_QUERY );
92 xDia->setLegend( xResult );
93 }
94 }
95 else if(bCreate)
96 {
97 OSL_FAIL("need diagram for creation of legend");
98 }
99 }
100 catch( const uno::Exception & )
101 {
102 DBG_UNHANDLED_EXCEPTION("chart2");
103 }
104
105 return xResult;
106 }
107
hasLegend(const uno::Reference<chart2::XDiagram> & xDiagram)108 bool LegendHelper::hasLegend( const uno::Reference< chart2::XDiagram > & xDiagram )
109 {
110 bool bReturn = false;
111 if( xDiagram.is())
112 {
113 uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
114 if( xLegendProp.is())
115 xLegendProp->getPropertyValue( "Show") >>= bReturn;
116 }
117
118 return bReturn;
119 }
120
121 } //namespace chart
122
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
124