1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <BaseCoordinateSystem.hxx>
21 #include <PropertyHelper.hxx>
22 #include <UserDefinedProperties.hxx>
23 #include <CloneHelper.hxx>
24 #include <ModifyListenerHelper.hxx>
26 #include <ChartType.hxx>
27 #include <com/sun/star/chart2/AxisType.hpp>
28 #include <com/sun/star/container/NoSuchElementException.hpp>
29 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
30 #include <o3tl/safeint.hxx>
31 #include <comphelper/diagnose_ex.hxx>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 using namespace ::com::sun::star
;
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::uno::Sequence
;
40 using ::com::sun::star::beans::Property
;
46 PROP_COORDINATESYSTEM_SWAPXANDYAXIS
49 void lcl_AddPropertiesToVector(
50 std::vector
< Property
> & rOutProperties
)
52 rOutProperties
.emplace_back( "SwapXAndYAxis",
53 PROP_COORDINATESYSTEM_SWAPXANDYAXIS
,
54 cppu::UnoType
<bool>::get(),
55 beans::PropertyAttribute::BOUND
56 | beans::PropertyAttribute::MAYBEVOID
);
59 const ::chart::tPropertyValueMap
& StaticCooSysDefaults()
61 static ::chart::tPropertyValueMap aStaticDefaults
= []()
63 ::chart::tPropertyValueMap aMap
;
64 ::chart::PropertyHelper::setPropertyValueDefault( aMap
, PROP_COORDINATESYSTEM_SWAPXANDYAXIS
, false );
67 return aStaticDefaults
;
70 ::cppu::OPropertyArrayHelper
& StaticCooSysInfoHelper()
72 static ::cppu::OPropertyArrayHelper aPropHelper
= []()
74 std::vector
< css::beans::Property
> aProperties
;
75 lcl_AddPropertiesToVector( aProperties
);
76 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties
);
78 std::sort( aProperties
.begin(), aProperties
.end(),
79 ::chart::PropertyNameLess() );
81 return comphelper::containerToSequence( aProperties
);
86 } // anonymous namespace
91 BaseCoordinateSystem::BaseCoordinateSystem(
92 sal_Int32 nDimensionCount
/* = 2 */ ) :
93 m_xModifyEventForwarder( new ModifyEventForwarder() ),
94 m_nDimensionCount( nDimensionCount
)
96 m_aAllAxis
.resize( m_nDimensionCount
);
97 for( sal_Int32 nN
=0; nN
<m_nDimensionCount
; nN
++ )
99 m_aAllAxis
[nN
].resize( 1 );
100 rtl::Reference
< Axis
> xAxis( new Axis
);
101 m_aAllAxis
[nN
][0] = xAxis
;
103 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis
[nN
], m_xModifyEventForwarder
);
104 chart2::ScaleData
aScaleData( xAxis
->getScaleData() );
107 aScaleData
.AxisType
= chart2::AxisType::CATEGORY
;
111 aScaleData
.AxisType
= chart2::AxisType::REALNUMBER
;
115 aScaleData
.AxisType
= chart2::AxisType::SERIES
;
117 xAxis
->setScaleData( aScaleData
);
120 setFastPropertyValue_NoBroadcast( PROP_COORDINATESYSTEM_SWAPXANDYAXIS
, uno::Any( false ));
124 BaseCoordinateSystem::BaseCoordinateSystem(
125 const BaseCoordinateSystem
& rSource
) :
126 impl::BaseCoordinateSystem_Base(rSource
),
127 ::property::OPropertySet( rSource
),
128 m_xModifyEventForwarder( new ModifyEventForwarder() ),
129 m_nDimensionCount( rSource
.m_nDimensionCount
)
131 m_aAllAxis
.resize(rSource
.m_aAllAxis
.size());
132 tAxisVecVecType::size_type nN
=0;
133 for( nN
=0; nN
<m_aAllAxis
.size(); nN
++ )
134 CloneHelper::CloneRefVector( rSource
.m_aAllAxis
[nN
], m_aAllAxis
[nN
] );
135 for (const auto & rxChartType
: rSource
.m_aChartTypes
)
136 m_aChartTypes
.push_back(rxChartType
->cloneChartType());
138 for( nN
=0; nN
<m_aAllAxis
.size(); nN
++ )
139 ModifyListenerHelper::addListenerToAllElements( m_aAllAxis
[nN
], m_xModifyEventForwarder
);
140 for (const auto & rxChartType
: m_aChartTypes
)
141 rxChartType
->addModifyListener( m_xModifyEventForwarder
);
144 BaseCoordinateSystem::~BaseCoordinateSystem()
148 for(const tAxisVecVecType::value_type
& i
: m_aAllAxis
)
149 ModifyListenerHelper::removeListenerFromAllElements( i
, m_xModifyEventForwarder
);
150 for (const auto & rxChartType
: m_aChartTypes
)
151 rxChartType
->removeModifyListener( m_xModifyEventForwarder
);
153 catch( const uno::Exception
& )
155 DBG_UNHANDLED_EXCEPTION("chart2" );
159 // ____ XCoordinateSystem ____
160 sal_Int32 SAL_CALL
BaseCoordinateSystem::getDimension()
162 return m_nDimensionCount
;
165 void SAL_CALL
BaseCoordinateSystem::setAxisByDimension(
166 sal_Int32 nDimensionIndex
,
167 const Reference
< chart2::XAxis
>& xAxis
,
170 if( nDimensionIndex
< 0 || nDimensionIndex
>= getDimension() )
171 throw lang::IndexOutOfBoundsException();
174 throw lang::IndexOutOfBoundsException();
176 assert(!xAxis
|| dynamic_cast<Axis
*>(xAxis
.get()));
178 if( m_aAllAxis
[ nDimensionIndex
].size() < o3tl::make_unsigned( nIndex
+1 ))
180 m_aAllAxis
[ nDimensionIndex
].resize( nIndex
+1 );
181 m_aAllAxis
[ nDimensionIndex
][nIndex
] = nullptr;
184 rtl::Reference
< Axis
> xOldAxis( m_aAllAxis
[ nDimensionIndex
][nIndex
] );
186 ModifyListenerHelper::removeListener( xOldAxis
, m_xModifyEventForwarder
);
187 m_aAllAxis
[ nDimensionIndex
][nIndex
] = dynamic_cast<Axis
*>(xAxis
.get());
189 ModifyListenerHelper::addListener( xAxis
, m_xModifyEventForwarder
);
193 void BaseCoordinateSystem::setAxisByDimension(
194 sal_Int32 nDimensionIndex
,
195 const rtl::Reference
< Axis
>& xAxis
,
198 if( nDimensionIndex
< 0 || nDimensionIndex
>= getDimension() )
199 throw lang::IndexOutOfBoundsException();
202 throw lang::IndexOutOfBoundsException();
204 if( m_aAllAxis
[ nDimensionIndex
].size() < o3tl::make_unsigned( nIndex
+1 ))
206 m_aAllAxis
[ nDimensionIndex
].resize( nIndex
+1 );
207 m_aAllAxis
[ nDimensionIndex
][nIndex
] = nullptr;
210 rtl::Reference
< Axis
> xOldAxis( m_aAllAxis
[ nDimensionIndex
][nIndex
] );
212 ModifyListenerHelper::removeListener( xOldAxis
, m_xModifyEventForwarder
);
213 m_aAllAxis
[ nDimensionIndex
][nIndex
] = xAxis
;
215 ModifyListenerHelper::addListener( xAxis
, m_xModifyEventForwarder
);
219 Reference
< chart2::XAxis
> SAL_CALL
BaseCoordinateSystem::getAxisByDimension(
220 sal_Int32 nDimensionIndex
, sal_Int32 nAxisIndex
)
222 if( nDimensionIndex
< 0 || nDimensionIndex
>= getDimension() )
223 throw lang::IndexOutOfBoundsException();
225 OSL_ASSERT( m_aAllAxis
.size() == static_cast< size_t >( getDimension()));
227 if( nAxisIndex
< 0 || nAxisIndex
> getMaximumAxisIndexByDimension(nDimensionIndex
) )
228 throw lang::IndexOutOfBoundsException();
230 return m_aAllAxis
[ nDimensionIndex
][nAxisIndex
];
233 const rtl::Reference
< Axis
> & BaseCoordinateSystem::getAxisByDimension2(
234 sal_Int32 nDimensionIndex
, sal_Int32 nAxisIndex
) const
236 if( nDimensionIndex
< 0 || nDimensionIndex
>= m_nDimensionCount
)
237 throw lang::IndexOutOfBoundsException();
239 OSL_ASSERT( m_aAllAxis
.size() == static_cast< size_t >( m_nDimensionCount
));
241 if( nAxisIndex
< 0 || o3tl::make_unsigned(nAxisIndex
) > m_aAllAxis
[ nDimensionIndex
].size() )
242 throw lang::IndexOutOfBoundsException();
244 return m_aAllAxis
[ nDimensionIndex
][nAxisIndex
];
247 sal_Int32 SAL_CALL
BaseCoordinateSystem::getMaximumAxisIndexByDimension( sal_Int32 nDimensionIndex
)
249 if( nDimensionIndex
< 0 || nDimensionIndex
>= getDimension() )
250 throw lang::IndexOutOfBoundsException();
252 OSL_ASSERT( m_aAllAxis
.size() == static_cast< size_t >( getDimension()));
254 sal_Int32 nRet
= m_aAllAxis
[ nDimensionIndex
].size();
261 // ____ XChartTypeContainer ____
262 void SAL_CALL
BaseCoordinateSystem::addChartType( const Reference
< chart2::XChartType
>& aChartType
)
264 auto pChartType
= dynamic_cast<ChartType
*>(aChartType
.get());
267 if( std::find( m_aChartTypes
.begin(), m_aChartTypes
.end(), pChartType
)
268 != m_aChartTypes
.end())
269 throw lang::IllegalArgumentException(u
"type not found"_ustr
, static_cast<cppu::OWeakObject
*>(this), 1);
271 m_aChartTypes
.push_back( pChartType
);
272 ModifyListenerHelper::addListener( aChartType
, m_xModifyEventForwarder
);
276 void SAL_CALL
BaseCoordinateSystem::removeChartType( const Reference
< chart2::XChartType
>& aChartType
)
278 auto pChartType
= dynamic_cast<ChartType
*>(aChartType
.get());
280 auto aIt( std::find( m_aChartTypes
.begin(), m_aChartTypes
.end(), pChartType
));
281 if( aIt
== m_aChartTypes
.end())
282 throw container::NoSuchElementException(
283 u
"The given chart type is no element of the container"_ustr
,
284 static_cast< uno::XWeak
* >( this ));
286 m_aChartTypes
.erase( aIt
);
287 ModifyListenerHelper::removeListener( aChartType
, m_xModifyEventForwarder
);
291 Sequence
< Reference
< chart2::XChartType
> > SAL_CALL
BaseCoordinateSystem::getChartTypes()
293 return comphelper::containerToSequence
< Reference
< chart2::XChartType
> >( m_aChartTypes
);
296 void SAL_CALL
BaseCoordinateSystem::setChartTypes( const Sequence
< Reference
< chart2::XChartType
> >& aChartTypes
)
298 for (auto const & aChartType
: m_aChartTypes
)
299 aChartType
->removeModifyListener( m_xModifyEventForwarder
);
300 m_aChartTypes
.clear();
301 for (auto const & aChartType
: aChartTypes
)
303 auto pChartType
= dynamic_cast<ChartType
*>(aChartType
.get());
305 m_aChartTypes
.push_back(pChartType
);
306 pChartType
->addModifyListener( m_xModifyEventForwarder
);
311 void BaseCoordinateSystem::setChartTypes( const std::vector
< rtl::Reference
< ChartType
> >& aChartTypes
)
313 for (auto const & aChartType
: m_aChartTypes
)
314 aChartType
->removeModifyListener( m_xModifyEventForwarder
);
315 m_aChartTypes
= aChartTypes
;
316 for (auto const & aChartType
: m_aChartTypes
)
317 aChartType
->addModifyListener( m_xModifyEventForwarder
);
321 // ____ XModifyBroadcaster ____
322 void SAL_CALL
BaseCoordinateSystem::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
324 m_xModifyEventForwarder
->addModifyListener( aListener
);
327 void SAL_CALL
BaseCoordinateSystem::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
329 m_xModifyEventForwarder
->removeModifyListener( aListener
);
332 // ____ XModifyListener ____
333 void SAL_CALL
BaseCoordinateSystem::modified( const lang::EventObject
& aEvent
)
335 m_xModifyEventForwarder
->modified( aEvent
);
338 // ____ XEventListener (base of XModifyListener) ____
339 void SAL_CALL
BaseCoordinateSystem::disposing( const lang::EventObject
& /* Source */ )
344 // ____ OPropertySet ____
345 void BaseCoordinateSystem::firePropertyChangeEvent()
350 void BaseCoordinateSystem::fireModifyEvent()
352 m_xModifyEventForwarder
->modified( lang::EventObject( static_cast< uno::XWeak
* >( this )));
355 // ____ OPropertySet ____
356 void BaseCoordinateSystem::GetDefaultValue( sal_Int32 nHandle
, uno::Any
& rAny
) const
358 const tPropertyValueMap
& rStaticDefaults
= StaticCooSysDefaults();
359 tPropertyValueMap::const_iterator
aFound( rStaticDefaults
.find( nHandle
) );
360 if( aFound
== rStaticDefaults
.end() )
363 rAny
= (*aFound
).second
;
366 // ____ OPropertySet ____
367 ::cppu::IPropertyArrayHelper
& SAL_CALL
BaseCoordinateSystem::getInfoHelper()
369 return StaticCooSysInfoHelper();
372 // ____ XPropertySet ____
373 Reference
< beans::XPropertySetInfo
> SAL_CALL
BaseCoordinateSystem::getPropertySetInfo()
375 static uno::Reference
< beans::XPropertySetInfo
> xPropertySetInfo(
376 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticCooSysInfoHelper() ) );
377 return xPropertySetInfo
;
380 using impl::BaseCoordinateSystem_Base
;
382 IMPLEMENT_FORWARD_XINTERFACE2( BaseCoordinateSystem
, BaseCoordinateSystem_Base
, ::property::OPropertySet
)
383 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BaseCoordinateSystem
, BaseCoordinateSystem_Base
, ::property::OPropertySet
)
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */