1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <RegressionCurveHelper.hxx>
21 #include <RegressionCurveItemConverter.hxx>
22 #include "SchWhichPairs.hxx"
23 #include <GraphicPropertyItemConverter.hxx>
25 #include <com/sun/star/chart2/XRegressionCurve.hpp>
26 #include <osl/diagnose.h>
28 #include <svl/eitem.hxx>
29 #include <svl/intitem.hxx>
30 #include <svl/stritem.hxx>
32 using namespace ::com::sun::star
;
36 template <class T
, class D
>
37 bool lclConvertToPropertySet(const SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, const uno::Reference
<beans::XPropertySet
>& xProperties
, const OUString
& aPropertyID
)
39 OSL_ASSERT(xProperties
.is());
40 if( xProperties
.is() )
42 T aValue
= static_cast<T
>(static_cast<const D
&>(rItemSet
.Get( nWhichId
)).GetValue());
44 bool aSuccess
= xProperties
->getPropertyValue( aPropertyID
) >>= aOldValue
;
45 if (!aSuccess
|| aOldValue
!= aValue
)
47 xProperties
->setPropertyValue( aPropertyID
, uno::Any( aValue
));
54 template <class T
, class D
>
55 void lclConvertToItemSet(SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, const uno::Reference
<beans::XPropertySet
>& xProperties
, const OUString
& aPropertyID
)
57 OSL_ASSERT(xProperties
.is());
58 if( xProperties
.is() )
60 T aValue
= static_cast<T
>(static_cast<const D
&>(rItemSet
.Get( nWhichId
)).GetValue());
61 if(xProperties
->getPropertyValue( aPropertyID
) >>= aValue
)
63 rItemSet
.Put(D( nWhichId
, aValue
));
68 void lclConvertToItemSetDouble(SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, const uno::Reference
<beans::XPropertySet
>& xProperties
, const OUString
& aPropertyID
)
70 OSL_ASSERT(xProperties
.is());
71 if( xProperties
.is() )
73 double aValue
= static_cast<const SvxDoubleItem
&>(rItemSet
.Get( nWhichId
)).GetValue();
74 if(xProperties
->getPropertyValue( aPropertyID
) >>= aValue
)
76 rItemSet
.Put(SvxDoubleItem( aValue
, nWhichId
));
81 } // anonymous namespace
83 namespace chart::wrapper
86 RegressionCurveItemConverter::RegressionCurveItemConverter(
87 const uno::Reference
< beans::XPropertySet
>& rPropertySet
,
88 const uno::Reference
< chart2::XRegressionCurveContainer
>& xContainer
,
89 SfxItemPool
& rItemPool
,
91 const uno::Reference
< lang::XMultiServiceFactory
> & xNamedPropertyContainerFactory
) :
92 ItemConverter( rPropertySet
, rItemPool
),
93 m_spGraphicConverter( std::make_shared
<GraphicPropertyItemConverter
>(
94 rPropertySet
, rItemPool
, rDrawModel
,
95 xNamedPropertyContainerFactory
,
96 GraphicObjectType::LineProperties
)),
97 m_xCurveContainer( xContainer
)
100 RegressionCurveItemConverter::~RegressionCurveItemConverter()
103 void RegressionCurveItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
105 m_spGraphicConverter
->FillItemSet( rOutItemSet
);
108 ItemConverter::FillItemSet( rOutItemSet
);
111 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
113 bool bResult
= m_spGraphicConverter
->ApplyItemSet( rItemSet
);
116 return ItemConverter::ApplyItemSet( rItemSet
) || bResult
;
119 const sal_uInt16
* RegressionCurveItemConverter::GetWhichPairs() const
121 // must span all used items!
122 return nRegressionCurveWhichPairs
;
125 bool RegressionCurveItemConverter::GetItemProperty(
126 tWhichIdType
/* nWhichId */, tPropertyNameWithMemberId
& /* rOutProperty */ ) const
128 // No own (non-special) properties
132 bool RegressionCurveItemConverter::ApplySpecialItem(
133 sal_uInt16 nWhichId
, const SfxItemSet
& rItemSet
)
135 uno::Reference
< chart2::XRegressionCurve
> xCurve( GetPropertySet(), uno::UNO_QUERY
);
136 bool bChanged
= false;
138 OSL_ASSERT(xCurve
.is());
144 case SCHATTR_REGRESSION_TYPE
:
146 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
147 SvxChartRegress eNewRegress
= static_cast< const SvxChartRegressItem
& >(
148 rItemSet
.Get( nWhichId
)).GetValue();
149 if( eRegress
!= eNewRegress
)
151 // note that changing the regression type changes the object
152 // for which this converter was created. Not optimal, but
153 // currently the only way to handle the type in the
154 // regression curve properties dialog
155 xCurve
= RegressionCurveHelper::changeRegressionCurveType(
159 uno::Reference
<beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
160 resetPropertySet( xProperties
);
166 case SCHATTR_REGRESSION_DEGREE
:
168 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
169 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, "PolynomialDegree");
173 case SCHATTR_REGRESSION_PERIOD
:
175 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
176 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
180 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
182 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
183 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
187 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
189 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
190 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
194 case SCHATTR_REGRESSION_SET_INTERCEPT
:
196 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
197 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xProperties
, "ForceIntercept");
201 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
203 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
204 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "InterceptValue");
208 case SCHATTR_REGRESSION_CURVE_NAME
:
210 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
211 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xProperties
, "CurveName");
215 case SCHATTR_REGRESSION_SHOW_EQUATION
:
217 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
218 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowEquation");
222 case SCHATTR_REGRESSION_XNAME
:
224 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
225 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xEqProp
, "XName");
229 case SCHATTR_REGRESSION_YNAME
:
231 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
232 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xEqProp
, "YName");
236 case SCHATTR_REGRESSION_SHOW_COEFF
:
238 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
239 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowCorrelationCoefficient");
247 void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId
, SfxItemSet
& rOutItemSet
) const
249 uno::Reference
<chart2::XRegressionCurve
> xCurve(GetPropertySet(), uno::UNO_QUERY
);
250 OSL_ASSERT(xCurve
.is());
254 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
258 case SCHATTR_REGRESSION_TYPE
:
260 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
261 rOutItemSet
.Put( SvxChartRegressItem( eRegress
, SCHATTR_REGRESSION_TYPE
));
265 case SCHATTR_REGRESSION_DEGREE
:
267 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "PolynomialDegree");
271 case SCHATTR_REGRESSION_PERIOD
:
273 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
277 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
279 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
283 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
285 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
289 case SCHATTR_REGRESSION_SET_INTERCEPT
:
291 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xProperties
, "ForceIntercept");
295 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
297 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "InterceptValue");
301 case SCHATTR_REGRESSION_CURVE_NAME
:
303 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xProperties
, "CurveName");
307 case SCHATTR_REGRESSION_SHOW_EQUATION
:
309 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowEquation");
313 case SCHATTR_REGRESSION_XNAME
:
315 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "XName");
319 case SCHATTR_REGRESSION_YNAME
:
321 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "YName");
325 case SCHATTR_REGRESSION_SHOW_COEFF
:
327 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowCorrelationCoefficient");
333 } // namespace chart::wrapper
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */