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 <CachedDataSequence.hxx>
21 #include <CommonFunctors.hxx>
22 #include <ModifyListenerHelper.hxx>
24 #include <comphelper/sequenceashashmap.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <tools/diagnose_ex.h>
30 using namespace ::com::sun::star
;
32 using ::com::sun::star::uno::Sequence
;
33 using ::com::sun::star::uno::Reference
;
34 using ::com::sun::star::uno::Any
;
35 using ::osl::MutexGuard
;
37 // necessary for MS compiler
38 using ::comphelper::OPropertyContainer
;
39 using ::comphelper::OMutexAndBroadcastHelper
;
40 using ::comphelper::OPropertyArrayUsageHelper
;
41 using ::chart::impl::CachedDataSequence_Base
;
45 const char lcl_aServiceName
[] = "com.sun.star.comp.chart.CachedDataSequence";
49 // PROP_SOURCE_IDENTIFIER,
50 PROP_NUMBERFORMAT_KEY
,
53 } // anonymous namespace
58 CachedDataSequence::CachedDataSequence()
59 : OPropertyContainer( GetBroadcastHelper()),
60 CachedDataSequence_Base( GetMutex()),
61 m_eCurrentDataType( NUMERICAL
),
62 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
66 CachedDataSequence::CachedDataSequence( const Reference
< uno::XComponentContext
> & /*xContext*/ )
67 : OPropertyContainer( GetBroadcastHelper()),
68 CachedDataSequence_Base( GetMutex()),
69 m_eCurrentDataType( MIXED
),
70 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder( ))
75 CachedDataSequence::CachedDataSequence( const OUString
& rSingleText
)
76 : OPropertyContainer( GetBroadcastHelper()),
77 CachedDataSequence_Base( GetMutex()),
78 m_eCurrentDataType( TEXTUAL
),
79 m_aTextualSequence({rSingleText
}),
80 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
85 CachedDataSequence::CachedDataSequence( const CachedDataSequence
& rSource
)
86 : OMutexAndBroadcastHelper(),
87 OPropertyContainer( GetBroadcastHelper()),
88 OPropertyArrayUsageHelper
< CachedDataSequence
>(),
89 CachedDataSequence_Base( GetMutex()),
90 m_nNumberFormatKey( rSource
.m_nNumberFormatKey
),
91 m_sRole( rSource
.m_sRole
),
92 m_eCurrentDataType( rSource
.m_eCurrentDataType
),
93 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
95 switch( m_eCurrentDataType
)
98 m_aTextualSequence
= rSource
.m_aTextualSequence
;
101 m_aNumericalSequence
= rSource
.m_aNumericalSequence
;
104 m_aMixedSequence
= rSource
.m_aMixedSequence
;
108 registerProperties();
111 CachedDataSequence::~CachedDataSequence()
114 void CachedDataSequence::registerProperties()
116 registerProperty( "NumberFormatKey",
117 PROP_NUMBERFORMAT_KEY
,
118 0, // PropertyAttributes
119 & m_nNumberFormatKey
,
120 cppu::UnoType
<decltype(m_nNumberFormatKey
)>::get() );
122 registerProperty( "Role",
124 0, // PropertyAttributes
126 cppu::UnoType
<decltype(m_sRole
)>::get() );
129 Sequence
< double > CachedDataSequence::Impl_getNumericalData() const
131 if( m_eCurrentDataType
== NUMERICAL
)
132 return m_aNumericalSequence
;
134 sal_Int32 nSize
= ( m_eCurrentDataType
== TEXTUAL
)
135 ? m_aTextualSequence
.getLength()
136 : m_aMixedSequence
.getLength();
138 Sequence
< double > aResult( nSize
);
139 double * pResultArray
= aResult
.getArray();
141 if( m_eCurrentDataType
== TEXTUAL
)
143 const OUString
* pTextArray
= m_aTextualSequence
.getConstArray();
144 std::transform( pTextArray
, pTextArray
+ nSize
,
146 CommonFunctors::OUStringToDouble() );
150 OSL_ASSERT( m_eCurrentDataType
== MIXED
);
151 const Any
* pMixedArray
= m_aMixedSequence
.getConstArray();
152 std::transform( pMixedArray
, pMixedArray
+ nSize
,
154 CommonFunctors::AnyToDouble() );
159 Sequence
< OUString
> CachedDataSequence::Impl_getTextualData() const
161 if( m_eCurrentDataType
== TEXTUAL
)
162 return m_aTextualSequence
;
164 sal_Int32 nSize
= ( m_eCurrentDataType
== NUMERICAL
)
165 ? m_aNumericalSequence
.getLength()
166 : m_aMixedSequence
.getLength();
168 Sequence
< OUString
> aResult( nSize
);
169 OUString
* pResultArray
= aResult
.getArray();
171 if( m_eCurrentDataType
== NUMERICAL
)
173 const double * pTextArray
= m_aNumericalSequence
.getConstArray();
174 std::transform( pTextArray
, pTextArray
+ nSize
,
176 CommonFunctors::DoubleToOUString() );
180 OSL_ASSERT( m_eCurrentDataType
== MIXED
);
181 const Any
* pMixedArray
= m_aMixedSequence
.getConstArray();
182 std::transform( pMixedArray
, pMixedArray
+ nSize
,
184 CommonFunctors::AnyToString() );
190 Sequence
< Any
> CachedDataSequence::Impl_getMixedData() const
192 if( m_eCurrentDataType
== MIXED
)
193 return m_aMixedSequence
;
195 sal_Int32 nSize
= ( m_eCurrentDataType
== NUMERICAL
)
196 ? m_aNumericalSequence
.getLength()
197 : m_aTextualSequence
.getLength();
199 Sequence
< Any
> aResult( nSize
);
200 Any
* pResultArray
= aResult
.getArray();
202 if( m_eCurrentDataType
== NUMERICAL
)
204 const double * pTextArray
= m_aNumericalSequence
.getConstArray();
205 std::transform( pTextArray
, pTextArray
+ nSize
,
207 CommonFunctors::makeAny
< double >() );
211 OSL_ASSERT( m_eCurrentDataType
== TEXTUAL
);
212 const OUString
* pMixedArray
= m_aTextualSequence
.getConstArray();
213 std::transform( pMixedArray
, pMixedArray
+ nSize
,
215 CommonFunctors::makeAny
< OUString
>() );
221 IMPLEMENT_FORWARD_XINTERFACE2( CachedDataSequence
, CachedDataSequence_Base
, OPropertyContainer
)
222 IMPLEMENT_FORWARD_XTYPEPROVIDER2( CachedDataSequence
, CachedDataSequence_Base
, OPropertyContainer
)
224 // ____ XPropertySet ____
225 Reference
< beans::XPropertySetInfo
> SAL_CALL
CachedDataSequence::getPropertySetInfo()
227 return createPropertySetInfo( getInfoHelper() );
230 // ____ ::comphelper::OPropertySetHelper ____
231 ::cppu::IPropertyArrayHelper
& CachedDataSequence::getInfoHelper()
233 return *getArrayHelper();
236 // ____ ::comphelper::OPropertyArrayHelper ____
237 ::cppu::IPropertyArrayHelper
* CachedDataSequence::createArrayHelper() const
239 Sequence
< beans::Property
> aProps
;
240 // describes all properties which have been registered in the ctor
241 describeProperties( aProps
);
243 return new ::cppu::OPropertyArrayHelper( aProps
);
246 OUString SAL_CALL
CachedDataSequence::getImplementationName()
248 return lcl_aServiceName
;
251 sal_Bool SAL_CALL
CachedDataSequence::supportsService( const OUString
& rServiceName
)
253 return cppu::supportsService(this, rServiceName
);
256 css::uno::Sequence
< OUString
> SAL_CALL
CachedDataSequence::getSupportedServiceNames()
260 "com.sun.star.chart2.data.DataSequence",
261 "com.sun.star.chart2.data.NumericalDataSequence",
262 "com.sun.star.chart2.data.TextualDataSequence"
266 // ________ XNumericalDataSequence ________
267 Sequence
< double > SAL_CALL
CachedDataSequence::getNumericalData()
269 MutexGuard
aGuard( GetMutex() );
271 if( m_eCurrentDataType
== NUMERICAL
)
272 return m_aNumericalSequence
;
274 return Impl_getNumericalData();
277 // ________ XTextualDataSequence ________
278 Sequence
< OUString
> SAL_CALL
CachedDataSequence::getTextualData()
280 MutexGuard
aGuard( GetMutex() );
282 if( m_eCurrentDataType
== TEXTUAL
)
283 return m_aTextualSequence
;
285 return Impl_getTextualData();
288 // ________ XDataSequence ________
289 Sequence
< Any
> SAL_CALL
CachedDataSequence::getData()
291 MutexGuard
aGuard( GetMutex() );
292 return Impl_getMixedData();
295 OUString SAL_CALL
CachedDataSequence::getSourceRangeRepresentation()
300 Sequence
< OUString
> SAL_CALL
CachedDataSequence::generateLabel( chart2::data::LabelOrigin
/*eLabelOrigin*/ )
302 // return empty label, as we have no range representations to determine something useful
303 return Sequence
< OUString
>();
306 ::sal_Int32 SAL_CALL
CachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32
/*nIndex*/ )
311 Reference
< util::XCloneable
> SAL_CALL
CachedDataSequence::createClone()
313 CachedDataSequence
* pNewSeq
= new CachedDataSequence( *this );
315 return Reference
< util::XCloneable
>( pNewSeq
);
318 void SAL_CALL
CachedDataSequence::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
322 Reference
< util::XModifyBroadcaster
> xBroadcaster( m_xModifyEventForwarder
, uno::UNO_QUERY_THROW
);
323 xBroadcaster
->addModifyListener( aListener
);
325 catch( const uno::Exception
& )
327 DBG_UNHANDLED_EXCEPTION("chart2");
331 void SAL_CALL
CachedDataSequence::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
335 Reference
< util::XModifyBroadcaster
> xBroadcaster( m_xModifyEventForwarder
, uno::UNO_QUERY_THROW
);
336 xBroadcaster
->removeModifyListener( aListener
);
338 catch( const uno::Exception
& )
340 DBG_UNHANDLED_EXCEPTION("chart2");
344 // lang::XInitialization:
345 void SAL_CALL
CachedDataSequence::initialize(const uno::Sequence
< uno::Any
> & _aArguments
)
347 ::comphelper::SequenceAsHashMap
aMap(_aArguments
);
348 m_aNumericalSequence
= aMap
.getUnpackedValueOrDefault( "DataSequence" ,m_aNumericalSequence
);
349 if ( m_aNumericalSequence
.hasElements() )
350 m_eCurrentDataType
= NUMERICAL
;
353 m_aTextualSequence
= aMap
.getUnpackedValueOrDefault( "DataSequence" ,m_aTextualSequence
);
354 if ( m_aTextualSequence
.hasElements() )
355 m_eCurrentDataType
= TEXTUAL
;
358 m_aMixedSequence
= aMap
.getUnpackedValueOrDefault( "DataSequence" ,m_aMixedSequence
);
359 if ( m_aMixedSequence
.hasElements() )
360 m_eCurrentDataType
= MIXED
;
366 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
367 com_sun_star_comp_chart_CachedDataSequence_get_implementation(css::uno::XComponentContext
*context
,
368 css::uno::Sequence
<css::uno::Any
> const &)
370 return cppu::acquire(new ::chart::CachedDataSequence(context
));
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */