fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / LegendWrapper.cxx
blob755b78ecbcaac65bcb5c610e90dfe95d9138c584
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 "macros.hxx"
22 #include "Chart2ModelContact.hxx"
23 #include "LegendHelper.hxx"
24 #include "ContainerHelper.hxx"
25 #include <comphelper/InlineContainer.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/chart2/XTitled.hpp>
29 #include <com/sun/star/chart/ChartLegendPosition.hpp>
30 #include <com/sun/star/chart2/LegendPosition.hpp>
31 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
32 #include <com/sun/star/chart2/RelativePosition.hpp>
33 #include <com/sun/star/drawing/FillStyle.hpp>
35 #include "CharacterProperties.hxx"
36 #include "LinePropertiesHelper.hxx"
37 #include "FillProperties.hxx"
38 #include "UserDefinedProperties.hxx"
39 #include "WrappedCharacterHeightProperty.hxx"
40 #include "PositionAndSizeHelper.hxx"
41 #include "WrappedDirectStateProperty.hxx"
42 #include "WrappedAutomaticPositionProperties.hxx"
43 #include "WrappedScaleTextProperties.hxx"
45 #include <algorithm>
46 #include <rtl/ustrbuf.hxx>
48 using namespace ::com::sun::star;
49 using ::com::sun::star::beans::Property;
50 using ::osl::MutexGuard;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::Sequence;
55 namespace chart
57 class WrappedLegendAlignmentProperty : public WrappedProperty
59 public:
60 WrappedLegendAlignmentProperty();
61 virtual ~WrappedLegendAlignmentProperty();
63 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
64 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException) SAL_OVERRIDE;
65 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
66 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) SAL_OVERRIDE;
68 protected:
69 virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const SAL_OVERRIDE;
70 virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const SAL_OVERRIDE;
73 WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
74 : ::chart::WrappedProperty( "Alignment", "AnchorPosition" )
77 WrappedLegendAlignmentProperty::~WrappedLegendAlignmentProperty()
81 Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
82 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
84 Any aRet;
85 if( xInnerPropertySet.is() )
87 bool bShowLegend = true;
88 xInnerPropertySet->getPropertyValue( "Show" ) >>= bShowLegend;
89 if(!bShowLegend)
91 aRet = uno::makeAny( ::com::sun::star::chart::ChartLegendPosition_NONE );
93 else
95 aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
96 aRet = this->convertInnerToOuterValue( aRet );
99 return aRet;
102 void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
103 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
105 if(xInnerPropertySet.is())
107 bool bNewShowLegend = true;
108 bool bOldShowLegend = true;
110 ::com::sun::star::chart::ChartLegendPosition eOuterPos(::com::sun::star::chart::ChartLegendPosition_NONE);
111 if( (rOuterValue >>= eOuterPos) && eOuterPos == ::com::sun::star::chart::ChartLegendPosition_NONE )
112 bNewShowLegend = false;
113 xInnerPropertySet->getPropertyValue( "Show" ) >>= bOldShowLegend;
115 if(bNewShowLegend!=bOldShowLegend)
117 xInnerPropertySet->setPropertyValue( "Show", uno::makeAny(bNewShowLegend) );
119 if(!bNewShowLegend)
120 return;
122 //set corresponding LegendPosition
123 Any aInnerValue = this->convertOuterToInnerValue( rOuterValue );
124 xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
126 //correct LegendExpansion
127 chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
128 if( aInnerValue >>= eNewInnerPos )
130 ::com::sun::star::chart::ChartLegendExpansion eNewExpansion =
131 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
132 eNewInnerPos == chart2::LegendPosition_LINE_START )
133 ? ::com::sun::star::chart::ChartLegendExpansion_HIGH
134 : ::com::sun::star::chart::ChartLegendExpansion_WIDE;
136 ::com::sun::star::chart::ChartLegendExpansion eOldExpansion( ::com::sun::star::chart::ChartLegendExpansion_HIGH );
137 bool bExpansionWasSet(
138 xInnerPropertySet->getPropertyValue( "Expansion" ) >>= eOldExpansion );
140 if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
141 xInnerPropertySet->setPropertyValue( "Expansion", uno::makeAny( eNewExpansion ));
144 //correct RelativePosition
145 Any aRelativePosition( xInnerPropertySet->getPropertyValue("RelativePosition") );
146 if(aRelativePosition.hasValue())
148 xInnerPropertySet->setPropertyValue( "RelativePosition", Any() );
153 Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
155 ::com::sun::star::chart::ChartLegendPosition ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
157 chart2::LegendPosition eNewPos;
158 if( rInnerValue >>= eNewPos )
160 switch( eNewPos )
162 case chart2::LegendPosition_LINE_START:
163 ePos = ::com::sun::star::chart::ChartLegendPosition_LEFT;
164 break;
165 case chart2::LegendPosition_LINE_END:
166 ePos = ::com::sun::star::chart::ChartLegendPosition_RIGHT;
167 break;
168 case chart2::LegendPosition_PAGE_START:
169 ePos = ::com::sun::star::chart::ChartLegendPosition_TOP;
170 break;
171 case chart2::LegendPosition_PAGE_END:
172 ePos = ::com::sun::star::chart::ChartLegendPosition_BOTTOM;
173 break;
175 default:
176 ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
177 break;
180 return uno::makeAny( ePos );
182 Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
184 chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
186 ::com::sun::star::chart::ChartLegendPosition ePos;
187 if( rOuterValue >>= ePos )
189 switch( ePos )
191 case ::com::sun::star::chart::ChartLegendPosition_LEFT:
192 eNewPos = chart2::LegendPosition_LINE_START;
193 break;
194 case ::com::sun::star::chart::ChartLegendPosition_RIGHT:
195 eNewPos = chart2::LegendPosition_LINE_END;
196 break;
197 case ::com::sun::star::chart::ChartLegendPosition_TOP:
198 eNewPos = chart2::LegendPosition_PAGE_START;
199 break;
200 case ::com::sun::star::chart::ChartLegendPosition_BOTTOM:
201 eNewPos = chart2::LegendPosition_PAGE_END;
202 break;
203 default: // NONE
204 break;
208 return uno::makeAny( eNewPos );
212 namespace
214 static const char lcl_aServiceName[] = "com.sun.star.comp.chart.Legend";
216 enum
218 PROP_LEGEND_ALIGNMENT,
219 PROP_LEGEND_EXPANSION
222 void lcl_AddPropertiesToVector(
223 ::std::vector< Property > & rOutProperties )
225 rOutProperties.push_back(
226 Property( "Alignment",
227 PROP_LEGEND_ALIGNMENT,
228 cppu::UnoType<com::sun::star::chart::ChartLegendPosition>::get(),
229 //#i111967# no PropertyChangeEvent is fired on change so far
230 beans::PropertyAttribute::MAYBEDEFAULT ));
232 rOutProperties.push_back(
233 Property( "Expansion",
234 PROP_LEGEND_EXPANSION,
235 cppu::UnoType<com::sun::star::chart::ChartLegendExpansion>::get(),
236 //#i111967# no PropertyChangeEvent is fired on change so far
237 beans::PropertyAttribute::MAYBEDEFAULT ));
240 struct StaticLegendWrapperPropertyArray_Initializer
242 Sequence< Property >* operator()()
244 static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
245 return &aPropSeq;
248 private:
249 static Sequence< Property > lcl_GetPropertySequence()
251 ::std::vector< ::com::sun::star::beans::Property > aProperties;
252 lcl_AddPropertiesToVector( aProperties );
253 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
254 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
255 ::chart::FillProperties::AddPropertiesToVector( aProperties );
256 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
257 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
258 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
260 ::std::sort( aProperties.begin(), aProperties.end(),
261 ::chart::PropertyNameLess() );
263 return ::chart::ContainerHelper::ContainerToSequence( aProperties );
267 struct StaticLegendWrapperPropertyArray : public rtl::StaticAggregate< Sequence< Property >, StaticLegendWrapperPropertyArray_Initializer >
271 } // anonymous namespace
273 namespace chart
275 namespace wrapper
278 LegendWrapper::LegendWrapper( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact ) :
279 m_spChart2ModelContact( spChart2ModelContact ),
280 m_aEventListenerContainer( m_aMutex )
284 LegendWrapper::~LegendWrapper()
288 // ____ XShape ____
289 awt::Point SAL_CALL LegendWrapper::getPosition()
290 throw (uno::RuntimeException, std::exception)
292 return m_spChart2ModelContact->GetLegendPosition();
295 void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
296 throw (uno::RuntimeException, std::exception)
298 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
299 if( xProp.is() )
301 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
303 chart2::RelativePosition aRelativePosition;
304 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
305 aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
306 aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
307 xProp->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition) );
311 awt::Size SAL_CALL LegendWrapper::getSize()
312 throw (uno::RuntimeException, std::exception)
314 return m_spChart2ModelContact->GetLegendSize();
317 void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
318 throw (beans::PropertyVetoException,
319 uno::RuntimeException, std::exception)
321 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
322 if( xProp.is() )
324 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
325 awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
327 awt::Point aPos( this->getPosition() );
328 awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
330 PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
331 , xProp, aNewPositionAndSize, aPageRectangle );
335 // ____ XShapeDescriptor (base of XShape) ____
336 OUString SAL_CALL LegendWrapper::getShapeType()
337 throw (uno::RuntimeException, std::exception)
339 return OUString( "com.sun.star.chart.ChartLegend" );
342 // ____ XComponent ____
343 void SAL_CALL LegendWrapper::dispose()
344 throw (uno::RuntimeException, std::exception)
346 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
347 m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
349 MutexGuard aGuard( GetMutex());
350 clearWrappedPropertySet();
353 void SAL_CALL LegendWrapper::addEventListener(
354 const Reference< lang::XEventListener >& xListener )
355 throw (uno::RuntimeException, std::exception)
357 m_aEventListenerContainer.addInterface( xListener );
360 void SAL_CALL LegendWrapper::removeEventListener(
361 const Reference< lang::XEventListener >& aListener )
362 throw (uno::RuntimeException, std::exception)
364 m_aEventListenerContainer.removeInterface( aListener );
367 //ReferenceSizePropertyProvider
368 void LegendWrapper::updateReferenceSize()
370 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
371 if( xProp.is() )
373 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
374 xProp->setPropertyValue( "ReferencePageSize", uno::makeAny(
375 m_spChart2ModelContact->GetPageSize() ));
378 Any LegendWrapper::getReferenceSize()
380 Any aRet;
381 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
382 if( xProp.is() )
383 aRet = xProp->getPropertyValue( "ReferencePageSize" );
385 return aRet;
387 awt::Size LegendWrapper::getCurrentSizeForReference()
389 return m_spChart2ModelContact->GetPageSize();
392 // WrappedPropertySet
393 Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
395 Reference< beans::XPropertySet > xRet;
396 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
397 if( xDiagram.is() )
398 xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
399 OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
400 return xRet;
403 const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
405 return *StaticLegendWrapperPropertyArray::get();
408 const std::vector< WrappedProperty* > LegendWrapper::createWrappedProperties()
410 ::std::vector< ::chart::WrappedProperty* > aWrappedProperties;
412 aWrappedProperties.push_back( new WrappedLegendAlignmentProperty() );
413 aWrappedProperties.push_back( new WrappedProperty( "Expansion", "Expansion"));
414 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
415 //same problem as for wall: thje defaults ion the old chart are different for different charttypes, so we need to export explicitly
416 aWrappedProperties.push_back( new WrappedDirectStateProperty("FillStyle", "FillStyle"));
417 aWrappedProperties.push_back( new WrappedDirectStateProperty("FillColor", "FillColor"));
418 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
419 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
421 return aWrappedProperties;
424 Sequence< OUString > LegendWrapper::getSupportedServiceNames_Static()
426 Sequence< OUString > aServices( 4 );
427 aServices[ 0 ] = "com.sun.star.chart.ChartLegend";
428 aServices[ 1 ] = "com.sun.star.drawing.Shape";
429 aServices[ 2 ] = "com.sun.star.xml.UserDefinedAttributesSupplier";
430 aServices[ 3 ] = "com.sun.star.style.CharacterProperties";
432 return aServices;
435 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
436 OUString SAL_CALL LegendWrapper::getImplementationName()
437 throw( css::uno::RuntimeException, std::exception )
439 return getImplementationName_Static();
442 OUString LegendWrapper::getImplementationName_Static()
444 return OUString(lcl_aServiceName);
447 sal_Bool SAL_CALL LegendWrapper::supportsService( const OUString& rServiceName )
448 throw( css::uno::RuntimeException, std::exception )
450 return cppu::supportsService(this, rServiceName);
453 css::uno::Sequence< OUString > SAL_CALL LegendWrapper::getSupportedServiceNames()
454 throw( css::uno::RuntimeException, std::exception )
456 return getSupportedServiceNames_Static();
459 } // namespace wrapper
460 } // namespace chart
462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */