Debian Codegraph @

chart2/source/controller/chartapiwrapper/LegendWrapper.cxx

raw
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 "LegendWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <comphelper/sequence.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/chart/ChartLegendPosition.hpp>
26 #include <com/sun/star/chart2/LegendPosition.hpp>
27 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
28 #include <com/sun/star/chart2/RelativePosition.hpp>
29
30 #include <CharacterProperties.hxx>
31 #include <LinePropertiesHelper.hxx>
32 #include <FillProperties.hxx>
33 #include <UserDefinedProperties.hxx>
34 #include "WrappedCharacterHeightProperty.hxx"
35 #include <PositionAndSizeHelper.hxx>
36 #include <WrappedDirectStateProperty.hxx>
37 #include "WrappedAutomaticPositionProperties.hxx"
38 #include "WrappedScaleTextProperties.hxx"
39
40 #include <algorithm>
41 #include <utility>
42
43 using namespace ::com::sun::star;
44 using ::com::sun::star::beans::Property;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48
49 namespace chart
50 {
51 namespace {
52
53 class WrappedLegendAlignmentProperty : public WrappedProperty
54 {
55 public:
56 WrappedLegendAlignmentProperty();
57
58 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
59 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
60
61 protected:
62 virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const override;
63 virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const override;
64 };
65
66 }
67
68 WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
69 : ::chart::WrappedProperty( u"Alignment"_ustr, u"AnchorPosition"_ustr )
70 {
71 }
72
73 Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
74 {
75 Any aRet;
76 if( xInnerPropertySet.is() )
77 {
78 bool bShowLegend = true;
79 xInnerPropertySet->getPropertyValue( u"Show"_ustr ) >>= bShowLegend;
80 if(!bShowLegend)
81 {
82 aRet <<= css::chart::ChartLegendPosition_NONE;
83 }
84 else
85 {
86 aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
87 aRet = convertInnerToOuterValue( aRet );
88 }
89 }
90 return aRet;
91 }
92
93 void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
94 {
95 if(!xInnerPropertySet.is())
96 return;
97
98 bool bNewShowLegend = true;
99 bool bOldShowLegend = true;
100 {
101 css::chart::ChartLegendPosition eOuterPos(css::chart::ChartLegendPosition_NONE);
102 if( (rOuterValue >>= eOuterPos) && eOuterPos == css::chart::ChartLegendPosition_NONE )
103 bNewShowLegend = false;
104 xInnerPropertySet->getPropertyValue( u"Show"_ustr ) >>= bOldShowLegend;
105 }
106 if(bNewShowLegend!=bOldShowLegend)
107 {
108 xInnerPropertySet->setPropertyValue( u"Show"_ustr, uno::Any(bNewShowLegend) );
109 }
110 if(!bNewShowLegend)
111 return;
112
113 //set corresponding LegendPosition
114 Any aInnerValue = convertOuterToInnerValue( rOuterValue );
115 xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
116
117 //correct LegendExpansion
118 chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
119 if( aInnerValue >>= eNewInnerPos )
120 {
121 css::chart::ChartLegendExpansion eNewExpansion =
122 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
123 eNewInnerPos == chart2::LegendPosition_LINE_START )
124 ? css::chart::ChartLegendExpansion_HIGH
125 : css::chart::ChartLegendExpansion_WIDE;
126
127 css::chart::ChartLegendExpansion eOldExpansion( css::chart::ChartLegendExpansion_HIGH );
128 bool bExpansionWasSet(
129 xInnerPropertySet->getPropertyValue( u"Expansion"_ustr ) >>= eOldExpansion );
130
131 if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
132 xInnerPropertySet->setPropertyValue( u"Expansion"_ustr, uno::Any( eNewExpansion ));
133 }
134
135 //correct RelativePosition
136 Any aRelativePosition( xInnerPropertySet->getPropertyValue(u"RelativePosition"_ustr) );
137 if(aRelativePosition.hasValue())
138 {
139 xInnerPropertySet->setPropertyValue( u"RelativePosition"_ustr, Any() );
140 }
141 }
142
143 Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
144 {
145 css::chart::ChartLegendPosition ePos = css::chart::ChartLegendPosition_NONE;
146
147 chart2::LegendPosition eNewPos;
148 if( rInnerValue >>= eNewPos )
149 {
150 switch( eNewPos )
151 {
152 case chart2::LegendPosition_LINE_START:
153 ePos = css::chart::ChartLegendPosition_LEFT;
154 break;
155 case chart2::LegendPosition_LINE_END:
156 ePos = css::chart::ChartLegendPosition_RIGHT;
157 break;
158 case chart2::LegendPosition_PAGE_START:
159 ePos = css::chart::ChartLegendPosition_TOP;
160 break;
161 case chart2::LegendPosition_PAGE_END:
162 ePos = css::chart::ChartLegendPosition_BOTTOM;
163 break;
164
165 default:
166 ePos = css::chart::ChartLegendPosition_NONE;
167 break;
168 }
169 }
170 return uno::Any( ePos );
171 }
172 Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
173 {
174 chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
175
176 css::chart::ChartLegendPosition ePos;
177 if( rOuterValue >>= ePos )
178 {
179 switch( ePos )
180 {
181 case css::chart::ChartLegendPosition_LEFT:
182 eNewPos = chart2::LegendPosition_LINE_START;
183 break;
184 case css::chart::ChartLegendPosition_RIGHT:
185 eNewPos = chart2::LegendPosition_LINE_END;
186 break;
187 case css::chart::ChartLegendPosition_TOP:
188 eNewPos = chart2::LegendPosition_PAGE_START;
189 break;
190 case css::chart::ChartLegendPosition_BOTTOM:
191 eNewPos = chart2::LegendPosition_PAGE_END;
192 break;
193 default: // NONE
194 break;
195 }
196 }
197
198 return uno::Any( eNewPos );
199 }
200 }
201
202 namespace
203 {
204
205 enum
206 {
207 PROP_LEGEND_ALIGNMENT,
208 PROP_LEGEND_EXPANSION
209 };
210
211 void lcl_AddPropertiesToVector(
212 std::vector< Property > & rOutProperties )
213 {
214 rOutProperties.emplace_back( "Alignment",
215 PROP_LEGEND_ALIGNMENT,
216 cppu::UnoType<css::chart::ChartLegendPosition>::get(),
217 //#i111967# no PropertyChangeEvent is fired on change so far
218 beans::PropertyAttribute::MAYBEDEFAULT );
219
220 rOutProperties.emplace_back( "Expansion",
221 PROP_LEGEND_EXPANSION,
222 cppu::UnoType<css::chart::ChartLegendExpansion>::get(),
223 //#i111967# no PropertyChangeEvent is fired on change so far
224 beans::PropertyAttribute::MAYBEDEFAULT );
225 }
226
227 const Sequence< Property >& StaticLegendWrapperPropertyArray()
228 {
229 static Sequence< Property > aPropSeq = []()
230 {
231 std::vector< css::beans::Property > aProperties;
232 lcl_AddPropertiesToVector( aProperties );
233 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
234 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
235 ::chart::FillProperties::AddPropertiesToVector( aProperties );
236 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
237 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
238 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
239
240 std::sort( aProperties.begin(), aProperties.end(),
241 ::chart::PropertyNameLess() );
242
243 return comphelper::containerToSequence( aProperties );
244 }();
245 return aPropSeq;
246 };
247
248 } // anonymous namespace
249
250 namespace chart::wrapper
251 {
252
253 LegendWrapper::LegendWrapper(std::shared_ptr<Chart2ModelContact> spChart2ModelContact)
254 : m_spChart2ModelContact(std::move(spChart2ModelContact))
255 {
256 }
257
258 LegendWrapper::~LegendWrapper()
259 {
260 }
261
262 // ____ XShape ____
263 awt::Point SAL_CALL LegendWrapper::getPosition()
264 {
265 return m_spChart2ModelContact->GetLegendPosition();
266 }
267
268 void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
269 {
270 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
271 if( xProp.is() )
272 {
273 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
274
275 chart2::RelativePosition aRelativePosition;
276 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
277 aRelativePosition.Primary = aPageSize.Width == 0 ? 0 : double(aPosition.X)/double(aPageSize.Width);
278 aRelativePosition.Secondary = aPageSize.Height == 0 ? 0 : double(aPosition.Y)/double(aPageSize.Height);
279 xProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
280 }
281 }
282
283 awt::Size SAL_CALL LegendWrapper::getSize()
284 {
285 return m_spChart2ModelContact->GetLegendSize();
286 }
287
288 void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
289 {
290 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
291 if( xProp.is() )
292 {
293 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
294 awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
295
296 awt::Point aPos( getPosition() );
297 awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
298
299 PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
300 , xProp, aNewPositionAndSize, awt::Rectangle(), aPageRectangle );
301 }
302 }
303
304 // ____ XShapeDescriptor (base of XShape) ____
305 OUString SAL_CALL LegendWrapper::getShapeType()
306 {
307 return u"com.sun.star.chart.ChartLegend"_ustr;
308 }
309
310 // ____ XComponent ____
311 void SAL_CALL LegendWrapper::dispose()
312 {
313 std::unique_lock g(m_aMutex);
314 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
315 m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
316
317 clearWrappedPropertySet();
318 }
319
320 void SAL_CALL LegendWrapper::addEventListener(
321 const Reference< lang::XEventListener >& xListener )
322 {
323 std::unique_lock g(m_aMutex);
324 m_aEventListenerContainer.addInterface( g, xListener );
325 }
326
327 void SAL_CALL LegendWrapper::removeEventListener(
328 const Reference< lang::XEventListener >& aListener )
329 {
330 std::unique_lock g(m_aMutex);
331 m_aEventListenerContainer.removeInterface( g, aListener );
332 }
333
334 //ReferenceSizePropertyProvider
335 void LegendWrapper::updateReferenceSize()
336 {
337 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
338 if( xProp.is() )
339 {
340 if( xProp->getPropertyValue( u"ReferencePageSize"_ustr ).hasValue() )
341 xProp->setPropertyValue( u"ReferencePageSize"_ustr, uno::Any(
342 m_spChart2ModelContact->GetPageSize() ));
343 }
344 }
345 Any LegendWrapper::getReferenceSize()
346 {
347 Any aRet;
348 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
349 if( xProp.is() )
350 aRet = xProp->getPropertyValue( u"ReferencePageSize"_ustr );
351
352 return aRet;
353 }
354 awt::Size LegendWrapper::getCurrentSizeForReference()
355 {
356 return m_spChart2ModelContact->GetPageSize();
357 }
358
359 // WrappedPropertySet
360 Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
361 {
362 Reference< beans::XPropertySet > xRet;
363 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
364 if( xDiagram.is() )
365 xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
366 OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
367 return xRet;
368 }
369
370 const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
371 {
372 return StaticLegendWrapperPropertyArray();
373 }
374
375 std::vector< std::unique_ptr<WrappedProperty> > LegendWrapper::createWrappedProperties()
376 {
377 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
378
379 aWrappedProperties.emplace_back( new WrappedLegendAlignmentProperty() );
380 aWrappedProperties.emplace_back( new WrappedProperty( u"Expansion"_ustr, u"Expansion"_ustr));
381 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
382 //same problem as for wall: the defaults in the old chart are different for different charttypes, so we need to export explicitly
383 aWrappedProperties.emplace_back( new WrappedDirectStateProperty(u"FillStyle"_ustr, u"FillStyle"_ustr));
384 aWrappedProperties.emplace_back( new WrappedDirectStateProperty(u"FillColor"_ustr, u"FillColor"_ustr));
385 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
386 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
387
388 return aWrappedProperties;
389 }
390
391 OUString SAL_CALL LegendWrapper::getImplementationName()
392 {
393 return u"com.sun.star.comp.chart.Legend"_ustr;
394 }
395
396 sal_Bool SAL_CALL LegendWrapper::supportsService( const OUString& rServiceName )
397 {
398 return cppu::supportsService(this, rServiceName);
399 }
400
401 css::uno::Sequence< OUString > SAL_CALL LegendWrapper::getSupportedServiceNames()
402 {
403 return {
404 u"com.sun.star.chart.ChartLegend"_ustr,
405 u"com.sun.star.drawing.Shape"_ustr,
406 u"com.sun.star.xml.UserDefinedAttributesSupplier"_ustr,
407 u"com.sun.star.style.CharacterProperties"_ustr
408 };
409 }
410
411 } // namespace chart::wrapper
412
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */