merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / main / Legend.cxx
blobc1143ecdf77ae9dbb8c9ff2f985897269d5b093f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "Legend.hxx"
31 #include "macros.hxx"
32 #include "LineProperties.hxx"
33 #include "FillProperties.hxx"
34 #include "CharacterProperties.hxx"
35 #include "UserDefinedProperties.hxx"
36 #include "LegendHelper.hxx"
37 #include "ContainerHelper.hxx"
38 #include "CloneHelper.hxx"
39 #include "PropertyHelper.hxx"
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
41 #include <com/sun/star/awt/Size.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>
46 #include <algorithm>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::beans::PropertyAttribute;
51 using ::rtl::OUString;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::beans::Property;
57 namespace
60 static const OUString lcl_aServiceName(
61 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.Legend" ));
63 enum
65 PROP_LEGEND_ANCHOR_POSITION,
66 PROP_LEGEND_PREFERRED_EXPANSION,
67 PROP_LEGEND_SHOW,
68 PROP_LEGEND_REF_PAGE_SIZE,
69 PROP_LEGEND_REL_POS
72 void lcl_AddPropertiesToVector(
73 ::std::vector< Property > & rOutProperties )
75 rOutProperties.push_back(
76 Property( C2U( "AnchorPosition" ),
77 PROP_LEGEND_ANCHOR_POSITION,
78 ::getCppuType( reinterpret_cast< const chart2::LegendPosition * >(0)),
79 beans::PropertyAttribute::BOUND
80 | beans::PropertyAttribute::MAYBEDEFAULT ));
82 rOutProperties.push_back(
83 Property( C2U( "Expansion" ),
84 PROP_LEGEND_PREFERRED_EXPANSION,
85 ::getCppuType( reinterpret_cast< const chart2::LegendExpansion * >(0)),
86 beans::PropertyAttribute::BOUND
87 | beans::PropertyAttribute::MAYBEDEFAULT ));
89 rOutProperties.push_back(
90 Property( C2U( "Show" ),
91 PROP_LEGEND_SHOW,
92 ::getBooleanCppuType(),
93 beans::PropertyAttribute::BOUND
94 | beans::PropertyAttribute::MAYBEDEFAULT ));
95 rOutProperties.push_back(
96 Property( C2U( "ReferencePageSize" ),
97 PROP_LEGEND_REF_PAGE_SIZE,
98 ::getCppuType( reinterpret_cast< const awt::Size * >(0)),
99 beans::PropertyAttribute::BOUND
100 | beans::PropertyAttribute::MAYBEVOID ));
102 rOutProperties.push_back(
103 Property( C2U( "RelativePosition" ),
104 PROP_LEGEND_REL_POS,
105 ::getCppuType( reinterpret_cast< const chart2::RelativePosition * >(0)),
106 beans::PropertyAttribute::BOUND
107 | beans::PropertyAttribute::MAYBEVOID ));
110 void lcl_AddDefaultsToMap(
111 ::chart::tPropertyValueMap & rOutMap )
113 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_ANCHOR_POSITION, chart2::LegendPosition_LINE_END );
114 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_PREFERRED_EXPANSION, chart2::LegendExpansion_HIGH );
115 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_LEGEND_SHOW, true );
117 float fDefaultCharHeight = 10.0;
118 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_CHAR_HEIGHT, fDefaultCharHeight );
119 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_ASIAN_CHAR_HEIGHT, fDefaultCharHeight );
120 ::chart::PropertyHelper::setPropertyValue( rOutMap, ::chart::CharacterProperties::PROP_CHAR_COMPLEX_CHAR_HEIGHT, fDefaultCharHeight );
123 const Sequence< Property > & lcl_GetPropertySequence()
125 static Sequence< Property > aPropSeq;
127 // /--
128 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
129 if( 0 == aPropSeq.getLength() )
131 // get properties
132 ::std::vector< ::com::sun::star::beans::Property > aProperties;
133 lcl_AddPropertiesToVector( aProperties );
134 ::chart::LineProperties::AddPropertiesToVector( aProperties );
135 ::chart::FillProperties::AddPropertiesToVector( aProperties );
136 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
137 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
139 // and sort them for access via bsearch
140 ::std::sort( aProperties.begin(), aProperties.end(),
141 ::chart::PropertyNameLess() );
143 // transfer result to static Sequence
144 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
147 return aPropSeq;
150 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
152 static ::cppu::OPropertyArrayHelper aArrayHelper(
153 lcl_GetPropertySequence(),
154 /* bSorted = */ sal_True );
156 return aArrayHelper;
159 } // anonymous namespace
161 namespace chart
164 Legend::Legend( Reference< uno::XComponentContext > const & /* xContext */ ) :
165 ::property::OPropertySet( m_aMutex ),
166 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
170 Legend::Legend( const Legend & rOther ) :
171 MutexContainer(),
172 impl::Legend_Base(),
173 ::property::OPropertySet( rOther, m_aMutex ),
174 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
176 CloneHelper::CloneRefVector< Reference< chart2::XLegendEntry > >( rOther.m_aLegendEntries, m_aLegendEntries );
177 ModifyListenerHelper::addListenerToAllElements( m_aLegendEntries, m_xModifyEventForwarder );
180 Legend::~Legend()
184 ModifyListenerHelper::removeListenerFromAllElements( m_aLegendEntries, m_xModifyEventForwarder );
186 catch( const uno::Exception & ex )
188 ASSERT_EXCEPTION( ex );
192 // ____ XLegend ____
193 void SAL_CALL Legend::registerEntry( const Reference< chart2::XLegendEntry >& xEntry )
194 throw (lang::IllegalArgumentException,
195 uno::RuntimeException)
197 if( ::std::find( m_aLegendEntries.begin(),
198 m_aLegendEntries.end(),
199 xEntry ) != m_aLegendEntries.end())
200 throw lang::IllegalArgumentException();
202 m_aLegendEntries.push_back( xEntry );
203 ModifyListenerHelper::addListener( xEntry, m_xModifyEventForwarder );
204 fireModifyEvent();
207 void SAL_CALL Legend::revokeEntry( const Reference< chart2::XLegendEntry >& xEntry )
208 throw (container::NoSuchElementException,
209 uno::RuntimeException)
211 tLegendEntries::iterator aIt(
212 ::std::find( m_aLegendEntries.begin(),
213 m_aLegendEntries.end(),
214 xEntry ));
216 if( aIt == m_aLegendEntries.end())
217 throw container::NoSuchElementException();
219 m_aLegendEntries.erase( aIt );
220 ModifyListenerHelper::removeListener( xEntry, m_xModifyEventForwarder );
221 fireModifyEvent();
224 Sequence< Reference< chart2::XLegendEntry > > SAL_CALL Legend::getEntries()
225 throw (uno::RuntimeException)
227 return ContainerHelper::ContainerToSequence( m_aLegendEntries );
230 // ____ XCloneable ____
231 Reference< util::XCloneable > SAL_CALL Legend::createClone()
232 throw (uno::RuntimeException)
234 return Reference< util::XCloneable >( new Legend( *this ));
237 // ____ XModifyBroadcaster ____
238 void SAL_CALL Legend::addModifyListener( const Reference< util::XModifyListener >& aListener )
239 throw (uno::RuntimeException)
243 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
244 xBroadcaster->addModifyListener( aListener );
246 catch( const uno::Exception & ex )
248 ASSERT_EXCEPTION( ex );
252 void SAL_CALL Legend::removeModifyListener( const Reference< util::XModifyListener >& aListener )
253 throw (uno::RuntimeException)
257 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
258 xBroadcaster->removeModifyListener( aListener );
260 catch( const uno::Exception & ex )
262 ASSERT_EXCEPTION( ex );
266 // ____ XModifyListener ____
267 void SAL_CALL Legend::modified( const lang::EventObject& aEvent )
268 throw (uno::RuntimeException)
270 m_xModifyEventForwarder->modified( aEvent );
273 // ____ XEventListener (base of XModifyListener) ____
274 void SAL_CALL Legend::disposing( const lang::EventObject& /* Source */ )
275 throw (uno::RuntimeException)
277 // nothing
280 // ____ OPropertySet ____
281 void Legend::firePropertyChangeEvent()
283 fireModifyEvent();
286 void Legend::fireModifyEvent()
288 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
291 // ================================================================================
293 Sequence< OUString > Legend::getSupportedServiceNames_Static()
295 const sal_Int32 nNumServices( 6 );
296 sal_Int32 nI = 0;
297 Sequence< OUString > aServices( nNumServices );
298 aServices[ nI++ ] = C2U( "com.sun.star.chart2.Legend" );
299 aServices[ nI++ ] = C2U( "com.sun.star.beans.PropertySet" );
300 aServices[ nI++ ] = C2U( "com.sun.star.drawing.FillProperties" );
301 aServices[ nI++ ] = C2U( "com.sun.star.drawing.LineProperties" );
302 aServices[ nI++ ] = C2U( "com.sun.star.style.CharacterProperties" );
303 aServices[ nI++ ] = C2U( "com.sun.star.layout.LayoutElement" );
304 OSL_ASSERT( nNumServices == nI );
305 return aServices;
308 // ____ OPropertySet ____
309 Any Legend::GetDefaultValue( sal_Int32 nHandle ) const
310 throw(beans::UnknownPropertyException)
312 static tPropertyValueMap aStaticDefaults;
314 // /--
315 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
316 if( 0 == aStaticDefaults.size() )
318 LineProperties::AddDefaultsToMap( aStaticDefaults );
319 FillProperties::AddDefaultsToMap( aStaticDefaults );
320 CharacterProperties::AddDefaultsToMap( aStaticDefaults );
321 // call last to overwrite some character property defaults
322 lcl_AddDefaultsToMap( aStaticDefaults );
325 tPropertyValueMap::const_iterator aFound(
326 aStaticDefaults.find( nHandle ));
328 if( aFound == aStaticDefaults.end())
329 return Any();
331 return (*aFound).second;
332 // \--
335 ::cppu::IPropertyArrayHelper & SAL_CALL Legend::getInfoHelper()
337 return lcl_getInfoHelper();
341 // ____ XPropertySet ____
342 Reference< beans::XPropertySetInfo > SAL_CALL
343 Legend::getPropertySetInfo()
344 throw (uno::RuntimeException)
346 static Reference< beans::XPropertySetInfo > xInfo;
348 // /--
349 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
350 if( !xInfo.is())
352 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
353 getInfoHelper());
356 return xInfo;
357 // \--
360 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
361 APPHELPER_XSERVICEINFO_IMPL( Legend, lcl_aServiceName );
363 // needed by MSC compiler
364 using impl::Legend_Base;
366 IMPLEMENT_FORWARD_XINTERFACE2( Legend, Legend_Base, ::property::OPropertySet )
367 IMPLEMENT_FORWARD_XTYPEPROVIDER2( Legend, Legend_Base, ::property::OPropertySet )
369 } // namespace chart