xref: /trunk/main/chart2/source/tools/ErrorBar.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "ErrorBar.hxx"
31 #include "macros.hxx"
32 #include "LineProperties.hxx"
33 #include "ContainerHelper.hxx"
34 #include "EventListenerHelper.hxx"
35 #include "PropertyHelper.hxx"
36 #include "CloneHelper.hxx"
37 
38 #include <com/sun/star/beans/PropertyAttribute.hpp>
39 #include <com/sun/star/chart/ErrorBarStyle.hpp>
40 
41 #include <rtl/math.hxx>
42 #include <rtl/ustrbuf.hxx>
43 
44 using namespace ::com::sun::star;
45 
46 using ::rtl::OUString;
47 using ::rtl::OUStringBuffer;
48 using ::com::sun::star::beans::Property;
49 using ::osl::MutexGuard;
50 
51 namespace
52 {
53 static const OUString lcl_aServiceName(
54     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ErrorBar" ));
55 
56 enum
57 {
58     PROP_ERROR_BAR_STYLE,
59     PROP_ERROR_BAR_POS_ERROR,
60     PROP_ERROR_BAR_NEG_ERROR,
61     PROP_ERROR_BAR_WEIGHT,
62     PROP_ERROR_BAR_SHOW_POS_ERROR,
63     PROP_ERROR_BAR_SHOW_NEG_ERROR
64 };
65 
66 void lcl_AddPropertiesToVector(
67     ::std::vector< Property > & rOutProperties )
68 {
69     rOutProperties.push_back(
70         Property( C2U( "ErrorBarStyle" ),
71                   PROP_ERROR_BAR_STYLE,
72                   ::getCppuType( reinterpret_cast< sal_Int32 * >(0)),
73                   beans::PropertyAttribute::BOUND
74                   | beans::PropertyAttribute::MAYBEDEFAULT ));
75 
76     rOutProperties.push_back(
77         Property( C2U( "PositiveError" ),
78                   PROP_ERROR_BAR_POS_ERROR,
79                   ::getCppuType( reinterpret_cast< const double * >(0)),
80                   beans::PropertyAttribute::BOUND
81                   | beans::PropertyAttribute::MAYBEDEFAULT ));
82     rOutProperties.push_back(
83         Property( C2U( "NegativeError" ),
84                   PROP_ERROR_BAR_NEG_ERROR,
85                   ::getCppuType( reinterpret_cast< const double * >(0)),
86                   beans::PropertyAttribute::BOUND
87                   | beans::PropertyAttribute::MAYBEDEFAULT ));
88 
89     rOutProperties.push_back(
90         Property( C2U( "Weight" ),
91                   PROP_ERROR_BAR_WEIGHT,
92                   ::getCppuType( reinterpret_cast< const double * >(0)),
93                   beans::PropertyAttribute::BOUND
94                   | beans::PropertyAttribute::MAYBEDEFAULT ));
95 
96     rOutProperties.push_back(
97         Property( C2U( "ShowPositiveError" ),
98                   PROP_ERROR_BAR_SHOW_POS_ERROR,
99                   ::getBooleanCppuType(),
100                   beans::PropertyAttribute::BOUND
101                   | beans::PropertyAttribute::MAYBEDEFAULT ));
102     rOutProperties.push_back(
103         Property( C2U( "ShowNegativeError" ),
104                   PROP_ERROR_BAR_SHOW_NEG_ERROR,
105                   ::getBooleanCppuType(),
106                   beans::PropertyAttribute::BOUND
107                   | beans::PropertyAttribute::MAYBEDEFAULT ));
108 }
109 
110 struct StaticErrorBarDefaults_Initializer
111 {
112     ::chart::tPropertyValueMap* operator()()
113     {
114         static ::chart::tPropertyValueMap aStaticDefaults;
115         lcl_AddDefaultsToMap( aStaticDefaults );
116         return &aStaticDefaults;
117     }
118 private:
119     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
120     {
121         ::chart::LineProperties::AddDefaultsToMap( rOutMap );
122 
123         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_ERROR_BAR_STYLE, ::com::sun::star::chart::ErrorBarStyle::NONE );
124         ::chart::PropertyHelper::setPropertyValueDefault< double >( rOutMap, PROP_ERROR_BAR_POS_ERROR, 0.0 );
125         ::chart::PropertyHelper::setPropertyValueDefault< double >( rOutMap, PROP_ERROR_BAR_NEG_ERROR, 0.0 );
126         ::chart::PropertyHelper::setPropertyValueDefault< double >( rOutMap, PROP_ERROR_BAR_WEIGHT, 1.0 );
127         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_ERROR_BAR_SHOW_POS_ERROR, true );
128         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_ERROR_BAR_SHOW_NEG_ERROR, true );
129     }
130 };
131 
132 struct StaticErrorBarDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticErrorBarDefaults_Initializer >
133 {
134 };
135 
136 struct StaticErrorBarInfoHelper_Initializer
137 {
138     ::cppu::OPropertyArrayHelper* operator()()
139     {
140         static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
141         return &aPropHelper;
142     }
143 
144 private:
145     uno::Sequence< Property > lcl_GetPropertySequence()
146     {
147         ::std::vector< ::com::sun::star::beans::Property > aProperties;
148         lcl_AddPropertiesToVector( aProperties );
149         ::chart::LineProperties::AddPropertiesToVector( aProperties );
150 
151         ::std::sort( aProperties.begin(), aProperties.end(),
152                      ::chart::PropertyNameLess() );
153 
154         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
155     }
156 
157 };
158 
159 struct StaticErrorBarInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticErrorBarInfoHelper_Initializer >
160 {
161 };
162 
163 struct StaticErrorBarInfo_Initializer
164 {
165     uno::Reference< beans::XPropertySetInfo >* operator()()
166     {
167         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
168             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticErrorBarInfoHelper::get() ) );
169         return &xPropertySetInfo;
170     }
171 };
172 
173 struct StaticErrorBarInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticErrorBarInfo_Initializer >
174 {
175 };
176 
177 bool lcl_isInternalData( const uno::Reference< chart2::data::XLabeledDataSequence > & xLSeq )
178 {
179     uno::Reference< lang::XServiceInfo > xServiceInfo( xLSeq, uno::UNO_QUERY );
180     return ( xServiceInfo.is() && xServiceInfo->getImplementationName().equalsAsciiL(
181                  RTL_CONSTASCII_STRINGPARAM("com.sun.star.comp.chart2.LabeledDataSequence")));
182 }
183 
184 } // anonymous namespace
185 
186 namespace chart
187 {
188 
189 uno::Reference< beans::XPropertySet > createErrorBar( const uno::Reference< uno::XComponentContext > & xContext )
190 {
191     return new ErrorBar( xContext );
192 }
193 
194 ErrorBar::ErrorBar(
195     uno::Reference< uno::XComponentContext > const & xContext ) :
196         ::property::OPropertySet( m_aMutex ),
197     m_xContext( xContext ),
198     m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
199 {}
200 
201 ErrorBar::ErrorBar( const ErrorBar & rOther ) :
202         MutexContainer(),
203         impl::ErrorBar_Base(),
204         ::property::OPropertySet( rOther, m_aMutex ),
205     m_xContext( rOther.m_xContext ),
206     m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
207 {
208     if( ! rOther.m_aDataSequences.empty())
209     {
210         if( lcl_isInternalData( rOther.m_aDataSequences.front()))
211             CloneHelper::CloneRefVector< tDataSequenceContainer::value_type >(
212                 rOther.m_aDataSequences, m_aDataSequences );
213         else
214             m_aDataSequences = rOther.m_aDataSequences;
215         ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
216     }
217 }
218 
219 ErrorBar::~ErrorBar()
220 {}
221 
222 uno::Reference< util::XCloneable > SAL_CALL ErrorBar::createClone()
223     throw (uno::RuntimeException)
224 {
225     return uno::Reference< util::XCloneable >( new ErrorBar( *this ));
226 }
227 
228 // ================================================================================
229 
230 // ____ OPropertySet ____
231 uno::Any ErrorBar::GetDefaultValue( sal_Int32 nHandle ) const
232     throw(beans::UnknownPropertyException)
233 {
234     const tPropertyValueMap& rStaticDefaults = *StaticErrorBarDefaults::get();
235     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
236     if( aFound == rStaticDefaults.end() )
237         return uno::Any();
238     return (*aFound).second;
239 }
240 
241 ::cppu::IPropertyArrayHelper & SAL_CALL ErrorBar::getInfoHelper()
242 {
243     return *StaticErrorBarInfoHelper::get();
244 }
245 
246 // ____ XPropertySet ____
247 uno::Reference< beans::XPropertySetInfo > SAL_CALL ErrorBar::getPropertySetInfo()
248     throw (uno::RuntimeException)
249 {
250     return *StaticErrorBarInfo::get();
251 }
252 
253 // ____ XModifyBroadcaster ____
254 void SAL_CALL ErrorBar::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
255     throw (uno::RuntimeException)
256 {
257     try
258     {
259         uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
260         xBroadcaster->addModifyListener( aListener );
261     }
262     catch( const uno::Exception & ex )
263     {
264         ASSERT_EXCEPTION( ex );
265     }
266 }
267 
268 void SAL_CALL ErrorBar::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
269     throw (uno::RuntimeException)
270 {
271     try
272     {
273         uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
274         xBroadcaster->removeModifyListener( aListener );
275     }
276     catch( const uno::Exception & ex )
277     {
278         ASSERT_EXCEPTION( ex );
279     }
280 }
281 
282 // ____ XModifyListener ____
283 void SAL_CALL ErrorBar::modified( const lang::EventObject& aEvent )
284     throw (uno::RuntimeException)
285 {
286     m_xModifyEventForwarder->modified( aEvent );
287 }
288 
289 // ____ XEventListener (base of XModifyListener) ____
290 void SAL_CALL ErrorBar::disposing( const lang::EventObject& /* Source */ )
291     throw (uno::RuntimeException)
292 {
293     // nothing
294 }
295 
296 // ____ XDataSink ____
297 void SAL_CALL ErrorBar::setData( const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > >& aData )
298     throw (uno::RuntimeException)
299 {
300     ModifyListenerHelper::removeListenerFromAllElements( m_aDataSequences, m_xModifyEventForwarder );
301     EventListenerHelper::removeListenerFromAllElements( m_aDataSequences, this );
302     m_aDataSequences = ContainerHelper::SequenceToVector( aData );
303     EventListenerHelper::addListenerToAllElements( m_aDataSequences, this );
304     ModifyListenerHelper::addListenerToAllElements( m_aDataSequences, m_xModifyEventForwarder );
305 }
306 
307 // ____ XDataSource ____
308 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > SAL_CALL ErrorBar::getDataSequences()
309     throw (uno::RuntimeException)
310 {
311     return ContainerHelper::ContainerToSequence( m_aDataSequences );
312 }
313 
314 // ____ XChild ____
315 uno::Reference< uno::XInterface > SAL_CALL ErrorBar::getParent()
316     throw (uno::RuntimeException)
317 {
318     return m_xParent;
319 }
320 
321 void SAL_CALL ErrorBar::setParent(
322     const uno::Reference< uno::XInterface >& Parent )
323     throw (lang::NoSupportException,
324            uno::RuntimeException)
325 {
326     m_xParent.set( Parent );
327 }
328 
329 // ____ OPropertySet ____
330 void ErrorBar::firePropertyChangeEvent()
331 {
332     fireModifyEvent();
333 }
334 
335 void ErrorBar::fireModifyEvent()
336 {
337     m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
338 }
339 
340 // ================================================================================
341 
342 uno::Sequence< ::rtl::OUString > ErrorBar::getSupportedServiceNames_Static()
343 {
344     uno::Sequence< ::rtl::OUString > aServices( 2 );
345     aServices[ 0 ] = lcl_aServiceName;
346     aServices[ 1 ] = C2U( "com.sun.star.chart2.ErrorBar" );
347     return aServices;
348 }
349 
350 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
351 APPHELPER_XSERVICEINFO_IMPL( ErrorBar, lcl_aServiceName );
352 
353 // needed by MSC compiler
354 using impl::ErrorBar_Base;
355 
356 IMPLEMENT_FORWARD_XINTERFACE2( ErrorBar, ErrorBar_Base, OPropertySet )
357 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ErrorBar, ErrorBar_Base, OPropertySet )
358 
359 } //  namespace chart
360