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 :
29 : // for SfxBoolItem
30 : #include <svl/eitem.hxx>
31 : #include <svl/intitem.hxx>
32 : #include <svx/chrtitem.hxx>
33 :
34 : #include <functional>
35 : #include <algorithm>
36 :
37 : using namespace ::com::sun::star;
38 :
39 : namespace
40 : {
41 :
42 0 : ::chart::RegressionCurveHelper::tRegressionType lcl_convertRegressionType( SvxChartRegress eRegress )
43 : {
44 0 : ::chart::RegressionCurveHelper::tRegressionType eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_NONE;
45 0 : switch( eRegress )
46 : {
47 : case CHREGRESS_LINEAR:
48 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LINEAR;
49 0 : break;
50 : case CHREGRESS_LOG:
51 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LOG;
52 0 : break;
53 : case CHREGRESS_EXP:
54 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_EXP;
55 0 : break;
56 : case CHREGRESS_POWER:
57 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_POWER;
58 0 : break;
59 : case CHREGRESS_POLYNOMIAL:
60 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_POLYNOMIAL;
61 0 : break;
62 : case CHREGRESS_MOVING_AVERAGE:
63 0 : eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_MOVING_AVERAGE;
64 0 : break;
65 : case CHREGRESS_NONE:
66 0 : break;
67 : }
68 0 : return eType;
69 : }
70 :
71 : } // anonymous namespace
72 :
73 : namespace chart
74 : {
75 : namespace wrapper
76 : {
77 :
78 0 : RegressionCurveItemConverter::RegressionCurveItemConverter(
79 : const uno::Reference< beans::XPropertySet >& rPropertySet,
80 : const uno::Reference< chart2::XRegressionCurveContainer >& xContainer,
81 : SfxItemPool& rItemPool,
82 : SdrModel& rDrawModel,
83 : const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
84 : ItemConverter( rPropertySet, rItemPool ),
85 : m_spGraphicConverter( new GraphicPropertyItemConverter(
86 : rPropertySet, rItemPool, rDrawModel,
87 : xNamedPropertyContainerFactory,
88 0 : GraphicPropertyItemConverter::LINE_PROPERTIES )),
89 0 : m_xCurveContainer( xContainer )
90 0 : {}
91 :
92 0 : RegressionCurveItemConverter::~RegressionCurveItemConverter()
93 0 : {}
94 :
95 0 : void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
96 : {
97 0 : m_spGraphicConverter->FillItemSet( rOutItemSet );
98 :
99 : // own items
100 0 : ItemConverter::FillItemSet( rOutItemSet );
101 0 : }
102 :
103 0 : bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
104 : {
105 0 : bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
106 :
107 : // own items
108 0 : return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
109 : }
110 :
111 0 : const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
112 : {
113 : // must span all used items!
114 0 : return nRegressionCurveWhichPairs;
115 : }
116 :
117 0 : bool RegressionCurveItemConverter::GetItemProperty(
118 : tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
119 : {
120 : // No own (non-special) properties
121 0 : return false;
122 : }
123 :
124 :
125 0 : bool RegressionCurveItemConverter::ApplySpecialItem(
126 : sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
127 : throw( uno::Exception )
128 : {
129 0 : uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
130 0 : bool bChanged = false;
131 :
132 0 : switch( nWhichId )
133 : {
134 : case SCHATTR_REGRESSION_TYPE:
135 : {
136 : OSL_ASSERT( xCurve.is());
137 0 : if( xCurve.is())
138 : {
139 : SvxChartRegress eRegress = static_cast< SvxChartRegress >(
140 0 : static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
141 : SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
142 0 : rItemSet.Get( nWhichId )).GetValue();
143 0 : if( eRegress != eNewRegress )
144 : {
145 : // note that changing the regression type changes the object
146 : // for which this converter was created. Not optimal, but
147 : // currently the only way to handle the type in the
148 : // regression curve properties dialog
149 0 : xCurve = RegressionCurveHelper::changeRegressionCurveType(
150 : lcl_convertRegressionType( eNewRegress ),
151 : m_xCurveContainer,
152 : xCurve,
153 0 : uno::Reference< uno::XComponentContext >());
154 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
155 0 : resetPropertySet( xProperties );
156 0 : bChanged = true;
157 : }
158 : }
159 : }
160 0 : break;
161 :
162 : case SCHATTR_REGRESSION_DEGREE:
163 : {
164 0 : if( xCurve.is())
165 : {
166 : sal_Int32 aDegree = static_cast< sal_Int32 >(
167 : static_cast< const SfxInt32Item & >(
168 0 : rItemSet.Get( nWhichId )).GetValue());
169 :
170 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
171 : OSL_ASSERT( xProperties.is());
172 0 : if( xProperties.is() )
173 : {
174 0 : sal_Int32 aOldDegree = 2;
175 0 : xProperties->getPropertyValue( "PolynomialDegree" ) >>= aOldDegree;
176 0 : if (aOldDegree != aDegree)
177 : {
178 0 : xProperties->setPropertyValue( "PolynomialDegree" , uno::makeAny( aDegree ));
179 0 : bChanged = true;
180 : }
181 0 : }
182 : }
183 : }
184 0 : break;
185 :
186 : case SCHATTR_REGRESSION_PERIOD:
187 : {
188 0 : if( xCurve.is())
189 : {
190 : sal_Int32 aPeriod = static_cast< sal_Int32 >(
191 : static_cast< const SfxInt32Item & >(
192 0 : rItemSet.Get( nWhichId )).GetValue());
193 :
194 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
195 : OSL_ASSERT( xProperties.is());
196 0 : if( xProperties.is() )
197 : {
198 0 : sal_Int32 aOldPeriod = 2;
199 0 : xProperties->getPropertyValue( "MovingAveragePeriod" ) >>= aOldPeriod;
200 0 : if (aOldPeriod != aPeriod)
201 : {
202 0 : xProperties->setPropertyValue( "MovingAveragePeriod" , uno::makeAny( aPeriod ));
203 0 : bChanged = true;
204 : }
205 0 : }
206 : }
207 : }
208 0 : break;
209 :
210 : case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
211 : {
212 0 : if( xCurve.is())
213 : {
214 : double aValue = static_cast< double >(
215 : static_cast< const SvxDoubleItem & >(
216 0 : rItemSet.Get( nWhichId )).GetValue());
217 :
218 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
219 : OSL_ASSERT( xProperties.is());
220 0 : if( xProperties.is() )
221 : {
222 0 : double aOldValue = 0.0;
223 0 : xProperties->getPropertyValue( "ExtrapolateForward" ) >>= aOldValue;
224 0 : if (aOldValue != aValue)
225 : {
226 0 : xProperties->setPropertyValue( "ExtrapolateForward" , uno::makeAny( aValue ));
227 0 : bChanged = true;
228 : }
229 0 : }
230 : }
231 : }
232 0 : break;
233 :
234 : case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
235 : {
236 0 : if( xCurve.is())
237 : {
238 : double aValue = static_cast< double >(
239 : static_cast< const SvxDoubleItem & >(
240 0 : rItemSet.Get( nWhichId )).GetValue());
241 :
242 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
243 : OSL_ASSERT( xProperties.is());
244 0 : if( xProperties.is() )
245 : {
246 0 : double aOldValue = 0.0;
247 0 : xProperties->getPropertyValue( "ExtrapolateBackward" ) >>= aOldValue;
248 0 : if (aOldValue != aValue)
249 : {
250 0 : xProperties->setPropertyValue( "ExtrapolateBackward" , uno::makeAny( aValue ));
251 0 : bChanged = true;
252 : }
253 0 : }
254 : }
255 : }
256 0 : break;
257 :
258 : case SCHATTR_REGRESSION_SET_INTERCEPT:
259 : {
260 0 : if( xCurve.is())
261 : {
262 : sal_Bool bNewValue = static_cast< sal_Bool >(
263 : static_cast< const SfxBoolItem & >(
264 0 : rItemSet.Get( nWhichId )).GetValue());
265 :
266 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
267 : OSL_ASSERT( xProperties.is());
268 0 : if( xProperties.is() )
269 : {
270 0 : sal_Bool bOldValue = false;
271 0 : xProperties->getPropertyValue( "ForceIntercept" ) >>= bOldValue;
272 0 : if (bOldValue != bNewValue)
273 : {
274 0 : xProperties->setPropertyValue( "ForceIntercept" , uno::makeAny( bNewValue ));
275 0 : bChanged = true;
276 : }
277 0 : }
278 : }
279 : }
280 0 : break;
281 :
282 : case SCHATTR_REGRESSION_INTERCEPT_VALUE:
283 : {
284 0 : if( xCurve.is())
285 : {
286 : double aValue = static_cast< double >(
287 : static_cast< const SvxDoubleItem & >(
288 0 : rItemSet.Get( nWhichId )).GetValue());
289 :
290 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
291 : OSL_ASSERT( xProperties.is());
292 0 : if( xProperties.is() )
293 : {
294 0 : double aOldValue = 0.0;
295 0 : xProperties->getPropertyValue( "InterceptValue" ) >>= aOldValue;
296 0 : if (aOldValue != aValue)
297 : {
298 0 : xProperties->setPropertyValue( "InterceptValue" , uno::makeAny( aValue ));
299 0 : bChanged = true;
300 : }
301 0 : }
302 : }
303 : }
304 0 : break;
305 :
306 : case SCHATTR_REGRESSION_SHOW_EQUATION:
307 : {
308 : OSL_ASSERT( xCurve.is());
309 0 : if( xCurve.is())
310 : {
311 : bool bNewShow = static_cast< sal_Bool >(
312 : static_cast< const SfxBoolItem & >(
313 0 : rItemSet.Get( nWhichId )).GetValue());
314 :
315 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
316 : OSL_ASSERT( xEqProp.is());
317 0 : bool bOldShow = false;
318 0 : if( xEqProp.is() &&
319 0 : (xEqProp->getPropertyValue( "ShowEquation" ) >>= bOldShow) &&
320 0 : bOldShow != bNewShow )
321 : {
322 0 : xEqProp->setPropertyValue( "ShowEquation" , uno::makeAny( bNewShow ));
323 0 : bChanged = true;
324 0 : }
325 : }
326 : }
327 0 : break;
328 :
329 : case SCHATTR_REGRESSION_SHOW_COEFF:
330 : {
331 : OSL_ASSERT( xCurve.is());
332 0 : if( xCurve.is())
333 : {
334 : bool bNewShow = static_cast< sal_Bool >(
335 : static_cast< const SfxBoolItem & >(
336 0 : rItemSet.Get( nWhichId )).GetValue());
337 :
338 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
339 : OSL_ASSERT( xEqProp.is());
340 0 : bool bOldShow = false;
341 0 : if( xEqProp.is() &&
342 0 : (xEqProp->getPropertyValue( "ShowCorrelationCoefficient" ) >>= bOldShow) &&
343 0 : bOldShow != bNewShow )
344 : {
345 0 : xEqProp->setPropertyValue( "ShowCorrelationCoefficient" , uno::makeAny( bNewShow ));
346 0 : bChanged = true;
347 0 : }
348 : }
349 : }
350 0 : break;
351 : }
352 :
353 0 : return bChanged;
354 : }
355 :
356 0 : void RegressionCurveItemConverter::FillSpecialItem(
357 : sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
358 : throw( uno::Exception )
359 : {
360 0 : uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
361 :
362 0 : switch( nWhichId )
363 : {
364 : case SCHATTR_REGRESSION_TYPE:
365 : {
366 : OSL_ASSERT( xCurve.is());
367 0 : if( xCurve.is())
368 : {
369 : SvxChartRegress eRegress = static_cast< SvxChartRegress >(
370 0 : static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
371 0 : rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
372 : }
373 : }
374 0 : break;
375 :
376 : case SCHATTR_REGRESSION_DEGREE:
377 : {
378 : OSL_ASSERT( xCurve.is());
379 0 : if( xCurve.is())
380 : {
381 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
382 : OSL_ASSERT( xProperties.is());
383 0 : sal_Int32 aDegree = 1;
384 0 : if( xProperties.is() &&
385 0 : (xProperties->getPropertyValue( "PolynomialDegree" ) >>= aDegree))
386 : {
387 0 : rOutItemSet.Put( SfxInt32Item( nWhichId, aDegree ));
388 0 : }
389 : }
390 : }
391 0 : break;
392 :
393 : case SCHATTR_REGRESSION_PERIOD:
394 : {
395 : OSL_ASSERT( xCurve.is());
396 0 : if( xCurve.is())
397 : {
398 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
399 : OSL_ASSERT( xProperties.is());
400 0 : sal_Int32 aPeriod = 2;
401 0 : if( xProperties.is() &&
402 0 : (xProperties->getPropertyValue( "MovingAveragePeriod" ) >>= aPeriod))
403 : {
404 0 : rOutItemSet.Put( SfxInt32Item( nWhichId, aPeriod ));
405 0 : }
406 : }
407 : }
408 0 : break;
409 :
410 : case SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD:
411 : {
412 : OSL_ASSERT( xCurve.is());
413 0 : if( xCurve.is())
414 : {
415 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
416 : OSL_ASSERT( xProperties.is());
417 0 : double aValue = 0.0;
418 0 : if( xProperties.is() &&
419 0 : (xProperties->getPropertyValue( "ExtrapolateForward" ) >>= aValue))
420 : {
421 0 : rOutItemSet.Put( SvxDoubleItem( aValue, nWhichId ));
422 0 : }
423 : }
424 : }
425 0 : break;
426 :
427 : case SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD:
428 : {
429 : OSL_ASSERT( xCurve.is());
430 0 : if( xCurve.is())
431 : {
432 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
433 : OSL_ASSERT( xProperties.is());
434 0 : double aValue = 0.0;
435 0 : if( xProperties.is() &&
436 0 : (xProperties->getPropertyValue( "ExtrapolateBackward" ) >>= aValue))
437 : {
438 0 : rOutItemSet.Put( SvxDoubleItem( aValue, nWhichId ));
439 0 : }
440 : }
441 : }
442 0 : break;
443 :
444 : case SCHATTR_REGRESSION_SET_INTERCEPT:
445 : {
446 : OSL_ASSERT( xCurve.is());
447 0 : if( xCurve.is())
448 : {
449 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
450 : OSL_ASSERT( xProperties.is());
451 0 : sal_Bool bForceIntercept = false;
452 0 : if( xProperties.is() &&
453 0 : (xProperties->getPropertyValue( "ForceIntercept" ) >>= bForceIntercept))
454 : {
455 0 : rOutItemSet.Put( SfxBoolItem( nWhichId, bForceIntercept ));
456 0 : }
457 : }
458 : }
459 0 : break;
460 :
461 : case SCHATTR_REGRESSION_INTERCEPT_VALUE:
462 : {
463 : OSL_ASSERT( xCurve.is());
464 0 : if( xCurve.is())
465 : {
466 0 : uno::Reference< beans::XPropertySet > xProperties( xCurve, uno::UNO_QUERY );
467 : OSL_ASSERT( xProperties.is());
468 0 : double aValue = 0.0;
469 0 : if( xProperties.is() &&
470 0 : (xProperties->getPropertyValue( "InterceptValue" ) >>= aValue))
471 : {
472 0 : rOutItemSet.Put( SvxDoubleItem( aValue, nWhichId ));
473 0 : }
474 : }
475 : }
476 0 : break;
477 :
478 : case SCHATTR_REGRESSION_SHOW_EQUATION:
479 : {
480 : OSL_ASSERT( xCurve.is());
481 0 : if( xCurve.is())
482 : {
483 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
484 : OSL_ASSERT( xEqProp.is());
485 0 : bool bShow = false;
486 0 : if( xEqProp.is() &&
487 0 : (xEqProp->getPropertyValue( "ShowEquation" ) >>= bShow))
488 : {
489 0 : rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
490 0 : }
491 : }
492 : }
493 0 : break;
494 :
495 : case SCHATTR_REGRESSION_SHOW_COEFF:
496 : {
497 : OSL_ASSERT( xCurve.is());
498 0 : if( xCurve.is())
499 : {
500 0 : uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
501 : OSL_ASSERT( xEqProp.is());
502 0 : bool bShow = false;
503 0 : if( xEqProp.is() &&
504 0 : (xEqProp->getPropertyValue( "ShowCorrelationCoefficient" ) >>= bShow))
505 : {
506 0 : rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
507 0 : }
508 : }
509 : }
510 0 : break;
511 0 : }
512 0 : }
513 :
514 : } // namespace wrapper
515 : } // namespace chart
516 :
517 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|