Line data Source code
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 "ErrorBar.hxx"
21 : #include "macros.hxx"
22 : #include "LineProperties.hxx"
23 : #include "ContainerHelper.hxx"
24 : #include "EventListenerHelper.hxx"
25 : #include "PropertyHelper.hxx"
26 : #include "CloneHelper.hxx"
27 :
28 : #include <svl/itemprop.hxx>
29 : #include <vcl/svapp.hxx>
30 :
31 : #include <com/sun/star/beans/PropertyAttribute.hpp>
32 : #include <com/sun/star/chart/ErrorBarStyle.hpp>
33 :
34 : #include <com/sun/star/drawing/LineStyle.hpp>
35 : #include <com/sun/star/util/Color.hpp>
36 : #include <com/sun/star/drawing/LineJoint.hpp>
37 :
38 : #include <rtl/math.hxx>
39 : #include <rtl/ustrbuf.hxx>
40 :
41 : using namespace ::com::sun::star;
42 :
43 : namespace
44 : {
45 :
46 0 : static const OUString lcl_aServiceName( "com.sun.star.comp.chart2.ErrorBar" );
47 :
48 0 : bool lcl_isInternalData( const uno::Reference< chart2::data::XLabeledDataSequence > & xLSeq )
49 : {
50 0 : uno::Reference< lang::XServiceInfo > xServiceInfo( xLSeq, uno::UNO_QUERY );
51 0 : return ( xServiceInfo.is() && xServiceInfo->getImplementationName() == "com.sun.star.comp.chart2.LabeledDataSequence" );
52 : }
53 :
54 0 : const SfxItemPropertySet* GetErrorBarPropertySet()
55 : {
56 : static const SfxItemPropertyMapEntry aErrorBarPropertyMap_Impl[] =
57 : {
58 0 : {OUString("ShowPositiveError"),0,getBooleanCppuType(), 0, 0},
59 0 : {OUString("ShowNegativeError"),1,getBooleanCppuType(), 0, 0},
60 0 : {OUString("PositiveError"),2,getCppuType((const double*)0),0,0},
61 0 : {OUString("NegativeError"),3,getCppuType((const double*)0), 0, 0},
62 0 : {OUString("PercentageError"),4,getCppuType((const double*)0), 0, 0},
63 0 : {OUString("ErrorBarStyle"),5,getCppuType((sal_Int32*)0),0,0},
64 0 : {OUString("ErrorBarRangePositive"),6,getCppuType((OUString*)0),0,0}, // read-only for export
65 0 : {OUString("ErrorBarRangeNegative"),7,getCppuType((OUString*)0),0,0}, // read-only for export
66 0 : {OUString("Weight"),8,getCppuType((const double*)0),0,0},
67 0 : {OUString("LineStyle"),9,getCppuType((com::sun::star::drawing::LineStyle*)0),0,0},
68 0 : {OUString("LineDash"),10,getCppuType((drawing::LineDash*)0),0,0},
69 0 : {OUString("LineWidth"),11,getCppuType((sal_Int32*)0),0,0},
70 0 : {OUString("LineColor"),12,getCppuType((com::sun::star::util::Color*)0),0,0},
71 0 : {OUString("LineTransparence"),13,getCppuType((sal_Int16*)0),0,0},
72 0 : {OUString("LineJoint"),14,getCppuType((com::sun::star::drawing::LineJoint*)0),0,0},
73 : { OUString(), 0, css::uno::Type(), 0, 0 }
74 0 : };
75 0 : static SfxItemPropertySet aPropSet( aErrorBarPropertyMap_Impl );
76 0 : return &aPropSet;
77 : }
78 :
79 : } // anonymous namespace
80 :
81 : namespace chart
82 : {
83 :
84 0 : uno::Reference< beans::XPropertySet > createErrorBar( const uno::Reference< uno::XComponentContext > & xContext )
85 : {
86 0 : return new ErrorBar( xContext );
87 : }
88 :
89 0 : ErrorBar::ErrorBar(
90 : uno::Reference< uno::XComponentContext > const & xContext ) :
91 : LineProperties(),
92 : mbShowPositiveError(true),
93 : mbShowNegativeError(true),
94 : mfPositiveError(0),
95 : mfNegativeError(0),
96 : mfWeight(1),
97 : meStyle(com::sun::star::chart::ErrorBarStyle::NONE),
98 : m_xContext( xContext ),
99 0 : m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
100 0 : {}
101 :
102 0 : ErrorBar::ErrorBar( const ErrorBar & rOther ) :
103 : MutexContainer(),
104 : impl::ErrorBar_Base(),
105 : LineProperties(rOther),
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_xContext( rOther.m_xContext ),
113 0 : m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
114 : {
115 0 : if( ! rOther.m_aDataSequences.empty())
116 : {
117 0 : if( lcl_isInternalData( rOther.m_aDataSequences.front()))
118 : CloneHelper::CloneRefVector< tDataSequenceContainer::value_type >(
119 0 : rOther.m_aDataSequences, m_aDataSequences );
120 : else
121 0 : m_aDataSequences = rOther.m_aDataSequences;
122 0 : ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
123 : }
124 0 : }
125 :
126 0 : ErrorBar::~ErrorBar()
127 0 : {}
128 :
129 0 : uno::Reference< util::XCloneable > SAL_CALL ErrorBar::createClone()
130 : throw (uno::RuntimeException, std::exception)
131 : {
132 0 : return uno::Reference< util::XCloneable >( new ErrorBar( *this ));
133 : }
134 :
135 : // ____ XPropertySet ____
136 0 : uno::Reference< beans::XPropertySetInfo > SAL_CALL ErrorBar::getPropertySetInfo()
137 : throw (uno::RuntimeException, std::exception)
138 : {
139 : static uno::Reference< beans::XPropertySetInfo > aRef (
140 0 : new SfxItemPropertySetInfo( GetErrorBarPropertySet()->getPropertyMap() ) );
141 0 : return aRef;
142 : }
143 :
144 0 : void ErrorBar::setPropertyValue( const OUString& rPropName, const uno::Any& rAny )
145 : throw (beans::UnknownPropertyException, beans::PropertyVetoException,
146 : lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
147 : {
148 0 : SolarMutexGuard aGuard;
149 :
150 0 : if(rPropName == "ErrorBarStyle")
151 0 : rAny >>= meStyle;
152 0 : else if(rPropName == "PositiveError")
153 0 : rAny >>= mfPositiveError;
154 0 : else if(rPropName == "PercentageError")
155 : {
156 0 : rAny >>= mfPositiveError;
157 0 : rAny >>= mfNegativeError;
158 : }
159 0 : else if(rPropName == "Weight")
160 : {
161 0 : rAny >>= mfWeight;
162 : }
163 0 : else if(rPropName == "NegativeError")
164 0 : rAny >>= mfNegativeError;
165 0 : else if(rPropName == "ShowPositiveError")
166 0 : rAny >>= mbShowPositiveError;
167 0 : else if(rPropName == "ShowNegativeError")
168 0 : rAny >>= mbShowNegativeError;
169 0 : else if(rPropName == "ErrorBarRangePositive" || rPropName == "ErrorBarRangeNegative")
170 0 : throw uno::RuntimeException("read-only property", static_cast< uno::XWeak*>(this));
171 : else
172 0 : LineProperties::setPropertyValue(rPropName, rAny);
173 :
174 0 : m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
175 0 : }
176 :
177 : namespace {
178 :
179 0 : OUString getSourceRangeStrFromLabeledSequences( uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences, bool bPositive )
180 : {
181 0 : const OUString aRolePrefix( "error-bars" );
182 0 : OUString aDirection;
183 0 : if(bPositive)
184 0 : aDirection = "positive";
185 : else
186 0 : aDirection = "negative";
187 :
188 0 : for( sal_Int32 nI=0; nI< aSequences.getLength(); ++nI )
189 : {
190 : try
191 : {
192 0 : if( aSequences[nI].is())
193 : {
194 0 : uno::Reference< chart2::data::XDataSequence > xSequence( aSequences[nI]->getValues());
195 0 : uno::Reference< beans::XPropertySet > xSeqProp( xSequence, uno::UNO_QUERY_THROW );
196 0 : OUString aRole;
197 0 : if( ( xSeqProp->getPropertyValue(
198 0 : OUString( "Role" )) >>= aRole ) &&
199 0 : aRole.match( aRolePrefix ) && aRole.indexOf(aDirection) >= 0 )
200 : {
201 0 : return xSequence->getSourceRangeRepresentation();
202 0 : }
203 : }
204 : }
205 0 : catch (...)
206 : {
207 : // we can't be sure that this is 100% safe and we don't want to kill the export
208 : // we should at least check why the exception is thrown
209 : SAL_WARN("chart2", "unexpected exception!");
210 : }
211 : }
212 :
213 0 : return OUString();
214 : }
215 :
216 : }
217 :
218 0 : uno::Any ErrorBar::getPropertyValue(const OUString& rPropName)
219 : throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
220 : {
221 0 : SolarMutexGuard aGuard;
222 :
223 0 : uno::Any aRet;
224 0 : if(rPropName == "ErrorBarStyle")
225 0 : aRet <<= meStyle;
226 0 : else if(rPropName == "PositiveError")
227 0 : aRet <<= mfPositiveError;
228 0 : else if(rPropName == "NegativeError")
229 0 : aRet <<= mfNegativeError;
230 0 : else if(rPropName == "PercentageError")
231 0 : aRet <<= mfPositiveError;
232 0 : else if(rPropName == "ShowPositiveError")
233 0 : aRet <<= mbShowPositiveError;
234 0 : else if(rPropName == "ShowNegativeError")
235 0 : aRet <<= mbShowNegativeError;
236 0 : else if(rPropName == "Weight")
237 0 : aRet <<= mfWeight;
238 0 : else if(rPropName == "ErrorBarRangePositive")
239 : {
240 0 : OUString aRange;
241 0 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA)
242 : {
243 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
244 0 : getDataSequences();
245 :
246 0 : aRange = getSourceRangeStrFromLabeledSequences( aSequences, true );
247 : }
248 :
249 0 : aRet <<= aRange;
250 : }
251 0 : else if(rPropName == "ErrorBarRangeNegative")
252 : {
253 0 : OUString aRange;
254 0 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA)
255 : {
256 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
257 0 : getDataSequences();
258 :
259 0 : aRange = getSourceRangeStrFromLabeledSequences( aSequences, false );
260 : }
261 :
262 0 : aRet <<= aRange;
263 : }
264 : else
265 0 : aRet = LineProperties::getPropertyValue(rPropName);
266 :
267 : SAL_WARN_IF(!aRet.hasValue(), "chart2", "asked for property value: " << rPropName);
268 0 : return aRet;
269 : }
270 :
271 0 : beans::PropertyState ErrorBar::getPropertyState( const OUString& rPropName )
272 : throw (com::sun::star::beans::UnknownPropertyException, std::exception)
273 : {
274 0 : if(rPropName == "ErrorBarStyle")
275 : {
276 0 : if(meStyle == com::sun::star::chart::ErrorBarStyle::NONE)
277 0 : return beans::PropertyState_DEFAULT_VALUE;
278 0 : return beans::PropertyState_DIRECT_VALUE;
279 : }
280 0 : else if(rPropName == "PositiveError")
281 : {
282 0 : if(mbShowPositiveError)
283 : {
284 0 : switch(meStyle)
285 : {
286 : case com::sun::star::chart::ErrorBarStyle::ABSOLUTE:
287 : case com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN:
288 0 : return beans::PropertyState_DIRECT_VALUE;
289 : default:
290 0 : break;
291 : }
292 : }
293 0 : return beans::PropertyState_DEFAULT_VALUE;
294 : }
295 0 : else if(rPropName == "NegativeError")
296 : {
297 0 : if(mbShowNegativeError)
298 : {
299 0 : switch(meStyle)
300 : {
301 : case com::sun::star::chart::ErrorBarStyle::ABSOLUTE:
302 : case com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN:
303 0 : return beans::PropertyState_DIRECT_VALUE;
304 : default:
305 0 : break;
306 : }
307 : }
308 0 : return beans::PropertyState_DEFAULT_VALUE;
309 : }
310 0 : else if(rPropName == "PercentageError")
311 : {
312 0 : if(meStyle != com::sun::star::chart::ErrorBarStyle::RELATIVE)
313 0 : return beans::PropertyState_DEFAULT_VALUE;
314 0 : return beans::PropertyState_DIRECT_VALUE;
315 : }
316 0 : else if(rPropName == "ShowPositiveError")
317 : {
318 : // this value should be never default
319 0 : return beans::PropertyState_DIRECT_VALUE;
320 : }
321 0 : else if(rPropName == "ShowNegativeError")
322 : {
323 : // this value should be never default
324 0 : return beans::PropertyState_DIRECT_VALUE;
325 : }
326 0 : else if(rPropName == "ErrorBarRangePositive")
327 : {
328 0 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA && mbShowPositiveError)
329 0 : return beans::PropertyState_DIRECT_VALUE;
330 0 : return beans::PropertyState_DEFAULT_VALUE;
331 : }
332 0 : else if(rPropName == "ErrorBarRangeNegative")
333 : {
334 0 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA && mbShowNegativeError)
335 0 : return beans::PropertyState_DIRECT_VALUE;
336 0 : return beans::PropertyState_DEFAULT_VALUE;
337 : }
338 : else
339 0 : return beans::PropertyState_DIRECT_VALUE;
340 : }
341 :
342 0 : uno::Sequence< beans::PropertyState > ErrorBar::getPropertyStates( const uno::Sequence< OUString >& rPropNames )
343 : throw (com::sun::star::beans::UnknownPropertyException, std::exception)
344 : {
345 0 : uno::Sequence< beans::PropertyState > aRet( rPropNames.getLength() );
346 0 : for(sal_Int32 i = 0; i < rPropNames.getLength(); ++i)
347 : {
348 0 : aRet[i] = getPropertyState(rPropNames[i]);
349 : }
350 0 : return aRet;
351 : }
352 :
353 0 : void ErrorBar::setPropertyToDefault( const OUString& )
354 : throw (beans::UnknownPropertyException, std::exception)
355 : {
356 : //keep them unimplemented for now
357 0 : }
358 :
359 0 : uno::Any ErrorBar::getPropertyDefault( const OUString& )
360 : throw (beans::UnknownPropertyException, lang::WrappedTargetException, std::exception)
361 : {
362 : //keep them unimplemented for now
363 0 : return uno::Any();
364 : }
365 :
366 0 : void ErrorBar::addPropertyChangeListener( const OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& )
367 : throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
368 : {
369 0 : }
370 :
371 0 : void ErrorBar::removePropertyChangeListener( const OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& )
372 : throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
373 : {
374 0 : }
375 :
376 0 : void ErrorBar::addVetoableChangeListener( const OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& )
377 : throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
378 : {
379 0 : }
380 :
381 0 : void ErrorBar::removeVetoableChangeListener( const OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& )
382 : throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
383 : {
384 0 : }
385 :
386 : // ____ XModifyBroadcaster ____
387 0 : void SAL_CALL ErrorBar::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
388 : throw (uno::RuntimeException, std::exception)
389 : {
390 : try
391 : {
392 0 : uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
393 0 : xBroadcaster->addModifyListener( aListener );
394 : }
395 0 : catch( const uno::Exception & ex )
396 : {
397 : ASSERT_EXCEPTION( ex );
398 : }
399 0 : }
400 :
401 0 : void SAL_CALL ErrorBar::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
402 : throw (uno::RuntimeException, std::exception)
403 : {
404 : try
405 : {
406 0 : uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
407 0 : xBroadcaster->removeModifyListener( aListener );
408 : }
409 0 : catch( const uno::Exception & ex )
410 : {
411 : ASSERT_EXCEPTION( ex );
412 : }
413 0 : }
414 :
415 : // ____ XModifyListener ____
416 0 : void SAL_CALL ErrorBar::modified( const lang::EventObject& aEvent )
417 : throw (uno::RuntimeException, std::exception)
418 : {
419 0 : m_xModifyEventForwarder->modified( aEvent );
420 0 : }
421 :
422 : // ____ XEventListener (base of XModifyListener) ____
423 0 : void SAL_CALL ErrorBar::disposing( const lang::EventObject& /* Source */ )
424 : throw (uno::RuntimeException, std::exception)
425 : {
426 : // nothing
427 0 : }
428 :
429 : // ____ XDataSink ____
430 0 : void SAL_CALL ErrorBar::setData( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aData )
431 : throw (uno::RuntimeException, std::exception)
432 : {
433 0 : ModifyListenerHelper::removeListenerFromAllElements( m_aDataSequences, m_xModifyEventForwarder );
434 0 : EventListenerHelper::removeListenerFromAllElements( m_aDataSequences, this );
435 0 : m_aDataSequences = ContainerHelper::SequenceToVector( aData );
436 0 : EventListenerHelper::addListenerToAllElements( m_aDataSequences, this );
437 0 : ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
438 0 : }
439 :
440 : // ____ XDataSource ____
441 0 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > SAL_CALL ErrorBar::getDataSequences()
442 : throw (uno::RuntimeException, std::exception)
443 : {
444 0 : return ContainerHelper::ContainerToSequence( m_aDataSequences );
445 : }
446 :
447 : // ____ XChild ____
448 0 : uno::Reference< uno::XInterface > SAL_CALL ErrorBar::getParent()
449 : throw (uno::RuntimeException)
450 : {
451 0 : return m_xParent;
452 : }
453 :
454 0 : void SAL_CALL ErrorBar::setParent(
455 : const uno::Reference< uno::XInterface >& Parent )
456 : throw (lang::NoSupportException,
457 : uno::RuntimeException)
458 : {
459 0 : m_xParent.set( Parent );
460 0 : }
461 :
462 0 : uno::Sequence< OUString > ErrorBar::getSupportedServiceNames_Static()
463 : {
464 0 : uno::Sequence< OUString > aServices( 2 );
465 0 : aServices[ 0 ] = lcl_aServiceName;
466 0 : aServices[ 1 ] = "com.sun.star.chart2.ErrorBar";
467 0 : return aServices;
468 : }
469 :
470 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
471 0 : APPHELPER_XSERVICEINFO_IMPL( ErrorBar, lcl_aServiceName );
472 :
473 : // needed by MSC compiler
474 : using impl::ErrorBar_Base;
475 :
476 0 : } // namespace chart
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|