Debian Codegraph @

chart2/source/controller/itemsetwrapper/ErrorBarItemConverter.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <ErrorBarItemConverter.hxx>
21 #include "SchWhichPairs.hxx"
22 #include <StatisticsHelper.hxx>
23
24 #include <GraphicPropertyItemConverter.hxx>
25 #include <ChartModel.hxx>
26
27 #include <svl/stritem.hxx>
28 #include <svx/chrtitem.hxx>
29 #include <rtl/math.hxx>
30
31 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
32 #include <com/sun/star/chart2/XChartDocument.hpp>
33 #include <com/sun/star/chart/ErrorBarStyle.hpp>
34 #include <comphelper/diagnose_ex.hxx>
35
36 using namespace ::com::sun::star;
37
38 namespace
39 {
40
41 void lcl_getErrorValues( const uno::Reference< beans::XPropertySet > & xErrorBarProp,
42 double & rOutPosError, double & rOutNegError )
43 {
44 if( ! xErrorBarProp.is())
45 return;
46
47 try
48 {
49 xErrorBarProp->getPropertyValue( u"PositiveError"_ustr ) >>= rOutPosError;
50 xErrorBarProp->getPropertyValue( u"NegativeError"_ustr ) >>= rOutNegError;
51 }
52 catch( const uno::Exception & )
53 {
54 DBG_UNHANDLED_EXCEPTION("chart2");
55 }
56 }
57
58 void lcl_getErrorIndicatorValues(
59 const uno::Reference< beans::XPropertySet > & xErrorBarProp,
60 bool & rOutShowPosError, bool & rOutShowNegError )
61 {
62 if( ! xErrorBarProp.is())
63 return;
64
65 try
66 {
67 xErrorBarProp->getPropertyValue( u"ShowPositiveError"_ustr ) >>= rOutShowPosError;
68 xErrorBarProp->getPropertyValue( u"ShowNegativeError"_ustr ) >>= rOutShowNegError;
69 }
70 catch( const uno::Exception & )
71 {
72 DBG_UNHANDLED_EXCEPTION("chart2");
73 }
74 }
75
76 } // anonymous namespace
77
78 namespace chart::wrapper
79 {
80
81 ErrorBarItemConverter::ErrorBarItemConverter(
82 const rtl::Reference< ChartModel > & xChartModel,
83 const uno::Reference< beans::XPropertySet > & rPropertySet,
84 SfxItemPool& rItemPool,
85 SdrModel& rDrawModel ) :
86 ItemConverter( rPropertySet, rItemPool ),
87 m_spGraphicConverter( std::make_shared<GraphicPropertyItemConverter>(
88 rPropertySet, rItemPool, rDrawModel,
89 xChartModel,
90 GraphicObjectType::LineProperties )),
91 m_xModel(xChartModel)
92 {}
93
94 ErrorBarItemConverter::~ErrorBarItemConverter()
95 {}
96
97 void ErrorBarItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
98 {
99 m_spGraphicConverter->FillItemSet( rOutItemSet );
100
101 // own items
102 ItemConverter::FillItemSet( rOutItemSet );
103 }
104
105 bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
106 {
107 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
108
109 // own items
110 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
111 }
112
113 const WhichRangesContainer& ErrorBarItemConverter::GetWhichPairs() const
114 {
115 // must span all used items!
116 return nErrorBarWhichPairs;
117 }
118
119 bool ErrorBarItemConverter::GetItemProperty(
120 tWhichIdType /* nWhichId */,
121 tPropertyNameWithMemberId & /* rOutProperty */ ) const
122 {
123 return false;
124 }
125
126 bool ErrorBarItemConverter::ApplySpecialItem(
127 sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
128 {
129 bool bChanged = false;
130
131 switch( nWhichId )
132 {
133 // Attention !!! This case must be passed before SCHATTR_STAT_PERCENT,
134 // SCHATTR_STAT_BIGERROR, SCHATTR_STAT_CONSTPLUS,
135 // SCHATTR_STAT_CONSTMINUS and SCHATTR_STAT_INDICATE
136 case SCHATTR_STAT_KIND_ERROR:
137 {
138 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
139
140 SvxChartKindError eErrorKind =
141 static_cast< const SvxChartKindErrorItem & >(
142 rItemSet.Get( nWhichId )).GetValue();
143
144 if( !xErrorBarProp.is() && eErrorKind == SvxChartKindError::NONE)
145 {
146 //nothing to do
147 }
148 else
149 {
150 sal_Int32 nStyle = css::chart::ErrorBarStyle::NONE;
151
152 switch( eErrorKind )
153 {
154 case SvxChartKindError::NONE:
155 nStyle = css::chart::ErrorBarStyle::NONE; break;
156 case SvxChartKindError::Variant:
157 nStyle = css::chart::ErrorBarStyle::VARIANCE; break;
158 case SvxChartKindError::Sigma:
159 nStyle = css::chart::ErrorBarStyle::STANDARD_DEVIATION; break;
160 case SvxChartKindError::Percent:
161 nStyle = css::chart::ErrorBarStyle::RELATIVE; break;
162 case SvxChartKindError::BigError:
163 nStyle = css::chart::ErrorBarStyle::ERROR_MARGIN; break;
164 case SvxChartKindError::Const:
165 nStyle = css::chart::ErrorBarStyle::ABSOLUTE; break;
166 case SvxChartKindError::StdError:
167 nStyle = css::chart::ErrorBarStyle::STANDARD_ERROR; break;
168 case SvxChartKindError::Range:
169 nStyle = css::chart::ErrorBarStyle::FROM_DATA; break;
170 }
171
172 xErrorBarProp->setPropertyValue( u"ErrorBarStyle"_ustr , uno::Any( nStyle ));
173 bChanged = true;
174 }
175 }
176 break;
177
178 case SCHATTR_STAT_PERCENT:
179 case SCHATTR_STAT_BIGERROR:
180 {
181 OSL_FAIL( "Deprecated item" );
182 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
183
184 double fValue =
185 static_cast< const SvxDoubleItem & >(
186 rItemSet.Get( nWhichId )).GetValue();
187 double fPos(0.0), fNeg(0.0);
188 lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
189
190 if( ! ( ::rtl::math::approxEqual( fPos, fValue ) &&
191 ::rtl::math::approxEqual( fNeg, fValue )))
192 {
193 xErrorBarProp->setPropertyValue( u"PositiveError"_ustr , uno::Any( fValue ));
194 xErrorBarProp->setPropertyValue( u"NegativeError"_ustr , uno::Any( fValue ));
195 bChanged = true;
196 }
197 }
198 break;
199
200 case SCHATTR_STAT_CONSTPLUS:
201 {
202 double fValue =
203 static_cast< const SvxDoubleItem & >(
204 rItemSet.Get( nWhichId )).GetValue();
205 double fPos(0.0), fNeg(0.0);
206 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
207
208 if( ! ::rtl::math::approxEqual( fPos, fValue ))
209 {
210 GetPropertySet()->setPropertyValue( u"PositiveError"_ustr , uno::Any( fValue ));
211 bChanged = true;
212 }
213 }
214 break;
215
216 case SCHATTR_STAT_CONSTMINUS:
217 {
218 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
219
220 double fValue =
221 static_cast< const SvxDoubleItem & >(
222 rItemSet.Get( nWhichId )).GetValue();
223 double fPos(0.0), fNeg(0.0);
224 lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
225
226 if( ! ::rtl::math::approxEqual( fNeg, fValue ))
227 {
228 xErrorBarProp->setPropertyValue( u"NegativeError"_ustr , uno::Any( fValue ));
229 bChanged = true;
230 }
231 }
232 break;
233
234 case SCHATTR_STAT_INDICATE:
235 {
236 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
237
238 SvxChartIndicate eIndicate =
239 static_cast< const SvxChartIndicateItem & >(
240 rItemSet.Get( nWhichId )).GetValue();
241
242 bool bNewIndPos = (eIndicate == SvxChartIndicate::Both || eIndicate == SvxChartIndicate::Up );
243 bool bNewIndNeg = (eIndicate == SvxChartIndicate::Both || eIndicate == SvxChartIndicate::Down );
244
245 bool bShowPos(false), bShowNeg(false);
246 lcl_getErrorIndicatorValues( xErrorBarProp, bShowPos, bShowNeg );
247
248 if( bShowPos != bNewIndPos ||
249 bShowNeg != bNewIndNeg )
250 {
251 xErrorBarProp->setPropertyValue( u"ShowPositiveError"_ustr , uno::Any( bNewIndPos ));
252 xErrorBarProp->setPropertyValue( u"ShowNegativeError"_ustr , uno::Any( bNewIndNeg ));
253 bChanged = true;
254 }
255 }
256 break;
257
258 case SCHATTR_STAT_RANGE_POS:
259 case SCHATTR_STAT_RANGE_NEG:
260 {
261 // @todo: also be able to deal with x-error bars
262 const bool bYError =
263 rItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE).GetValue();
264
265 uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
266 uno::Reference< chart2::XChartDocument > xChartDoc( m_xModel, uno::UNO_QUERY );
267 uno::Reference< chart2::data::XDataProvider > xDataProvider;
268
269 if( xChartDoc.is())
270 xDataProvider.set( xChartDoc->getDataProvider());
271 if( xErrorBarSource.is() && xDataProvider.is())
272 {
273 OUString aNewRange( static_cast< const SfxStringItem & >( rItemSet.Get( nWhichId )).GetValue());
274 bool bApplyNewRange = false;
275
276 bool bIsPositiveValue( nWhichId == SCHATTR_STAT_RANGE_POS );
277 if( xChartDoc->hasInternalDataProvider())
278 {
279 if( !aNewRange.isEmpty())
280 {
281 uno::Reference< chart2::data::XDataSequence > xSeq(
282 StatisticsHelper::getErrorDataSequenceFromDataSource(
283 xErrorBarSource, bIsPositiveValue, bYError ));
284 if( ! xSeq.is())
285 {
286 // no data range for error bars yet => create
287 uno::Reference< chart2::XInternalDataProvider > xIntDataProvider( xDataProvider, uno::UNO_QUERY );
288 OSL_ASSERT( xIntDataProvider.is());
289 if( xIntDataProvider.is())
290 {
291 xIntDataProvider->appendSequence();
292 aNewRange = "last";
293 bApplyNewRange = true;
294 }
295 }
296 }
297 }
298 else
299 {
300 uno::Reference< chart2::data::XDataSequence > xSeq(
301 StatisticsHelper::getErrorDataSequenceFromDataSource(
302 xErrorBarSource, bIsPositiveValue, bYError ));
303 bApplyNewRange =
304 ! ( xSeq.is() && (aNewRange == xSeq->getSourceRangeRepresentation()));
305 }
306
307 if( bApplyNewRange )
308 StatisticsHelper::setErrorDataSequence(
309 xErrorBarSource, xDataProvider, aNewRange, bIsPositiveValue, bYError );
310 }
311 }
312 break;
313 }
314
315 return bChanged;
316 }
317
318 void ErrorBarItemConverter::FillSpecialItem(
319 sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
320 {
321 switch( nWhichId )
322 {
323 case SCHATTR_STAT_KIND_ERROR:
324 {
325 SvxChartKindError eErrorKind = SvxChartKindError::NONE;
326 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
327
328 sal_Int32 nStyle = 0;
329 if( xErrorBarProp->getPropertyValue( u"ErrorBarStyle"_ustr ) >>= nStyle )
330 {
331 switch( nStyle )
332 {
333 case css::chart::ErrorBarStyle::NONE:
334 break;
335 case css::chart::ErrorBarStyle::VARIANCE:
336 eErrorKind = SvxChartKindError::Variant; break;
337 case css::chart::ErrorBarStyle::STANDARD_DEVIATION:
338 eErrorKind = SvxChartKindError::Sigma; break;
339 case css::chart::ErrorBarStyle::ABSOLUTE:
340 eErrorKind = SvxChartKindError::Const; break;
341 case css::chart::ErrorBarStyle::RELATIVE:
342 eErrorKind = SvxChartKindError::Percent; break;
343 case css::chart::ErrorBarStyle::ERROR_MARGIN:
344 eErrorKind = SvxChartKindError::BigError; break;
345 case css::chart::ErrorBarStyle::STANDARD_ERROR:
346 eErrorKind = SvxChartKindError::StdError; break;
347 case css::chart::ErrorBarStyle::FROM_DATA:
348 eErrorKind = SvxChartKindError::Range; break;
349 default:
350 eErrorKind = SvxChartKindError::NONE; break;
351 }
352 }
353 rOutItemSet.Put( SvxChartKindErrorItem( eErrorKind, SCHATTR_STAT_KIND_ERROR ));
354 }
355 break;
356
357 case SCHATTR_STAT_PERCENT:
358 {
359 double fPos(0.0), fNeg(0.0);
360 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
361 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, SCHATTR_STAT_PERCENT ));
362 }
363 break;
364
365 case SCHATTR_STAT_BIGERROR:
366 {
367 double fPos(0.0), fNeg(0.0);
368 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
369 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, SCHATTR_STAT_BIGERROR ));
370 }
371 break;
372
373 case SCHATTR_STAT_CONSTPLUS:
374 {
375 double fPos(0.0), fNeg(0.0);
376 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
377 rOutItemSet.Put( SvxDoubleItem( fPos, SCHATTR_STAT_CONSTPLUS ));
378 }
379 break;
380
381 case SCHATTR_STAT_CONSTMINUS:
382 {
383 double fPos(0.0), fNeg(0.0);
384 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
385 rOutItemSet.Put( SvxDoubleItem( fNeg, SCHATTR_STAT_CONSTMINUS ));
386 }
387 break;
388
389 case SCHATTR_STAT_INDICATE:
390 {
391 SvxChartIndicate eIndicate = SvxChartIndicate::Both;
392 bool bShowPos(false), bShowNeg(false);
393 lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos, bShowNeg );
394
395 if( bShowPos )
396 {
397 if( bShowNeg )
398 eIndicate = SvxChartIndicate::Both;
399 else
400 eIndicate = SvxChartIndicate::Up;
401 }
402 else
403 {
404 if( bShowNeg )
405 eIndicate = SvxChartIndicate::Down;
406 else
407 eIndicate = SvxChartIndicate::NONE;
408 }
409 rOutItemSet.Put( SvxChartIndicateItem( eIndicate, SCHATTR_STAT_INDICATE ));
410 }
411 break;
412
413 case SCHATTR_STAT_RANGE_POS:
414 case SCHATTR_STAT_RANGE_NEG:
415 {
416 const bool bYError =
417 rOutItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE).GetValue();
418
419 uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
420 if( xErrorBarSource.is())
421 {
422 uno::Reference< chart2::data::XDataSequence > xSeq(
423 StatisticsHelper::getErrorDataSequenceFromDataSource(
424 xErrorBarSource, (nWhichId == SCHATTR_STAT_RANGE_POS), bYError ));
425 if( xSeq.is())
426 rOutItemSet.Put( SfxStringItem( nWhichId, xSeq->getSourceRangeRepresentation()));
427 }
428 }
429 break;
430 }
431 }
432
433 } // namespace chart::wrapper
434
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */