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