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