tdf#155526 Remove Insert R2 in all cases

When opening a file containing a Moving average trend line,
addRegressionCurve is not used, then MayHaveCorrelationCoefficient property is not
correctly set.
This change modify this property in all cases.
Update property in firePropertyChangeEvent() as it is not possible in
constructor: JunitTest_chart2_unoapi fails in MeanValue as SolarMutex
is not owned

Add QA test

Change-Id: I13bdb81239a7362431edcf28bfc38ac4820a7776
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153859
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 6fc12ad600a1ee10253c269a5988907a2894be76)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155173
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/chart2/qa/extras/chart2export.cxx b/chart2/qa/extras/chart2export.cxx
index 6e9a18e..2f5f030 100644
--- a/chart2/qa/extras/chart2export.cxx
+++ b/chart2/qa/extras/chart2export.cxx
@@ -52,7 +52,7 @@ void checkCommonTrendline(
        Reference<chart2::XRegressionCurve> const & xCurve,
        double aExpectedExtrapolateForward, double aExpectedExtrapolateBackward,
        bool aExpectedForceIntercept, double aExpectedInterceptValue,
        bool aExpectedShowEquation, bool aExpectedR2)
        bool aExpectedShowEquation, bool aExpectedR2, bool aExpectedMayHaveR2)
{
    Reference<XPropertySet> xProperties( xCurve , uno::UNO_QUERY );
    CPPUNIT_ASSERT(xProperties.is());
@@ -86,6 +86,10 @@ void checkCommonTrendline(
    bool bShowCorrelationCoefficient = false;
    CPPUNIT_ASSERT(xEquationProperties->getPropertyValue("ShowCorrelationCoefficient") >>= bShowCorrelationCoefficient);
    CPPUNIT_ASSERT_EQUAL(aExpectedR2, bShowCorrelationCoefficient);

    bool bMayHaveR2 = false;
    CPPUNIT_ASSERT(xEquationProperties->getPropertyValue("MayHaveCorrelationCoefficient") >>= bMayHaveR2);
    CPPUNIT_ASSERT_EQUAL(aExpectedMayHaveR2, bMayHaveR2);
}

void checkNameAndType(Reference<XPropertySet> const & xProperties, const OUString& aExpectedName, const OUString& aExpectedServiceName)
@@ -115,7 +119,7 @@ void checkLinearTrendline(
        xCurve,
        aExpectedExtrapolateForward, aExpectedExtrapolateBackward,
        /*aExpectedForceIntercept*/false, aExpectedInterceptValue,
        /*aExpectedShowEquation*/true, /*aExpectedR2*/false);
        /*aExpectedShowEquation*/true, /*aExpectedR2*/false, /*aExpectedMayHaveR2*/true);
}

void checkPolynomialTrendline(
@@ -137,7 +141,7 @@ void checkPolynomialTrendline(
        xCurve,
        aExpectedExtrapolateForward, aExpectedExtrapolateBackward,
        /*aExpectedForceIntercept*/true, aExpectedInterceptValue,
        /*aExpectedShowEquation*/true, /*aExpectedR2*/true);
        /*aExpectedShowEquation*/true, /*aExpectedR2*/true, /*aExpectedMayHaveR2*/true);
}

void checkMovingAverageTrendline(
@@ -151,6 +155,12 @@ void checkMovingAverageTrendline(
    sal_Int32 aPeriod = 2;
    CPPUNIT_ASSERT(xProperties->getPropertyValue("MovingAveragePeriod") >>= aPeriod);
    CPPUNIT_ASSERT_EQUAL(aExpectedPeriod, aPeriod);

    checkCommonTrendline(
        xCurve,
        /*aExpectedExtrapolateForward*/0.0, /*aExpectedExtrapolateBackward*/0.0,
        /*aExpectedForceIntercept*/false, /*aExpectedInterceptValue*/0.0,
        /*aExpectedShowEquation*/false, /*aExpectedR2*/false, /*aExpectedMayHaveR2*/false);
}

void checkTrendlinesInChart(uno::Reference< chart2::XChartDocument > const & xChartDoc)
diff --git a/chart2/source/inc/RegressionCurveModel.hxx b/chart2/source/inc/RegressionCurveModel.hxx
index 89955f1..ddd75a8 100644
--- a/chart2/source/inc/RegressionCurveModel.hxx
+++ b/chart2/source/inc/RegressionCurveModel.hxx
@@ -119,6 +119,7 @@ private:

    rtl::Reference<ModifyEventForwarder> m_xModifyEventForwarder;
    css::uno::Reference< css::beans::XPropertySet > m_xEquationProperties;
    void setPropertyMayHaveR2();
};

// implementations for factory instantiation
diff --git a/chart2/source/tools/RegressionCurveModel.cxx b/chart2/source/tools/RegressionCurveModel.cxx
index 5c1082e..06be003 100644
--- a/chart2/source/tools/RegressionCurveModel.cxx
+++ b/chart2/source/tools/RegressionCurveModel.cxx
@@ -183,12 +183,20 @@ void SAL_CALL RegressionCurveModel::setEquationProperties( const uno::Reference<
            ModifyListenerHelper::removeListener( m_xEquationProperties, m_xModifyEventForwarder );

        m_xEquationProperties.set( xEquationProperties );
        m_xEquationProperties->setPropertyValue( "MayHaveCorrelationCoefficient", uno::Any( m_eRegressionCurveType != CURVE_TYPE_MOVING_AVERAGE ) );
        setPropertyMayHaveR2();
        ModifyListenerHelper::addListener( m_xEquationProperties, m_xModifyEventForwarder );
        fireModifyEvent();
    }
}

void RegressionCurveModel::setPropertyMayHaveR2()
{
    if( m_xEquationProperties.is()) {
        bool bMayHaveR2 = m_eRegressionCurveType != CURVE_TYPE_MOVING_AVERAGE;
        m_xEquationProperties->setPropertyValue( "MayHaveCorrelationCoefficient", uno::Any( bMayHaveR2 ) );
    }
}

// ____ XServiceName ____
OUString SAL_CALL RegressionCurveModel::getServiceName()
{
@@ -239,6 +247,7 @@ void SAL_CALL RegressionCurveModel::disposing( const lang::EventObject& /* Sourc
// ____ OPropertySet ____
void RegressionCurveModel::firePropertyChangeEvent()
{
    setPropertyMayHaveR2();
    fireModifyEvent();
}