1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <LegendHelper.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>
28 #include <comphelper/diagnose_ex.hxx>
30 using namespace ::com::sun::star
;
31 using ::com::sun::star::uno::Reference
;
36 rtl::Reference
< Legend
> LegendHelper::showLegend( ChartModel
& rModel
37 , const uno::Reference
< uno::XComponentContext
>& xContext
)
39 rtl::Reference
< Legend
> xLegend
= LegendHelper::getLegend( rModel
, xContext
, true );
42 xLegend
->setPropertyValue( u
"Show"_ustr
, uno::Any(true) );
44 chart2::RelativePosition aRelativePosition
;
45 if( !(xLegend
->getPropertyValue( u
"RelativePosition"_ustr
) >>= aRelativePosition
) )
47 chart2::LegendPosition ePos
= chart2::LegendPosition_LINE_END
;
48 if( !(xLegend
->getPropertyValue( u
"AnchorPosition"_ustr
) >>= ePos
) )
49 xLegend
->setPropertyValue( u
"AnchorPosition"_ustr
, uno::Any( ePos
));
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( u
"Expansion"_ustr
) >>= eExpansion
) )
57 xLegend
->setPropertyValue( u
"Expansion"_ustr
, uno::Any( eExpansion
));
59 xLegend
->setPropertyValue( u
"RelativePosition"_ustr
, uno::Any());
66 void LegendHelper::hideLegend( ChartModel
& rModel
)
68 rtl::Reference
< Legend
> xLegend
= LegendHelper::getLegend( rModel
, nullptr );
71 xLegend
->setPropertyValue( u
"Show"_ustr
, uno::Any(false) );
75 rtl::Reference
< Legend
> LegendHelper::getLegend(
77 , const uno::Reference
< uno::XComponentContext
>& xContext
80 rtl::Reference
< Legend
> xResult
;
84 rtl::Reference
< Diagram
> xDia( rModel
.getFirstChartDiagram());
87 xResult
= xDia
->getLegend2();
88 if( bCreate
&& !xResult
.is() && xContext
.is() )
90 xResult
= new Legend();
91 xDia
->setLegend( xResult
);
96 OSL_FAIL("need diagram for creation of legend");
99 catch( const uno::Exception
& )
101 DBG_UNHANDLED_EXCEPTION("chart2");
107 bool LegendHelper::hasLegend( const rtl::Reference
< Diagram
> & xDiagram
)
109 bool bReturn
= false;
112 uno::Reference
< beans::XPropertySet
> xLegendProp( xDiagram
->getLegend(), uno::UNO_QUERY
);
113 if( xLegendProp
.is())
114 xLegendProp
->getPropertyValue( u
"Show"_ustr
) >>= bReturn
;
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */