Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / WrappedScaleProperty.cxx
blob73f2fbc61ec1ce2493392c5b855ac560756c31e7
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 "WrappedScaleProperty.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <CommonConverters.hxx>
23 #include <AxisHelper.hxx>
24 #include <com/sun/star/chart2/AxisType.hpp>
25 #include <com/sun/star/chart2/XAxis.hpp>
26 #include <com/sun/star/chart/ChartAxisType.hpp>
27 #include <chartview/ExplicitScaleValues.hxx>
28 #include <osl/diagnose.h>
30 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Any;
32 using namespace ::com::sun::star::chart2;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::Sequence;
35 using ::com::sun::star::chart::TimeIncrement;
37 namespace chart
39 namespace wrapper
42 WrappedScaleProperty::WrappedScaleProperty(tScaleProperty eScaleProperty
43 , const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
44 : WrappedProperty(OUString(),OUString())
45 , m_spChart2ModelContact( spChart2ModelContact )
46 , m_eScaleProperty( eScaleProperty )
48 switch( m_eScaleProperty )
50 case SCALE_PROP_MAX:
51 m_aOuterName = "Max";
52 break;
53 case SCALE_PROP_MIN:
54 m_aOuterName = "Min";
55 break;
56 case SCALE_PROP_ORIGIN:
57 m_aOuterName = "Origin";
58 break;
59 case SCALE_PROP_STEPMAIN:
60 m_aOuterName = "StepMain";
61 break;
62 case SCALE_PROP_STEPHELP:
63 m_aOuterName = "StepHelp";
64 break;
65 case SCALE_PROP_STEPHELP_COUNT:
66 m_aOuterName = "StepHelpCount";
67 break;
68 case SCALE_PROP_AUTO_MAX:
69 m_aOuterName = "AutoMax";
70 break;
71 case SCALE_PROP_AUTO_MIN:
72 m_aOuterName = "AutoMin";
73 break;
74 case SCALE_PROP_AUTO_ORIGIN:
75 m_aOuterName = "AutoOrigin";
76 break;
77 case SCALE_PROP_AUTO_STEPMAIN:
78 m_aOuterName = "AutoStepMain";
79 break;
80 case SCALE_PROP_AUTO_STEPHELP:
81 m_aOuterName = "AutoStepHelp";
82 break;
83 case SCALE_PROP_AXIS_TYPE:
84 m_aOuterName = "AxisType";
85 break;
86 case SCALE_PROP_DATE_INCREMENT:
87 m_aOuterName = "TimeIncrement";
88 break;
89 case SCALE_PROP_EXPLICIT_DATE_INCREMENT:
90 m_aOuterName = "ExplicitTimeIncrement";
91 break;
92 case SCALE_PROP_LOGARITHMIC:
93 m_aOuterName = "Logarithmic";
94 break;
95 case SCALE_PROP_REVERSEDIRECTION:
96 m_aOuterName = "ReverseDirection";
97 break;
98 default:
99 OSL_FAIL("unknown scale property");
100 break;
104 WrappedScaleProperty::~WrappedScaleProperty()
108 void WrappedScaleProperty::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
109 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
111 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_MAX, spChart2ModelContact ) );
112 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_MIN, spChart2ModelContact ) );
113 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_ORIGIN, spChart2ModelContact ) );
114 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_STEPMAIN, spChart2ModelContact ) );
115 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_STEPHELP, spChart2ModelContact ) );
116 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_STEPHELP_COUNT, spChart2ModelContact ) );
117 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AUTO_MAX, spChart2ModelContact ) );
118 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AUTO_MIN, spChart2ModelContact ) );
119 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AUTO_ORIGIN, spChart2ModelContact ) );
120 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AUTO_STEPMAIN, spChart2ModelContact ) );
121 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AUTO_STEPHELP, spChart2ModelContact ) );
122 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_AXIS_TYPE, spChart2ModelContact ) );
123 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_DATE_INCREMENT, spChart2ModelContact ) );
124 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_EXPLICIT_DATE_INCREMENT, spChart2ModelContact ) );
125 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_LOGARITHMIC, spChart2ModelContact ) );
126 rList.emplace_back( new WrappedScaleProperty( SCALE_PROP_REVERSEDIRECTION, spChart2ModelContact ) );
129 void WrappedScaleProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
131 setPropertyValue( m_eScaleProperty, rOuterValue, xInnerPropertySet );
134 Any WrappedScaleProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
136 return getPropertyValue( m_eScaleProperty, xInnerPropertySet );
139 void WrappedScaleProperty::setPropertyValue( tScaleProperty eScaleProperty, const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
141 m_aOuterValue = rOuterValue;
143 Reference< chart2::XAxis > xAxis( xInnerPropertySet, uno::UNO_QUERY );
144 OSL_ENSURE(xAxis.is(),"need an XAxis");
145 if(!xAxis.is())
146 return;
148 bool bSetScaleData = false;
150 chart2::ScaleData aScaleData( xAxis->getScaleData() );
152 bool bBool = false;
153 switch( eScaleProperty )
155 case SCALE_PROP_MAX:
157 aScaleData.Maximum = rOuterValue;
158 bSetScaleData = true;
159 break;
161 case SCALE_PROP_MIN:
163 aScaleData.Minimum = rOuterValue;
164 bSetScaleData = true;
165 break;
167 case SCALE_PROP_STEPMAIN:
169 aScaleData.IncrementData.Distance = rOuterValue;
170 bSetScaleData = true;
171 break;
173 case SCALE_PROP_STEPHELP:
175 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
176 if( !rSubIncrements.hasElements() )
177 rSubIncrements.realloc( 1 );
179 double fStepHelp = 0;
180 if( rOuterValue >>= fStepHelp )
182 double fStepMain = 0;
183 if( AxisHelper::isLogarithmic(aScaleData.Scaling) )
185 sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepHelp);
186 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
188 else if( (fStepHelp != 0.0) &&
189 (aScaleData.IncrementData.Distance >>= fStepMain) )
191 // approximate interval count
192 sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepMain / fStepHelp);//cppcheck-suppress zerodiv
193 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
196 bSetScaleData = true;
197 break;
199 case SCALE_PROP_STEPHELP_COUNT:
201 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
202 if( !rSubIncrements.hasElements() )
203 rSubIncrements.realloc( 1 );
204 sal_Int32 nIntervalCount=0;
205 if( rOuterValue>>=nIntervalCount )
206 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
207 else
208 rSubIncrements[ 0 ].IntervalCount = Any();
209 bSetScaleData = true;
210 break;
212 case SCALE_PROP_AUTO_MAX:
214 if( (rOuterValue >>= bBool) && bBool )
215 aScaleData.Maximum = Any();
216 else
217 aScaleData.Maximum = getPropertyValue( SCALE_PROP_MAX, xInnerPropertySet );
218 bSetScaleData = true;
219 break;
221 case SCALE_PROP_AUTO_MIN:
223 if( (rOuterValue >>= bBool) && bBool )
224 aScaleData.Minimum = Any();
225 else
226 aScaleData.Minimum = getPropertyValue( SCALE_PROP_MIN, xInnerPropertySet );
227 bSetScaleData = true;
228 break;
230 case SCALE_PROP_AUTO_STEPMAIN:
232 if( (rOuterValue >>= bBool) && bBool )
233 aScaleData.IncrementData.Distance = Any();
234 else
235 aScaleData.IncrementData.Distance = getPropertyValue( SCALE_PROP_STEPMAIN, xInnerPropertySet );
236 bSetScaleData = true;
237 break;
239 case SCALE_PROP_AUTO_STEPHELP:
241 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
242 if( !rSubIncrements.hasElements() )
243 rSubIncrements.realloc( 1 );
245 if( (rOuterValue >>= bBool) && bBool )
246 rSubIncrements[ 0 ].IntervalCount = Any();
247 else
248 rSubIncrements[ 0 ].IntervalCount = getPropertyValue( SCALE_PROP_STEPHELP_COUNT, xInnerPropertySet );
249 bSetScaleData = true;
250 break;
252 case SCALE_PROP_ORIGIN:
254 aScaleData.Origin = rOuterValue;
255 bSetScaleData = true;
256 break;
258 case SCALE_PROP_AUTO_ORIGIN:
260 if( (rOuterValue >>= bBool) && bBool )
261 aScaleData.Origin = Any();
262 else
263 aScaleData.Origin = getPropertyValue( SCALE_PROP_ORIGIN, xInnerPropertySet );
264 bSetScaleData = true;
265 break;
267 case SCALE_PROP_AXIS_TYPE:
269 sal_Int32 nType = 0;
270 if( rOuterValue >>= nType )
272 if( nType == css::chart::ChartAxisType::AUTOMATIC )
274 aScaleData.AutoDateAxis = true;
275 if( aScaleData.AxisType == AxisType::DATE )
276 aScaleData.AxisType = AxisType::CATEGORY;
278 else if( nType == css::chart::ChartAxisType::CATEGORY )
280 aScaleData.AutoDateAxis = false;
281 if( aScaleData.AxisType == AxisType::DATE )
282 aScaleData.AxisType = AxisType::CATEGORY;
284 else if( nType == css::chart::ChartAxisType::DATE )
286 if( aScaleData.AxisType == AxisType::CATEGORY )
287 aScaleData.AxisType = AxisType::DATE;
289 bSetScaleData = true;
291 break;
293 case SCALE_PROP_DATE_INCREMENT:
295 TimeIncrement aTimeIncrement;
296 rOuterValue >>= aTimeIncrement;
297 aScaleData.TimeIncrement = aTimeIncrement;
298 bSetScaleData = true;
299 break;
301 case SCALE_PROP_EXPLICIT_DATE_INCREMENT:
302 //read only property
303 break;
304 case SCALE_PROP_LOGARITHMIC:
306 if( rOuterValue >>= bBool )
308 bool bWasLogarithm = AxisHelper::isLogarithmic( aScaleData.Scaling );
310 // safe comparison between sal_Bool and bool
311 if( (!bBool) != (!bWasLogarithm) )
313 if( bBool )
314 aScaleData.Scaling = AxisHelper::createLogarithmicScaling( 10.0 );
315 else
316 aScaleData.Scaling = nullptr;
317 bSetScaleData = true;
320 break;
322 case SCALE_PROP_REVERSEDIRECTION:
324 if( rOuterValue >>= bBool )
326 bool bWasReverse = ( aScaleData.Orientation == AxisOrientation_REVERSE );
327 if( (!bBool) != (!bWasReverse) ) // safe comparison between sal_Bool and bool
329 aScaleData.Orientation = bBool ? AxisOrientation_REVERSE : AxisOrientation_MATHEMATICAL;
330 bSetScaleData = true;
333 break;
335 default:
337 OSL_FAIL("unknown scale property");
338 break;
342 if( bSetScaleData )
343 xAxis->setScaleData( aScaleData );
346 Any WrappedScaleProperty::getPropertyValue( tScaleProperty eScaleProperty, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
348 Any aRet( m_aOuterValue );
350 Reference< chart2::XAxis > xAxis( xInnerPropertySet, uno::UNO_QUERY );
351 OSL_ENSURE(xAxis.is(),"need an XAxis");
352 if(!xAxis.is())
353 return aRet;
355 chart2::ScaleData aScaleData( xAxis->getScaleData() );
357 ExplicitScaleData aExplicitScale;
358 ExplicitIncrementData aExplicitIncrement;
360 switch( eScaleProperty )
362 case SCALE_PROP_MAX:
364 aRet = aScaleData.Maximum;
365 if( !aRet.hasValue() )
367 m_spChart2ModelContact->getExplicitValuesForAxis(
368 xAxis, aExplicitScale, aExplicitIncrement );
369 aRet <<= aExplicitScale.Maximum;
371 break;
373 case SCALE_PROP_MIN:
375 aRet = aScaleData.Minimum;
376 if( !aRet.hasValue() )
378 m_spChart2ModelContact->getExplicitValuesForAxis(
379 xAxis, aExplicitScale, aExplicitIncrement );
380 aRet <<= aExplicitScale.Minimum;
382 break;
385 case SCALE_PROP_STEPMAIN:
387 aRet = aScaleData.IncrementData.Distance;
388 if( !aRet.hasValue() )
390 m_spChart2ModelContact->getExplicitValuesForAxis(
391 xAxis, aExplicitScale, aExplicitIncrement );
392 aRet <<= aExplicitIncrement.Distance;
394 break;
396 case SCALE_PROP_STEPHELP:
398 // todo: evaluate PostEquidistant
399 bool bNeedToCalculateExplicitValues = true;
401 bool bLogarithmic( AxisHelper::isLogarithmic(aScaleData.Scaling) );
402 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
403 if( bLogarithmic )
405 if( rSubIncrements.hasElements() )
407 sal_Int32 nIntervalCount = 0;
408 rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount;
409 aRet <<= double(nIntervalCount);
410 bNeedToCalculateExplicitValues = false;
413 else if( aScaleData.IncrementData.Distance.hasValue() )
415 if( rSubIncrements.hasElements() )
417 double fStepMain = 0;
418 sal_Int32 nIntervalCount = 0;
419 if( (aScaleData.IncrementData.Distance >>= fStepMain) &&
420 (rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount) &&
421 nIntervalCount > 0 )
423 aRet <<= fStepMain / static_cast< double >( nIntervalCount );
424 bNeedToCalculateExplicitValues = false;
427 else
429 aRet = aScaleData.IncrementData.Distance;
430 bNeedToCalculateExplicitValues = false;
434 if( bNeedToCalculateExplicitValues )
436 m_spChart2ModelContact->getExplicitValuesForAxis(
437 xAxis, aExplicitScale, aExplicitIncrement );
439 if( !aExplicitIncrement.SubIncrements.empty() &&
440 aExplicitIncrement.SubIncrements[ 0 ].IntervalCount > 0 )
442 if( bLogarithmic )
444 if( rSubIncrements.hasElements() )
446 sal_Int32 nIntervalCount = aExplicitIncrement.SubIncrements[ 0 ].IntervalCount;
447 aRet <<= double(nIntervalCount);
450 else
451 aRet <<= aExplicitIncrement.Distance /
452 static_cast< double >(
453 aExplicitIncrement.SubIncrements[ 0 ].IntervalCount );
455 else
457 if( bLogarithmic )
458 aRet <<= 5.0;
459 else
460 aRet <<= aExplicitIncrement.Distance;
463 break;
465 case SCALE_PROP_STEPHELP_COUNT:
467 sal_Int32 nIntervalCount = 0;
468 bool bNeedToCalculateExplicitValues = true;
469 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
470 if( rSubIncrements.hasElements() )
472 if( (rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount) && (nIntervalCount > 0) )
473 bNeedToCalculateExplicitValues = false;
475 if( bNeedToCalculateExplicitValues )
477 m_spChart2ModelContact->getExplicitValuesForAxis( xAxis, aExplicitScale, aExplicitIncrement );
478 if( !aExplicitIncrement.SubIncrements.empty() )
479 nIntervalCount = aExplicitIncrement.SubIncrements[ 0 ].IntervalCount;
481 aRet <<= nIntervalCount;
482 break;
484 case SCALE_PROP_AUTO_MAX:
486 aRet <<= !aScaleData.Maximum.hasValue();
487 break;
489 case SCALE_PROP_AUTO_MIN:
491 aRet <<= !aScaleData.Minimum.hasValue();
492 break;
494 case SCALE_PROP_AUTO_STEPMAIN:
496 aRet <<= !aScaleData.IncrementData.Distance.hasValue();
497 break;
499 case SCALE_PROP_AUTO_STEPHELP:
501 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
502 if( rSubIncrements.hasElements() )
503 aRet <<= !rSubIncrements[ 0 ].IntervalCount.hasValue();
504 else
505 aRet <<= true;
506 break;
508 case SCALE_PROP_ORIGIN:
510 aRet = aScaleData.Origin;
511 if( !aRet.hasValue() )
513 m_spChart2ModelContact->getExplicitValuesForAxis(
514 xAxis, aExplicitScale, aExplicitIncrement );
515 aRet <<= aExplicitScale.Origin;
517 break;
519 case SCALE_PROP_AUTO_ORIGIN:
521 aRet <<= !hasDoubleValue(aScaleData.Origin);
522 break;
524 case SCALE_PROP_AXIS_TYPE:
526 sal_Int32 nType = css::chart::ChartAxisType::AUTOMATIC;
527 if( aScaleData.AxisType == AxisType::DATE )
529 nType = css::chart::ChartAxisType::DATE;
531 else if( aScaleData.AxisType == AxisType::CATEGORY )
533 if( !aScaleData.AutoDateAxis )
534 nType = css::chart::ChartAxisType::CATEGORY;
536 aRet <<= nType;
537 break;
539 case SCALE_PROP_DATE_INCREMENT:
541 if( aScaleData.AxisType == AxisType::DATE || aScaleData.AutoDateAxis )
542 aRet <<= aScaleData.TimeIncrement;
543 break;
545 case SCALE_PROP_EXPLICIT_DATE_INCREMENT:
547 if( aScaleData.AxisType == AxisType::DATE || aScaleData.AutoDateAxis )
549 m_spChart2ModelContact->getExplicitValuesForAxis( xAxis, aExplicitScale, aExplicitIncrement );
550 if( aExplicitScale.AxisType == AxisType::DATE )
552 TimeIncrement aTimeIncrement;
553 aTimeIncrement.MajorTimeInterval <<= aExplicitIncrement.MajorTimeInterval;
554 aTimeIncrement.MinorTimeInterval <<= aExplicitIncrement.MinorTimeInterval;
555 aTimeIncrement.TimeResolution <<= aExplicitScale.TimeResolution;
556 aRet <<= aTimeIncrement;
558 else
559 aRet <<= aScaleData.TimeIncrement;
562 break;
564 case SCALE_PROP_LOGARITHMIC:
566 aRet <<= AxisHelper::isLogarithmic(aScaleData.Scaling);
567 break;
569 case SCALE_PROP_REVERSEDIRECTION:
571 aRet <<= aScaleData.Orientation == AxisOrientation_REVERSE;
572 break;
574 default:
576 OSL_FAIL("unknown scale property");
577 break;
581 return aRet;
584 } // namespace wrapper
585 } // namespace chart
587 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */