Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / RegressionCurveModel.cxx
blob06be003ef1066552a6bd7672a01b96d1163797e7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <RegressionCurveModel.hxx>
21 #include <LinePropertiesHelper.hxx>
22 #include <RegressionCurveCalculator.hxx>
23 #include <RegressionCurveHelper.hxx>
24 #include "RegressionEquation.hxx"
25 #include <CloneHelper.hxx>
26 #include <PropertyHelper.hxx>
27 #include <ModifyListenerHelper.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 namespace com::sun::star::uno { class XComponentContext; }
33 using namespace ::com::sun::star;
35 using ::com::sun::star::beans::Property;
37 namespace
39 enum
41 PROPERTY_DEGREE,
42 PROPERTY_PERIOD,
43 PROPERTY_EXTRAPOLATE_FORWARD,
44 PROPERTY_EXTRAPOLATE_BACKWARD,
45 PROPERTY_FORCE_INTERCEPT,
46 PROPERTY_INTERCEPT_VALUE,
47 PROPERTY_CURVE_NAME,
48 PROPERTY_MOVING_AVERAGE_TYPE
51 void lcl_AddPropertiesToVector(
52 std::vector< Property > & rOutProperties )
54 rOutProperties.emplace_back( "PolynomialDegree",
55 PROPERTY_DEGREE,
56 cppu::UnoType<sal_Int32>::get(),
57 beans::PropertyAttribute::BOUND |
58 beans::PropertyAttribute::MAYBEDEFAULT );
60 rOutProperties.emplace_back( "MovingAveragePeriod",
61 PROPERTY_PERIOD,
62 cppu::UnoType<sal_Int32>::get(),
63 beans::PropertyAttribute::BOUND |
64 beans::PropertyAttribute::MAYBEDEFAULT );
66 rOutProperties.emplace_back( "MovingAverageType",
67 PROPERTY_MOVING_AVERAGE_TYPE,
68 cppu::UnoType<sal_Int32>::get(),
69 beans::PropertyAttribute::BOUND |
70 beans::PropertyAttribute::MAYBEDEFAULT );
72 rOutProperties.emplace_back( "ExtrapolateForward",
73 PROPERTY_EXTRAPOLATE_FORWARD,
74 cppu::UnoType<double>::get(),
75 beans::PropertyAttribute::BOUND |
76 beans::PropertyAttribute::MAYBEDEFAULT );
78 rOutProperties.emplace_back( "ExtrapolateBackward",
79 PROPERTY_EXTRAPOLATE_BACKWARD,
80 cppu::UnoType<double>::get(),
81 beans::PropertyAttribute::BOUND |
82 beans::PropertyAttribute::MAYBEDEFAULT );
84 rOutProperties.emplace_back( "ForceIntercept",
85 PROPERTY_FORCE_INTERCEPT,
86 cppu::UnoType<bool>::get(),
87 beans::PropertyAttribute::BOUND
88 | beans::PropertyAttribute::MAYBEDEFAULT );
90 rOutProperties.emplace_back( "InterceptValue",
91 PROPERTY_INTERCEPT_VALUE,
92 cppu::UnoType<double>::get(),
93 beans::PropertyAttribute::BOUND |
94 beans::PropertyAttribute::MAYBEDEFAULT );
96 rOutProperties.emplace_back( "CurveName",
97 PROPERTY_CURVE_NAME,
98 cppu::UnoType<OUString>::get(),
99 beans::PropertyAttribute::BOUND );
102 const ::chart::tPropertyValueMap & GetStaticXXXDefaults()
104 static ::chart::tPropertyValueMap aStaticDefaults =
105 [](){
106 ::chart::tPropertyValueMap aTmp;
107 ::chart::LinePropertiesHelper::AddDefaultsToMap( aTmp );
108 return aTmp;
109 }();
110 return aStaticDefaults;
113 ::cppu::OPropertyArrayHelper& GetStaticRegressionCurveInfoHelper()
115 static ::cppu::OPropertyArrayHelper aPropHelper =
116 [](){
117 std::vector< css::beans::Property > aProperties;
118 lcl_AddPropertiesToVector( aProperties );
119 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
121 std::sort( aProperties.begin(), aProperties.end(),
122 ::chart::PropertyNameLess() );
124 return comphelper::containerToSequence( aProperties );
125 }();
126 return aPropHelper;
129 uno::Reference< beans::XPropertySetInfo >& GetStaticRegressionCurveInfo()
131 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
132 ::cppu::OPropertySetHelper::createPropertySetInfo(GetStaticRegressionCurveInfoHelper() ) );
133 return xPropertySetInfo;
136 } // anonymous namespace
138 namespace chart
141 RegressionCurveModel::RegressionCurveModel( tCurveType eCurveType ) :
142 m_eRegressionCurveType( eCurveType ),
143 m_xModifyEventForwarder( new ModifyEventForwarder() ),
144 m_xEquationProperties( new RegressionEquation )
146 // set 0 line width (default) hard, so that it is always written to XML,
147 // because the old implementation uses different defaults
148 setFastPropertyValue_NoBroadcast(
149 LinePropertiesHelper::PROP_LINE_WIDTH, uno::Any( sal_Int32( 0 )));
150 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder );
153 RegressionCurveModel::RegressionCurveModel( const RegressionCurveModel & rOther ) :
154 impl::RegressionCurveModel_Base(rOther),
155 ::property::OPropertySet( rOther ),
156 m_eRegressionCurveType( rOther.m_eRegressionCurveType ),
157 m_xModifyEventForwarder( new ModifyEventForwarder() )
159 m_xEquationProperties.set( CloneHelper::CreateRefClone< beans::XPropertySet >()( rOther.m_xEquationProperties ));
160 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder );
163 RegressionCurveModel::~RegressionCurveModel()
166 // ____ XRegressionCurve ____
167 uno::Reference< chart2::XRegressionCurveCalculator > SAL_CALL
168 RegressionCurveModel::getCalculator()
170 return RegressionCurveHelper::createRegressionCurveCalculatorByServiceName( getServiceName());
173 uno::Reference< beans::XPropertySet > SAL_CALL RegressionCurveModel::getEquationProperties()
175 return m_xEquationProperties;
178 void SAL_CALL RegressionCurveModel::setEquationProperties( const uno::Reference< beans::XPropertySet >& xEquationProperties )
180 if( xEquationProperties.is())
182 if( m_xEquationProperties.is())
183 ModifyListenerHelper::removeListener( m_xEquationProperties, m_xModifyEventForwarder );
185 m_xEquationProperties.set( xEquationProperties );
186 setPropertyMayHaveR2();
187 ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder );
188 fireModifyEvent();
192 void RegressionCurveModel::setPropertyMayHaveR2()
194 if( m_xEquationProperties.is()) {
195 bool bMayHaveR2 = m_eRegressionCurveType != CURVE_TYPE_MOVING_AVERAGE;
196 m_xEquationProperties->setPropertyValue( "MayHaveCorrelationCoefficient", uno::Any( bMayHaveR2 ) );
200 // ____ XServiceName ____
201 OUString SAL_CALL RegressionCurveModel::getServiceName()
203 switch( m_eRegressionCurveType )
205 case CURVE_TYPE_MEAN_VALUE:
206 return "com.sun.star.chart2.MeanValueRegressionCurve";
207 case CURVE_TYPE_LINEAR:
208 return "com.sun.star.chart2.LinearRegressionCurve";
209 case CURVE_TYPE_LOGARITHM:
210 return "com.sun.star.chart2.LogarithmicRegressionCurve";
211 case CURVE_TYPE_EXPONENTIAL:
212 return "com.sun.star.chart2.ExponentialRegressionCurve";
213 case CURVE_TYPE_POWER:
214 return "com.sun.star.chart2.PotentialRegressionCurve";
215 case CURVE_TYPE_POLYNOMIAL:
216 return "com.sun.star.chart2.PolynomialRegressionCurve";
217 case CURVE_TYPE_MOVING_AVERAGE:
218 return "com.sun.star.chart2.MovingAverageRegressionCurve";
221 return OUString();
224 // ____ XModifyBroadcaster ____
225 void SAL_CALL RegressionCurveModel::addModifyListener( const uno::Reference< util::XModifyListener >& aListener )
227 m_xModifyEventForwarder->addModifyListener( aListener );
230 void SAL_CALL RegressionCurveModel::removeModifyListener( const uno::Reference< util::XModifyListener >& aListener )
232 m_xModifyEventForwarder->removeModifyListener( aListener );
235 // ____ XModifyListener ____
236 void SAL_CALL RegressionCurveModel::modified( const lang::EventObject& aEvent )
238 m_xModifyEventForwarder->modified( aEvent );
241 // ____ XEventListener (base of XModifyListener) ____
242 void SAL_CALL RegressionCurveModel::disposing( const lang::EventObject& /* Source */ )
244 // nothing
247 // ____ OPropertySet ____
248 void RegressionCurveModel::firePropertyChangeEvent()
250 setPropertyMayHaveR2();
251 fireModifyEvent();
254 void RegressionCurveModel::fireModifyEvent()
256 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
259 // ____ OPropertySet ____
260 void RegressionCurveModel::GetDefaultValue( sal_Int32 nHandle, uno::Any& rAny ) const
262 const tPropertyValueMap& rStaticDefaults = GetStaticXXXDefaults();
263 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
264 if( aFound == rStaticDefaults.end() )
265 rAny.clear();
266 else
267 rAny = (*aFound).second;
270 ::cppu::IPropertyArrayHelper & SAL_CALL RegressionCurveModel::getInfoHelper()
272 return GetStaticRegressionCurveInfoHelper();
275 // ____ XPropertySet ____
276 uno::Reference< beans::XPropertySetInfo > SAL_CALL RegressionCurveModel::getPropertySetInfo()
278 return GetStaticRegressionCurveInfo();
281 // needed by MSC compiler
282 using impl::RegressionCurveModel_Base;
284 IMPLEMENT_FORWARD_XINTERFACE2( RegressionCurveModel, RegressionCurveModel_Base, OPropertySet )
285 IMPLEMENT_FORWARD_XTYPEPROVIDER2( RegressionCurveModel, RegressionCurveModel_Base, OPropertySet )
287 // implementations
289 MeanValueRegressionCurve::MeanValueRegressionCurve()
290 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_MEAN_VALUE )
292 MeanValueRegressionCurve::MeanValueRegressionCurve(
293 const MeanValueRegressionCurve & rOther ) :
294 RegressionCurveModel( rOther )
296 MeanValueRegressionCurve::~MeanValueRegressionCurve()
299 OUString SAL_CALL MeanValueRegressionCurve::getImplementationName()
301 return "com.sun.star.comp.chart2.MeanValueRegressionCurve";
304 sal_Bool SAL_CALL MeanValueRegressionCurve::supportsService( const OUString& rServiceName )
306 return cppu::supportsService(this, rServiceName);
309 css::uno::Sequence< OUString > SAL_CALL MeanValueRegressionCurve::getSupportedServiceNames()
311 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.MeanValueRegressionCurve" };
314 uno::Reference< util::XCloneable > SAL_CALL MeanValueRegressionCurve::createClone()
316 return uno::Reference< util::XCloneable >( new MeanValueRegressionCurve( *this ));
319 LinearRegressionCurve::LinearRegressionCurve()
320 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_LINEAR )
322 LinearRegressionCurve::LinearRegressionCurve(
323 const LinearRegressionCurve & rOther ) :
324 RegressionCurveModel( rOther )
326 LinearRegressionCurve::~LinearRegressionCurve()
329 OUString SAL_CALL LinearRegressionCurve::getImplementationName()
331 return "com.sun.star.comp.chart2.LinearRegressionCurve";
334 sal_Bool SAL_CALL LinearRegressionCurve::supportsService( const OUString& rServiceName )
336 return cppu::supportsService(this, rServiceName);
339 css::uno::Sequence< OUString > SAL_CALL LinearRegressionCurve::getSupportedServiceNames()
341 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.LinearRegressionCurve" };
344 uno::Reference< util::XCloneable > SAL_CALL LinearRegressionCurve::createClone()
346 return uno::Reference< util::XCloneable >( new LinearRegressionCurve( *this ));
349 LogarithmicRegressionCurve::LogarithmicRegressionCurve()
350 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_LOGARITHM )
352 LogarithmicRegressionCurve::LogarithmicRegressionCurve(
353 const LogarithmicRegressionCurve & rOther ) :
354 RegressionCurveModel( rOther )
356 LogarithmicRegressionCurve::~LogarithmicRegressionCurve()
359 OUString SAL_CALL LogarithmicRegressionCurve::getImplementationName()
361 return "com.sun.star.comp.chart2.LogarithmicRegressionCurve";
364 sal_Bool SAL_CALL LogarithmicRegressionCurve::supportsService( const OUString& rServiceName )
366 return cppu::supportsService(this, rServiceName);
369 css::uno::Sequence< OUString > SAL_CALL LogarithmicRegressionCurve::getSupportedServiceNames()
371 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.LogarithmicRegressionCurve" };
374 uno::Reference< util::XCloneable > SAL_CALL LogarithmicRegressionCurve::createClone()
376 return uno::Reference< util::XCloneable >( new LogarithmicRegressionCurve( *this ));
379 ExponentialRegressionCurve::ExponentialRegressionCurve()
380 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_EXPONENTIAL )
382 ExponentialRegressionCurve::ExponentialRegressionCurve(
383 const ExponentialRegressionCurve & rOther ) :
384 RegressionCurveModel( rOther )
386 ExponentialRegressionCurve::~ExponentialRegressionCurve()
389 OUString SAL_CALL ExponentialRegressionCurve::getImplementationName()
391 return "com.sun.star.comp.chart2.ExponentialRegressionCurve";
394 sal_Bool SAL_CALL ExponentialRegressionCurve::supportsService( const OUString& rServiceName )
396 return cppu::supportsService(this, rServiceName);
399 css::uno::Sequence< OUString > SAL_CALL ExponentialRegressionCurve::getSupportedServiceNames()
401 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.ExponentialRegressionCurve" };
404 uno::Reference< util::XCloneable > SAL_CALL ExponentialRegressionCurve::createClone()
406 return uno::Reference< util::XCloneable >( new ExponentialRegressionCurve( *this ));
409 PotentialRegressionCurve::PotentialRegressionCurve()
410 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_POWER )
412 PotentialRegressionCurve::PotentialRegressionCurve(
413 const PotentialRegressionCurve & rOther ) :
414 RegressionCurveModel( rOther )
416 PotentialRegressionCurve::~PotentialRegressionCurve()
419 OUString SAL_CALL PotentialRegressionCurve::getImplementationName()
421 return "com.sun.star.comp.chart2.PotentialRegressionCurve";
424 sal_Bool SAL_CALL PotentialRegressionCurve::supportsService( const OUString& rServiceName )
426 return cppu::supportsService(this, rServiceName);
429 css::uno::Sequence< OUString > SAL_CALL PotentialRegressionCurve::getSupportedServiceNames()
431 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.PotentialRegressionCurve" };
434 uno::Reference< util::XCloneable > SAL_CALL PotentialRegressionCurve::createClone()
436 return uno::Reference< util::XCloneable >( new PotentialRegressionCurve( *this ));
439 PolynomialRegressionCurve::PolynomialRegressionCurve()
440 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_POLYNOMIAL )
442 PolynomialRegressionCurve::PolynomialRegressionCurve(
443 const PolynomialRegressionCurve & rOther ) :
444 RegressionCurveModel( rOther )
446 PolynomialRegressionCurve::~PolynomialRegressionCurve()
449 OUString SAL_CALL PolynomialRegressionCurve::getImplementationName()
451 return "com.sun.star.comp.chart2.PolynomialRegressionCurve";
454 sal_Bool SAL_CALL PolynomialRegressionCurve::supportsService( const OUString& rServiceName )
456 return cppu::supportsService(this, rServiceName);
459 css::uno::Sequence< OUString > SAL_CALL PolynomialRegressionCurve::getSupportedServiceNames()
461 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.PolynomialRegressionCurve" };
464 uno::Reference< util::XCloneable > SAL_CALL PolynomialRegressionCurve::createClone()
466 return uno::Reference< util::XCloneable >( new PolynomialRegressionCurve( *this ));
469 MovingAverageRegressionCurve::MovingAverageRegressionCurve()
470 : RegressionCurveModel( RegressionCurveModel::CURVE_TYPE_MOVING_AVERAGE )
472 MovingAverageRegressionCurve::MovingAverageRegressionCurve(
473 const MovingAverageRegressionCurve & rOther ) :
474 RegressionCurveModel( rOther )
476 MovingAverageRegressionCurve::~MovingAverageRegressionCurve()
479 OUString SAL_CALL MovingAverageRegressionCurve::getImplementationName()
481 return "com.sun.star.comp.chart2.MovingAverageRegressionCurve";
484 sal_Bool SAL_CALL MovingAverageRegressionCurve::supportsService( const OUString& rServiceName )
486 return cppu::supportsService(this, rServiceName);
489 css::uno::Sequence< OUString > SAL_CALL MovingAverageRegressionCurve::getSupportedServiceNames()
491 return { "com.sun.star.chart2.RegressionCurve", "com.sun.star.chart2.MovingAverageRegressionCurve" };
494 uno::Reference< util::XCloneable > SAL_CALL MovingAverageRegressionCurve::createClone()
496 return uno::Reference< util::XCloneable >( new MovingAverageRegressionCurve( *this ));
499 } // namespace chart
501 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
502 com_sun_star_comp_chart2_ExponentialRegressionCurve_get_implementation(css::uno::XComponentContext *,
503 css::uno::Sequence<css::uno::Any> const &)
505 return cppu::acquire(new ::chart::ExponentialRegressionCurve );
508 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
509 com_sun_star_comp_chart2_LinearRegressionCurve_get_implementation(css::uno::XComponentContext *,
510 css::uno::Sequence<css::uno::Any> const &)
512 return cppu::acquire(new ::chart::LinearRegressionCurve );
515 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
516 com_sun_star_comp_chart2_LogarithmicRegressionCurve_get_implementation(css::uno::XComponentContext *,
517 css::uno::Sequence<css::uno::Any> const &)
519 return cppu::acquire(new ::chart::LogarithmicRegressionCurve );
522 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
523 com_sun_star_comp_chart2_MeanValueRegressionCurve_get_implementation(css::uno::XComponentContext *,
524 css::uno::Sequence<css::uno::Any> const &)
526 return cppu::acquire(new ::chart::MeanValueRegressionCurve );
529 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
530 com_sun_star_comp_chart2_PotentialRegressionCurve_get_implementation(css::uno::XComponentContext *,
531 css::uno::Sequence<css::uno::Any> const &)
533 return cppu::acquire(new ::chart::PotentialRegressionCurve );
536 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
537 com_sun_star_comp_chart2_PolynomialRegressionCurve_get_implementation(css::uno::XComponentContext *,
538 css::uno::Sequence<css::uno::Any> const &)
540 return cppu::acquire(new ::chart::PolynomialRegressionCurve );
543 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
544 com_sun_star_comp_chart2_MovingAverageRegressionCurve_get_implementation(css::uno::XComponentContext *,
545 css::uno::Sequence<css::uno::Any> const &)
547 return cppu::acquire(new ::chart::MovingAverageRegressionCurve );
551 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */