tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / tools / LegendHelper.cxx
blob05609541d4ef98eda1c046577efb38baffad9037
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
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>
28 #include <comphelper/diagnose_ex.hxx>
30 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Reference;
33 namespace chart
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 );
40 if( xLegend.is())
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());
63 return xLegend;
66 void LegendHelper::hideLegend( ChartModel& rModel )
68 rtl::Reference< Legend > xLegend = LegendHelper::getLegend( rModel, nullptr );
69 if( xLegend.is())
71 xLegend->setPropertyValue( u"Show"_ustr, uno::Any(false) );
75 rtl::Reference< Legend > LegendHelper::getLegend(
76 ChartModel& rModel
77 , const uno::Reference< uno::XComponentContext >& xContext
78 , bool bCreate )
80 rtl::Reference< Legend > xResult;
82 try
84 rtl::Reference< Diagram > xDia( rModel.getFirstChartDiagram());
85 if( xDia.is() )
87 xResult = xDia->getLegend2();
88 if( bCreate && !xResult.is() && xContext.is() )
90 xResult = new Legend();
91 xDia->setLegend( xResult );
94 else if(bCreate)
96 OSL_FAIL("need diagram for creation of legend");
99 catch( const uno::Exception & )
101 DBG_UNHANDLED_EXCEPTION("chart2");
104 return xResult;
107 bool LegendHelper::hasLegend( const rtl::Reference< Diagram > & xDiagram )
109 bool bReturn = false;
110 if( xDiagram.is())
112 uno::Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
113 if( xLegendProp.is())
114 xLegendProp->getPropertyValue( u"Show"_ustr) >>= bReturn;
117 return bReturn;
120 } //namespace chart
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */