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"
24 #include "ItemPropertyMap.hxx"
25 #include "GraphicPropertyItemConverter.hxx"
27 #include <com/sun/star/chart2/XRegressionCurve.hpp>
28 #include <osl/diagnose.h>
30 #include <svl/eitem.hxx>
31 #include <svl/intitem.hxx>
32 #include <svl/stritem.hxx>
37 #include <boost/checked_delete.hpp>
39 using namespace ::com::sun::star
;
43 template <class T
, class D
>
44 bool lclConvertToPropertySet(const SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, uno::Reference
<beans::XPropertySet
> xProperties
, const OUString
& aPropertyID
)
46 OSL_ASSERT(xProperties
.is());
47 if( xProperties
.is() )
49 T aValue
= static_cast<T
>(static_cast<const D
&>(rItemSet
.Get( nWhichId
)).GetValue());
51 bool aSuccess
= xProperties
->getPropertyValue( aPropertyID
) >>= aOldValue
;
52 if (!aSuccess
|| aOldValue
!= aValue
)
54 xProperties
->setPropertyValue( aPropertyID
, uno::makeAny( aValue
));
61 template <class T
, class D
>
62 void lclConvertToItemSet(SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, uno::Reference
<beans::XPropertySet
> xProperties
, const OUString
& aPropertyID
)
64 OSL_ASSERT(xProperties
.is());
65 if( xProperties
.is() )
67 T aValue
= static_cast<T
>(static_cast<const D
&>(rItemSet
.Get( nWhichId
)).GetValue());
68 if(xProperties
->getPropertyValue( aPropertyID
) >>= aValue
)
70 rItemSet
.Put(D( nWhichId
, aValue
));
75 void lclConvertToItemSetDouble(SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, uno::Reference
<beans::XPropertySet
> xProperties
, const OUString
& aPropertyID
)
77 OSL_ASSERT(xProperties
.is());
78 if( xProperties
.is() )
80 double aValue
= static_cast<double>(static_cast<const SvxDoubleItem
&>(rItemSet
.Get( nWhichId
)).GetValue());
81 if(xProperties
->getPropertyValue( aPropertyID
) >>= aValue
)
83 rItemSet
.Put(SvxDoubleItem( aValue
, nWhichId
));
88 } // anonymous namespace
95 RegressionCurveItemConverter::RegressionCurveItemConverter(
96 const uno::Reference
< beans::XPropertySet
>& rPropertySet
,
97 const uno::Reference
< chart2::XRegressionCurveContainer
>& xContainer
,
98 SfxItemPool
& rItemPool
,
100 const uno::Reference
< lang::XMultiServiceFactory
> & xNamedPropertyContainerFactory
) :
101 ItemConverter( rPropertySet
, rItemPool
),
102 m_spGraphicConverter( new GraphicPropertyItemConverter(
103 rPropertySet
, rItemPool
, rDrawModel
,
104 xNamedPropertyContainerFactory
,
105 GraphicPropertyItemConverter::LINE_PROPERTIES
)),
106 m_xCurveContainer( xContainer
)
109 RegressionCurveItemConverter::~RegressionCurveItemConverter()
112 void RegressionCurveItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
114 m_spGraphicConverter
->FillItemSet( rOutItemSet
);
117 ItemConverter::FillItemSet( rOutItemSet
);
120 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
122 bool bResult
= m_spGraphicConverter
->ApplyItemSet( rItemSet
);
125 return ItemConverter::ApplyItemSet( rItemSet
) || bResult
;
128 const sal_uInt16
* RegressionCurveItemConverter::GetWhichPairs() const
130 // must span all used items!
131 return nRegressionCurveWhichPairs
;
134 bool RegressionCurveItemConverter::GetItemProperty(
135 tWhichIdType
/* nWhichId */, tPropertyNameWithMemberId
& /* rOutProperty */ ) const
137 // No own (non-special) properties
141 bool RegressionCurveItemConverter::ApplySpecialItem(
142 sal_uInt16 nWhichId
, const SfxItemSet
& rItemSet
)
143 throw( uno::Exception
)
145 uno::Reference
< chart2::XRegressionCurve
> xCurve( GetPropertySet(), uno::UNO_QUERY
);
146 bool bChanged
= false;
148 OSL_ASSERT(xCurve
.is());
154 case SCHATTR_REGRESSION_TYPE
:
156 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
157 SvxChartRegress eNewRegress
= static_cast< const SvxChartRegressItem
& >(
158 rItemSet
.Get( nWhichId
)).GetValue();
159 if( eRegress
!= eNewRegress
)
161 // note that changing the regression type changes the object
162 // for which this converter was created. Not optimal, but
163 // currently the only way to handle the type in the
164 // regression curve properties dialog
165 xCurve
= RegressionCurveHelper::changeRegressionCurveType(
169 uno::Reference
< uno::XComponentContext
>());
170 uno::Reference
<beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
171 resetPropertySet( xProperties
);
177 case SCHATTR_REGRESSION_DEGREE
:
179 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
180 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, OUString("PolynomialDegree"));
184 case SCHATTR_REGRESSION_PERIOD
:
186 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
187 bChanged
= lclConvertToPropertySet
<sal_Int32
, SfxInt32Item
>(rItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
191 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
193 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
194 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
198 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
200 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
201 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
205 case SCHATTR_REGRESSION_SET_INTERCEPT
:
207 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
208 bChanged
= lclConvertToPropertySet
<sal_Bool
, SfxBoolItem
>(rItemSet
, nWhichId
, xProperties
, "ForceIntercept");
212 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
214 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
215 bChanged
= lclConvertToPropertySet
<double, SvxDoubleItem
>(rItemSet
, nWhichId
, xProperties
, "InterceptValue");
219 case SCHATTR_REGRESSION_CURVE_NAME
:
221 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
222 bChanged
= lclConvertToPropertySet
<OUString
, SfxStringItem
>(rItemSet
, nWhichId
, xProperties
, "CurveName");
226 case SCHATTR_REGRESSION_SHOW_EQUATION
:
228 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
229 bChanged
= lclConvertToPropertySet
<sal_Bool
, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowEquation");
233 case SCHATTR_REGRESSION_SHOW_COEFF
:
235 uno::Reference
< beans::XPropertySet
> xEqProp( xCurve
->getEquationProperties());
236 bChanged
= lclConvertToPropertySet
<sal_Bool
, SfxBoolItem
>(rItemSet
, nWhichId
, xEqProp
, "ShowCorrelationCoefficient");
244 void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId
, SfxItemSet
& rOutItemSet
) const
245 throw( uno::Exception
)
247 uno::Reference
<chart2::XRegressionCurve
> xCurve(GetPropertySet(), uno::UNO_QUERY
);
248 OSL_ASSERT(xCurve
.is());
252 uno::Reference
< beans::XPropertySet
> xProperties( xCurve
, uno::UNO_QUERY
);
256 case SCHATTR_REGRESSION_TYPE
:
258 SvxChartRegress eRegress
= RegressionCurveHelper::getRegressionType(xCurve
);
259 rOutItemSet
.Put( SvxChartRegressItem( eRegress
, SCHATTR_REGRESSION_TYPE
));
263 case SCHATTR_REGRESSION_DEGREE
:
265 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "PolynomialDegree");
269 case SCHATTR_REGRESSION_PERIOD
:
271 lclConvertToItemSet
<sal_Int32
, SfxInt32Item
>(rOutItemSet
, nWhichId
, xProperties
, "MovingAveragePeriod");
275 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
:
277 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateForward");
281 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
:
283 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "ExtrapolateBackward");
287 case SCHATTR_REGRESSION_SET_INTERCEPT
:
289 lclConvertToItemSet
<sal_Bool
, SfxBoolItem
>(rOutItemSet
, nWhichId
, xProperties
, "ForceIntercept");
293 case SCHATTR_REGRESSION_INTERCEPT_VALUE
:
295 lclConvertToItemSetDouble(rOutItemSet
, nWhichId
, xProperties
, "InterceptValue");
299 case SCHATTR_REGRESSION_CURVE_NAME
:
301 lclConvertToItemSet
<OUString
, SfxStringItem
>(rOutItemSet
, nWhichId
, xProperties
, "CurveName");
305 case SCHATTR_REGRESSION_SHOW_EQUATION
:
307 lclConvertToItemSet
<sal_Bool
, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowEquation");
311 case SCHATTR_REGRESSION_SHOW_COEFF
:
313 lclConvertToItemSet
<sal_Bool
, SfxBoolItem
>(rOutItemSet
, nWhichId
, xCurve
->getEquationProperties(), "ShowCorrelationCoefficient");
319 } // namespace wrapper
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */