Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / RegressionCurveItemConverter.cxx
blob9842121c170c4b2072d67536bceaa5ff5ab8c8c9
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 <RegressionCurveHelper.hxx>
21 #include <RegressionCurveItemConverter.hxx>
22 #include "SchWhichPairs.hxx"
23 #include <ItemPropertyMap.hxx>
24 #include <GraphicPropertyItemConverter.hxx>
26 #include <com/sun/star/chart2/XRegressionCurve.hpp>
27 #include <osl/diagnose.h>
29 #include <svl/eitem.hxx>
30 #include <svl/intitem.hxx>
31 #include <svl/stritem.hxx>
33 #include <functional>
34 #include <algorithm>
36 using namespace ::com::sun::star;
38 namespace
40 template <class T, class D>
41 bool lclConvertToPropertySet(const SfxItemSet& rItemSet, sal_uInt16 nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
43 OSL_ASSERT(xProperties.is());
44 if( xProperties.is() )
46 T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
47 T aOldValue = aValue;
48 bool aSuccess = xProperties->getPropertyValue( aPropertyID ) >>= aOldValue;
49 if (!aSuccess || aOldValue != aValue)
51 xProperties->setPropertyValue( aPropertyID , uno::Any( aValue ));
52 return true;
55 return false;
58 template <class T, class D>
59 void lclConvertToItemSet(SfxItemSet& rItemSet, sal_uInt16 nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
61 OSL_ASSERT(xProperties.is());
62 if( xProperties.is() )
64 T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
65 if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
67 rItemSet.Put(D( nWhichId, aValue ));
72 void lclConvertToItemSetDouble(SfxItemSet& rItemSet, sal_uInt16 nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
74 OSL_ASSERT(xProperties.is());
75 if( xProperties.is() )
77 double aValue = static_cast<const SvxDoubleItem&>(rItemSet.Get( nWhichId )).GetValue();
78 if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
80 rItemSet.Put(SvxDoubleItem( aValue, nWhichId ));
85 } // anonymous namespace
87 namespace chart
89 namespace wrapper
92 RegressionCurveItemConverter::RegressionCurveItemConverter(
93 const uno::Reference< beans::XPropertySet >& rPropertySet,
94 const uno::Reference< chart2::XRegressionCurveContainer >& xContainer,
95 SfxItemPool& rItemPool,
96 SdrModel& rDrawModel,
97 const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
98 ItemConverter( rPropertySet, rItemPool ),
99 m_spGraphicConverter( new GraphicPropertyItemConverter(
100 rPropertySet, rItemPool, rDrawModel,
101 xNamedPropertyContainerFactory,
102 GraphicObjectType::LineProperties )),
103 m_xCurveContainer( xContainer )
106 RegressionCurveItemConverter::~RegressionCurveItemConverter()
109 void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
111 m_spGraphicConverter->FillItemSet( rOutItemSet );
113 // own items
114 ItemConverter::FillItemSet( rOutItemSet );
117 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
119 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
121 // own items
122 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
125 const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
127 // must span all used items!
128 return nRegressionCurveWhichPairs;
131 bool RegressionCurveItemConverter::GetItemProperty(
132 tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
134 // No own (non-special) properties
135 return false;
138 bool RegressionCurveItemConverter::ApplySpecialItem(
139 sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
141 uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
142 bool bChanged = false;
144 OSL_ASSERT(xCurve.is());
145 if(!xCurve.is())
146 return false;
148 switch( nWhichId )
150 case SCHATTR_REGRESSION_TYPE:
152 SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
153 SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
154 rItemSet.Get( nWhichId )).GetValue();
155 if( eRegress != eNewRegress )
157 // note that changing the regression type changes the object
158 // for which this converter was created. Not optimal, but
159 // currently the only way to handle the type in the
160 // regression curve properties dialog
161 xCurve = RegressionCurveHelper::changeRegressionCurveType(
162 eNewRegress,
163 m_xCurveContainer,
164 xCurve);
165 uno::Reference<beans::XPropertySet> xProperties( xCurve, uno::UNO_QUERY );
166 resetPropertySet( xProperties );
167 bChanged = true;
170 break;
172 case SCHATTR_REGRESSION_DEGREE:
174 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
175 bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, "PolynomialDegree");
177 break;
179 case SCHATTR_REGRESSION_PERIOD:
181 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
182 bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, "MovingAveragePeriod");
184 break;
186 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
188 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
189 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "ExtrapolateForward");
191 break;
193 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
195 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
196 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "ExtrapolateBackward");
198 break;
200 case SCHATTR_REGRESSION_SET_INTERCEPT:
202 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
203 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xProperties, "ForceIntercept");
205 break;
207 case SCHATTR_REGRESSION_INTERCEPT_VALUE:
209 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
210 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "InterceptValue");
212 break;
214 case SCHATTR_REGRESSION_CURVE_NAME:
216 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
217 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xProperties, "CurveName");
219 break;
221 case SCHATTR_REGRESSION_SHOW_EQUATION:
223 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
224 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowEquation");
226 break;
228 case SCHATTR_REGRESSION_XNAME:
230 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
231 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, "XName");
233 break;
235 case SCHATTR_REGRESSION_YNAME:
237 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
238 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, "YName");
240 break;
242 case SCHATTR_REGRESSION_SHOW_COEFF:
244 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
245 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowCorrelationCoefficient");
247 break;
250 return bChanged;
253 void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId, SfxItemSet& rOutItemSet ) const
255 uno::Reference<chart2::XRegressionCurve> xCurve(GetPropertySet(), uno::UNO_QUERY);
256 OSL_ASSERT(xCurve.is());
257 if(!xCurve.is())
258 return;
260 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
262 switch( nWhichId )
264 case SCHATTR_REGRESSION_TYPE:
266 SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
267 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
269 break;
271 case SCHATTR_REGRESSION_DEGREE:
273 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "PolynomialDegree");
275 break;
277 case SCHATTR_REGRESSION_PERIOD:
279 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "MovingAveragePeriod");
281 break;
283 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
285 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateForward");
287 break;
289 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
291 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateBackward");
293 break;
295 case SCHATTR_REGRESSION_SET_INTERCEPT:
297 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xProperties, "ForceIntercept");
299 break;
301 case SCHATTR_REGRESSION_INTERCEPT_VALUE:
303 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "InterceptValue");
305 break;
307 case SCHATTR_REGRESSION_CURVE_NAME:
309 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xProperties, "CurveName");
311 break;
313 case SCHATTR_REGRESSION_SHOW_EQUATION:
315 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowEquation");
317 break;
319 case SCHATTR_REGRESSION_XNAME:
321 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "XName");
323 break;
325 case SCHATTR_REGRESSION_YNAME:
327 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "YName");
329 break;
331 case SCHATTR_REGRESSION_SHOW_COEFF:
333 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowCorrelationCoefficient");
335 break;
339 } // namespace wrapper
340 } // namespace chart
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */