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 36 : 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 4 : const SfxItemPropertySet* GetErrorBarPropertySet()
55 : {
56 : static const SfxItemPropertyMapEntry aErrorBarPropertyMap_Impl[] =
57 : {
58 4 : {OUString("ShowPositiveError"),0,getBooleanCppuType(), 0, 0},
59 4 : {OUString("ShowNegativeError"),1,getBooleanCppuType(), 0, 0},
60 4 : {OUString("PositiveError"),2,cppu::UnoType<double>::get(),0,0},
61 4 : {OUString("NegativeError"),3,cppu::UnoType<double>::get(), 0, 0},
62 4 : {OUString("PercentageError"),4,cppu::UnoType<double>::get(), 0, 0},
63 4 : {OUString("ErrorBarStyle"),5,cppu::UnoType<sal_Int32>::get(),0,0},
64 4 : {OUString("ErrorBarRangePositive"),6,cppu::UnoType<OUString>::get(),0,0}, // read-only for export
65 4 : {OUString("ErrorBarRangeNegative"),7,cppu::UnoType<OUString>::get(),0,0}, // read-only for export
66 4 : {OUString("Weight"),8,cppu::UnoType<double>::get(),0,0},
67 4 : {OUString("LineStyle"),9,cppu::UnoType<com::sun::star::drawing::LineStyle>::get(),0,0},
68 4 : {OUString("LineDash"),10,cppu::UnoType<drawing::LineDash>::get(),0,0},
69 4 : {OUString("LineWidth"),11,cppu::UnoType<sal_Int32>::get(),0,0},
70 4 : {OUString("LineColor"),12,cppu::UnoType<com::sun::star::util::Color>::get(),0,0},
71 4 : {OUString("LineTransparence"),13,cppu::UnoType<sal_Int16>::get(),0,0},
72 4 : {OUString("LineJoint"),14,cppu::UnoType<com::sun::star::drawing::LineJoint>::get(),0,0},
73 : { OUString(), 0, css::uno::Type(), 0, 0 }
74 68 : };
75 4 : static SfxItemPropertySet aPropSet( aErrorBarPropertyMap_Impl );
76 4 : return &aPropSet;
77 : }
78 :
79 : } // anonymous namespace
80 :
81 : namespace chart
82 : {
83 :
84 36 : uno::Reference< beans::XPropertySet > createErrorBar( const uno::Reference< uno::XComponentContext > & xContext )
85 : {
86 36 : return new ErrorBar( xContext );
87 : }
88 :
89 50 : 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 50 : m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
100 50 : {}
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 64 : ErrorBar::~ErrorBar()
127 64 : {}
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 42 : uno::Reference< beans::XPropertySetInfo > SAL_CALL ErrorBar::getPropertySetInfo()
137 : throw (uno::RuntimeException, std::exception)
138 : {
139 : static uno::Reference< beans::XPropertySetInfo > aRef (
140 42 : new SfxItemPropertySetInfo( GetErrorBarPropertySet()->getPropertyMap() ) );
141 42 : return aRef;
142 : }
143 :
144 696 : 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 696 : SolarMutexGuard aGuard;
149 :
150 696 : if(rPropName == "ErrorBarStyle")
151 176 : rAny >>= meStyle;
152 520 : else if(rPropName == "PositiveError")
153 38 : rAny >>= mfPositiveError;
154 482 : else if(rPropName == "PercentageError")
155 : {
156 18 : rAny >>= mfPositiveError;
157 18 : rAny >>= mfNegativeError;
158 : }
159 464 : else if(rPropName == "Weight")
160 : {
161 36 : rAny >>= mfWeight;
162 : }
163 428 : else if(rPropName == "NegativeError")
164 38 : rAny >>= mfNegativeError;
165 390 : else if(rPropName == "ShowPositiveError")
166 92 : rAny >>= mbShowPositiveError;
167 298 : else if(rPropName == "ShowNegativeError")
168 92 : rAny >>= mbShowNegativeError;
169 206 : else if(rPropName == "ErrorBarRangePositive" || rPropName == "ErrorBarRangeNegative")
170 36 : throw beans::UnknownPropertyException("read-only property", static_cast< uno::XWeak*>(this));
171 : else
172 170 : LineProperties::setPropertyValue(rPropName, rAny);
173 :
174 660 : m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
175 660 : }
176 :
177 : namespace {
178 :
179 14 : OUString getSourceRangeStrFromLabeledSequences( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aSequences, bool bPositive )
180 : {
181 14 : const OUString aRolePrefix( "error-bars" );
182 28 : OUString aDirection;
183 14 : if(bPositive)
184 8 : aDirection = "positive";
185 : else
186 6 : aDirection = "negative";
187 :
188 20 : for( sal_Int32 nI=0; nI< aSequences.getLength(); ++nI )
189 : {
190 : try
191 : {
192 20 : if( aSequences[nI].is())
193 : {
194 20 : uno::Reference< chart2::data::XDataSequence > xSequence( aSequences[nI]->getValues());
195 26 : uno::Reference< beans::XPropertySet > xSeqProp( xSequence, uno::UNO_QUERY_THROW );
196 26 : OUString aRole;
197 60 : if( ( xSeqProp->getPropertyValue(
198 100 : OUString( "Role" )) >>= aRole ) &&
199 80 : aRole.match( aRolePrefix ) && aRole.indexOf(aDirection) >= 0 )
200 : {
201 14 : return xSequence->getSourceRangeRepresentation();
202 6 : }
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 14 : return OUString();
214 : }
215 :
216 : }
217 :
218 12033 : uno::Any ErrorBar::getPropertyValue(const OUString& rPropName)
219 : throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
220 : {
221 12033 : SolarMutexGuard aGuard;
222 :
223 12033 : uno::Any aRet;
224 12033 : if(rPropName == "ErrorBarStyle")
225 3721 : aRet <<= meStyle;
226 8312 : else if(rPropName == "PositiveError")
227 458 : aRet <<= mfPositiveError;
228 7854 : else if(rPropName == "NegativeError")
229 430 : aRet <<= mfNegativeError;
230 7424 : else if(rPropName == "PercentageError")
231 0 : aRet <<= mfPositiveError;
232 7424 : else if(rPropName == "ShowPositiveError")
233 2344 : aRet <<= mbShowPositiveError;
234 5080 : else if(rPropName == "ShowNegativeError")
235 2344 : aRet <<= mbShowNegativeError;
236 2736 : else if(rPropName == "Weight")
237 4 : aRet <<= mfWeight;
238 2732 : else if(rPropName == "ErrorBarRangePositive")
239 : {
240 8 : OUString aRange;
241 8 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA)
242 : {
243 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
244 8 : getDataSequences();
245 :
246 8 : aRange = getSourceRangeStrFromLabeledSequences( aSequences, true );
247 : }
248 :
249 8 : aRet <<= aRange;
250 : }
251 2724 : else if(rPropName == "ErrorBarRangeNegative")
252 : {
253 6 : OUString aRange;
254 6 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA)
255 : {
256 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences =
257 6 : getDataSequences();
258 :
259 6 : aRange = getSourceRangeStrFromLabeledSequences( aSequences, false );
260 : }
261 :
262 6 : aRet <<= aRange;
263 : }
264 : else
265 2718 : aRet = LineProperties::getPropertyValue(rPropName);
266 :
267 : SAL_WARN_IF(!aRet.hasValue(), "chart2", "asked for property value: " << rPropName);
268 12033 : return aRet;
269 : }
270 :
271 56 : beans::PropertyState ErrorBar::getPropertyState( const OUString& rPropName )
272 : throw (com::sun::star::beans::UnknownPropertyException, std::exception)
273 : {
274 56 : if(rPropName == "ErrorBarStyle")
275 : {
276 4 : if(meStyle == com::sun::star::chart::ErrorBarStyle::NONE)
277 0 : return beans::PropertyState_DEFAULT_VALUE;
278 4 : return beans::PropertyState_DIRECT_VALUE;
279 : }
280 52 : else if(rPropName == "PositiveError")
281 : {
282 4 : if(mbShowPositiveError)
283 : {
284 4 : 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 4 : break;
291 : }
292 : }
293 4 : return beans::PropertyState_DEFAULT_VALUE;
294 : }
295 48 : else if(rPropName == "NegativeError")
296 : {
297 4 : if(mbShowNegativeError)
298 : {
299 4 : 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 4 : break;
306 : }
307 : }
308 4 : return beans::PropertyState_DEFAULT_VALUE;
309 : }
310 44 : else if(rPropName == "PercentageError")
311 : {
312 4 : if(meStyle != com::sun::star::chart::ErrorBarStyle::RELATIVE)
313 4 : return beans::PropertyState_DEFAULT_VALUE;
314 0 : return beans::PropertyState_DIRECT_VALUE;
315 : }
316 40 : else if(rPropName == "ShowPositiveError")
317 : {
318 : // this value should be never default
319 4 : return beans::PropertyState_DIRECT_VALUE;
320 : }
321 36 : else if(rPropName == "ShowNegativeError")
322 : {
323 : // this value should be never default
324 4 : return beans::PropertyState_DIRECT_VALUE;
325 : }
326 32 : else if(rPropName == "ErrorBarRangePositive")
327 : {
328 4 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA && mbShowPositiveError)
329 4 : return beans::PropertyState_DIRECT_VALUE;
330 0 : return beans::PropertyState_DEFAULT_VALUE;
331 : }
332 28 : else if(rPropName == "ErrorBarRangeNegative")
333 : {
334 4 : if(meStyle == com::sun::star::chart::ErrorBarStyle::FROM_DATA && mbShowNegativeError)
335 4 : return beans::PropertyState_DIRECT_VALUE;
336 0 : return beans::PropertyState_DEFAULT_VALUE;
337 : }
338 : else
339 24 : return beans::PropertyState_DIRECT_VALUE;
340 : }
341 :
342 4 : uno::Sequence< beans::PropertyState > ErrorBar::getPropertyStates( const uno::Sequence< OUString >& rPropNames )
343 : throw (com::sun::star::beans::UnknownPropertyException, std::exception)
344 : {
345 4 : uno::Sequence< beans::PropertyState > aRet( rPropNames.getLength() );
346 60 : for(sal_Int32 i = 0; i < rPropNames.getLength(); ++i)
347 : {
348 56 : aRet[i] = getPropertyState(rPropNames[i]);
349 : }
350 4 : 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 50 : void SAL_CALL ErrorBar::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
388 : throw (uno::RuntimeException, std::exception)
389 : {
390 : try
391 : {
392 50 : uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
393 50 : xBroadcaster->addModifyListener( aListener );
394 : }
395 0 : catch( const uno::Exception & ex )
396 : {
397 : ASSERT_EXCEPTION( ex );
398 : }
399 50 : }
400 :
401 32 : void SAL_CALL ErrorBar::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
402 : throw (uno::RuntimeException, std::exception)
403 : {
404 : try
405 : {
406 32 : uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
407 32 : xBroadcaster->removeModifyListener( aListener );
408 : }
409 0 : catch( const uno::Exception & ex )
410 : {
411 : ASSERT_EXCEPTION( ex );
412 : }
413 32 : }
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 24 : void SAL_CALL ErrorBar::setData( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aData )
431 : throw (uno::RuntimeException, std::exception)
432 : {
433 24 : ModifyListenerHelper::removeListenerFromAllElements( m_aDataSequences, m_xModifyEventForwarder );
434 24 : EventListenerHelper::removeListenerFromAllElements( m_aDataSequences, this );
435 24 : m_aDataSequences = ContainerHelper::SequenceToVector( aData );
436 24 : EventListenerHelper::addListenerToAllElements( m_aDataSequences, this );
437 24 : ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
438 24 : }
439 :
440 : // ____ XDataSource ____
441 140 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > SAL_CALL ErrorBar::getDataSequences()
442 : throw (uno::RuntimeException, std::exception)
443 : {
444 140 : 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 4 : uno::Sequence< OUString > ErrorBar::getSupportedServiceNames_Static()
463 : {
464 4 : uno::Sequence< OUString > aServices( 2 );
465 4 : aServices[ 0 ] = lcl_aServiceName;
466 4 : aServices[ 1 ] = "com.sun.star.chart2.ErrorBar";
467 4 : return aServices;
468 : }
469 :
470 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
471 94 : APPHELPER_XSERVICEINFO_IMPL( ErrorBar, lcl_aServiceName );
472 :
473 : // needed by MSC compiler
474 : using impl::ErrorBar_Base;
475 :
476 108 : } // namespace chart
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|