nss: upgrade to release 3.73
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / RegressionCurveItemConverter.cxx
blob8859158ac5b815c2c12edbed78e3355774c561d2
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 <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;
34 namespace
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());
43 T aOldValue = aValue;
44 bool aSuccess = xProperties->getPropertyValue( aPropertyID ) >>= aOldValue;
45 if (!aSuccess || aOldValue != aValue)
47 xProperties->setPropertyValue( aPropertyID , uno::Any( aValue ));
48 return true;
51 return false;
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,
90 SdrModel& rDrawModel,
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 );
107 // own items
108 ItemConverter::FillItemSet( rOutItemSet );
111 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
113 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
115 // own items
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
129 return false;
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());
139 if(!xCurve.is())
140 return false;
142 switch( nWhichId )
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(
156 eNewRegress,
157 m_xCurveContainer,
158 xCurve);
159 uno::Reference<beans::XPropertySet> xProperties( xCurve, uno::UNO_QUERY );
160 resetPropertySet( xProperties );
161 bChanged = true;
164 break;
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");
171 break;
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");
178 break;
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");
185 break;
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");
192 break;
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");
199 break;
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");
206 break;
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");
213 break;
215 case SCHATTR_REGRESSION_SHOW_EQUATION:
217 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
218 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowEquation");
220 break;
222 case SCHATTR_REGRESSION_XNAME:
224 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
225 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, "XName");
227 break;
229 case SCHATTR_REGRESSION_YNAME:
231 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
232 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, "YName");
234 break;
236 case SCHATTR_REGRESSION_SHOW_COEFF:
238 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
239 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowCorrelationCoefficient");
241 break;
244 return bChanged;
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());
251 if(!xCurve.is())
252 return;
254 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
256 switch( nWhichId )
258 case SCHATTR_REGRESSION_TYPE:
260 SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
261 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
263 break;
265 case SCHATTR_REGRESSION_DEGREE:
267 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "PolynomialDegree");
269 break;
271 case SCHATTR_REGRESSION_PERIOD:
273 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "MovingAveragePeriod");
275 break;
277 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
279 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateForward");
281 break;
283 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
285 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateBackward");
287 break;
289 case SCHATTR_REGRESSION_SET_INTERCEPT:
291 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xProperties, "ForceIntercept");
293 break;
295 case SCHATTR_REGRESSION_INTERCEPT_VALUE:
297 lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "InterceptValue");
299 break;
301 case SCHATTR_REGRESSION_CURVE_NAME:
303 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xProperties, "CurveName");
305 break;
307 case SCHATTR_REGRESSION_SHOW_EQUATION:
309 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowEquation");
311 break;
313 case SCHATTR_REGRESSION_XNAME:
315 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "XName");
317 break;
319 case SCHATTR_REGRESSION_YNAME:
321 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "YName");
323 break;
325 case SCHATTR_REGRESSION_SHOW_COEFF:
327 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowCorrelationCoefficient");
329 break;
333 } // namespace chart::wrapper
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */