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