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