merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / model / main / BaseCoordinateSystem.cxx
blob61ef5be1506d249b9393ee0d4ac7c53cbfdac375
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: BaseCoordinateSystem.cxx,v $
10 * $Revision: 1.6 $
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"
34 #include "BaseCoordinateSystem.hxx"
35 #include "macros.hxx"
36 #include "PropertyHelper.hxx"
37 #include "UserDefinedProperties.hxx"
38 #include "ContainerHelper.hxx"
39 #include "CloneHelper.hxx"
40 #include "Axis.hxx"
41 #include "AxisHelper.hxx"
42 #include <com/sun/star/chart2/AxisType.hpp>
44 #include <algorithm>
46 #if OSL_DEBUG_LEVEL > 1
47 #include <rtl/math.hxx>
48 #endif
49 #include <com/sun/star/beans/PropertyAttribute.hpp>
51 using namespace ::com::sun::star;
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::Sequence;
54 using ::rtl::OUString;
55 using ::com::sun::star::beans::Property;
57 namespace
59 enum
61 PROP_COORDINATESYSTEM_SWAPXANDYAXIS
64 void lcl_AddPropertiesToVector(
65 ::std::vector< Property > & rOutProperties )
67 rOutProperties.push_back(
68 Property( C2U( "SwapXAndYAxis" ),
69 PROP_COORDINATESYSTEM_SWAPXANDYAXIS,
70 ::getBooleanCppuType(),
71 beans::PropertyAttribute::BOUND
72 | beans::PropertyAttribute::MAYBEVOID ));
75 void lcl_AddDefaultsToMap(
76 ::chart::tPropertyValueMap & rOutMap )
78 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_COORDINATESYSTEM_SWAPXANDYAXIS, false );
81 const Sequence< Property > & lcl_GetPropertySequence()
83 static Sequence< Property > aPropSeq;
85 // /--
86 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
87 if( 0 == aPropSeq.getLength() )
89 // get properties
90 ::std::vector< ::com::sun::star::beans::Property > aProperties;
91 lcl_AddPropertiesToVector( aProperties );
92 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
94 // and sort them for access via bsearch
95 ::std::sort( aProperties.begin(), aProperties.end(),
96 ::chart::PropertyNameLess() );
98 // transfer result to static Sequence
99 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
102 return aPropSeq;
105 } // anonymous namespace
107 namespace chart
110 BaseCoordinateSystem::BaseCoordinateSystem(
111 const Reference< uno::XComponentContext > & xContext,
112 sal_Int32 nDimensionCount /* = 2 */,
113 sal_Bool bSwapXAndYAxis /* = sal_False */ ) :
114 ::property::OPropertySet( m_aMutex ),
115 m_xContext( xContext ),
116 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
117 m_nDimensionCount( nDimensionCount )
119 m_aAllAxis.resize( m_nDimensionCount );
120 for( sal_Int32 nN=0; nN<m_nDimensionCount; nN++ )
122 m_aAllAxis[nN].resize( 1 );
123 Reference< chart2::XAxis > xAxis( new Axis(m_xContext) );
124 m_aAllAxis[nN][0] = xAxis;
126 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
127 chart2::ScaleData aScaleData( xAxis->getScaleData() );
128 if(nN==0)
130 aScaleData.AxisType = chart2::AxisType::CATEGORY;
132 else if( nN==1)
134 aScaleData.AxisType = chart2::AxisType::REALNUMBER;
136 else if( nN==2)
138 aScaleData.AxisType = chart2::AxisType::SERIES;
140 xAxis->setScaleData( aScaleData );
143 m_aOrigin.realloc( m_nDimensionCount );
144 for( sal_Int32 i = 0; i < m_nDimensionCount; ++i )
145 m_aOrigin[ i ] = uno::makeAny( double( 0.0 ) );
147 setFastPropertyValue_NoBroadcast( PROP_COORDINATESYSTEM_SWAPXANDYAXIS, uno::makeAny( sal_Bool( bSwapXAndYAxis )));
150 // explicit
151 BaseCoordinateSystem::BaseCoordinateSystem(
152 const BaseCoordinateSystem & rSource ) :
153 impl::BaseCoordinateSystem_Base(),
154 MutexContainer(),
155 ::property::OPropertySet( rSource, m_aMutex ),
156 m_xContext( rSource.m_xContext ),
157 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
158 m_nDimensionCount( rSource.m_nDimensionCount ),
159 m_aOrigin( rSource.m_aOrigin )
161 m_aAllAxis.resize(rSource.m_aAllAxis.size());
162 tAxisVecVecType::size_type nN=0;
163 for( nN=0; nN<m_aAllAxis.size(); nN++ )
164 CloneHelper::CloneRefVector< Reference< chart2::XAxis > >( rSource.m_aAllAxis[nN], m_aAllAxis[nN] );
165 CloneHelper::CloneRefVector< Reference< chart2::XChartType > >( rSource.m_aChartTypes, m_aChartTypes );
167 for( nN=0; nN<m_aAllAxis.size(); nN++ )
168 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
169 ModifyListenerHelper::addListenerToAllElements( m_aChartTypes, m_xModifyEventForwarder );
172 BaseCoordinateSystem::~BaseCoordinateSystem()
176 for( tAxisVecVecType::size_type nN=0; nN<m_aAllAxis.size(); nN++ )
177 ModifyListenerHelper::removeListenerFromAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
178 ModifyListenerHelper::removeListenerFromAllElements( m_aChartTypes, m_xModifyEventForwarder );
180 catch( const uno::Exception & ex )
182 ASSERT_EXCEPTION( ex );
186 // ____ XCoordinateSystem ____
187 sal_Int32 SAL_CALL BaseCoordinateSystem::getDimension()
188 throw (uno::RuntimeException)
190 return m_nDimensionCount;
193 void SAL_CALL BaseCoordinateSystem::setAxisByDimension(
194 sal_Int32 nDimensionIndex,
195 const Reference< chart2::XAxis >& xAxis,
196 sal_Int32 nIndex )
197 throw (lang::IndexOutOfBoundsException,
198 uno::RuntimeException)
200 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
201 throw lang::IndexOutOfBoundsException();
203 if( nIndex < 0 )
204 throw lang::IndexOutOfBoundsException();
206 if( m_aAllAxis[ nDimensionIndex ].size() < static_cast< tAxisVecVecType::size_type >( nIndex+1 ))
208 m_aAllAxis[ nDimensionIndex ].resize( nIndex+1 );
209 m_aAllAxis[ nDimensionIndex ][nIndex] = 0;
212 Reference< chart2::XAxis > xOldAxis( m_aAllAxis[ nDimensionIndex ][nIndex] );
213 if( xOldAxis.is())
214 ModifyListenerHelper::removeListener( xOldAxis, m_xModifyEventForwarder );
215 m_aAllAxis[ nDimensionIndex ][nIndex] = xAxis;
216 if( xAxis.is())
217 ModifyListenerHelper::addListener( xAxis, m_xModifyEventForwarder );
218 fireModifyEvent();
221 Reference< chart2::XAxis > SAL_CALL BaseCoordinateSystem::getAxisByDimension(
222 sal_Int32 nDimensionIndex, sal_Int32 nAxisIndex )
223 throw (lang::IndexOutOfBoundsException,
224 uno::RuntimeException)
226 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
227 throw lang::IndexOutOfBoundsException();
229 OSL_ASSERT( m_aAllAxis.size() == static_cast< size_t >( getDimension()));
231 if( nAxisIndex < 0 || nAxisIndex > this->getMaximumAxisIndexByDimension(nDimensionIndex) )
232 throw lang::IndexOutOfBoundsException();
234 return m_aAllAxis[ nDimensionIndex ][nAxisIndex];
237 sal_Int32 SAL_CALL BaseCoordinateSystem::getMaximumAxisIndexByDimension( sal_Int32 nDimensionIndex )
238 throw (lang::IndexOutOfBoundsException,
239 uno::RuntimeException)
241 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
242 throw lang::IndexOutOfBoundsException();
244 OSL_ASSERT( m_aAllAxis.size() == static_cast< size_t >( getDimension()));
246 sal_Int32 nRet = m_aAllAxis[ nDimensionIndex ].size();
247 if(nRet)
248 nRet-=1;
250 return nRet;
253 // ____ XChartTypeContainer ____
254 void SAL_CALL BaseCoordinateSystem::addChartType( const Reference< chart2::XChartType >& aChartType )
255 throw (lang::IllegalArgumentException,
256 uno::RuntimeException)
258 if( ::std::find( m_aChartTypes.begin(), m_aChartTypes.end(), aChartType )
259 != m_aChartTypes.end())
260 throw lang::IllegalArgumentException();
262 m_aChartTypes.push_back( aChartType );
263 ModifyListenerHelper::addListener( aChartType, m_xModifyEventForwarder );
264 fireModifyEvent();
267 void SAL_CALL BaseCoordinateSystem::removeChartType( const Reference< chart2::XChartType >& aChartType )
268 throw (container::NoSuchElementException,
269 uno::RuntimeException)
271 ::std::vector< uno::Reference< chart2::XChartType > >::iterator
272 aIt( ::std::find( m_aChartTypes.begin(), m_aChartTypes.end(), aChartType ));
273 if( aIt == m_aChartTypes.end())
274 throw container::NoSuchElementException(
275 C2U( "The given chart type is no element of the container" ),
276 static_cast< uno::XWeak * >( this ));
278 m_aChartTypes.erase( aIt );
279 ModifyListenerHelper::removeListener( aChartType, m_xModifyEventForwarder );
280 fireModifyEvent();
283 Sequence< Reference< chart2::XChartType > > SAL_CALL BaseCoordinateSystem::getChartTypes()
284 throw (uno::RuntimeException)
286 return ContainerHelper::ContainerToSequence( m_aChartTypes );
289 void SAL_CALL BaseCoordinateSystem::setChartTypes( const Sequence< Reference< chart2::XChartType > >& aChartTypes )
290 throw (lang::IllegalArgumentException,
291 uno::RuntimeException)
293 ModifyListenerHelper::removeListenerFromAllElements( m_aChartTypes, m_xModifyEventForwarder );
294 m_aChartTypes = ContainerHelper::SequenceToVector( aChartTypes );
295 ModifyListenerHelper::addListenerToAllElements( m_aChartTypes, m_xModifyEventForwarder );
296 fireModifyEvent();
299 // ____ XModifyBroadcaster ____
300 void SAL_CALL BaseCoordinateSystem::addModifyListener( const Reference< util::XModifyListener >& aListener )
301 throw (uno::RuntimeException)
305 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
306 xBroadcaster->addModifyListener( aListener );
308 catch( const uno::Exception & ex )
310 ASSERT_EXCEPTION( ex );
314 void SAL_CALL BaseCoordinateSystem::removeModifyListener( const Reference< util::XModifyListener >& aListener )
315 throw (uno::RuntimeException)
319 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
320 xBroadcaster->removeModifyListener( aListener );
322 catch( const uno::Exception & ex )
324 ASSERT_EXCEPTION( ex );
328 // ____ XModifyListener ____
329 void SAL_CALL BaseCoordinateSystem::modified( const lang::EventObject& aEvent )
330 throw (uno::RuntimeException)
332 m_xModifyEventForwarder->modified( aEvent );
335 // ____ XEventListener (base of XModifyListener) ____
336 void SAL_CALL BaseCoordinateSystem::disposing( const lang::EventObject& /* Source */ )
337 throw (uno::RuntimeException)
339 // nothing
342 // ____ OPropertySet ____
343 void BaseCoordinateSystem::firePropertyChangeEvent()
345 fireModifyEvent();
348 void BaseCoordinateSystem::fireModifyEvent()
350 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
354 // ____ OPropertySet ____
355 uno::Any BaseCoordinateSystem::GetDefaultValue( sal_Int32 nHandle ) const
356 throw(beans::UnknownPropertyException)
358 static tPropertyValueMap aStaticDefaults;
360 // /--
361 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
362 if( 0 == aStaticDefaults.size() )
364 // initialize defaults
365 lcl_AddDefaultsToMap( aStaticDefaults );
368 tPropertyValueMap::const_iterator aFound(
369 aStaticDefaults.find( nHandle ));
371 if( aFound == aStaticDefaults.end())
372 return uno::Any();
374 return (*aFound).second;
375 // \--
378 // ____ OPropertySet ____
379 ::cppu::IPropertyArrayHelper & SAL_CALL BaseCoordinateSystem::getInfoHelper()
381 static ::cppu::OPropertyArrayHelper aArrayHelper( lcl_GetPropertySequence(),
382 /* bSorted = */ sal_True );
384 return aArrayHelper;
388 // ____ XPropertySet ____
389 Reference< beans::XPropertySetInfo > SAL_CALL
390 BaseCoordinateSystem::getPropertySetInfo()
391 throw (uno::RuntimeException)
393 static Reference< beans::XPropertySetInfo > xInfo;
395 // /--
396 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
397 if( !xInfo.is())
399 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
400 getInfoHelper());
403 return xInfo;
404 // \--
407 using impl::BaseCoordinateSystem_Base;
409 IMPLEMENT_FORWARD_XINTERFACE2( BaseCoordinateSystem, BaseCoordinateSystem_Base, ::property::OPropertySet )
410 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BaseCoordinateSystem, BaseCoordinateSystem_Base, ::property::OPropertySet )
412 } // namespace chart