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
88 RegressionCurveItemConverter::RegressionCurveItemConverter(
89 const uno::Reference
< beans::XPropertySet
>& rPropertySet
,
90 const uno::Reference
< chart2::XRegressionCurveContainer
>& xContainer
,
91 SfxItemPool
& rItemPool
,
93 const uno::Reference
< lang::XMultiServiceFactory
> & xNamedPropertyContainerFactory
) :
94 ItemConverter( rPropertySet
, rItemPool
),
95 m_spGraphicConverter( new GraphicPropertyItemConverter(
96 rPropertySet
, rItemPool
, rDrawModel
,
97 xNamedPropertyContainerFactory
,
98 GraphicObjectType::LineProperties
)),
99 m_xCurveContainer( xContainer
)
102 RegressionCurveItemConverter::~RegressionCurveItemConverter()
105 void RegressionCurveItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
107 m_spGraphicConverter
->FillItemSet( rOutItemSet
);
110 ItemConverter::FillItemSet( rOutItemSet
);
113 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
115 bool bResult
= m_spGraphicConverter
->ApplyItemSet( rItemSet
);
118 return ItemConverter::ApplyItemSet( rItemSet
) || bResult
;
121 const sal_uInt16
* RegressionCurveItemConverter::GetWhichPairs() const
123 // must span all used items!
124 return nRegressionCurveWhichPairs
;
127 bool RegressionCurveItemConverter::GetItemProperty(
128 tWhichIdType
/* nWhichId */, tPropertyNameWithMemberId
& /* rOutProperty */ ) const
130 // No own (non-special) properties
134 bool RegressionCurveItemConverter::ApplySpecialItem(
135 sal_uInt16 nWhichId
, const SfxItemSet
& rItemSet
)
137 uno::Reference
< chart2::XRegressionCurve
> xCurve( GetPropertySet(), uno::UNO_QUERY
);
138 bool bChanged
= false;
140 OSL_ASSERT(xCurve
.is());
146 case SCHATTR_REGRESSION_TYPE
:
148 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
149 SvxChartRegress eNewRegress
= static_cast< const SvxChartRegressItem
& >(
150 rItemSet
.Get( nWhichId
)).GetValue();
151 if( eRegress
!= eNewRegress
)
153 // note that changing the regression type changes the object
154 // for which this converter was created. Not optimal, but
155 // currently the only way to handle the type in the
156 // regression curve properties dialog
157 xCurve
= RegressionCurveHelper::changeRegressionCurveType(
161 uno::Reference
<beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
162 resetPropertySet( xProperties
);
168 case SCHATTR_REGRESSION_DEGREE
:
170 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
171 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, "PolynomialDegree");
175 case SCHATTR_REGRESSION_PERIOD
:
177 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
178 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
182 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
184 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
185 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
189 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
191 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
192 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
196 case SCHATTR_REGRESSION_SET_INTERCEPT
:
198 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
199 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xProperties
, "ForceIntercept");
203 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
205 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
206 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "InterceptValue");
210 case SCHATTR_REGRESSION_CURVE_NAME
:
212 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
213 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xProperties
, "CurveName");
217 case SCHATTR_REGRESSION_SHOW_EQUATION
:
219 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
220 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowEquation");
224 case SCHATTR_REGRESSION_XNAME
:
226 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
227 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xEqProp
, "XName");
231 case SCHATTR_REGRESSION_YNAME
:
233 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
234 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xEqProp
, "YName");
238 case SCHATTR_REGRESSION_SHOW_COEFF
:
240 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
241 bChanged
= lclConvertToPropertySet
<bool, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowCorrelationCoefficient");
249 void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId
, SfxItemSet
& rOutItemSet
) const
251 uno::Reference
<chart2::XRegressionCurve
> xCurve(GetPropertySet(), uno::UNO_QUERY
);
252 OSL_ASSERT(xCurve
.is());
256 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
260 case SCHATTR_REGRESSION_TYPE
:
262 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
263 rOutItemSet
.Put( SvxChartRegressItem( eRegress
, SCHATTR_REGRESSION_TYPE
));
267 case SCHATTR_REGRESSION_DEGREE
:
269 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "PolynomialDegree");
273 case SCHATTR_REGRESSION_PERIOD
:
275 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
279 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
281 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
285 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
287 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
291 case SCHATTR_REGRESSION_SET_INTERCEPT
:
293 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xProperties
, "ForceIntercept");
297 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
299 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "InterceptValue");
303 case SCHATTR_REGRESSION_CURVE_NAME
:
305 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xProperties
, "CurveName");
309 case SCHATTR_REGRESSION_SHOW_EQUATION
:
311 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowEquation");
315 case SCHATTR_REGRESSION_XNAME
:
317 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "XName");
321 case SCHATTR_REGRESSION_YNAME
:
323 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "YName");
327 case SCHATTR_REGRESSION_SHOW_COEFF
:
329 lclConvertToItemSet
<bool, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowCorrelationCoefficient");
335 } // namespace wrapper
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */