Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / ErrorBar.cxx
blob90c686f9f672e3400d29115d045705cc94d8de72
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 <ErrorBar.hxx>
21 #include <EventListenerHelper.hxx>
22 #include <CloneHelper.hxx>
23 #include <ModifyListenerHelper.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <svl/itemprop.hxx>
28 #include <vcl/svapp.hxx>
30 #include <com/sun/star/chart/ErrorBarStyle.hpp>
32 #include <com/sun/star/drawing/LineStyle.hpp>
33 #include <com/sun/star/util/Color.hpp>
34 #include <com/sun/star/drawing/LineJoint.hpp>
36 #include <tools/diagnose_ex.h>
37 #include <sal/log.hxx>
39 using namespace ::com::sun::star;
41 namespace
44 static const char lcl_aServiceName[] = "com.sun.star.comp.chart2.ErrorBar";
46 bool lcl_isInternalData( const uno::Reference< chart2::data::XLabeledDataSequence > & xLSeq )
48 uno::Reference< lang::XServiceInfo > xServiceInfo( xLSeq, uno::UNO_QUERY );
49 return ( xServiceInfo.is() && xServiceInfo->getImplementationName() == "com.sun.star.comp.chart2.LabeledDataSequence" );
52 const SfxItemPropertySet* GetErrorBarPropertySet()
54 static const SfxItemPropertyMapEntry aErrorBarPropertyMap_Impl[] =
56 {OUString("ShowPositiveError"),0,cppu::UnoType<bool>::get(), 0, 0},
57 {OUString("ShowNegativeError"),1,cppu::UnoType<bool>::get(), 0, 0},
58 {OUString("PositiveError"),2,cppu::UnoType<double>::get(),0,0},
59 {OUString("NegativeError"),3,cppu::UnoType<double>::get(), 0, 0},
60 {OUString("PercentageError"),4,cppu::UnoType<double>::get(), 0, 0},
61 {OUString("ErrorBarStyle"),5,cppu::UnoType<sal_Int32>::get(),0,0},
62 {OUString("ErrorBarRangePositive"),6,cppu::UnoType<OUString>::get(),0,0}, // read-only for export
63 {OUString("ErrorBarRangeNegative"),7,cppu::UnoType<OUString>::get(),0,0}, // read-only for export
64 {OUString("Weight"),8,cppu::UnoType<double>::get(),0,0},
65 {OUString("LineStyle"),9,cppu::UnoType<css::drawing::LineStyle>::get(),0,0},
66 {OUString("LineDash"),10,cppu::UnoType<drawing::LineDash>::get(),0,0},
67 {OUString("LineWidth"),11,cppu::UnoType<sal_Int32>::get(),0,0},
68 {OUString("LineColor"),12,cppu::UnoType<css::util::Color>::get(),0,0},
69 {OUString("LineTransparence"),13,cppu::UnoType<sal_Int16>::get(),0,0},
70 {OUString("LineJoint"),14,cppu::UnoType<css::drawing::LineJoint>::get(),0,0},
71 { OUString(), 0, css::uno::Type(), 0, 0 }
73 static SfxItemPropertySet aPropSet( aErrorBarPropertyMap_Impl );
74 return &aPropSet;
77 } // anonymous namespace
79 namespace chart
82 ErrorBar::ErrorBar() :
83 mnLineWidth(0),
84 meLineStyle(drawing::LineStyle_SOLID),
85 maLineColor(0),
86 mnLineTransparence(0),
87 meLineJoint(drawing::LineJoint_ROUND),
88 mbShowPositiveError(true),
89 mbShowNegativeError(true),
90 mfPositiveError(0),
91 mfNegativeError(0),
92 mfWeight(1),
93 meStyle(css::chart::ErrorBarStyle::NONE),
94 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
97 ErrorBar::ErrorBar( const ErrorBar & rOther ) :
98 impl::ErrorBar_Base(rOther),
99 maDashName(rOther.maDashName),
100 maLineDash(rOther.maLineDash),
101 mnLineWidth(rOther.mnLineWidth),
102 meLineStyle(rOther.meLineStyle),
103 maLineColor(rOther.maLineColor),
104 mnLineTransparence(rOther.mnLineTransparence),
105 meLineJoint(rOther.meLineJoint),
106 mbShowPositiveError(rOther.mbShowPositiveError),
107 mbShowNegativeError(rOther.mbShowNegativeError),
108 mfPositiveError(rOther.mfPositiveError),
109 mfNegativeError(rOther.mfNegativeError),
110 mfWeight(rOther.mfWeight),
111 meStyle(rOther.meStyle),
112 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
114 if( ! rOther.m_aDataSequences.empty())
116 if( lcl_isInternalData( rOther.m_aDataSequences.front()))
117 CloneHelper::CloneRefVector< css::chart2::data::XLabeledDataSequence >(
118 rOther.m_aDataSequences, m_aDataSequences );
119 else
120 m_aDataSequences = rOther.m_aDataSequences;
121 ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
125 ErrorBar::~ErrorBar()
128 uno::Reference< util::XCloneable > SAL_CALL ErrorBar::createClone()
130 return uno::Reference< util::XCloneable >( new ErrorBar( *this ));
133 // ____ XPropertySet ____
134 uno::Reference< beans::XPropertySetInfo > SAL_CALL ErrorBar::getPropertySetInfo()
136 static uno::Reference< beans::XPropertySetInfo > aRef (
137 new SfxItemPropertySetInfo( GetErrorBarPropertySet()->getPropertyMap() ) );
138 return aRef;
141 void ErrorBar::setPropertyValue( const OUString& rPropName, const uno::Any& rAny )
143 SolarMutexGuard aGuard;
145 if(rPropName == "ErrorBarStyle")
146 rAny >>= meStyle;
147 else if(rPropName == "PositiveError")
148 rAny >>= mfPositiveError;
149 else if(rPropName == "PercentageError")
151 rAny >>= mfPositiveError;
152 rAny >>= mfNegativeError;
154 else if(rPropName == "Weight")
156 rAny >>= mfWeight;
158 else if(rPropName == "NegativeError")
159 rAny >>= mfNegativeError;
160 else if(rPropName == "ShowPositiveError")
161 rAny >>= mbShowPositiveError;
162 else if(rPropName == "ShowNegativeError")
163 rAny >>= mbShowNegativeError;
164 else if(rPropName == "ErrorBarRangePositive" || rPropName == "ErrorBarRangeNegative")
165 throw beans::UnknownPropertyException("read-only property", static_cast< uno::XWeak*>(this));
166 else if(rPropName == "LineDashName")
167 rAny >>= maDashName;
168 else if(rPropName == "LineDash")
169 rAny >>= maLineDash;
170 else if(rPropName == "LineWidth")
171 rAny >>= mnLineWidth;
172 else if(rPropName == "LineStyle")
173 rAny >>= meLineStyle;
174 else if(rPropName == "LineColor")
175 rAny >>= maLineColor;
176 else if(rPropName == "LineTransparence")
177 rAny >>= mnLineTransparence;
178 else if(rPropName == "LineJoint")
179 rAny >>= meLineJoint;
181 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
184 namespace {
186 OUString getSourceRangeStrFromLabeledSequences( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aSequences, bool bPositive )
188 OUString aDirection;
189 if(bPositive)
190 aDirection = "positive";
191 else
192 aDirection = "negative";
194 for( sal_Int32 nI=0; nI< aSequences.getLength(); ++nI )
198 if( aSequences[nI].is())
200 uno::Reference< chart2::data::XDataSequence > xSequence( aSequences[nI]->getValues());
201 uno::Reference< beans::XPropertySet > xSeqProp( xSequence, uno::UNO_QUERY_THROW );
202 OUString aRole;
203 if( ( xSeqProp->getPropertyValue( "Role" ) >>= aRole ) &&
204 aRole.match( "error-bars" ) && aRole.indexOf(aDirection) >= 0 )
206 return xSequence->getSourceRangeRepresentation();
210 catch (uno::Exception const &)
212 // we can't be sure that this is 100% safe and we don't want to kill the export
213 // we should at least check why the exception is thrown
214 TOOLS_WARN_EXCEPTION("chart2", "unexpected exception");
216 catch (...)
218 // we can't be sure that this is 100% safe and we don't want to kill the export
219 // we should at least check why the exception is thrown
220 SAL_WARN("chart2", "unexpected exception! ");
224 return OUString();
229 uno::Any ErrorBar::getPropertyValue(const OUString& rPropName)
231 SolarMutexGuard aGuard;
233 uno::Any aRet;
234 if(rPropName == "ErrorBarStyle")
235 aRet <<= meStyle;
236 else if(rPropName == "PositiveError")
237 aRet <<= mfPositiveError;
238 else if(rPropName == "NegativeError")
239 aRet <<= mfNegativeError;
240 else if(rPropName == "PercentageError")
241 aRet <<= mfPositiveError;
242 else if(rPropName == "ShowPositiveError")
243 aRet <<= mbShowPositiveError;
244 else if(rPropName == "ShowNegativeError")
245 aRet <<= mbShowNegativeError;
246 else if(rPropName == "Weight")
247 aRet <<= mfWeight;
248 else if(rPropName == "ErrorBarRangePositive")
250 OUString aRange;
251 if(meStyle == css::chart::ErrorBarStyle::FROM_DATA)
253 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
254 getDataSequences();
256 aRange = getSourceRangeStrFromLabeledSequences( aSequences, true );
259 aRet <<= aRange;
261 else if(rPropName == "ErrorBarRangeNegative")
263 OUString aRange;
264 if(meStyle == css::chart::ErrorBarStyle::FROM_DATA)
266 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
267 getDataSequences();
269 aRange = getSourceRangeStrFromLabeledSequences( aSequences, false );
272 aRet <<= aRange;
274 else if(rPropName == "LineDashName")
275 aRet <<= maDashName;
276 else if(rPropName == "LineDash")
277 aRet <<= maLineDash;
278 else if(rPropName == "LineWidth")
279 aRet <<= mnLineWidth;
280 else if(rPropName == "LineStyle")
281 aRet <<= meLineStyle;
282 else if(rPropName == "LineColor")
283 aRet <<= maLineColor;
284 else if(rPropName == "LineTransparence")
285 aRet <<= mnLineTransparence;
286 else if(rPropName == "LineJoint")
287 aRet <<= meLineJoint;
289 SAL_WARN_IF(!aRet.hasValue(), "chart2", "asked for property value: " << rPropName);
290 return aRet;
293 beans::PropertyState ErrorBar::getPropertyState( const OUString& rPropName )
295 if(rPropName == "ErrorBarStyle")
297 if(meStyle == css::chart::ErrorBarStyle::NONE)
298 return beans::PropertyState_DEFAULT_VALUE;
299 return beans::PropertyState_DIRECT_VALUE;
301 else if(rPropName == "PositiveError")
303 if(mbShowPositiveError)
305 switch(meStyle)
307 case css::chart::ErrorBarStyle::ABSOLUTE:
308 case css::chart::ErrorBarStyle::ERROR_MARGIN:
309 return beans::PropertyState_DIRECT_VALUE;
310 default:
311 break;
314 return beans::PropertyState_DEFAULT_VALUE;
316 else if(rPropName == "NegativeError")
318 if(mbShowNegativeError)
320 switch(meStyle)
322 case css::chart::ErrorBarStyle::ABSOLUTE:
323 case css::chart::ErrorBarStyle::ERROR_MARGIN:
324 return beans::PropertyState_DIRECT_VALUE;
325 default:
326 break;
329 return beans::PropertyState_DEFAULT_VALUE;
331 else if(rPropName == "PercentageError")
333 if(meStyle != css::chart::ErrorBarStyle::RELATIVE)
334 return beans::PropertyState_DEFAULT_VALUE;
335 return beans::PropertyState_DIRECT_VALUE;
337 else if(rPropName == "ShowPositiveError")
339 // this value should be never default
340 return beans::PropertyState_DIRECT_VALUE;
342 else if(rPropName == "ShowNegativeError")
344 // this value should be never default
345 return beans::PropertyState_DIRECT_VALUE;
347 else if(rPropName == "ErrorBarRangePositive")
349 if(meStyle == css::chart::ErrorBarStyle::FROM_DATA && mbShowPositiveError)
350 return beans::PropertyState_DIRECT_VALUE;
351 return beans::PropertyState_DEFAULT_VALUE;
353 else if(rPropName == "ErrorBarRangeNegative")
355 if(meStyle == css::chart::ErrorBarStyle::FROM_DATA && mbShowNegativeError)
356 return beans::PropertyState_DIRECT_VALUE;
357 return beans::PropertyState_DEFAULT_VALUE;
359 else
360 return beans::PropertyState_DIRECT_VALUE;
363 uno::Sequence< beans::PropertyState > ErrorBar::getPropertyStates( const uno::Sequence< OUString >& rPropNames )
365 uno::Sequence< beans::PropertyState > aRet( rPropNames.getLength() );
366 for(sal_Int32 i = 0; i < rPropNames.getLength(); ++i)
368 aRet[i] = getPropertyState(rPropNames[i]);
370 return aRet;
373 void ErrorBar::setPropertyToDefault( const OUString& )
375 //keep them unimplemented for now
378 uno::Any ErrorBar::getPropertyDefault( const OUString& )
380 //keep them unimplemented for now
381 return uno::Any();
384 void ErrorBar::addPropertyChangeListener( const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener >& )
388 void ErrorBar::removePropertyChangeListener( const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener >& )
392 void ErrorBar::addVetoableChangeListener( const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener >& )
396 void ErrorBar::removeVetoableChangeListener( const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener >& )
400 // ____ XModifyBroadcaster ____
401 void SAL_CALL ErrorBar::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
405 uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
406 xBroadcaster->addModifyListener( aListener );
408 catch( const uno::Exception & )
410 DBG_UNHANDLED_EXCEPTION("chart2");
414 void SAL_CALL ErrorBar::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
418 uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
419 xBroadcaster->removeModifyListener( aListener );
421 catch( const uno::Exception & )
423 DBG_UNHANDLED_EXCEPTION("chart2");
427 // ____ XModifyListener ____
428 void SAL_CALL ErrorBar::modified( const lang::EventObject& aEvent )
430 m_xModifyEventForwarder->modified( aEvent );
433 // ____ XEventListener (base of XModifyListener) ____
434 void SAL_CALL ErrorBar::disposing( const lang::EventObject& /* Source */ )
436 // nothing
439 // ____ XDataSink ____
440 void SAL_CALL ErrorBar::setData( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aData )
442 ModifyListenerHelper::removeListenerFromAllElements( m_aDataSequences, m_xModifyEventForwarder );
443 EventListenerHelper::removeListenerFromAllElements( m_aDataSequences, this );
444 m_aDataSequences = comphelper::sequenceToContainer<tDataSequenceContainer>( aData );
445 EventListenerHelper::addListenerToAllElements( m_aDataSequences, this );
446 ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
449 // ____ XDataSource ____
450 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > SAL_CALL ErrorBar::getDataSequences()
452 return comphelper::containerToSequence( m_aDataSequences );
455 OUString SAL_CALL ErrorBar::getImplementationName()
457 return lcl_aServiceName;
460 sal_Bool SAL_CALL ErrorBar::supportsService( const OUString& rServiceName )
462 return cppu::supportsService(this, rServiceName);
465 css::uno::Sequence< OUString > SAL_CALL ErrorBar::getSupportedServiceNames()
467 return {
468 lcl_aServiceName,
469 "com.sun.star.chart2.ErrorBar"
473 // needed by MSC compiler
474 using impl::ErrorBar_Base;
476 } // namespace chart
478 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
479 com_sun_star_comp_chart2_ErrorBar_get_implementation(css::uno::XComponentContext *,
480 css::uno::Sequence<css::uno::Any> const &)
482 return cppu::acquire(new ::chart::ErrorBar);
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */