Update ooo320-m1
[ooovba.git] / chart2 / source / controller / itemsetwrapper / ErrorBarItemConverter.cxx
blob04953fd1dce3c57b0329f9ef70a6e48ab0d98c7c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ErrorBarItemConverter.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "ErrorBarItemConverter.hxx"
34 #include "SchWhichPairs.hxx"
35 #include "macros.hxx"
36 #include "ItemPropertyMap.hxx"
37 #include "ErrorBar.hxx"
38 #include "PropertyHelper.hxx"
39 #include "ChartModelHelper.hxx"
40 #include "ChartTypeHelper.hxx"
41 #include "StatisticsHelper.hxx"
43 #include "GraphicPropertyItemConverter.hxx"
45 #include <svtools/stritem.hxx>
46 #include <svx/chrtitem.hxx>
47 #include <svtools/intitem.hxx>
48 #include <rtl/math.hxx>
50 #include <com/sun/star/chart2/DataPointLabel.hpp>
51 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
52 #include <com/sun/star/chart/ErrorBarStyle.hpp>
53 #include <com/sun/star/lang/XServiceName.hpp>
55 #include <functional>
56 #include <algorithm>
57 #include <vector>
59 using namespace ::com::sun::star;
61 namespace
64 void lcl_getErrorValues( const uno::Reference< beans::XPropertySet > & xErrorBarProp,
65 double & rOutPosError, double & rOutNegError )
67 if( ! xErrorBarProp.is())
68 return;
70 try
72 xErrorBarProp->getPropertyValue( C2U( "PositiveError" )) >>= rOutPosError;
73 xErrorBarProp->getPropertyValue( C2U( "NegativeError" )) >>= rOutNegError;
75 catch( uno::Exception & ex )
77 ASSERT_EXCEPTION( ex );
81 void lcl_getErrorIndicatorValues(
82 const uno::Reference< beans::XPropertySet > & xErrorBarProp,
83 bool & rOutShowPosError, bool & rOutShowNegError )
85 if( ! xErrorBarProp.is())
86 return;
88 try
90 xErrorBarProp->getPropertyValue( C2U( "ShowPositiveError" )) >>= rOutShowPosError;
91 xErrorBarProp->getPropertyValue( C2U( "ShowNegativeError" )) >>= rOutShowNegError;
93 catch( uno::Exception & ex )
95 ASSERT_EXCEPTION( ex );
99 } // anonymous namespace
101 namespace chart
103 namespace wrapper
106 ErrorBarItemConverter::ErrorBarItemConverter(
107 const uno::Reference< frame::XModel > & xModel,
108 const uno::Reference< beans::XPropertySet > & rPropertySet,
109 SfxItemPool& rItemPool,
110 SdrModel& rDrawModel,
111 const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
112 ItemConverter( rPropertySet, rItemPool ),
113 m_spGraphicConverter( new GraphicPropertyItemConverter(
114 rPropertySet, rItemPool, rDrawModel,
115 xNamedPropertyContainerFactory,
116 GraphicPropertyItemConverter::LINE_PROPERTIES )),
117 m_xModel( xModel )
120 ErrorBarItemConverter::~ErrorBarItemConverter()
123 void ErrorBarItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
125 m_spGraphicConverter->FillItemSet( rOutItemSet );
127 // own items
128 ItemConverter::FillItemSet( rOutItemSet );
131 bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
133 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
135 // own items
136 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
139 const USHORT * ErrorBarItemConverter::GetWhichPairs() const
141 // must span all used items!
142 return nErrorBarWhichPairs;
145 bool ErrorBarItemConverter::GetItemProperty(
146 tWhichIdType /* nWhichId */,
147 tPropertyNameWithMemberId & /* rOutProperty */ ) const
149 return false;
152 bool ErrorBarItemConverter::ApplySpecialItem(
153 USHORT nWhichId, const SfxItemSet & rItemSet )
154 throw( uno::Exception )
156 bool bChanged = false;
157 uno::Any aValue;
159 switch( nWhichId )
161 // Attention !!! This case must be passed before SCHATTR_STAT_PERCENT,
162 // SCHATTR_STAT_BIGERROR, SCHATTR_STAT_CONSTPLUS,
163 // SCHATTR_STAT_CONSTMINUS and SCHATTR_STAT_INDICATE
164 case SCHATTR_STAT_KIND_ERROR:
166 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
168 SvxChartKindError eErrorKind =
169 static_cast< const SvxChartKindErrorItem & >(
170 rItemSet.Get( nWhichId )).GetValue();
172 if( !xErrorBarProp.is() && eErrorKind == CHERROR_NONE)
174 //nothing to do
176 else
178 sal_Int32 nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE;
180 switch( eErrorKind )
182 case CHERROR_NONE:
183 nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE; break;
184 case CHERROR_VARIANT:
185 nStyle = ::com::sun::star::chart::ErrorBarStyle::VARIANCE; break;
186 case CHERROR_SIGMA:
187 nStyle = ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION; break;
188 case CHERROR_PERCENT:
189 nStyle = ::com::sun::star::chart::ErrorBarStyle::RELATIVE; break;
190 case CHERROR_BIGERROR:
191 nStyle = ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN; break;
192 case CHERROR_CONST:
193 nStyle = ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE; break;
194 case CHERROR_STDERROR:
195 nStyle = ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR; break;
196 case CHERROR_RANGE:
197 nStyle = ::com::sun::star::chart::ErrorBarStyle::FROM_DATA; break;
200 xErrorBarProp->setPropertyValue( C2U( "ErrorBarStyle" ),
201 uno::makeAny( nStyle ));
202 bChanged = true;
205 break;
207 case SCHATTR_STAT_PERCENT:
208 case SCHATTR_STAT_BIGERROR:
210 OSL_ENSURE( false, "Deprectaed item" );
211 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
213 double fValue =
214 static_cast< const SvxDoubleItem & >(
215 rItemSet.Get( nWhichId )).GetValue();
216 double fPos, fNeg;
217 lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
219 if( ! ( ::rtl::math::approxEqual( fPos, fValue ) &&
220 ::rtl::math::approxEqual( fNeg, fValue )))
222 xErrorBarProp->setPropertyValue( C2U( "PositiveError" ),
223 uno::makeAny( fValue ));
224 xErrorBarProp->setPropertyValue( C2U( "NegativeError" ),
225 uno::makeAny( fValue ));
226 bChanged = true;
229 break;
231 case SCHATTR_STAT_CONSTPLUS:
233 double fValue =
234 static_cast< const SvxDoubleItem & >(
235 rItemSet.Get( nWhichId )).GetValue();
236 double fPos, fNeg;
237 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
239 if( ! ::rtl::math::approxEqual( fPos, fValue ))
241 GetPropertySet()->setPropertyValue( C2U( "PositiveError" ), uno::makeAny( fValue ));
242 bChanged = true;
245 break;
247 case SCHATTR_STAT_CONSTMINUS:
249 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
251 double fValue =
252 static_cast< const SvxDoubleItem & >(
253 rItemSet.Get( nWhichId )).GetValue();
254 double fPos, fNeg;
255 lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
257 if( ! ::rtl::math::approxEqual( fNeg, fValue ))
259 xErrorBarProp->setPropertyValue( C2U( "NegativeError" ), uno::makeAny( fValue ));
260 bChanged = true;
263 break;
265 case SCHATTR_STAT_INDICATE:
267 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
269 SvxChartIndicate eIndicate =
270 static_cast< const SvxChartIndicateItem & >(
271 rItemSet.Get( nWhichId )).GetValue();
273 bool bNewIndPos = (eIndicate == CHINDICATE_BOTH || eIndicate == CHINDICATE_UP );
274 bool bNewIndNeg = (eIndicate == CHINDICATE_BOTH || eIndicate == CHINDICATE_DOWN );
276 bool bShowPos, bShowNeg;
277 lcl_getErrorIndicatorValues( xErrorBarProp, bShowPos, bShowNeg );
279 if( ( bShowPos != bNewIndPos ||
280 bShowNeg != bNewIndNeg ))
282 xErrorBarProp->setPropertyValue( C2U( "ShowPositiveError" ), uno::makeAny( bNewIndPos ));
283 xErrorBarProp->setPropertyValue( C2U( "ShowNegativeError" ), uno::makeAny( bNewIndNeg ));
284 bChanged = true;
287 break;
289 case SCHATTR_STAT_RANGE_POS:
290 case SCHATTR_STAT_RANGE_NEG:
292 // @todo: also be able to deal with x-error bars
293 const bool bYError = true;
294 uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
295 uno::Reference< chart2::XChartDocument > xChartDoc( m_xModel, uno::UNO_QUERY );
296 uno::Reference< chart2::data::XDataProvider > xDataProvider;
298 if( xChartDoc.is())
299 xDataProvider.set( xChartDoc->getDataProvider());
300 if( xErrorBarSource.is() && xDataProvider.is())
302 ::rtl::OUString aNewRange( static_cast< const SfxStringItem & >( rItemSet.Get( nWhichId )).GetValue());
303 bool bApplyNewRange = false;
305 bool bIsPositiveValue( nWhichId == SCHATTR_STAT_RANGE_POS );
306 if( xChartDoc->hasInternalDataProvider())
308 if( aNewRange.getLength())
310 uno::Reference< chart2::data::XDataSequence > xSeq(
311 StatisticsHelper::getErrorDataSequenceFromDataSource(
312 xErrorBarSource, bIsPositiveValue, bYError ));
313 if( ! xSeq.is())
315 // no data range for error bars yet => create
316 uno::Reference< chart2::XInternalDataProvider > xIntDataProvider( xDataProvider, uno::UNO_QUERY );
317 OSL_ASSERT( xIntDataProvider.is());
318 if( xIntDataProvider.is())
320 xIntDataProvider->appendSequence();
321 aNewRange = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("last"));
322 bApplyNewRange = true;
327 else
329 uno::Reference< chart2::data::XDataSequence > xSeq(
330 StatisticsHelper::getErrorDataSequenceFromDataSource(
331 xErrorBarSource, bIsPositiveValue, bYError ));
332 bApplyNewRange =
333 ! ( xSeq.is() && aNewRange.equals( xSeq->getSourceRangeRepresentation()));
336 if( bApplyNewRange )
337 StatisticsHelper::setErrorDataSequence(
338 xErrorBarSource, xDataProvider, aNewRange, bIsPositiveValue, bYError );
341 break;
344 return bChanged;
347 void ErrorBarItemConverter::FillSpecialItem(
348 USHORT nWhichId, SfxItemSet & rOutItemSet ) const
349 throw( uno::Exception )
351 switch( nWhichId )
353 case SCHATTR_STAT_KIND_ERROR:
355 SvxChartKindError eErrorKind = CHERROR_NONE;
356 uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
358 sal_Int32 nStyle = 0;
359 if( xErrorBarProp->getPropertyValue( C2U( "ErrorBarStyle" )) >>= nStyle )
361 switch( nStyle )
363 case ::com::sun::star::chart::ErrorBarStyle::NONE:
364 break;
365 case ::com::sun::star::chart::ErrorBarStyle::VARIANCE:
366 eErrorKind = CHERROR_VARIANT; break;
367 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION:
368 eErrorKind = CHERROR_SIGMA; break;
369 case ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE:
370 eErrorKind = CHERROR_CONST; break;
371 case ::com::sun::star::chart::ErrorBarStyle::RELATIVE:
372 eErrorKind = CHERROR_PERCENT; break;
373 case ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN:
374 eErrorKind = CHERROR_BIGERROR; break;
375 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR:
376 eErrorKind = CHERROR_STDERROR; break;
377 case ::com::sun::star::chart::ErrorBarStyle::FROM_DATA:
378 eErrorKind = CHERROR_RANGE; break;
381 rOutItemSet.Put( SvxChartKindErrorItem( eErrorKind, SCHATTR_STAT_KIND_ERROR ));
383 break;
385 case SCHATTR_STAT_PERCENT:
387 double fPos, fNeg;
388 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
389 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
391 break;
393 case SCHATTR_STAT_BIGERROR:
395 double fPos, fNeg;
396 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
397 rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
399 break;
401 case SCHATTR_STAT_CONSTPLUS:
403 double fPos, fNeg;
404 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
405 rOutItemSet.Put( SvxDoubleItem( fPos, nWhichId ));
407 break;
409 case SCHATTR_STAT_CONSTMINUS:
411 double fPos, fNeg;
412 lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
413 rOutItemSet.Put( SvxDoubleItem( fNeg, nWhichId ));
415 break;
417 case SCHATTR_STAT_INDICATE:
419 SvxChartIndicate eIndicate = CHINDICATE_BOTH;
420 bool bShowPos, bShowNeg;
421 lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos, bShowNeg );
423 if( bShowPos )
425 if( bShowNeg )
426 eIndicate = CHINDICATE_BOTH;
427 else
428 eIndicate = CHINDICATE_UP;
430 else
432 if( bShowNeg )
433 eIndicate = CHINDICATE_DOWN;
434 else
435 eIndicate = CHINDICATE_NONE;
437 rOutItemSet.Put( SvxChartIndicateItem( eIndicate, SCHATTR_STAT_INDICATE ));
439 break;
441 case SCHATTR_STAT_RANGE_POS:
442 case SCHATTR_STAT_RANGE_NEG:
444 uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
445 if( xErrorBarSource.is())
447 uno::Reference< chart2::data::XDataSequence > xSeq(
448 StatisticsHelper::getErrorDataSequenceFromDataSource(
449 xErrorBarSource, (nWhichId == SCHATTR_STAT_RANGE_POS) /*, true */ /* y */ ));
450 if( xSeq.is())
451 rOutItemSet.Put( SfxStringItem( nWhichId, String( xSeq->getSourceRangeRepresentation())));
454 break;
458 } // namespace wrapper
459 } // namespace chart