LibreOffice Module chart2 (master) 1
LegendHelper.cxx
Go to the documentation of this file.
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 <Legend.hxx>
22#include <ChartModel.hxx>
23#include <Diagram.hxx>
24#include <com/sun/star/chart/ChartLegendExpansion.hpp>
25#include <com/sun/star/chart2/LegendPosition.hpp>
26#include <com/sun/star/chart2/RelativePosition.hpp>
27#include <com/sun/star/uno/XComponentContext.hpp>
29
30using namespace ::com::sun::star;
31using ::com::sun::star::uno::Reference;
32
33namespace chart
34{
35
38{
39 rtl::Reference< Legend > xLegend = LegendHelper::getLegend( rModel, xContext, true );
40 if( xLegend.is())
41 {
42 xLegend->setPropertyValue( "Show", uno::Any(true) );
43
44 chart2::RelativePosition aRelativePosition;
45 if( !(xLegend->getPropertyValue( "RelativePosition") >>= aRelativePosition) )
46 {
47 chart2::LegendPosition ePos = chart2::LegendPosition_LINE_END;
48 if( !(xLegend->getPropertyValue( "AnchorPosition") >>= ePos ) )
49 xLegend->setPropertyValue( "AnchorPosition", uno::Any( ePos ));
50
51 css::chart::ChartLegendExpansion eExpansion =
52 ( ePos == chart2::LegendPosition_LINE_END ||
53 ePos == chart2::LegendPosition_LINE_START )
54 ? css::chart::ChartLegendExpansion_HIGH
55 : css::chart::ChartLegendExpansion_WIDE;
56 if( !(xLegend->getPropertyValue( "Expansion") >>= eExpansion ) )
57 xLegend->setPropertyValue( "Expansion", uno::Any( eExpansion ));
58
59 xLegend->setPropertyValue( "RelativePosition", uno::Any());
60 }
61
62 }
63 return xLegend;
64}
65
66void LegendHelper::hideLegend( ChartModel& rModel )
67{
68 rtl::Reference< Legend > xLegend = LegendHelper::getLegend( rModel, nullptr );
69 if( xLegend.is())
70 {
71 xLegend->setPropertyValue( "Show", uno::Any(false) );
72 }
73}
74
76 ChartModel& rModel
78 , bool bCreate )
79{
81
82 try
83 {
84 rtl::Reference< Diagram > xDia( rModel.getFirstChartDiagram());
85 if( xDia.is() )
86 {
87 xResult = xDia->getLegend2();
88 if( bCreate && !xResult.is() && xContext.is() )
89 {
90 xResult = new Legend();
91 xDia->setLegend( xResult );
92 }
93 }
94 else if(bCreate)
95 {
96 OSL_FAIL("need diagram for creation of legend");
97 }
98 }
99 catch( const uno::Exception & )
100 {
101 DBG_UNHANDLED_EXCEPTION("chart2");
102 }
103
104 return xResult;
105}
106
108{
109 bool bReturn = false;
110 if( xDiagram.is())
111 {
112 uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
113 if( xLegendProp.is())
114 xLegendProp->getPropertyValue( "Show") >>= bReturn;
115 }
116
117 return bReturn;
118}
119
120} //namespace chart
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::chart::ChartAxisLabelPosition ePos
static bool hasLegend(const rtl::Reference< ::chart::Diagram > &xDiagram)
returns <FALSE>, if either there is no legend at the diagram, or there is a legend which has a "Show"...
static rtl::Reference< ::chart::Legend > getLegend(ChartModel &rModel, const css::uno::Reference< css::uno::XComponentContext > &xContext=nullptr, bool bCreate=false)
static rtl::Reference< ::chart::Legend > showLegend(ChartModel &rModel, const css::uno::Reference< css::uno::XComponentContext > &xContext)
static void hideLegend(ChartModel &rModel)
#define DBG_UNHANDLED_EXCEPTION(...)