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_charttools.hxx" 26 #include "RegressionCurveModel.hxx" 27 #include "macros.hxx" 28 #include "LineProperties.hxx" 29 #include "RegressionCurveHelper.hxx" 30 #include "RegressionCalculationHelper.hxx" 31 #include "RegressionEquation.hxx" 32 #include "ContainerHelper.hxx" 33 #include "CloneHelper.hxx" 34 #include "PropertyHelper.hxx" 35 #include <com/sun/star/beans/PropertyAttribute.hpp> 36 #include <rtl/math.hxx> 37 #include <rtl/ustrbuf.hxx> 38 39 using namespace ::com::sun::star; 40 41 using ::rtl::OUString; 42 using ::rtl::OUStringBuffer; 43 using ::com::sun::star::beans::Property; 44 using ::osl::MutexGuard; 45 46 namespace 47 { 48 static const OUString lcl_aImplementationName_MeanValue( 49 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.MeanValueRegressionCurve" )); 50 static const OUString lcl_aImplementationName_Linear( 51 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LinearRegressionCurve" )); 52 static const OUString lcl_aImplementationName_Logarithmic( 53 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LogarithmicRegressionCurve" )); 54 static const OUString lcl_aImplementationName_Exponential( 55 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ExponentialRegressionCurve" )); 56 static const OUString lcl_aImplementationName_Potential( 57 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.PotentialRegressionCurve" )); 58 59 static const OUString lcl_aServiceName( 60 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.RegressionCurve" )); 61 62 struct StaticXXXDefaults_Initializer 63 { 64 ::chart::tPropertyValueMap* operator()() 65 { 66 static ::chart::tPropertyValueMap aStaticDefaults; 67 lcl_AddDefaultsToMap( aStaticDefaults ); 68 return &aStaticDefaults; 69 } 70 private: 71 void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap ) 72 { 73 ::chart::LineProperties::AddDefaultsToMap( rOutMap ); 74 } 75 }; 76 77 struct StaticXXXDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticXXXDefaults_Initializer > 78 { 79 }; 80 81 struct StaticRegressionCurveInfoHelper_Initializer 82 { 83 ::cppu::OPropertyArrayHelper* operator()() 84 { 85 static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() ); 86 return &aPropHelper; 87 } 88 89 private: 90 uno::Sequence< Property > lcl_GetPropertySequence() 91 { 92 ::std::vector< ::com::sun::star::beans::Property > aProperties; 93 ::chart::LineProperties::AddPropertiesToVector( aProperties ); 94 95 ::std::sort( aProperties.begin(), aProperties.end(), 96 ::chart::PropertyNameLess() ); 97 98 return ::chart::ContainerHelper::ContainerToSequence( aProperties ); 99 } 100 }; 101 102 struct StaticRegressionCurveInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticRegressionCurveInfoHelper_Initializer > 103 { 104 }; 105 106 struct StaticRegressionCurveInfo_Initializer 107 { 108 uno::Reference< beans::XPropertySetInfo >* operator()() 109 { 110 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo( 111 ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticRegressionCurveInfoHelper::get() ) ); 112 return &xPropertySetInfo; 113 } 114 }; 115 116 struct StaticRegressionCurveInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticRegressionCurveInfo_Initializer > 117 { 118 }; 119 120 } // anonymous namespace 121 122 namespace chart 123 { 124 125 RegressionCurveModel::RegressionCurveModel( 126 uno::Reference< uno::XComponentContext > const & xContext, 127 tCurveType eCurveType ) : 128 ::property::OPropertySet( m_aMutex ), 129 m_xContext( xContext ), 130 m_eRegressionCurveType( eCurveType ), 131 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()), 132 m_xEquationProperties( new RegressionEquation( xContext )) 133 { 134 // set 0 line width (default) hard, so that it is always written to XML, 135 // because the old implementation uses different defaults 136 setFastPropertyValue_NoBroadcast( 137 LineProperties::PROP_LINE_WIDTH, uno::makeAny( sal_Int32( 0 ))); 138 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder ); 139 } 140 141 RegressionCurveModel::RegressionCurveModel( const RegressionCurveModel & rOther ) : 142 MutexContainer(), 143 impl::RegressionCurveModel_Base(), 144 ::property::OPropertySet( rOther, m_aMutex ), 145 m_xContext( rOther.m_xContext ), 146 m_eRegressionCurveType( rOther.m_eRegressionCurveType ), 147 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder()) 148 { 149 m_xEquationProperties.set( CloneHelper::CreateRefClone< uno::Reference< beans::XPropertySet > >()( rOther.m_xEquationProperties )); 150 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder ); 151 } 152 153 RegressionCurveModel::~RegressionCurveModel() 154 {} 155 156 // ____ XRegressionCurve ____ 157 uno::Reference< chart2::XRegressionCurveCalculator > SAL_CALL 158 RegressionCurveModel::getCalculator() 159 { 160 return RegressionCurveHelper::createRegressionCurveCalculatorByServiceName( getServiceName()); 161 } 162 163 uno::Reference< beans::XPropertySet > SAL_CALL RegressionCurveModel::getEquationProperties() 164 { 165 return m_xEquationProperties; 166 } 167 168 void SAL_CALL RegressionCurveModel::setEquationProperties( const uno::Reference< beans::XPropertySet >& xEquationProperties ) 169 { 170 if( xEquationProperties.is()) 171 { 172 if( m_xEquationProperties.is()) 173 ModifyListenerHelper::removeListener( m_xEquationProperties, m_xModifyEventForwarder ); 174 175 m_xEquationProperties.set( xEquationProperties ); 176 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder ); 177 fireModifyEvent(); 178 } 179 } 180 181 // ____ XServiceName ____ 182 ::rtl::OUString SAL_CALL RegressionCurveModel::getServiceName() 183 { 184 switch( m_eRegressionCurveType ) 185 { 186 case CURVE_TYPE_MEAN_VALUE: 187 return C2U( "com.sun.star.chart2.MeanValueRegressionCurve" ); 188 case CURVE_TYPE_LINEAR: 189 return C2U( "com.sun.star.chart2.LinearRegressionCurve" ); 190 case CURVE_TYPE_LOGARITHM: 191 return C2U( "com.sun.star.chart2.LogarithmicRegressionCurve" ); 192 case CURVE_TYPE_EXPONENTIAL: 193 return C2U( "com.sun.star.chart2.ExponentialRegressionCurve" ); 194 case CURVE_TYPE_POWER: 195 return C2U( "com.sun.star.chart2.PotentialRegressionCurve" ); 196 } 197 198 return ::rtl::OUString(); 199 } 200 201 // ____ XModifyBroadcaster ____ 202 void SAL_CALL RegressionCurveModel::addModifyListener( const uno::Reference< util::XModifyListener >& aListener ) 203 { 204 try 205 { 206 uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW ); 207 xBroadcaster->addModifyListener( aListener ); 208 } 209 catch( const uno::Exception & ex ) 210 { 211 ASSERT_EXCEPTION( ex ); 212 } 213 } 214 215 void SAL_CALL RegressionCurveModel::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener ) 216 { 217 try 218 { 219 uno::Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW ); 220 xBroadcaster->removeModifyListener( aListener ); 221 } 222 catch( const uno::Exception & ex ) 223 { 224 ASSERT_EXCEPTION( ex ); 225 } 226 } 227 228 // ____ XModifyListener ____ 229 void SAL_CALL RegressionCurveModel::modified( const lang::EventObject& aEvent ) 230 { 231 m_xModifyEventForwarder->modified( aEvent ); 232 } 233 234 // ____ XEventListener (base of XModifyListener) ____ 235 void SAL_CALL RegressionCurveModel::disposing( const lang::EventObject& /* Source */ ) 236 { 237 // nothing 238 } 239 240 // ____ OPropertySet ____ 241 void RegressionCurveModel::firePropertyChangeEvent() 242 { 243 fireModifyEvent(); 244 } 245 246 void RegressionCurveModel::fireModifyEvent() 247 { 248 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this ))); 249 } 250 251 // ================================================================================ 252 253 // ____ OPropertySet ____ 254 uno::Any RegressionCurveModel::GetDefaultValue( sal_Int32 nHandle ) const 255 { 256 const tPropertyValueMap& rStaticDefaults = *StaticXXXDefaults::get(); 257 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) ); 258 if( aFound == rStaticDefaults.end() ) 259 return uno::Any(); 260 return (*aFound).second; 261 } 262 263 ::cppu::IPropertyArrayHelper & SAL_CALL RegressionCurveModel::getInfoHelper() 264 { 265 return *StaticRegressionCurveInfoHelper::get(); 266 } 267 268 // ____ XPropertySet ____ 269 uno::Reference< beans::XPropertySetInfo > SAL_CALL RegressionCurveModel::getPropertySetInfo() 270 { 271 return *StaticRegressionCurveInfo::get(); 272 } 273 274 // ================================================================================ 275 276 // needed by MSC compiler 277 using impl::RegressionCurveModel_Base; 278 279 IMPLEMENT_FORWARD_XINTERFACE2( RegressionCurveModel, RegressionCurveModel_Base, OPropertySet ) 280 IMPLEMENT_FORWARD_XTYPEPROVIDER2( RegressionCurveModel, RegressionCurveModel_Base, OPropertySet ) 281 282 283 284 // implementations 285 286 // -------------------------------------------------------------------------------- 287 288 MeanValueRegressionCurve::MeanValueRegressionCurve( 289 const uno::Reference< uno::XComponentContext > & xContext ) 290 : RegressionCurveModel( xContext, RegressionCurveModel::CURVE_TYPE_MEAN_VALUE ) 291 {} 292 MeanValueRegressionCurve::MeanValueRegressionCurve( 293 const MeanValueRegressionCurve & rOther ) : 294 RegressionCurveModel( rOther ) 295 {} 296 MeanValueRegressionCurve::~MeanValueRegressionCurve() 297 {} 298 uno::Sequence< ::rtl::OUString > MeanValueRegressionCurve::getSupportedServiceNames_Static() 299 { 300 uno::Sequence< ::rtl::OUString > aServices( 2 ); 301 aServices[ 0 ] = lcl_aServiceName; 302 aServices[ 1 ] = C2U( "com.sun.star.chart2.MeanValueRegressionCurve" ); 303 return aServices; 304 } 305 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static 306 APPHELPER_XSERVICEINFO_IMPL( MeanValueRegressionCurve, lcl_aImplementationName_MeanValue ); 307 308 uno::Reference< util::XCloneable > SAL_CALL MeanValueRegressionCurve::createClone() 309 { 310 return uno::Reference< util::XCloneable >( new MeanValueRegressionCurve( *this )); 311 } 312 313 // -------------------------------------------------------------------------------- 314 315 LinearRegressionCurve::LinearRegressionCurve( 316 const uno::Reference< uno::XComponentContext > & xContext ) 317 : RegressionCurveModel( xContext, RegressionCurveModel::CURVE_TYPE_LINEAR ) 318 {} 319 LinearRegressionCurve::LinearRegressionCurve( 320 const LinearRegressionCurve & rOther ) : 321 RegressionCurveModel( rOther ) 322 {} 323 LinearRegressionCurve::~LinearRegressionCurve() 324 {} 325 uno::Sequence< ::rtl::OUString > LinearRegressionCurve::getSupportedServiceNames_Static() 326 { 327 uno::Sequence< ::rtl::OUString > aServices( 2 ); 328 aServices[ 0 ] = lcl_aServiceName; 329 aServices[ 1 ] = C2U( "com.sun.star.chart2.LinearRegressionCurve" ); 330 return aServices; 331 } 332 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static 333 APPHELPER_XSERVICEINFO_IMPL( LinearRegressionCurve, lcl_aImplementationName_Linear ); 334 335 uno::Reference< util::XCloneable > SAL_CALL LinearRegressionCurve::createClone() 336 { 337 return uno::Reference< util::XCloneable >( new LinearRegressionCurve( *this )); 338 } 339 340 // -------------------------------------------------------------------------------- 341 342 LogarithmicRegressionCurve::LogarithmicRegressionCurve( 343 const uno::Reference< uno::XComponentContext > & xContext ) 344 : RegressionCurveModel( xContext, RegressionCurveModel::CURVE_TYPE_LOGARITHM ) 345 {} 346 LogarithmicRegressionCurve::LogarithmicRegressionCurve( 347 const LogarithmicRegressionCurve & rOther ) : 348 RegressionCurveModel( rOther ) 349 {} 350 LogarithmicRegressionCurve::~LogarithmicRegressionCurve() 351 {} 352 uno::Sequence< ::rtl::OUString > LogarithmicRegressionCurve::getSupportedServiceNames_Static() 353 { 354 uno::Sequence< ::rtl::OUString > aServices( 2 ); 355 aServices[ 0 ] = lcl_aServiceName; 356 aServices[ 1 ] = C2U( "com.sun.star.chart2.LogarithmicRegressionCurve" ); 357 return aServices; 358 } 359 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static 360 APPHELPER_XSERVICEINFO_IMPL( LogarithmicRegressionCurve, lcl_aImplementationName_Logarithmic ); 361 362 uno::Reference< util::XCloneable > SAL_CALL LogarithmicRegressionCurve::createClone() 363 { 364 return uno::Reference< util::XCloneable >( new LogarithmicRegressionCurve( *this )); 365 } 366 367 // -------------------------------------------------------------------------------- 368 369 ExponentialRegressionCurve::ExponentialRegressionCurve( 370 const uno::Reference< uno::XComponentContext > & xContext ) 371 : RegressionCurveModel( xContext, RegressionCurveModel::CURVE_TYPE_EXPONENTIAL ) 372 {} 373 ExponentialRegressionCurve::ExponentialRegressionCurve( 374 const ExponentialRegressionCurve & rOther ) : 375 RegressionCurveModel( rOther ) 376 {} 377 ExponentialRegressionCurve::~ExponentialRegressionCurve() 378 {} 379 uno::Sequence< ::rtl::OUString > ExponentialRegressionCurve::getSupportedServiceNames_Static() 380 { 381 uno::Sequence< ::rtl::OUString > aServices( 2 ); 382 aServices[ 0 ] = lcl_aServiceName; 383 aServices[ 1 ] = C2U( "com.sun.star.chart2.ExponentialRegressionCurve" ); 384 return aServices; 385 } 386 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static 387 APPHELPER_XSERVICEINFO_IMPL( ExponentialRegressionCurve, lcl_aImplementationName_Exponential ); 388 389 uno::Reference< util::XCloneable > SAL_CALL ExponentialRegressionCurve::createClone() 390 { 391 return uno::Reference< util::XCloneable >( new ExponentialRegressionCurve( *this )); 392 } 393 394 // -------------------------------------------------------------------------------- 395 396 PotentialRegressionCurve::PotentialRegressionCurve( 397 const uno::Reference< uno::XComponentContext > & xContext ) 398 : RegressionCurveModel( xContext, RegressionCurveModel::CURVE_TYPE_POWER ) 399 {} 400 PotentialRegressionCurve::PotentialRegressionCurve( 401 const PotentialRegressionCurve & rOther ) : 402 RegressionCurveModel( rOther ) 403 {} 404 PotentialRegressionCurve::~PotentialRegressionCurve() 405 {} 406 uno::Sequence< ::rtl::OUString > PotentialRegressionCurve::getSupportedServiceNames_Static() 407 { 408 uno::Sequence< ::rtl::OUString > aServices( 2 ); 409 aServices[ 0 ] = lcl_aServiceName; 410 aServices[ 1 ] = C2U( "com.sun.star.chart2.PotentialRegressionCurve" ); 411 return aServices; 412 } 413 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static 414 APPHELPER_XSERVICEINFO_IMPL( PotentialRegressionCurve, lcl_aImplementationName_Potential ); 415 416 uno::Reference< util::XCloneable > SAL_CALL PotentialRegressionCurve::createClone() 417 { 418 return uno::Reference< util::XCloneable >( new PotentialRegressionCurve( *this )); 419 } 420 421 422 } // namespace chart 423