nss: upgrade to release 3.73
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / ErrorBarItemConverter.cxx
blob5a8114c215c99df3c383b3db56755f449017d3f5
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 .
20 #include <ErrorBarItemConverter.hxx>
21 #include "SchWhichPairs.hxx"
22 #include <StatisticsHelper.hxx>
24 #include <GraphicPropertyItemConverter.hxx>
26 #include <svl/stritem.hxx>
27 #include <svx/chrtitem.hxx>
28 #include <rtl/math.hxx>
30 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
31 #include <com/sun/star/chart2/XChartDocument.hpp>
32 #include <com/sun/star/chart/ErrorBarStyle.hpp>
33 #include <tools/diagnose_ex.h>
35 using namespace ::com::sun::star;
37 namespace
40 void lcl_getErrorValues( const uno::Reference< beans::XPropertySet > & xErrorBarProp,
41 double & rOutPosError, double & rOutNegError )
43 if( ! xErrorBarProp.is())
44 return;
46 try
48 xErrorBarProp->getPropertyValue( "PositiveError" ) >>= rOutPosError;
49 xErrorBarProp->getPropertyValue( "NegativeError" ) >>= rOutNegError;
51 catch( const uno::Exception & )
53 DBG_UNHANDLED_EXCEPTION("chart2");
57 void lcl_getErrorIndicatorValues(
58 const uno::Reference< beans::XPropertySet > & xErrorBarProp,
59 bool & rOutShowPosError, bool & rOutShowNegError )
61 if( ! xErrorBarProp.is())
62 return;
64 try
66 xErrorBarProp->getPropertyValue( "ShowPositiveError" ) >>= rOutShowPosError;
67 xErrorBarProp->getPropertyValue( "ShowNegativeError" ) >>= rOutShowNegError;
69 catch( const uno::Exception & )
71 DBG_UNHANDLED_EXCEPTION("chart2");
75 } // anonymous namespace
77 namespace chart::wrapper
80 ErrorBarItemConverter::ErrorBarItemConverter(
81 const uno::Reference< frame::XModel > & xModel,
82 const uno::Reference< beans::XPropertySet > & rPropertySet,
83 SfxItemPool& rItemPool,
84 SdrModel& rDrawModel,
85 const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
86 ItemConverter( rPropertySet, rItemPool ),
87 m_spGraphicConverter( std::make_shared<GraphicPropertyItemConverter>(
88 rPropertySet, rItemPool, rDrawModel,
89 xNamedPropertyContainerFactory,
90 GraphicObjectType::LineProperties )),
91 m_xModel( xModel )
94 ErrorBarItemConverter::~ErrorBarItemConverter()
97 void ErrorBarItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
99 m_spGraphicConverter->FillItemSet( rOutItemSet );
101 // own items
102 ItemConverter::FillItemSet( rOutItemSet );
105 bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
107 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
109 // own items
110 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
113 const sal_uInt16 * ErrorBarItemConverter::GetWhichPairs() const
115 // must span all used items!
116 return nErrorBarWhichPairs;
119 bool ErrorBarItemConverter::GetItemProperty(
120 tWhichIdType /* nWhichId */,
121 tPropertyNameWithMemberId & /* rOutProperty */ ) const
123 return false;
126 bool ErrorBarItemConverter::ApplySpecialItem(
127 sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
129 bool bChanged = false;
131 switch( nWhichId )
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:
138 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
140 SvxChartKindError eErrorKind =
141 static_cast< const SvxChartKindErrorItem & >(
142 rItemSet.Get( nWhichId )).GetValue();
144 if( !xErrorBarProp.is() && eErrorKind == SvxChartKindError::NONE)
146 //nothing to do
148 else
150 sal_Int32 nStyle = css::chart::ErrorBarStyle::NONE;
152 switch( eErrorKind )
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;
172 xErrorBarProp->setPropertyValue( "ErrorBarStyle" , uno::Any( nStyle ));
173 bChanged = true;
176 break;
178 case SCHATTR_STAT_PERCENT:
179 case SCHATTR_STAT_BIGERROR:
181 OSL_FAIL( "Deprecated item" );
182 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
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 );
190 if( ! ( ::rtl::math::approxEqual( fPos, fValue ) &&
191 ::rtl::math::approxEqual( fNeg, fValue )))
193 xErrorBarProp->setPropertyValue( "PositiveError" , uno::Any( fValue ));
194 xErrorBarProp->setPropertyValue( "NegativeError" , uno::Any( fValue ));
195 bChanged = true;
198 break;
200 case SCHATTR_STAT_CONSTPLUS:
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 );
208 if( ! ::rtl::math::approxEqual( fPos, fValue ))
210 GetPropertySet()->setPropertyValue( "PositiveError" , uno::Any( fValue ));
211 bChanged = true;
214 break;
216 case SCHATTR_STAT_CONSTMINUS:
218 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
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 );
226 if( ! ::rtl::math::approxEqual( fNeg, fValue ))
228 xErrorBarProp->setPropertyValue( "NegativeError" , uno::Any( fValue ));
229 bChanged = true;
232 break;
234 case SCHATTR_STAT_INDICATE:
236 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
238 SvxChartIndicate eIndicate =
239 static_cast< const SvxChartIndicateItem & >(
240 rItemSet.Get( nWhichId )).GetValue();
242 bool bNewIndPos = (eIndicate == SvxChartIndicate::Both || eIndicate == SvxChartIndicate::Up );
243 bool bNewIndNeg = (eIndicate == SvxChartIndicate::Both || eIndicate == SvxChartIndicate::Down );
245 bool bShowPos(false), bShowNeg(false);
246 lcl_getErrorIndicatorValues( xErrorBarProp, bShowPos, bShowNeg );
248 if( bShowPos != bNewIndPos ||
249 bShowNeg != bNewIndNeg )
251 xErrorBarProp->setPropertyValue( "ShowPositiveError" , uno::Any( bNewIndPos ));
252 xErrorBarProp->setPropertyValue( "ShowNegativeError" , uno::Any( bNewIndNeg ));
253 bChanged = true;
256 break;
258 case SCHATTR_STAT_RANGE_POS:
259 case SCHATTR_STAT_RANGE_NEG:
261 // @todo: also be able to deal with x-error bars
262 const bool bYError =
263 rItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE).GetValue();
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;
269 if( xChartDoc.is())
270 xDataProvider.set( xChartDoc->getDataProvider());
271 if( xErrorBarSource.is() && xDataProvider.is())
273 OUString aNewRange( static_cast< const SfxStringItem & >( rItemSet.Get( nWhichId )).GetValue());
274 bool bApplyNewRange = false;
276 bool bIsPositiveValue( nWhichId == SCHATTR_STAT_RANGE_POS );
277 if( xChartDoc->hasInternalDataProvider())
279 if( !aNewRange.isEmpty())
281 uno::Reference< chart2::data::XDataSequence > xSeq(
282 StatisticsHelper::getErrorDataSequenceFromDataSource(
283 xErrorBarSource, bIsPositiveValue, bYError ));
284 if( ! xSeq.is())
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())
291 xIntDataProvider->appendSequence();
292 aNewRange = "last";
293 bApplyNewRange = true;
298 else
300 uno::Reference< chart2::data::XDataSequence > xSeq(
301 StatisticsHelper::getErrorDataSequenceFromDataSource(
302 xErrorBarSource, bIsPositiveValue, bYError ));
303 bApplyNewRange =
304 ! ( xSeq.is() && (aNewRange == xSeq->getSourceRangeRepresentation()));
307 if( bApplyNewRange )
308 StatisticsHelper::setErrorDataSequence(
309 xErrorBarSource, xDataProvider, aNewRange, bIsPositiveValue, bYError );
312 break;
315 return bChanged;
318 void ErrorBarItemConverter::FillSpecialItem(
319 sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
321 switch( nWhichId )
323 case SCHATTR_STAT_KIND_ERROR:
325 SvxChartKindError eErrorKind = SvxChartKindError::NONE;
326 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
328 sal_Int32 nStyle = 0;
329 if( xErrorBarProp->getPropertyValue( "ErrorBarStyle" ) >>= nStyle )
331 switch( nStyle )
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;
351 rOutItemSet.Put( SvxChartKindErrorItem( eErrorKind, SCHATTR_STAT_KIND_ERROR ));
353 break;
355 case SCHATTR_STAT_PERCENT:
357 double fPos(0.0), fNeg(0.0);
358 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
359 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
361 break;
363 case SCHATTR_STAT_BIGERROR:
365 double fPos(0.0), fNeg(0.0);
366 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
367 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
369 break;
371 case SCHATTR_STAT_CONSTPLUS:
373 double fPos(0.0), fNeg(0.0);
374 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
375 rOutItemSet.Put( SvxDoubleItem( fPos, nWhichId ));
377 break;
379 case SCHATTR_STAT_CONSTMINUS:
381 double fPos(0.0), fNeg(0.0);
382 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
383 rOutItemSet.Put( SvxDoubleItem( fNeg, nWhichId ));
385 break;
387 case SCHATTR_STAT_INDICATE:
389 SvxChartIndicate eIndicate = SvxChartIndicate::Both;
390 bool bShowPos(false), bShowNeg(false);
391 lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos, bShowNeg );
393 if( bShowPos )
395 if( bShowNeg )
396 eIndicate = SvxChartIndicate::Both;
397 else
398 eIndicate = SvxChartIndicate::Up;
400 else
402 if( bShowNeg )
403 eIndicate = SvxChartIndicate::Down;
404 else
405 eIndicate = SvxChartIndicate::NONE;
407 rOutItemSet.Put( SvxChartIndicateItem( eIndicate, SCHATTR_STAT_INDICATE ));
409 break;
411 case SCHATTR_STAT_RANGE_POS:
412 case SCHATTR_STAT_RANGE_NEG:
414 const bool bYError =
415 rOutItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE).GetValue();
417 uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
418 if( xErrorBarSource.is())
420 uno::Reference< chart2::data::XDataSequence > xSeq(
421 StatisticsHelper::getErrorDataSequenceFromDataSource(
422 xErrorBarSource, (nWhichId == SCHATTR_STAT_RANGE_POS), bYError ));
423 if( xSeq.is())
424 rOutItemSet.Put( SfxStringItem( nWhichId, xSeq->getSourceRangeRepresentation()));
427 break;
431 } // namespace chart::wrapper
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */