1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <DataSeries.hxx>
#include "DataSeriesProperties.hxx"
#include "DataPointProperties.hxx"
#include <CharacterProperties.hxx>
#include <UserDefinedProperties.hxx>
#include "DataPoint.hxx"
#include <DataSeriesHelper.hxx>
#include <CloneHelper.hxx>
#include <ModifyListenerHelper.hxx>
#include <EventListenerHelper.hxx>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <tools/diagnose_ex.h>
#include <algorithm>
namespace com::sun::star::uno { class XComponentContext; }
using namespace ::com::sun::star;
using ::com::sun::star::beans::Property;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::Reference;
using ::osl::MutexGuard;
namespace
{
struct StaticDataSeriesDefaults : public rtl::StaticWithInit< ::chart::tPropertyValueMap, StaticDataSeriesDefaults >
{
::chart::tPropertyValueMap operator()()
{
::chart::tPropertyValueMap aStaticDefaults;
::chart::DataSeriesProperties::AddDefaultsToMap( aStaticDefaults );
::chart::CharacterProperties::AddDefaultsToMap( aStaticDefaults );
float fDefaultCharHeight = 10.0;
::chart::PropertyHelper::setPropertyValue( aStaticDefaults, ::chart::CharacterProperties::PROP_CHAR_CHAR_HEIGHT, fDefaultCharHeight );
::chart::PropertyHelper::setPropertyValue( aStaticDefaults, ::chart::CharacterProperties::PROP_CHAR_ASIAN_CHAR_HEIGHT, fDefaultCharHeight );
::chart::PropertyHelper::setPropertyValue( aStaticDefaults, ::chart::CharacterProperties::PROP_CHAR_COMPLEX_CHAR_HEIGHT, fDefaultCharHeight );
return aStaticDefaults;
}
};
struct StaticDataSeriesInfoHelper : public rtl::StaticWithInit< ::cppu::OPropertyArrayHelper, StaticDataSeriesInfoHelper, StaticDataSeriesInfoHelper, uno::Sequence< Property > >
{
uno::Sequence< Property > operator()()
{
std::vector< css::beans::Property > aProperties;
::chart::DataSeriesProperties::AddPropertiesToVector( aProperties );
::chart::CharacterProperties::AddPropertiesToVector( aProperties );
::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
std::sort( aProperties.begin(), aProperties.end(),
::chart::PropertyNameLess() );
return comphelper::containerToSequence( aProperties );
}
};
struct StaticDataSeriesInfo : public rtl::StaticWithInit< uno::Reference< beans::XPropertySetInfo >, StaticDataSeriesInfo >
{
uno::Reference< beans::XPropertySetInfo > operator()()
{
return ::cppu::OPropertySetHelper::createPropertySetInfo(StaticDataSeriesInfoHelper::get() );
}
};
void lcl_SetParent(
const uno::Reference< uno::XInterface > & xChildInterface,
const uno::Reference< uno::XInterface > & xParentInterface )
{
uno::Reference< container::XChild > xChild( xChildInterface, uno::UNO_QUERY );
if( xChild.is())
xChild->setParent( xParentInterface );
}
typedef std::map< sal_Int32, css::uno::Reference< css::beans::XPropertySet > >
lcl_tDataPointMap;
void lcl_CloneAttributedDataPoints(
const lcl_tDataPointMap & rSource, lcl_tDataPointMap & rDestination,
const uno::Reference< uno::XInterface > & xSeries )
{
for (auto const& elem : rSource)
{
Reference< beans::XPropertySet > xPoint( elem.second );
if( xPoint.is())
{
Reference< util::XCloneable > xCloneable( xPoint, uno::UNO_QUERY );
if( xCloneable.is())
{
xPoint.set( xCloneable->createClone(), uno::UNO_QUERY );
if( xPoint.is())
{
lcl_SetParent( xPoint, xSeries );
rDestination.emplace( elem.first, xPoint );
}
}
}
}
}
} // anonymous namespace
namespace chart
{
DataSeries::DataSeries() :
::property::OPropertySet( m_aMutex ),
m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
{
}
DataSeries::DataSeries( const DataSeries & rOther ) :
impl::DataSeries_Base(rOther),
::property::OPropertySet( rOther, m_aMutex ),
m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
{
if( ! rOther.m_aDataSequences.empty())
{
CloneHelper::CloneRefVector<css::chart2::data::XLabeledDataSequence>(
rOther.m_aDataSequences, m_aDataSequences );
ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
}
CloneHelper::CloneRefVector< chart2::XRegressionCurve >( rOther.m_aRegressionCurves, m_aRegressionCurves );
ModifyListenerHelper::addListenerToAllElements( m_aRegressionCurves, m_xModifyEventForwarder );
// add as listener to XPropertySet properties
Reference< beans::XPropertySet > xPropertySet;
uno::Any aValue;
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_X );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
ModifyListenerHelper::addListener( xPropertySet, m_xModifyEventForwarder );
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_Y );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
ModifyListenerHelper::addListener( xPropertySet, m_xModifyEventForwarder );
}
// late initialization to call after copy-constructing
void DataSeries::Init( const DataSeries & rOther )
{
if( ! rOther.m_aDataSequences.empty())
EventListenerHelper::addListenerToAllElements( m_aDataSequences, this );
Reference< uno::XInterface > xThisInterface( static_cast< ::cppu::OWeakObject * >( this ));
if( ! rOther.m_aAttributedDataPoints.empty())
{
lcl_CloneAttributedDataPoints(
rOther.m_aAttributedDataPoints, m_aAttributedDataPoints, xThisInterface );
ModifyListenerHelper::addListenerToAllMapElements( m_aAttributedDataPoints, m_xModifyEventForwarder );
}
// add as parent to error bars
Reference< beans::XPropertySet > xPropertySet;
uno::Any aValue;
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_X );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
lcl_SetParent( xPropertySet, xThisInterface );
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_Y );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
lcl_SetParent( xPropertySet, xThisInterface );
}
DataSeries::~DataSeries()
{
try
{
ModifyListenerHelper::removeListenerFromAllMapElements( m_aAttributedDataPoints, m_xModifyEventForwarder );
ModifyListenerHelper::removeListenerFromAllElements( m_aRegressionCurves, m_xModifyEventForwarder );
ModifyListenerHelper::removeListenerFromAllElements( m_aDataSequences, m_xModifyEventForwarder );
// remove listener from XPropertySet properties
Reference< beans::XPropertySet > xPropertySet;
uno::Any aValue;
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_X );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
ModifyListenerHelper::removeListener( xPropertySet, m_xModifyEventForwarder );
getFastPropertyValue( aValue, DataPointProperties::PROP_DATAPOINT_ERROR_BAR_Y );
if( ( aValue >>= xPropertySet )
&& xPropertySet.is())
ModifyListenerHelper::removeListener( xPropertySet, m_xModifyEventForwarder );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
// ____ XCloneable ____
uno::Reference< util::XCloneable > SAL_CALL DataSeries::createClone()
{
DataSeries * pNewSeries( new DataSeries( *this ));
// hold a reference to the clone
uno::Reference< util::XCloneable > xResult( pNewSeries );
// do initialization that uses uno references to the clone
pNewSeries->Init( *this );
return xResult;
}
// ____ OPropertySet ____
uno::Any DataSeries::GetDefaultValue( sal_Int32 nHandle ) const
{
const tPropertyValueMap& rStaticDefaults = StaticDataSeriesDefaults::get();
tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
if( aFound == rStaticDefaults.end() )
return uno::Any();
return (*aFound).second;
}
// ____ OPropertySet ____
::cppu::IPropertyArrayHelper & SAL_CALL DataSeries::getInfoHelper()
{
return StaticDataSeriesInfoHelper::get();
}
// ____ XPropertySet ____
uno::Reference< beans::XPropertySetInfo > SAL_CALL DataSeries::getPropertySetInfo()
{
return StaticDataSeriesInfo::get();
}
void SAL_CALL DataSeries::getFastPropertyValue
( uno::Any& rValue,
sal_Int32 nHandle ) const
{
// special handling for get. set is not possible for this property
if( nHandle == DataSeriesProperties::PROP_DATASERIES_ATTRIBUTED_DATA_POINTS )
{
// TODO: only add those property sets that are really modified
rValue <<= comphelper::mapKeysToSequence(m_aAttributedDataPoints);
}
else
OPropertySet::getFastPropertyValue( rValue, nHandle );
}
void SAL_CALL DataSeries::setFastPropertyValue_NoBroadcast(
sal_Int32 nHandle, const uno::Any& rValue )
{
if( nHandle == DataPointProperties::PROP_DATAPOINT_ERROR_BAR_Y
|| nHandle == DataPointProperties::PROP_DATAPOINT_ERROR_BAR_X )
{
uno::Any aOldValue;
Reference< util::XModifyBroadcaster > xBroadcaster;
getFastPropertyValue( aOldValue, nHandle );
if( aOldValue.hasValue() &&
(aOldValue >>= xBroadcaster) &&
xBroadcaster.is())
{
ModifyListenerHelper::removeListener( xBroadcaster, m_xModifyEventForwarder );
}
OSL_ASSERT( rValue.getValueType().getTypeClass() == uno::TypeClass_INTERFACE );
if( rValue.hasValue() &&
(rValue >>= xBroadcaster) &&
xBroadcaster.is())
{
ModifyListenerHelper::addListener( xBroadcaster, m_xModifyEventForwarder );
}
}
::property::OPropertySet::setFastPropertyValue_NoBroadcast( nHandle, rValue );
}
Reference< beans::XPropertySet >
SAL_CALL DataSeries::getDataPointByIndex( sal_Int32 nIndex )
{
Reference< beans::XPropertySet > xResult;
Sequence< Reference< chart2::data::XLabeledDataSequence > > aSequences;
{
MutexGuard aGuard( m_aMutex );
aSequences = comphelper::containerToSequence( m_aDataSequences );
}
std::vector< Reference< chart2::data::XLabeledDataSequence > > aValuesSeries(
DataSeriesHelper::getAllDataSequencesByRole( aSequences , "values" ) );
if (aValuesSeries.empty())
throw lang::IndexOutOfBoundsException();
Reference< chart2::data::XDataSequence > xSeq( aValuesSeries.front()->getValues() );
if( 0 <= nIndex && nIndex < xSeq->getData().getLength() )
{
{
MutexGuard aGuard( m_aMutex );
tDataPointAttributeContainer::iterator aIt( m_aAttributedDataPoints.find( nIndex ) );
if( aIt != m_aAttributedDataPoints.end() )
xResult = (*aIt).second;
}
if( !xResult.is() )
{
Reference< beans::XPropertySet > xParentProperties;
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xParentProperties = this;
xModifyEventForwarder = m_xModifyEventForwarder;
}
// create a new XPropertySet for this data point
xResult.set( new DataPoint( xParentProperties ) );
{
MutexGuard aGuard( m_aMutex );
m_aAttributedDataPoints[ nIndex ] = xResult;
}
ModifyListenerHelper::addListener( xResult, xModifyEventForwarder );
}
}
return xResult;
}
void SAL_CALL DataSeries::resetDataPoint( sal_Int32 nIndex )
{
Reference< beans::XPropertySet > xDataPointProp;
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
tDataPointAttributeContainer::iterator aIt( m_aAttributedDataPoints.find( nIndex ));
if( aIt != m_aAttributedDataPoints.end())
{
xDataPointProp = (*aIt).second;
m_aAttributedDataPoints.erase(aIt);
}
}
if( xDataPointProp.is() )
{
Reference< util::XModifyBroadcaster > xBroadcaster( xDataPointProp, uno::UNO_QUERY );
if( xBroadcaster.is() && xModifyEventForwarder.is())
xBroadcaster->removeModifyListener( xModifyEventForwarder );
fireModifyEvent();
}
}
void SAL_CALL DataSeries::resetAllDataPoints()
{
tDataPointAttributeContainer aOldAttributedDataPoints;
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
std::swap( aOldAttributedDataPoints, m_aAttributedDataPoints );
}
ModifyListenerHelper::removeListenerFromAllMapElements( aOldAttributedDataPoints, xModifyEventForwarder );
aOldAttributedDataPoints.clear();
fireModifyEvent();
}
// ____ XDataSink ____
void SAL_CALL DataSeries::setData( const uno::Sequence< Reference< chart2::data::XLabeledDataSequence > >& aData )
{
tDataSequenceContainer aOldDataSequences;
tDataSequenceContainer aNewDataSequences;
Reference< util::XModifyListener > xModifyEventForwarder;
Reference< lang::XEventListener > xListener;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
xListener = this;
std::swap( aOldDataSequences, m_aDataSequences );
aNewDataSequences = comphelper::sequenceToContainer<tDataSequenceContainer>( aData );
m_aDataSequences = aNewDataSequences;
}
ModifyListenerHelper::removeListenerFromAllElements( aOldDataSequences, xModifyEventForwarder );
EventListenerHelper::removeListenerFromAllElements( aOldDataSequences, xListener );
EventListenerHelper::addListenerToAllElements( aNewDataSequences, xListener );
ModifyListenerHelper::addListenerToAllElements( aNewDataSequences, xModifyEventForwarder );
fireModifyEvent();
}
// ____ XDataSource ____
Sequence< Reference< chart2::data::XLabeledDataSequence > > SAL_CALL DataSeries::getDataSequences()
{
MutexGuard aGuard( m_aMutex );
return comphelper::containerToSequence( m_aDataSequences );
}
// ____ XRegressionCurveContainer ____
void SAL_CALL DataSeries::addRegressionCurve(
const uno::Reference< chart2::XRegressionCurve >& xRegressionCurve )
{
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
if( std::find( m_aRegressionCurves.begin(), m_aRegressionCurves.end(), xRegressionCurve )
!= m_aRegressionCurves.end())
throw lang::IllegalArgumentException();
m_aRegressionCurves.push_back( xRegressionCurve );
}
ModifyListenerHelper::addListener( xRegressionCurve, xModifyEventForwarder );
fireModifyEvent();
}
void SAL_CALL DataSeries::removeRegressionCurve(
const uno::Reference< chart2::XRegressionCurve >& xRegressionCurve )
{
if( !xRegressionCurve.is() )
throw container::NoSuchElementException();
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
tRegressionCurveContainerType::iterator aIt(
std::find( m_aRegressionCurves.begin(), m_aRegressionCurves.end(), xRegressionCurve ) );
if( aIt == m_aRegressionCurves.end())
throw container::NoSuchElementException(
"The given regression curve is no element of this series",
static_cast< uno::XWeak * >( this ));
m_aRegressionCurves.erase( aIt );
}
ModifyListenerHelper::removeListener( xRegressionCurve, xModifyEventForwarder );
fireModifyEvent();
}
uno::Sequence< uno::Reference< chart2::XRegressionCurve > > SAL_CALL DataSeries::getRegressionCurves()
{
MutexGuard aGuard( m_aMutex );
return comphelper::containerToSequence( m_aRegressionCurves );
}
void SAL_CALL DataSeries::setRegressionCurves(
const Sequence< Reference< chart2::XRegressionCurve > >& aRegressionCurves )
{
tRegressionCurveContainerType aOldCurves;
auto aNewCurves( comphelper::sequenceToContainer<tRegressionCurveContainerType>( aRegressionCurves ) );
Reference< util::XModifyListener > xModifyEventForwarder;
{
MutexGuard aGuard( m_aMutex );
xModifyEventForwarder = m_xModifyEventForwarder;
std::swap( aOldCurves, m_aRegressionCurves );
m_aRegressionCurves = aNewCurves;
}
ModifyListenerHelper::removeListenerFromAllElements( aOldCurves, xModifyEventForwarder );
ModifyListenerHelper::addListenerToAllElements( aNewCurves, xModifyEventForwarder );
fireModifyEvent();
}
// ____ XModifyBroadcaster ____
void SAL_CALL DataSeries::addModifyListener( const Reference< util::XModifyListener >& aListener )
{
try
{
Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
xBroadcaster->addModifyListener( aListener );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
void SAL_CALL DataSeries::removeModifyListener( const Reference< util::XModifyListener >& aListener )
{
try
{
Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
xBroadcaster->removeModifyListener( aListener );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
// ____ XModifyListener ____
void SAL_CALL DataSeries::modified( const lang::EventObject& aEvent )
{
m_xModifyEventForwarder->modified( aEvent );
}
// ____ XEventListener (base of XModifyListener) ____
void SAL_CALL DataSeries::disposing( const lang::EventObject& rEventObject )
{
// forget disposed data sequences
tDataSequenceContainer::iterator aIt(
std::find( m_aDataSequences.begin(), m_aDataSequences.end(), rEventObject.Source ));
if( aIt != m_aDataSequences.end())
m_aDataSequences.erase( aIt );
}
// ____ OPropertySet ____
void DataSeries::firePropertyChangeEvent()
{
fireModifyEvent();
}
void DataSeries::fireModifyEvent()
{
m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
}
using impl::DataSeries_Base;
using ::property::OPropertySet;
IMPLEMENT_FORWARD_XINTERFACE2( DataSeries, DataSeries_Base, OPropertySet )
IMPLEMENT_FORWARD_XTYPEPROVIDER2( DataSeries, DataSeries_Base, OPropertySet )
// implement XServiceInfo methods basing upon getSupportedServiceNames_Static
OUString SAL_CALL DataSeries::getImplementationName()
{
return "com.sun.star.comp.chart.DataSeries";
}
sal_Bool SAL_CALL DataSeries::supportsService( const OUString& rServiceName )
{
return cppu::supportsService(this, rServiceName);
}
css::uno::Sequence< OUString > SAL_CALL DataSeries::getSupportedServiceNames()
{
return {
"com.sun.star.chart2.DataSeries",
"com.sun.star.chart2.DataPointProperties",
"com.sun.star.beans.PropertySet" };
}
} // namespace chart
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_chart_DataSeries_get_implementation(css::uno::XComponentContext *,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new ::chart::DataSeries );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|