tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / RegressionCurveItemConverter.cxx
blob684c6fc1cdd5cb80bcd3f7176b5f62bd6bd6447e
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 <RegressionCurveModel.hxx>
22 #include <RegressionCurveItemConverter.hxx>
23 #include "SchWhichPairs.hxx"
24 #include <GraphicPropertyItemConverter.hxx>
25 #include <DataSeries.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>
33 #include <utility>
35 using namespace ::com::sun::star;
37 namespace
39 template <class T, class D>
40 bool lclConvertToPropertySet(const SfxItemSet& rItemSet, sal_uInt16 nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
42 OSL_ASSERT(xProperties.is());
43 if( xProperties.is() )
45 T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
46 T aOldValue = aValue;
47 bool aSuccess = xProperties->getPropertyValue( aPropertyID ) >>= aOldValue;
48 if (!aSuccess || aOldValue != aValue)
50 xProperties->setPropertyValue( aPropertyID , uno::Any( aValue ));
51 return true;
54 return false;
57 template <class T, class D>
58 void lclConvertToItemSet(SfxItemSet& rItemSet, sal_uInt16 nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
60 OSL_ASSERT(xProperties.is());
61 if( xProperties.is() )
63 T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
64 if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
66 rItemSet.Put(D( nWhichId, aValue ));
71 void lclConvertToItemSetDouble(SfxItemSet& rItemSet, TypedWhichId<SvxDoubleItem> nWhichId, const uno::Reference<beans::XPropertySet>& xProperties, const OUString& aPropertyID)
73 OSL_ASSERT(xProperties.is());
74 if( xProperties.is() )
76 double aValue = rItemSet.Get( nWhichId ).GetValue();
77 if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
79 rItemSet.Put(SvxDoubleItem( aValue, nWhichId ));
84 } // anonymous namespace
86 namespace chart::wrapper
89 RegressionCurveItemConverter::RegressionCurveItemConverter(
90 const uno::Reference< beans::XPropertySet >& rPropertySet,
91 rtl::Reference< DataSeries > xContainer,
92 SfxItemPool& rItemPool,
93 SdrModel& rDrawModel,
94 const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
95 ItemConverter( rPropertySet, rItemPool ),
96 m_spGraphicConverter( std::make_shared<GraphicPropertyItemConverter>(
97 rPropertySet, rItemPool, rDrawModel,
98 xNamedPropertyContainerFactory,
99 GraphicObjectType::LineProperties )),
100 m_xCurveContainer(std::move( xContainer ))
103 RegressionCurveItemConverter::~RegressionCurveItemConverter()
106 void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
108 m_spGraphicConverter->FillItemSet( rOutItemSet );
110 // own items
111 ItemConverter::FillItemSet( rOutItemSet );
114 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
116 bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
118 // own items
119 return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
122 const WhichRangesContainer& RegressionCurveItemConverter::GetWhichPairs() const
124 // must span all used items!
125 return nRegressionCurveWhichPairs;
128 bool RegressionCurveItemConverter::GetItemProperty(
129 tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
131 // No own (non-special) properties
132 return false;
135 bool RegressionCurveItemConverter::ApplySpecialItem(
136 sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
138 uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
139 bool bChanged = false;
141 OSL_ASSERT(xCurve.is());
142 if(!xCurve.is())
143 return false;
145 switch( nWhichId )
147 case SCHATTR_REGRESSION_TYPE:
149 SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
150 SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
151 rItemSet.Get( nWhichId )).GetValue();
152 if( eRegress != eNewRegress )
154 // note that changing the regression type changes the object
155 // for which this converter was created. Not optimal, but
156 // currently the only way to handle the type in the
157 // regression curve properties dialog
158 xCurve = RegressionCurveHelper::changeRegressionCurveType(
159 eNewRegress,
160 m_xCurveContainer,
161 xCurve);
162 uno::Reference<beans::XPropertySet> xProperties( xCurve, uno::UNO_QUERY );
163 resetPropertySet( xProperties );
164 bChanged = true;
167 break;
169 case SCHATTR_REGRESSION_DEGREE:
171 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
172 bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, u"PolynomialDegree"_ustr);
174 break;
176 case SCHATTR_REGRESSION_PERIOD:
178 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
179 bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, u"MovingAveragePeriod"_ustr);
181 break;
183 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
185 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
186 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, u"ExtrapolateForward"_ustr);
188 break;
190 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
192 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
193 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, u"ExtrapolateBackward"_ustr);
195 break;
197 case SCHATTR_REGRESSION_SET_INTERCEPT:
199 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
200 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xProperties, u"ForceIntercept"_ustr);
202 break;
204 case SCHATTR_REGRESSION_INTERCEPT_VALUE:
206 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
207 bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, u"InterceptValue"_ustr);
209 break;
211 case SCHATTR_REGRESSION_CURVE_NAME:
213 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
214 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xProperties, u"CurveName"_ustr);
216 break;
218 case SCHATTR_REGRESSION_MOVING_TYPE:
220 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
221 bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, u"MovingAverageType"_ustr);
223 break;
225 case SCHATTR_REGRESSION_SHOW_EQUATION:
227 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
228 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, u"ShowEquation"_ustr);
230 break;
232 case SCHATTR_REGRESSION_XNAME:
234 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
235 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, u"XName"_ustr);
237 break;
239 case SCHATTR_REGRESSION_YNAME:
241 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
242 bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xEqProp, u"YName"_ustr);
244 break;
246 case SCHATTR_REGRESSION_SHOW_COEFF:
248 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
249 bChanged = lclConvertToPropertySet<bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, u"ShowCorrelationCoefficient"_ustr);
251 break;
254 return bChanged;
257 void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId, SfxItemSet& rOutItemSet ) const
259 uno::Reference<chart2::XRegressionCurve> xCurve(GetPropertySet(), uno::UNO_QUERY);
260 OSL_ASSERT(xCurve.is());
261 if(!xCurve.is())
262 return;
264 uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
266 switch( nWhichId )
268 case SCHATTR_REGRESSION_TYPE:
270 SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
271 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
273 break;
275 case SCHATTR_REGRESSION_DEGREE:
277 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, u"PolynomialDegree"_ustr);
279 break;
281 case SCHATTR_REGRESSION_PERIOD:
283 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, u"MovingAveragePeriod"_ustr);
285 break;
287 case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
289 lclConvertToItemSetDouble(rOutItemSet, SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD, xProperties, u"ExtrapolateForward"_ustr);
291 break;
293 case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
295 lclConvertToItemSetDouble(rOutItemSet, SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD, xProperties, u"ExtrapolateBackward"_ustr);
297 break;
299 case SCHATTR_REGRESSION_SET_INTERCEPT:
301 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xProperties, u"ForceIntercept"_ustr);
303 break;
305 case SCHATTR_REGRESSION_INTERCEPT_VALUE:
307 lclConvertToItemSetDouble(rOutItemSet, SCHATTR_REGRESSION_INTERCEPT_VALUE, xProperties, u"InterceptValue"_ustr);
309 break;
311 case SCHATTR_REGRESSION_CURVE_NAME:
313 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xProperties, u"CurveName"_ustr);
315 break;
317 case SCHATTR_REGRESSION_MOVING_TYPE:
319 lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, u"MovingAverageType"_ustr);
321 break;
323 case SCHATTR_REGRESSION_SHOW_EQUATION:
325 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), u"ShowEquation"_ustr);
327 break;
329 case SCHATTR_REGRESSION_XNAME:
331 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), u"XName"_ustr);
333 break;
335 case SCHATTR_REGRESSION_YNAME:
337 lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), u"YName"_ustr);
339 break;
341 case SCHATTR_REGRESSION_SHOW_COEFF:
343 lclConvertToItemSet<bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), u"ShowCorrelationCoefficient"_ustr);
345 break;
349 } // namespace chart::wrapper
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */