1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chartmodel.hxx"
26
27 #include "BaseCoordinateSystem.hxx"
28 #include "macros.hxx"
29 #include "PropertyHelper.hxx"
30 #include "UserDefinedProperties.hxx"
31 #include "ContainerHelper.hxx"
32 #include "CloneHelper.hxx"
33 #include "Axis.hxx"
34 #include "AxisHelper.hxx"
35 #include <com/sun/star/chart2/AxisType.hpp>
36
37 #include <algorithm>
38
39 #if OSL_DEBUG_LEVEL > 1
40 #include <rtl/math.hxx>
41 #endif
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
43
44 using namespace ::com::sun::star;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Sequence;
47 using ::rtl::OUString;
48 using ::com::sun::star::beans::Property;
49
50 namespace
51 {
52 enum
53 {
54 PROP_COORDINATESYSTEM_SWAPXANDYAXIS
55 };
56
lcl_AddPropertiesToVector(::std::vector<Property> & rOutProperties)57 void lcl_AddPropertiesToVector(
58 ::std::vector< Property > & rOutProperties )
59 {
60 rOutProperties.push_back(
61 Property( C2U( "SwapXAndYAxis" ),
62 PROP_COORDINATESYSTEM_SWAPXANDYAXIS,
63 ::getBooleanCppuType(),
64 beans::PropertyAttribute::BOUND
65 | beans::PropertyAttribute::MAYBEVOID ));
66 }
67
68 struct StaticCooSysDefaults_Initializer
69 {
operator ()__anona2a3d1a60111::StaticCooSysDefaults_Initializer70 ::chart::tPropertyValueMap* operator()()
71 {
72 static ::chart::tPropertyValueMap aStaticDefaults;
73 lcl_AddDefaultsToMap( aStaticDefaults );
74 return &aStaticDefaults;
75 }
76 private:
lcl_AddDefaultsToMap__anona2a3d1a60111::StaticCooSysDefaults_Initializer77 void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
78 {
79 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_COORDINATESYSTEM_SWAPXANDYAXIS, false );
80 }
81 };
82
83 struct StaticCooSysDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticCooSysDefaults_Initializer >
84 {
85 };
86
87 struct StaticCooSysInfoHelper_Initializer
88 {
operator ()__anona2a3d1a60111::StaticCooSysInfoHelper_Initializer89 ::cppu::OPropertyArrayHelper* operator()()
90 {
91 static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
92 return &aPropHelper;
93 }
94
95 private:
lcl_GetPropertySequence__anona2a3d1a60111::StaticCooSysInfoHelper_Initializer96 Sequence< Property > lcl_GetPropertySequence()
97 {
98 ::std::vector< ::com::sun::star::beans::Property > aProperties;
99 lcl_AddPropertiesToVector( aProperties );
100 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
101
102 ::std::sort( aProperties.begin(), aProperties.end(),
103 ::chart::PropertyNameLess() );
104
105 return ::chart::ContainerHelper::ContainerToSequence( aProperties );
106 }
107 };
108
109 struct StaticCooSysInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticCooSysInfoHelper_Initializer >
110 {
111 };
112
113 struct StaticCooSysInfo_Initializer
114 {
operator ()__anona2a3d1a60111::StaticCooSysInfo_Initializer115 uno::Reference< beans::XPropertySetInfo >* operator()()
116 {
117 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
118 ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticCooSysInfoHelper::get() ) );
119 return &xPropertySetInfo;
120 }
121 };
122
123 struct StaticCooSysInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticCooSysInfo_Initializer >
124 {
125 };
126
127 } // anonymous namespace
128
129 namespace chart
130 {
131
BaseCoordinateSystem(const Reference<uno::XComponentContext> & xContext,sal_Int32 nDimensionCount,sal_Bool bSwapXAndYAxis)132 BaseCoordinateSystem::BaseCoordinateSystem(
133 const Reference< uno::XComponentContext > & xContext,
134 sal_Int32 nDimensionCount /* = 2 */,
135 sal_Bool bSwapXAndYAxis /* = sal_False */ ) :
136 ::property::OPropertySet( m_aMutex ),
137 m_xContext( xContext ),
138 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
139 m_nDimensionCount( nDimensionCount )
140 {
141 m_aAllAxis.resize( m_nDimensionCount );
142 for( sal_Int32 nN=0; nN<m_nDimensionCount; nN++ )
143 {
144 m_aAllAxis[nN].resize( 1 );
145 Reference< chart2::XAxis > xAxis( new Axis(m_xContext) );
146 m_aAllAxis[nN][0] = xAxis;
147
148 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
149 chart2::ScaleData aScaleData( xAxis->getScaleData() );
150 if(nN==0)
151 {
152 aScaleData.AxisType = chart2::AxisType::CATEGORY;
153 }
154 else if( nN==1)
155 {
156 aScaleData.AxisType = chart2::AxisType::REALNUMBER;
157 }
158 else if( nN==2)
159 {
160 aScaleData.AxisType = chart2::AxisType::SERIES;
161 }
162 xAxis->setScaleData( aScaleData );
163 }
164
165 m_aOrigin.realloc( m_nDimensionCount );
166 for( sal_Int32 i = 0; i < m_nDimensionCount; ++i )
167 m_aOrigin[ i ] = uno::makeAny( double( 0.0 ) );
168
169 setFastPropertyValue_NoBroadcast( PROP_COORDINATESYSTEM_SWAPXANDYAXIS, uno::makeAny( sal_Bool( bSwapXAndYAxis )));
170 }
171
172 // explicit
BaseCoordinateSystem(const BaseCoordinateSystem & rSource)173 BaseCoordinateSystem::BaseCoordinateSystem(
174 const BaseCoordinateSystem & rSource ) :
175 impl::BaseCoordinateSystem_Base(),
176 MutexContainer(),
177 ::property::OPropertySet( rSource, m_aMutex ),
178 m_xContext( rSource.m_xContext ),
179 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()),
180 m_nDimensionCount( rSource.m_nDimensionCount ),
181 m_aOrigin( rSource.m_aOrigin )
182 {
183 m_aAllAxis.resize(rSource.m_aAllAxis.size());
184 tAxisVecVecType::size_type nN=0;
185 for( nN=0; nN<m_aAllAxis.size(); nN++ )
186 CloneHelper::CloneRefVector< Reference< chart2::XAxis > >( rSource.m_aAllAxis[nN], m_aAllAxis[nN] );
187 CloneHelper::CloneRefVector< Reference< chart2::XChartType > >( rSource.m_aChartTypes, m_aChartTypes );
188
189 for( nN=0; nN<m_aAllAxis.size(); nN++ )
190 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
191 ModifyListenerHelper::addListenerToAllElements( m_aChartTypes, m_xModifyEventForwarder );
192 }
193
~BaseCoordinateSystem()194 BaseCoordinateSystem::~BaseCoordinateSystem()
195 {
196 try
197 {
198 for( tAxisVecVecType::size_type nN=0; nN<m_aAllAxis.size(); nN++ )
199 ModifyListenerHelper::removeListenerFromAllElements( m_aAllAxis[nN], m_xModifyEventForwarder );
200 ModifyListenerHelper::removeListenerFromAllElements( m_aChartTypes, m_xModifyEventForwarder );
201 }
202 catch( const uno::Exception & ex )
203 {
204 ASSERT_EXCEPTION( ex );
205 }
206 }
207
208 // ____ XCoordinateSystem ____
getDimension()209 sal_Int32 SAL_CALL BaseCoordinateSystem::getDimension()
210 {
211 return m_nDimensionCount;
212 }
213
setAxisByDimension(sal_Int32 nDimensionIndex,const Reference<chart2::XAxis> & xAxis,sal_Int32 nIndex)214 void SAL_CALL BaseCoordinateSystem::setAxisByDimension(
215 sal_Int32 nDimensionIndex,
216 const Reference< chart2::XAxis >& xAxis,
217 sal_Int32 nIndex )
218 {
219 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
220 throw lang::IndexOutOfBoundsException();
221
222 if( nIndex < 0 )
223 throw lang::IndexOutOfBoundsException();
224
225 if( m_aAllAxis[ nDimensionIndex ].size() < static_cast< tAxisVecVecType::size_type >( nIndex+1 ))
226 {
227 m_aAllAxis[ nDimensionIndex ].resize( nIndex+1 );
228 m_aAllAxis[ nDimensionIndex ][nIndex] = 0;
229 }
230
231 Reference< chart2::XAxis > xOldAxis( m_aAllAxis[ nDimensionIndex ][nIndex] );
232 if( xOldAxis.is())
233 ModifyListenerHelper::removeListener( xOldAxis, m_xModifyEventForwarder );
234 m_aAllAxis[ nDimensionIndex ][nIndex] = xAxis;
235 if( xAxis.is())
236 ModifyListenerHelper::addListener( xAxis, m_xModifyEventForwarder );
237 fireModifyEvent();
238 }
239
getAxisByDimension(sal_Int32 nDimensionIndex,sal_Int32 nAxisIndex)240 Reference< chart2::XAxis > SAL_CALL BaseCoordinateSystem::getAxisByDimension(
241 sal_Int32 nDimensionIndex, sal_Int32 nAxisIndex )
242 {
243 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
244 throw lang::IndexOutOfBoundsException();
245
246 OSL_ASSERT( m_aAllAxis.size() == static_cast< size_t >( getDimension()));
247
248 if( nAxisIndex < 0 || nAxisIndex > this->getMaximumAxisIndexByDimension(nDimensionIndex) )
249 throw lang::IndexOutOfBoundsException();
250
251 return m_aAllAxis[ nDimensionIndex ][nAxisIndex];
252 }
253
getMaximumAxisIndexByDimension(sal_Int32 nDimensionIndex)254 sal_Int32 SAL_CALL BaseCoordinateSystem::getMaximumAxisIndexByDimension( sal_Int32 nDimensionIndex )
255 {
256 if( nDimensionIndex < 0 || nDimensionIndex >= getDimension() )
257 throw lang::IndexOutOfBoundsException();
258
259 OSL_ASSERT( m_aAllAxis.size() == static_cast< size_t >( getDimension()));
260
261 sal_Int32 nRet = m_aAllAxis[ nDimensionIndex ].size();
262 if(nRet)
263 nRet-=1;
264
265 return nRet;
266 }
267
268 // ____ XChartTypeContainer ____
addChartType(const Reference<chart2::XChartType> & aChartType)269 void SAL_CALL BaseCoordinateSystem::addChartType( const Reference< chart2::XChartType >& aChartType )
270 {
271 if( ::std::find( m_aChartTypes.begin(), m_aChartTypes.end(), aChartType )
272 != m_aChartTypes.end())
273 throw lang::IllegalArgumentException();
274
275 m_aChartTypes.push_back( aChartType );
276 ModifyListenerHelper::addListener( aChartType, m_xModifyEventForwarder );
277 fireModifyEvent();
278 }
279
removeChartType(const Reference<chart2::XChartType> & aChartType)280 void SAL_CALL BaseCoordinateSystem::removeChartType( const Reference< chart2::XChartType >& aChartType )
281 {
282 ::std::vector< uno::Reference< chart2::XChartType > >::iterator
283 aIt( ::std::find( m_aChartTypes.begin(), m_aChartTypes.end(), aChartType ));
284 if( aIt == m_aChartTypes.end())
285 throw container::NoSuchElementException(
286 C2U( "The given chart type is no element of the container" ),
287 static_cast< uno::XWeak * >( this ));
288
289 m_aChartTypes.erase( aIt );
290 ModifyListenerHelper::removeListener( aChartType, m_xModifyEventForwarder );
291 fireModifyEvent();
292 }
293
getChartTypes()294 Sequence< Reference< chart2::XChartType > > SAL_CALL BaseCoordinateSystem::getChartTypes()
295 {
296 return ContainerHelper::ContainerToSequence( m_aChartTypes );
297 }
298
setChartTypes(const Sequence<Reference<chart2::XChartType>> & aChartTypes)299 void SAL_CALL BaseCoordinateSystem::setChartTypes( const Sequence< Reference< chart2::XChartType > >& aChartTypes )
300 {
301 ModifyListenerHelper::removeListenerFromAllElements( m_aChartTypes, m_xModifyEventForwarder );
302 m_aChartTypes = ContainerHelper::SequenceToVector( aChartTypes );
303 ModifyListenerHelper::addListenerToAllElements( m_aChartTypes, m_xModifyEventForwarder );
304 fireModifyEvent();
305 }
306
307 // ____ XModifyBroadcaster ____
addModifyListener(const Reference<util::XModifyListener> & aListener)308 void SAL_CALL BaseCoordinateSystem::addModifyListener( const Reference< util::XModifyListener >& aListener )
309 {
310 try
311 {
312 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
313 xBroadcaster->addModifyListener( aListener );
314 }
315 catch( const uno::Exception & ex )
316 {
317 ASSERT_EXCEPTION( ex );
318 }
319 }
320
removeModifyListener(const Reference<util::XModifyListener> & aListener)321 void SAL_CALL BaseCoordinateSystem::removeModifyListener( const Reference< util::XModifyListener >& aListener )
322 {
323 try
324 {
325 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
326 xBroadcaster->removeModifyListener( aListener );
327 }
328 catch( const uno::Exception & ex )
329 {
330 ASSERT_EXCEPTION( ex );
331 }
332 }
333
334 // ____ XModifyListener ____
modified(const lang::EventObject & aEvent)335 void SAL_CALL BaseCoordinateSystem::modified( const lang::EventObject& aEvent )
336 {
337 m_xModifyEventForwarder->modified( aEvent );
338 }
339
340 // ____ XEventListener (base of XModifyListener) ____
disposing(const lang::EventObject &)341 void SAL_CALL BaseCoordinateSystem::disposing( const lang::EventObject& /* Source */ )
342 {
343 // nothing
344 }
345
346 // ____ OPropertySet ____
firePropertyChangeEvent()347 void BaseCoordinateSystem::firePropertyChangeEvent()
348 {
349 fireModifyEvent();
350 }
351
fireModifyEvent()352 void BaseCoordinateSystem::fireModifyEvent()
353 {
354 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
355 }
356
357
358 // ____ OPropertySet ____
GetDefaultValue(sal_Int32 nHandle) const359 uno::Any BaseCoordinateSystem::GetDefaultValue( sal_Int32 nHandle ) const
360 {
361 const tPropertyValueMap& rStaticDefaults = *StaticCooSysDefaults::get();
362 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
363 if( aFound == rStaticDefaults.end() )
364 return uno::Any();
365 return (*aFound).second;
366 }
367
368 // ____ OPropertySet ____
getInfoHelper()369 ::cppu::IPropertyArrayHelper & SAL_CALL BaseCoordinateSystem::getInfoHelper()
370 {
371 return *StaticCooSysInfoHelper::get();
372 }
373
374
375 // ____ XPropertySet ____
getPropertySetInfo()376 Reference< beans::XPropertySetInfo > SAL_CALL BaseCoordinateSystem::getPropertySetInfo()
377 {
378 return *StaticCooSysInfo::get();
379 }
380
381 using impl::BaseCoordinateSystem_Base;
382
383 IMPLEMENT_FORWARD_XINTERFACE2( BaseCoordinateSystem, BaseCoordinateSystem_Base, ::property::OPropertySet )
384 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BaseCoordinateSystem, BaseCoordinateSystem_Base, ::property::OPropertySet )
385
386 } // namespace chart
387