merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / controller / chartapiwrapper / LegendWrapper.cxx
blob1902eca6685fb72068d75a8ea32f5acbd1619e30
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LegendWrapper.cxx,v $
10 * $Revision: 1.9.16.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "LegendWrapper.hxx"
34 #include "macros.hxx"
35 #include "Chart2ModelContact.hxx"
36 #include "LegendHelper.hxx"
37 #include "ContainerHelper.hxx"
38 #include <comphelper/InlineContainer.hxx>
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/chart2/XTitled.hpp>
41 #include <com/sun/star/chart/ChartLegendPosition.hpp>
42 #include <com/sun/star/chart2/LegendPosition.hpp>
43 #include <com/sun/star/chart2/LegendExpansion.hpp>
44 #include <com/sun/star/chart2/RelativePosition.hpp>
45 #include <com/sun/star/drawing/FillStyle.hpp>
47 #include "CharacterProperties.hxx"
48 #include "LineProperties.hxx"
49 #include "FillProperties.hxx"
50 #include "UserDefinedProperties.hxx"
51 #include "WrappedCharacterHeightProperty.hxx"
52 #include "PositionAndSizeHelper.hxx"
53 #include "WrappedDirectStateProperty.hxx"
54 #include "WrappedAutomaticPositionProperties.hxx"
55 #include "WrappedScaleTextProperties.hxx"
57 #include <algorithm>
58 #include <rtl/ustrbuf.hxx>
60 using namespace ::com::sun::star;
61 using ::com::sun::star::beans::Property;
62 using ::osl::MutexGuard;
63 using ::com::sun::star::uno::Any;
64 using ::com::sun::star::uno::Reference;
65 using ::com::sun::star::uno::Sequence;
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 namespace chart
72 class WrappedLegendAlignmentProperty : public WrappedProperty
74 public:
75 WrappedLegendAlignmentProperty();
76 virtual ~WrappedLegendAlignmentProperty();
78 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
79 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException);
80 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
81 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException);
83 protected:
84 virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const;
85 virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const;
88 WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
89 : ::chart::WrappedProperty( C2U( "Alignment" ), C2U( "AnchorPosition" ) )
92 WrappedLegendAlignmentProperty::~WrappedLegendAlignmentProperty()
96 Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
97 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
99 Any aRet;
100 if( xInnerPropertySet.is() )
102 sal_Bool bShowLegend = sal_True;
103 xInnerPropertySet->getPropertyValue( C2U( "Show" ) ) >>= bShowLegend;
104 if(!bShowLegend)
106 aRet = uno::makeAny( ::com::sun::star::chart::ChartLegendPosition_NONE );
108 else
110 aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
111 aRet = this->convertInnerToOuterValue( aRet );
114 return aRet;
117 void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
118 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
120 if(xInnerPropertySet.is())
122 sal_Bool bNewShowLegend = sal_True;
123 sal_Bool bOldShowLegend = sal_True;
125 ::com::sun::star::chart::ChartLegendPosition eOuterPos(::com::sun::star::chart::ChartLegendPosition_NONE);
126 if( (rOuterValue >>= eOuterPos) && eOuterPos == ::com::sun::star::chart::ChartLegendPosition_NONE )
127 bNewShowLegend = sal_False;
128 xInnerPropertySet->getPropertyValue( C2U( "Show" ) ) >>= bOldShowLegend;
130 if(bNewShowLegend!=bOldShowLegend)
132 xInnerPropertySet->setPropertyValue( C2U( "Show" ), uno::makeAny(bNewShowLegend) );
134 if(!bNewShowLegend)
135 return;
137 //set corresponding LegendPosition
138 Any aInnerValue = this->convertOuterToInnerValue( rOuterValue );
139 xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
141 //correct LegendExpansion
142 chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
143 if( aInnerValue >>= eNewInnerPos )
145 chart2::LegendExpansion eNewExpansion =
146 ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
147 eNewInnerPos == chart2::LegendPosition_LINE_START )
148 ? chart2::LegendExpansion_HIGH
149 : chart2::LegendExpansion_WIDE;
151 chart2::LegendExpansion eOldExpansion( chart2::LegendExpansion_HIGH );
152 bool bExpansionWasSet(
153 xInnerPropertySet->getPropertyValue( C2U( "Expansion" ) ) >>= eOldExpansion );
155 if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
156 xInnerPropertySet->setPropertyValue( C2U( "Expansion" ), uno::makeAny( eNewExpansion ));
159 //correct RelativePosition
160 Any aRelativePosition( xInnerPropertySet->getPropertyValue( C2U( "RelativePosition" ) ) );
161 if(aRelativePosition.hasValue())
163 xInnerPropertySet->setPropertyValue( C2U( "RelativePosition" ), Any() );
168 Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
170 ::com::sun::star::chart::ChartLegendPosition ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
172 chart2::LegendPosition eNewPos;
173 if( rInnerValue >>= eNewPos )
175 switch( eNewPos )
177 case chart2::LegendPosition_LINE_START:
178 ePos = ::com::sun::star::chart::ChartLegendPosition_LEFT;
179 break;
180 case chart2::LegendPosition_LINE_END:
181 ePos = ::com::sun::star::chart::ChartLegendPosition_RIGHT;
182 break;
183 case chart2::LegendPosition_PAGE_START:
184 ePos = ::com::sun::star::chart::ChartLegendPosition_TOP;
185 break;
186 case chart2::LegendPosition_PAGE_END:
187 ePos = ::com::sun::star::chart::ChartLegendPosition_BOTTOM;
188 break;
190 default:
191 ePos = ::com::sun::star::chart::ChartLegendPosition_NONE;
192 break;
195 return uno::makeAny( ePos );
197 Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
199 chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
201 ::com::sun::star::chart::ChartLegendPosition ePos;
202 if( rOuterValue >>= ePos )
204 switch( ePos )
206 case ::com::sun::star::chart::ChartLegendPosition_LEFT:
207 eNewPos = chart2::LegendPosition_LINE_START;
208 break;
209 case ::com::sun::star::chart::ChartLegendPosition_RIGHT:
210 eNewPos = chart2::LegendPosition_LINE_END;
211 break;
212 case ::com::sun::star::chart::ChartLegendPosition_TOP:
213 eNewPos = chart2::LegendPosition_PAGE_START;
214 break;
215 case ::com::sun::star::chart::ChartLegendPosition_BOTTOM:
216 eNewPos = chart2::LegendPosition_PAGE_END;
217 break;
218 default: // NONE
219 break;
223 return uno::makeAny( eNewPos );
226 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 namespace
233 static const ::rtl::OUString lcl_aServiceName(
234 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.Legend" ));
236 enum
238 PROP_LEGEND_ALIGNMENT
241 void lcl_AddPropertiesToVector(
242 ::std::vector< Property > & rOutProperties )
244 rOutProperties.push_back(
245 Property( C2U( "Alignment" ),
246 PROP_LEGEND_ALIGNMENT,
247 ::getCppuType( reinterpret_cast< const ::com::sun::star::chart::ChartLegendPosition * >(0)),
248 beans::PropertyAttribute::BOUND
249 | beans::PropertyAttribute::MAYBEDEFAULT ));
252 const Sequence< Property > & lcl_GetPropertySequence()
254 static Sequence< Property > aPropSeq;
256 // /--
257 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
258 if( 0 == aPropSeq.getLength() )
260 // get properties
261 ::std::vector< ::com::sun::star::beans::Property > aProperties;
262 lcl_AddPropertiesToVector( aProperties );
263 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
264 ::chart::LineProperties::AddPropertiesToVector( aProperties );
265 ::chart::FillProperties::AddPropertiesToVector( aProperties );
266 // ::chart::NamedProperties::AddPropertiesToVector( aProperties );
267 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
268 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
269 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
271 // and sort them for access via bsearch
272 ::std::sort( aProperties.begin(), aProperties.end(),
273 ::chart::PropertyNameLess() );
275 // transfer result to static Sequence
276 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
279 return aPropSeq;
281 } // anonymous namespace
283 // --------------------------------------------------------------------------------
284 // --------------------------------------------------------------------------------
286 namespace chart
288 namespace wrapper
291 LegendWrapper::LegendWrapper( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact ) :
292 m_spChart2ModelContact( spChart2ModelContact ),
293 m_aEventListenerContainer( m_aMutex )
297 LegendWrapper::~LegendWrapper()
301 // ____ XShape ____
302 awt::Point SAL_CALL LegendWrapper::getPosition()
303 throw (uno::RuntimeException)
305 return m_spChart2ModelContact->GetLegendPosition();
308 void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
309 throw (uno::RuntimeException)
311 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
312 if( xProp.is() )
314 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
316 chart2::RelativePosition aRelativePosition;
317 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
318 aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
319 aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
320 xProp->setPropertyValue( C2U( "RelativePosition" ), uno::makeAny(aRelativePosition) );
324 awt::Size SAL_CALL LegendWrapper::getSize()
325 throw (uno::RuntimeException)
327 return m_spChart2ModelContact->GetLegendSize();
330 void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
331 throw (beans::PropertyVetoException,
332 uno::RuntimeException)
334 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet() );
335 if( xProp.is() )
337 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
338 awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
340 awt::Point aPos( this->getPosition() );
341 awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
343 PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
344 , xProp, aNewPositionAndSize, aPageRectangle );
348 // ____ XShapeDescriptor (base of XShape) ____
349 ::rtl::OUString SAL_CALL LegendWrapper::getShapeType()
350 throw (uno::RuntimeException)
352 return C2U( "com.sun.star.chart.ChartLegend" );
355 // ____ XComponent ____
356 void SAL_CALL LegendWrapper::dispose()
357 throw (uno::RuntimeException)
359 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
360 m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
362 // /--
363 MutexGuard aGuard( GetMutex());
364 clearWrappedPropertySet();
365 // \--
368 void SAL_CALL LegendWrapper::addEventListener(
369 const Reference< lang::XEventListener >& xListener )
370 throw (uno::RuntimeException)
372 m_aEventListenerContainer.addInterface( xListener );
375 void SAL_CALL LegendWrapper::removeEventListener(
376 const Reference< lang::XEventListener >& aListener )
377 throw (uno::RuntimeException)
379 m_aEventListenerContainer.removeInterface( aListener );
382 // ================================================================================
384 //ReferenceSizePropertyProvider
385 void LegendWrapper::updateReferenceSize()
387 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
388 if( xProp.is() )
390 if( xProp->getPropertyValue( C2U("ReferencePageSize") ).hasValue() )
391 xProp->setPropertyValue( C2U("ReferencePageSize"), uno::makeAny(
392 m_spChart2ModelContact->GetPageSize() ));
395 Any LegendWrapper::getReferenceSize()
397 Any aRet;
398 Reference< beans::XPropertySet > xProp( this->getInnerPropertySet(), uno::UNO_QUERY );
399 if( xProp.is() )
400 aRet = xProp->getPropertyValue( C2U("ReferencePageSize") );
402 return aRet;
404 awt::Size LegendWrapper::getCurrentSizeForReference()
406 return m_spChart2ModelContact->GetPageSize();
409 // ================================================================================
411 // WrappedPropertySet
412 Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
414 Reference< beans::XPropertySet > xRet;
415 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
416 if( xDiagram.is() )
417 xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
418 OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
419 return xRet;
422 const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
424 return lcl_GetPropertySequence();
427 const std::vector< WrappedProperty* > LegendWrapper::createWrappedProperties()
429 ::std::vector< ::chart::WrappedProperty* > aWrappedProperties;
431 aWrappedProperties.push_back( new WrappedLegendAlignmentProperty() );
432 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
433 //same problem as for wall: thje defaults ion the old chart are different for different charttypes, so we need to export explicitly
434 aWrappedProperties.push_back( new WrappedDirectStateProperty( C2U("FillStyle"), C2U("FillStyle") ) );
435 aWrappedProperties.push_back( new WrappedDirectStateProperty( C2U("FillColor"), C2U("FillColor") ));
436 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
437 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
439 return aWrappedProperties;
442 // ================================================================================
444 Sequence< ::rtl::OUString > LegendWrapper::getSupportedServiceNames_Static()
446 Sequence< ::rtl::OUString > aServices( 4 );
447 aServices[ 0 ] = C2U( "com.sun.star.chart.ChartLegend" );
448 aServices[ 1 ] = C2U( "com.sun.star.drawing.Shape" );
449 aServices[ 2 ] = C2U( "com.sun.star.xml.UserDefinedAttributeSupplier" );
450 aServices[ 3 ] = C2U( "com.sun.star.style.CharacterProperties" );
451 // aServices[ 4 ] = C2U( "com.sun.star.beans.PropertySet" );
452 // aServices[ 5 ] = C2U( "com.sun.star.drawing.FillProperties" );
453 // aServices[ 6 ] = C2U( "com.sun.star.drawing.LineProperties" );
455 return aServices;
458 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
459 APPHELPER_XSERVICEINFO_IMPL( LegendWrapper, lcl_aServiceName );
461 } // namespace wrapper
462 } // namespace chart