Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / LegendWrapper.cxx
blob994dfcd034e42e29ee91f30df40963c1a456e8a8
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 "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 #include <com/sun/star/chart2/XDiagram.hpp>
31 #include <CharacterProperties.hxx>
32 #include <LinePropertiesHelper.hxx>
33 #include <FillProperties.hxx>
34 #include <UserDefinedProperties.hxx>
35 #include "WrappedCharacterHeightProperty.hxx"
36 #include <PositionAndSizeHelper.hxx>
37 #include <WrappedDirectStateProperty.hxx>
38 #include "WrappedAutomaticPositionProperties.hxx"
39 #include "WrappedScaleTextProperties.hxx"
41 #include <algorithm>
43 using namespace ::com::sun::star;
44 using ::com::sun::star::beans::Property;
45 using ::osl::MutexGuard;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
50 namespace chart
52 class WrappedLegendAlignmentProperty : public WrappedProperty
54 public:
55 WrappedLegendAlignmentProperty();
57 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
58 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
60 protected:
61 virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const override;
62 virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const override;
65 WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
66 : ::chart::WrappedProperty( "Alignment", "AnchorPosition" )
70 Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
72 Any aRet;
73 if( xInnerPropertySet.is() )
75 bool bShowLegend = true;
76 xInnerPropertySet->getPropertyValue( "Show" ) >>= bShowLegend;
77 if(!bShowLegend)
79 aRet <<= css::chart::ChartLegendPosition_NONE;
81 else
83 aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
84 aRet = convertInnerToOuterValue( aRet );
87 return aRet;
90 void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
92 if(xInnerPropertySet.is())
94 bool bNewShowLegend = true;
95 bool bOldShowLegend = true;
97 css::chart::ChartLegendPosition eOuterPos(css::chart::ChartLegendPosition_NONE);
98 if( (rOuterValue >>= eOuterPos) && eOuterPos == css::chart::ChartLegendPosition_NONE )
99 bNewShowLegend = false;
100 xInnerPropertySet->getPropertyValue( "Show" ) >>= bOldShowLegend;
102 if(bNewShowLegend!=bOldShowLegend)
104 xInnerPropertySet->setPropertyValue( "Show", uno::Any(bNewShowLegend) );
106 if(!bNewShowLegend)
107 return;
109 //set corresponding LegendPosition
110 Any aInnerValue = convertOuterToInnerValue( rOuterValue );
111 xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
113 //correct LegendExpansion
114 chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
115 if( aInnerValue >>= eNewInnerPos )
117 css::chart::ChartLegendExpansion eNewExpansion =
118 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
119 eNewInnerPos == chart2::LegendPosition_LINE_START )
120 ? css::chart::ChartLegendExpansion_HIGH
121 : css::chart::ChartLegendExpansion_WIDE;
123 css::chart::ChartLegendExpansion eOldExpansion( css::chart::ChartLegendExpansion_HIGH );
124 bool bExpansionWasSet(
125 xInnerPropertySet->getPropertyValue( "Expansion" ) >>= eOldExpansion );
127 if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
128 xInnerPropertySet->setPropertyValue( "Expansion", uno::Any( eNewExpansion ));
131 //correct RelativePosition
132 Any aRelativePosition( xInnerPropertySet->getPropertyValue("RelativePosition") );
133 if(aRelativePosition.hasValue())
135 xInnerPropertySet->setPropertyValue( "RelativePosition", Any() );
140 Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
142 css::chart::ChartLegendPosition ePos = css::chart::ChartLegendPosition_NONE;
144 chart2::LegendPosition eNewPos;
145 if( rInnerValue >>= eNewPos )
147 switch( eNewPos )
149 case chart2::LegendPosition_LINE_START:
150 ePos = css::chart::ChartLegendPosition_LEFT;
151 break;
152 case chart2::LegendPosition_LINE_END:
153 ePos = css::chart::ChartLegendPosition_RIGHT;
154 break;
155 case chart2::LegendPosition_PAGE_START:
156 ePos = css::chart::ChartLegendPosition_TOP;
157 break;
158 case chart2::LegendPosition_PAGE_END:
159 ePos = css::chart::ChartLegendPosition_BOTTOM;
160 break;
162 default:
163 ePos = css::chart::ChartLegendPosition_NONE;
164 break;
167 return uno::Any( ePos );
169 Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
171 chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
173 css::chart::ChartLegendPosition ePos;
174 if( rOuterValue >>= ePos )
176 switch( ePos )
178 case css::chart::ChartLegendPosition_LEFT:
179 eNewPos = chart2::LegendPosition_LINE_START;
180 break;
181 case css::chart::ChartLegendPosition_RIGHT:
182 eNewPos = chart2::LegendPosition_LINE_END;
183 break;
184 case css::chart::ChartLegendPosition_TOP:
185 eNewPos = chart2::LegendPosition_PAGE_START;
186 break;
187 case css::chart::ChartLegendPosition_BOTTOM:
188 eNewPos = chart2::LegendPosition_PAGE_END;
189 break;
190 default: // NONE
191 break;
195 return uno::Any( eNewPos );
199 namespace
202 enum
204 PROP_LEGEND_ALIGNMENT,
205 PROP_LEGEND_EXPANSION
208 void lcl_AddPropertiesToVector(
209 std::vector< Property > & rOutProperties )
211 rOutProperties.emplace_back( "Alignment",
212 PROP_LEGEND_ALIGNMENT,
213 cppu::UnoType<css::chart::ChartLegendPosition>::get(),
214 //#i111967# no PropertyChangeEvent is fired on change so far
215 beans::PropertyAttribute::MAYBEDEFAULT );
217 rOutProperties.emplace_back( "Expansion",
218 PROP_LEGEND_EXPANSION,
219 cppu::UnoType<css::chart::ChartLegendExpansion>::get(),
220 //#i111967# no PropertyChangeEvent is fired on change so far
221 beans::PropertyAttribute::MAYBEDEFAULT );
224 struct StaticLegendWrapperPropertyArray_Initializer
226 Sequence< Property >* operator()()
228 static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
229 return &aPropSeq;
232 private:
233 static Sequence< Property > lcl_GetPropertySequence()
235 std::vector< css::beans::Property > aProperties;
236 lcl_AddPropertiesToVector( aProperties );
237 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
238 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
239 ::chart::FillProperties::AddPropertiesToVector( aProperties );
240 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
241 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
242 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
244 std::sort( aProperties.begin(), aProperties.end(),
245 ::chart::PropertyNameLess() );
247 return comphelper::containerToSequence( aProperties );
251 struct StaticLegendWrapperPropertyArray : public rtl::StaticAggregate< Sequence< Property >, StaticLegendWrapperPropertyArray_Initializer >
255 } // anonymous namespace
257 namespace chart
259 namespace wrapper
262 LegendWrapper::LegendWrapper(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
263 : m_spChart2ModelContact(spChart2ModelContact)
264 , m_aEventListenerContainer(m_aMutex)
268 LegendWrapper::~LegendWrapper()
272 // ____ XShape ____
273 awt::Point SAL_CALL LegendWrapper::getPosition()
275 return m_spChart2ModelContact->GetLegendPosition();
278 void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
280 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
281 if( xProp.is() )
283 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
285 chart2::RelativePosition aRelativePosition;
286 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
287 aRelativePosition.Primary = aPageSize.Width == 0 ? 0 : double(aPosition.X)/double(aPageSize.Width);
288 aRelativePosition.Secondary = aPageSize.Height == 0 ? 0 : double(aPosition.Y)/double(aPageSize.Height);
289 xProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
293 awt::Size SAL_CALL LegendWrapper::getSize()
295 return m_spChart2ModelContact->GetLegendSize();
298 void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
300 Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
301 if( xProp.is() )
303 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
304 awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
306 awt::Point aPos( getPosition() );
307 awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
309 PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
310 , xProp, aNewPositionAndSize, aPageRectangle );
314 // ____ XShapeDescriptor (base of XShape) ____
315 OUString SAL_CALL LegendWrapper::getShapeType()
317 return "com.sun.star.chart.ChartLegend";
320 // ____ XComponent ____
321 void SAL_CALL LegendWrapper::dispose()
323 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
324 m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
326 MutexGuard aGuard( m_aMutex);
327 clearWrappedPropertySet();
330 void SAL_CALL LegendWrapper::addEventListener(
331 const Reference< lang::XEventListener >& xListener )
333 m_aEventListenerContainer.addInterface( xListener );
336 void SAL_CALL LegendWrapper::removeEventListener(
337 const Reference< lang::XEventListener >& aListener )
339 m_aEventListenerContainer.removeInterface( aListener );
342 //ReferenceSizePropertyProvider
343 void LegendWrapper::updateReferenceSize()
345 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
346 if( xProp.is() )
348 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
349 xProp->setPropertyValue( "ReferencePageSize", uno::Any(
350 m_spChart2ModelContact->GetPageSize() ));
353 Any LegendWrapper::getReferenceSize()
355 Any aRet;
356 Reference< beans::XPropertySet > xProp = getInnerPropertySet();
357 if( xProp.is() )
358 aRet = xProp->getPropertyValue( "ReferencePageSize" );
360 return aRet;
362 awt::Size LegendWrapper::getCurrentSizeForReference()
364 return m_spChart2ModelContact->GetPageSize();
367 // WrappedPropertySet
368 Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
370 Reference< beans::XPropertySet > xRet;
371 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
372 if( xDiagram.is() )
373 xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
374 OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
375 return xRet;
378 const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
380 return *StaticLegendWrapperPropertyArray::get();
383 std::vector< std::unique_ptr<WrappedProperty> > LegendWrapper::createWrappedProperties()
385 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
387 aWrappedProperties.emplace_back( new WrappedLegendAlignmentProperty() );
388 aWrappedProperties.emplace_back( new WrappedProperty( "Expansion", "Expansion"));
389 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
390 //same problem as for wall: the defaults in the old chart are different for different charttypes, so we need to export explicitly
391 aWrappedProperties.emplace_back( new WrappedDirectStateProperty("FillStyle", "FillStyle"));
392 aWrappedProperties.emplace_back( new WrappedDirectStateProperty("FillColor", "FillColor"));
393 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
394 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
396 return aWrappedProperties;
399 OUString SAL_CALL LegendWrapper::getImplementationName()
401 return "com.sun.star.comp.chart.Legend";
404 sal_Bool SAL_CALL LegendWrapper::supportsService( const OUString& rServiceName )
406 return cppu::supportsService(this, rServiceName);
409 css::uno::Sequence< OUString > SAL_CALL LegendWrapper::getSupportedServiceNames()
411 return {
412 "com.sun.star.chart.ChartLegend",
413 "com.sun.star.drawing.Shape",
414 "com.sun.star.xml.UserDefinedAttributesSupplier",
415 "com.sun.star.style.CharacterProperties"
419 } // namespace wrapper
420 } // namespace chart
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */