Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "RegressionCurveHelper.hxx"
21 : #include "RegressionCurveItemConverter.hxx"
22 : #include "SchWhichPairs.hxx"
23 : #include "macros.hxx"
24 : #include "ItemPropertyMap.hxx"
25 : #include "GraphicPropertyItemConverter.hxx"
26 :
27 : #include <com/sun/star/chart2/XRegressionCurve.hpp>
28 : #include <osl/diagnose.h>
29 :
30 : #include <svl/eitem.hxx>
31 : #include <svl/intitem.hxx>
32 : #include <svl/stritem.hxx>
33 :
34 : #include <functional>
35 : #include <algorithm>
36 :
37 : #include <boost/checked_delete.hpp>
38 :
39 : using namespace ::com::sun::star;
40 :
41 : namespace
42 : {
43 : template <class T, class D>
44 0 : bool lclConvertToPropertySet(const SfxItemSet& rItemSet, sal_uInt16 nWhichId, uno::Reference<beans::XPropertySet> xProperties, const OUString& aPropertyID)
45 : {
46 : OSL_ASSERT(xProperties.is());
47 0 : if( xProperties.is() )
48 : {
49 0 : T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
50 0 : T aOldValue = aValue;
51 0 : bool aSuccess = xProperties->getPropertyValue( aPropertyID ) >>= aOldValue;
52 0 : if (!aSuccess || aOldValue != aValue)
53 : {
54 0 : xProperties->setPropertyValue( aPropertyID , uno::makeAny( aValue ));
55 0 : return true;
56 0 : }
57 : }
58 0 : return false;
59 : }
60 :
61 : template <class T, class D>
62 0 : void lclConvertToItemSet(SfxItemSet& rItemSet, sal_uInt16 nWhichId, uno::Reference<beans::XPropertySet> xProperties, const OUString& aPropertyID)
63 : {
64 : OSL_ASSERT(xProperties.is());
65 0 : if( xProperties.is() )
66 : {
67 0 : T aValue = static_cast<T>(static_cast<const D&>(rItemSet.Get( nWhichId )).GetValue());
68 0 : if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
69 : {
70 0 : rItemSet.Put(D( nWhichId, aValue ));
71 0 : }
72 : }
73 0 : }
74 :
75 0 : void lclConvertToItemSetDouble(SfxItemSet& rItemSet, sal_uInt16 nWhichId, uno::Reference<beans::XPropertySet> xProperties, const OUString& aPropertyID)
76 : {
77 : OSL_ASSERT(xProperties.is());
78 0 : if( xProperties.is() )
79 : {
80 0 : double aValue = static_cast<double>(static_cast<const SvxDoubleItem&>(rItemSet.Get( nWhichId )).GetValue());
81 0 : if(xProperties->getPropertyValue( aPropertyID ) >>= aValue)
82 : {
83 0 : rItemSet.Put(SvxDoubleItem( aValue, nWhichId ));
84 : }
85 : }
86 0 : }
87 :
88 : } // anonymous namespace
89 :
90 : namespace chart
91 : {
92 : namespace wrapper
93 : {
94 :
95 0 : RegressionCurveItemConverter::RegressionCurveItemConverter(
96 : const uno::Reference< beans::XPropertySet >& rPropertySet,
97 : const uno::Reference< chart2::XRegressionCurveContainer >& xContainer,
98 : SfxItemPool& rItemPool,
99 : SdrModel& rDrawModel,
100 : const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
101 : ItemConverter( rPropertySet, rItemPool ),
102 : m_spGraphicConverter( new GraphicPropertyItemConverter(
103 : rPropertySet, rItemPool, rDrawModel,
104 : xNamedPropertyContainerFactory,
105 0 : GraphicPropertyItemConverter::LINE_PROPERTIES )),
106 0 : m_xCurveContainer( xContainer )
107 0 : {}
108 :
109 0 : RegressionCurveItemConverter::~RegressionCurveItemConverter()
110 0 : {}
111 :
112 0 : void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
113 : {
114 0 : m_spGraphicConverter->FillItemSet( rOutItemSet );
115 :
116 : // own items
117 0 : ItemConverter::FillItemSet( rOutItemSet );
118 0 : }
119 :
120 0 : bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
121 : {
122 0 : bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
123 :
124 : // own items
125 0 : return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
126 : }
127 :
128 0 : const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
129 : {
130 : // must span all used items!
131 0 : return nRegressionCurveWhichPairs;
132 : }
133 :
134 0 : bool RegressionCurveItemConverter::GetItemProperty(
135 : tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
136 : {
137 : // No own (non-special) properties
138 0 : return false;
139 : }
140 :
141 0 : bool RegressionCurveItemConverter::ApplySpecialItem(
142 : sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
143 : throw( uno::Exception )
144 : {
145 0 : uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
146 0 : bool bChanged = false;
147 :
148 : OSL_ASSERT(xCurve.is());
149 0 : if(!xCurve.is())
150 0 : return false;
151 :
152 0 : switch( nWhichId )
153 : {
154 : case SCHATTR_REGRESSION_TYPE:
155 : {
156 0 : SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
157 : SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
158 0 : rItemSet.Get( nWhichId )).GetValue();
159 0 : if( eRegress != eNewRegress )
160 : {
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 0 : xCurve = RegressionCurveHelper::changeRegressionCurveType(
166 : eNewRegress,
167 : m_xCurveContainer,
168 : xCurve,
169 0 : uno::Reference< uno::XComponentContext >());
170 0 : uno::Reference<beans::XPropertySet> xProperties( xCurve, uno::UNO_QUERY );
171 0 : resetPropertySet( xProperties );
172 0 : bChanged = true;
173 : }
174 : }
175 0 : break;
176 :
177 : case SCHATTR_REGRESSION_DEGREE:
178 : {
179 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
180 0 : bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, OUString("PolynomialDegree"));
181 : }
182 0 : break;
183 :
184 : case SCHATTR_REGRESSION_PERIOD:
185 : {
186 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
187 0 : bChanged = lclConvertToPropertySet<sal_Int32, SfxInt32Item>(rItemSet, nWhichId, xProperties, "MovingAveragePeriod");
188 : }
189 0 : break;
190 :
191 : case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
192 : {
193 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
194 0 : bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "ExtrapolateForward");
195 : }
196 0 : break;
197 :
198 : case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
199 : {
200 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
201 0 : bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "ExtrapolateBackward");
202 : }
203 0 : break;
204 :
205 : case SCHATTR_REGRESSION_SET_INTERCEPT:
206 : {
207 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
208 0 : bChanged = lclConvertToPropertySet<sal_Bool, SfxBoolItem>(rItemSet, nWhichId, xProperties, "ForceIntercept");
209 : }
210 0 : break;
211 :
212 : case SCHATTR_REGRESSION_INTERCEPT_VALUE:
213 : {
214 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
215 0 : bChanged = lclConvertToPropertySet<double, SvxDoubleItem>(rItemSet, nWhichId, xProperties, "InterceptValue");
216 : }
217 0 : break;
218 :
219 : case SCHATTR_REGRESSION_CURVE_NAME:
220 : {
221 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
222 0 : bChanged = lclConvertToPropertySet<OUString, SfxStringItem>(rItemSet, nWhichId, xProperties, "CurveName");
223 : }
224 0 : break;
225 :
226 : case SCHATTR_REGRESSION_SHOW_EQUATION:
227 : {
228 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
229 0 : bChanged = lclConvertToPropertySet<sal_Bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowEquation");
230 : }
231 0 : break;
232 :
233 : case SCHATTR_REGRESSION_SHOW_COEFF:
234 : {
235 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
236 0 : bChanged = lclConvertToPropertySet<sal_Bool, SfxBoolItem>(rItemSet, nWhichId, xEqProp, "ShowCorrelationCoefficient");
237 : }
238 0 : break;
239 :
240 : }
241 0 : return bChanged;
242 : }
243 :
244 0 : void RegressionCurveItemConverter::FillSpecialItem(sal_uInt16 nWhichId, SfxItemSet& rOutItemSet ) const
245 : throw( uno::Exception )
246 : {
247 0 : uno::Reference<chart2::XRegressionCurve> xCurve(GetPropertySet(), uno::UNO_QUERY);
248 : OSL_ASSERT(xCurve.is());
249 0 : if(!xCurve.is())
250 0 : return;
251 :
252 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
253 :
254 0 : switch( nWhichId )
255 : {
256 : case SCHATTR_REGRESSION_TYPE:
257 : {
258 0 : SvxChartRegress eRegress = RegressionCurveHelper::getRegressionType(xCurve);
259 0 : rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
260 : }
261 0 : break;
262 :
263 : case SCHATTR_REGRESSION_DEGREE:
264 : {
265 0 : lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "PolynomialDegree");
266 : }
267 0 : break;
268 :
269 : case SCHATTR_REGRESSION_PERIOD:
270 : {
271 0 : lclConvertToItemSet<sal_Int32, SfxInt32Item>(rOutItemSet, nWhichId, xProperties, "MovingAveragePeriod");
272 : }
273 0 : break;
274 :
275 : case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
276 : {
277 0 : lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateForward");
278 : }
279 0 : break;
280 :
281 : case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
282 : {
283 0 : lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "ExtrapolateBackward");
284 : }
285 0 : break;
286 :
287 : case SCHATTR_REGRESSION_SET_INTERCEPT:
288 : {
289 0 : lclConvertToItemSet<sal_Bool, SfxBoolItem>(rOutItemSet, nWhichId, xProperties, "ForceIntercept");
290 : }
291 0 : break;
292 :
293 : case SCHATTR_REGRESSION_INTERCEPT_VALUE:
294 : {
295 0 : lclConvertToItemSetDouble(rOutItemSet, nWhichId, xProperties, "InterceptValue");
296 : }
297 0 : break;
298 :
299 : case SCHATTR_REGRESSION_CURVE_NAME:
300 : {
301 0 : lclConvertToItemSet<OUString, SfxStringItem>(rOutItemSet, nWhichId, xProperties, "CurveName");
302 : }
303 0 : break;
304 :
305 : case SCHATTR_REGRESSION_SHOW_EQUATION:
306 : {
307 0 : lclConvertToItemSet<sal_Bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowEquation");
308 : }
309 0 : break;
310 :
311 : case SCHATTR_REGRESSION_SHOW_COEFF:
312 : {
313 0 : lclConvertToItemSet<sal_Bool, SfxBoolItem>(rOutItemSet, nWhichId, xCurve->getEquationProperties(), "ShowCorrelationCoefficient");
314 : }
315 0 : break;
316 0 : }
317 : }
318 :
319 : } // namespace wrapper
320 : } // namespace chart
321 :
322 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|