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 <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>
29 using namespace ::com::sun::star
;
30 using ::com::sun::star::uno::Reference
;
32 //.............................................................................
35 //.............................................................................
38 Reference
< chart2::XLegend
> LegendHelper::showLegend( const Reference
< frame::XModel
>& xModel
39 , const uno::Reference
< uno::XComponentContext
>& xContext
)
41 uno::Reference
< chart2::XLegend
> xLegend
= LegendHelper::getLegend( xModel
, xContext
, true );
42 uno::Reference
< beans::XPropertySet
> xProp( xLegend
, uno::UNO_QUERY
);
45 xProp
->setPropertyValue( "Show", uno::makeAny(sal_True
) );
47 chart2::RelativePosition aRelativePosition
;
48 if( !(xProp
->getPropertyValue( "RelativePosition") >>= aRelativePosition
) )
50 chart2::LegendPosition ePos
= chart2::LegendPosition_LINE_END
;
51 if( !(xProp
->getPropertyValue( "AnchorPosition") >>= ePos
) )
52 xProp
->setPropertyValue( "AnchorPosition", uno::makeAny( ePos
));
54 ::com::sun::star::chart::ChartLegendExpansion eExpansion
=
55 ( ePos
== chart2::LegendPosition_LINE_END
||
56 ePos
== chart2::LegendPosition_LINE_START
)
57 ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
58 : ::com::sun::star::chart::ChartLegendExpansion_WIDE
;
59 if( !(xProp
->getPropertyValue( "Expansion") >>= eExpansion
) )
60 xProp
->setPropertyValue( "Expansion", uno::makeAny( eExpansion
));
62 xProp
->setPropertyValue( "RelativePosition", uno::Any());
69 void LegendHelper::hideLegend( const Reference
< frame::XModel
>& xModel
)
71 uno::Reference
< chart2::XLegend
> xLegend
= LegendHelper::getLegend( xModel
, 0, false );
72 uno::Reference
< beans::XPropertySet
> xProp( xLegend
, uno::UNO_QUERY
);
75 xProp
->setPropertyValue( "Show", uno::makeAny(sal_False
) );
79 uno::Reference
< chart2::XLegend
> LegendHelper::getLegend(
80 const uno::Reference
< frame::XModel
>& xModel
81 , const uno::Reference
< uno::XComponentContext
>& xContext
84 uno::Reference
< chart2::XLegend
> xResult
;
86 uno::Reference
< chart2::XChartDocument
> xChartDoc( xModel
, uno::UNO_QUERY
);
91 uno::Reference
< chart2::XDiagram
> xDia( xChartDoc
->getFirstDiagram());
94 xResult
.set( xDia
->getLegend() );
95 if( bCreate
&& !xResult
.is() && xContext
.is() )
97 xResult
.set( xContext
->getServiceManager()->createInstanceWithContext(
98 "com.sun.star.chart2.Legend", xContext
), uno::UNO_QUERY
);
99 xDia
->setLegend( xResult
);
104 OSL_FAIL("need diagram for creation of legend");
107 catch( const uno::Exception
& ex
)
109 ASSERT_EXCEPTION( ex
);
116 bool LegendHelper::hasLegend( const uno::Reference
< chart2::XDiagram
> & xDiagram
)
118 bool bReturn
= false;
121 uno::Reference
< beans::XPropertySet
> xLegendProp( xDiagram
->getLegend(), uno::UNO_QUERY
);
122 if( xLegendProp
.is())
123 xLegendProp
->getPropertyValue( "Show") >>= bReturn
;
129 //.............................................................................
131 //.............................................................................
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */