bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / CachedDataSequence.cxx
blobdeada2065cb3d3119a3aa2d5f1a293cdb0ab0794
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "CachedDataSequence.hxx"
22 #include "macros.hxx"
23 #include "PropertyHelper.hxx"
24 #include "ContainerHelper.hxx"
25 #include "CommonFunctors.hxx"
26 #include "ModifyListenerHelper.hxx"
28 #include <comphelper/sequenceashashmap.hxx>
30 #include <algorithm>
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
32 #include <rtl/math.hxx>
34 using namespace ::com::sun::star;
35 using namespace ::chart::ContainerHelper;
37 using ::com::sun::star::uno::Sequence;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Any;
40 using ::osl::MutexGuard;
42 // necessary for MS compiler
43 using ::comphelper::OPropertyContainer;
44 using ::comphelper::OMutexAndBroadcastHelper;
45 using ::comphelper::OPropertyArrayUsageHelper;
46 using ::chart::impl::CachedDataSequence_Base;
48 namespace
50 static const OUString lcl_aServiceName( "com.sun.star.comp.chart.CachedDataSequence" );
52 enum
54 // PROP_SOURCE_IDENTIFIER,
55 PROP_NUMBERFORMAT_KEY,
56 PROP_PROPOSED_ROLE
58 } // anonymous namespace
61 // ____________________
62 namespace chart
65 CachedDataSequence::CachedDataSequence()
66 : OPropertyContainer( GetBroadcastHelper()),
67 CachedDataSequence_Base( GetMutex()),
68 m_eCurrentDataType( NUMERICAL ),
69 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
71 registerProperties();
73 CachedDataSequence::CachedDataSequence( const Reference< uno::XComponentContext > & /*xContext*/ )
74 : OPropertyContainer( GetBroadcastHelper()),
75 CachedDataSequence_Base( GetMutex()),
76 m_eCurrentDataType( MIXED ),
77 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder( ))
79 registerProperties();
82 CachedDataSequence::CachedDataSequence( const OUString & rSingleText )
83 : OPropertyContainer( GetBroadcastHelper()),
84 CachedDataSequence_Base( GetMutex()),
85 m_eCurrentDataType( TEXTUAL ),
86 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
88 m_aTextualSequence.realloc(1);
89 m_aTextualSequence[0] = rSingleText;
90 registerProperties();
93 CachedDataSequence::CachedDataSequence( const CachedDataSequence & rSource )
94 : OMutexAndBroadcastHelper(),
95 OPropertyContainer( GetBroadcastHelper()),
96 OPropertyArrayUsageHelper< CachedDataSequence >(),
97 CachedDataSequence_Base( GetMutex()),
98 m_nNumberFormatKey( rSource.m_nNumberFormatKey ),
99 m_sRole( rSource.m_sRole ),
100 m_eCurrentDataType( rSource.m_eCurrentDataType ),
101 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
103 switch( m_eCurrentDataType )
105 case TEXTUAL:
106 m_aTextualSequence = rSource.m_aTextualSequence;
107 break;
108 case NUMERICAL:
109 m_aNumericalSequence = rSource.m_aNumericalSequence;
110 break;
111 case MIXED:
112 m_aMixedSequence = rSource.m_aMixedSequence;
113 break;
116 registerProperties();
119 CachedDataSequence::~CachedDataSequence()
122 void CachedDataSequence::registerProperties()
124 registerProperty( "NumberFormatKey",
125 PROP_NUMBERFORMAT_KEY,
126 0, // PropertyAttributes
127 & m_nNumberFormatKey,
128 ::getCppuType( & m_nNumberFormatKey ) );
130 registerProperty( "Role",
131 PROP_PROPOSED_ROLE,
132 0, // PropertyAttributes
133 & m_sRole,
134 ::getCppuType( & m_sRole ) );
137 Sequence< double > CachedDataSequence::Impl_getNumericalData() const
139 if( m_eCurrentDataType == NUMERICAL )
140 return m_aNumericalSequence;
142 sal_Int32 nSize = ( m_eCurrentDataType == TEXTUAL )
143 ? m_aTextualSequence.getLength()
144 : m_aMixedSequence.getLength();
146 Sequence< double > aResult( nSize );
147 double * pResultArray = aResult.getArray();
149 if( m_eCurrentDataType == TEXTUAL )
151 const OUString * pTextArray = m_aTextualSequence.getConstArray();
152 ::std::transform( pTextArray, pTextArray + nSize,
153 pResultArray,
154 CommonFunctors::OUStringToDouble() );
156 else
158 OSL_ASSERT( m_eCurrentDataType == MIXED );
159 const Any * pMixedArray = m_aMixedSequence.getConstArray();
160 ::std::transform( pMixedArray, pMixedArray + nSize,
161 pResultArray,
162 CommonFunctors::AnyToDouble() );
164 return aResult;
167 Sequence< OUString > CachedDataSequence::Impl_getTextualData() const
169 if( m_eCurrentDataType == TEXTUAL )
170 return m_aTextualSequence;
172 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
173 ? m_aNumericalSequence.getLength()
174 : m_aMixedSequence.getLength();
176 Sequence< OUString > aResult( nSize );
177 OUString * pResultArray = aResult.getArray();
179 if( m_eCurrentDataType == NUMERICAL )
181 const double * pTextArray = m_aNumericalSequence.getConstArray();
182 ::std::transform( pTextArray, pTextArray + nSize,
183 pResultArray,
184 CommonFunctors::DoubleToOUString() );
186 else
188 OSL_ASSERT( m_eCurrentDataType == MIXED );
189 const Any * pMixedArray = m_aMixedSequence.getConstArray();
190 ::std::transform( pMixedArray, pMixedArray + nSize,
191 pResultArray,
192 CommonFunctors::AnyToString() );
195 return aResult;
198 Sequence< Any > CachedDataSequence::Impl_getMixedData() const
200 if( m_eCurrentDataType == MIXED )
201 return m_aMixedSequence;
203 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
204 ? m_aNumericalSequence.getLength()
205 : m_aTextualSequence.getLength();
207 Sequence< Any > aResult( nSize );
208 Any * pResultArray = aResult.getArray();
210 if( m_eCurrentDataType == NUMERICAL )
212 const double * pTextArray = m_aNumericalSequence.getConstArray();
213 ::std::transform( pTextArray, pTextArray + nSize,
214 pResultArray,
215 CommonFunctors::makeAny< double >() );
217 else
219 OSL_ASSERT( m_eCurrentDataType == TEXTUAL );
220 const OUString * pMixedArray = m_aTextualSequence.getConstArray();
221 ::std::transform( pMixedArray, pMixedArray + nSize,
222 pResultArray,
223 CommonFunctors::makeAny< OUString >() );
226 return aResult;
229 // ================================================================================
231 Sequence< OUString > CachedDataSequence::getSupportedServiceNames_Static()
233 Sequence< OUString > aServices( 4 );
234 aServices[ 0 ] = lcl_aServiceName;
235 aServices[ 1 ] = "com.sun.star.chart2.data.DataSequence";
236 aServices[ 2 ] = "com.sun.star.chart2.data.NumericalDataSequence";
237 aServices[ 3 ] = "com.sun.star.chart2.data.TextualDataSequence";
238 return aServices;
241 IMPLEMENT_FORWARD_XINTERFACE2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
242 IMPLEMENT_FORWARD_XTYPEPROVIDER2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
244 // ____ XPropertySet ____
245 Reference< beans::XPropertySetInfo > SAL_CALL CachedDataSequence::getPropertySetInfo()
246 throw(uno::RuntimeException)
248 return Reference< beans::XPropertySetInfo >( createPropertySetInfo( getInfoHelper() ) );
251 // ____ ::comphelper::OPropertySetHelper ____
252 // __________________________________________
253 ::cppu::IPropertyArrayHelper& CachedDataSequence::getInfoHelper()
255 return *getArrayHelper();
258 // ____ ::comphelper::OPropertyArrayHelper ____
259 // ____________________________________________
260 ::cppu::IPropertyArrayHelper* CachedDataSequence::createArrayHelper() const
262 Sequence< beans::Property > aProps;
263 // describes all properties which have been registered in the ctor
264 describeProperties( aProps );
266 return new ::cppu::OPropertyArrayHelper( aProps );
269 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
270 APPHELPER_XSERVICEINFO_IMPL( CachedDataSequence, lcl_aServiceName )
272 // ================================================================================
274 // ________ XNumericalDataSequence ________
275 Sequence< double > SAL_CALL CachedDataSequence::getNumericalData()
276 throw (uno::RuntimeException)
278 MutexGuard aGuard( GetMutex() );
280 if( m_eCurrentDataType == NUMERICAL )
281 return m_aNumericalSequence;
282 else
283 return Impl_getNumericalData();
286 // ________ XTextualDataSequence ________
287 Sequence< OUString > SAL_CALL CachedDataSequence::getTextualData()
288 throw (uno::RuntimeException)
290 MutexGuard aGuard( GetMutex() );
292 if( m_eCurrentDataType == TEXTUAL )
293 return m_aTextualSequence;
294 else
295 return Impl_getTextualData();
298 // ________ XDataSequence ________
299 Sequence< Any > SAL_CALL CachedDataSequence::getData()
300 throw (uno::RuntimeException)
302 MutexGuard aGuard( GetMutex() );
303 return Impl_getMixedData();
306 OUString SAL_CALL CachedDataSequence::getSourceRangeRepresentation()
307 throw (uno::RuntimeException)
309 return m_sRole;
312 Sequence< OUString > SAL_CALL CachedDataSequence::generateLabel( chart2::data::LabelOrigin /*eLabelOrigin*/ )
313 throw (uno::RuntimeException)
315 // return empty label, as we have no range representaions to determine something useful
316 return Sequence< OUString >();
319 ::sal_Int32 SAL_CALL CachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32 /*nIndex*/ )
320 throw (lang::IndexOutOfBoundsException,
321 uno::RuntimeException)
323 return 0;
326 Reference< util::XCloneable > SAL_CALL CachedDataSequence::createClone()
327 throw (uno::RuntimeException)
329 CachedDataSequence * pNewSeq = new CachedDataSequence( *this );
331 return Reference< util::XCloneable >( pNewSeq );
334 void SAL_CALL CachedDataSequence::addModifyListener( const Reference< util::XModifyListener >& aListener )
335 throw (uno::RuntimeException)
339 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
340 xBroadcaster->addModifyListener( aListener );
342 catch( const uno::Exception & ex )
344 ASSERT_EXCEPTION( ex );
348 void SAL_CALL CachedDataSequence::removeModifyListener( const Reference< util::XModifyListener >& aListener )
349 throw (uno::RuntimeException)
353 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
354 xBroadcaster->removeModifyListener( aListener );
356 catch( const uno::Exception & ex )
358 ASSERT_EXCEPTION( ex );
362 // lang::XInitialization:
363 void SAL_CALL CachedDataSequence::initialize(const uno::Sequence< uno::Any > & _aArguments) throw (uno::RuntimeException, uno::Exception)
365 ::comphelper::SequenceAsHashMap aMap(_aArguments);
366 m_aNumericalSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aNumericalSequence);
367 if ( m_aNumericalSequence.getLength() )
368 m_eCurrentDataType = NUMERICAL;
369 else
371 m_aTextualSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aTextualSequence);
372 if ( m_aTextualSequence.getLength() )
373 m_eCurrentDataType = TEXTUAL;
374 else
376 m_aMixedSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aMixedSequence);
377 if ( m_aMixedSequence.getLength() )
378 m_eCurrentDataType = MIXED;
382 } // namespace chart
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */