1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
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
)
56 case SCALE_PROP_ORIGIN
:
57 m_aOuterName
= "Origin";
59 case SCALE_PROP_STEPMAIN
:
60 m_aOuterName
= "StepMain";
62 case SCALE_PROP_STEPHELP
:
63 m_aOuterName
= "StepHelp";
65 case SCALE_PROP_STEPHELP_COUNT
:
66 m_aOuterName
= "StepHelpCount";
68 case SCALE_PROP_AUTO_MAX
:
69 m_aOuterName
= "AutoMax";
71 case SCALE_PROP_AUTO_MIN
:
72 m_aOuterName
= "AutoMin";
74 case SCALE_PROP_AUTO_ORIGIN
:
75 m_aOuterName
= "AutoOrigin";
77 case SCALE_PROP_AUTO_STEPMAIN
:
78 m_aOuterName
= "AutoStepMain";
80 case SCALE_PROP_AUTO_STEPHELP
:
81 m_aOuterName
= "AutoStepHelp";
83 case SCALE_PROP_AXIS_TYPE
:
84 m_aOuterName
= "AxisType";
86 case SCALE_PROP_DATE_INCREMENT
:
87 m_aOuterName
= "TimeIncrement";
89 case SCALE_PROP_EXPLICIT_DATE_INCREMENT
:
90 m_aOuterName
= "ExplicitTimeIncrement";
92 case SCALE_PROP_LOGARITHMIC
:
93 m_aOuterName
= "Logarithmic";
95 case SCALE_PROP_REVERSEDIRECTION
:
96 m_aOuterName
= "ReverseDirection";
99 OSL_FAIL("unknown scale property");
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");
148 bool bSetScaleData
= false;
150 chart2::ScaleData
aScaleData( xAxis
->getScaleData() );
153 switch( eScaleProperty
)
157 aScaleData
.Maximum
= rOuterValue
;
158 bSetScaleData
= true;
163 aScaleData
.Minimum
= rOuterValue
;
164 bSetScaleData
= true;
167 case SCALE_PROP_STEPMAIN
:
169 aScaleData
.IncrementData
.Distance
= rOuterValue
;
170 bSetScaleData
= true;
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;
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
;
208 rSubIncrements
[ 0 ].IntervalCount
= Any();
209 bSetScaleData
= true;
212 case SCALE_PROP_AUTO_MAX
:
214 if( (rOuterValue
>>= bBool
) && bBool
)
215 aScaleData
.Maximum
= Any();
217 aScaleData
.Maximum
= getPropertyValue( SCALE_PROP_MAX
, xInnerPropertySet
);
218 bSetScaleData
= true;
221 case SCALE_PROP_AUTO_MIN
:
223 if( (rOuterValue
>>= bBool
) && bBool
)
224 aScaleData
.Minimum
= Any();
226 aScaleData
.Minimum
= getPropertyValue( SCALE_PROP_MIN
, xInnerPropertySet
);
227 bSetScaleData
= true;
230 case SCALE_PROP_AUTO_STEPMAIN
:
232 if( (rOuterValue
>>= bBool
) && bBool
)
233 aScaleData
.IncrementData
.Distance
= Any();
235 aScaleData
.IncrementData
.Distance
= getPropertyValue( SCALE_PROP_STEPMAIN
, xInnerPropertySet
);
236 bSetScaleData
= true;
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();
248 rSubIncrements
[ 0 ].IntervalCount
= getPropertyValue( SCALE_PROP_STEPHELP_COUNT
, xInnerPropertySet
);
249 bSetScaleData
= true;
252 case SCALE_PROP_ORIGIN
:
254 aScaleData
.Origin
= rOuterValue
;
255 bSetScaleData
= true;
258 case SCALE_PROP_AUTO_ORIGIN
:
260 if( (rOuterValue
>>= bBool
) && bBool
)
261 aScaleData
.Origin
= Any();
263 aScaleData
.Origin
= getPropertyValue( SCALE_PROP_ORIGIN
, xInnerPropertySet
);
264 bSetScaleData
= true;
267 case SCALE_PROP_AXIS_TYPE
:
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;
293 case SCALE_PROP_DATE_INCREMENT
:
295 TimeIncrement aTimeIncrement
;
296 rOuterValue
>>= aTimeIncrement
;
297 aScaleData
.TimeIncrement
= aTimeIncrement
;
298 bSetScaleData
= true;
301 case SCALE_PROP_EXPLICIT_DATE_INCREMENT
:
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
) )
314 aScaleData
.Scaling
= AxisHelper::createLogarithmicScaling( 10.0 );
316 aScaleData
.Scaling
= nullptr;
317 bSetScaleData
= true;
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;
337 OSL_FAIL("unknown scale property");
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");
355 chart2::ScaleData
aScaleData( xAxis
->getScaleData() );
357 ExplicitScaleData aExplicitScale
;
358 ExplicitIncrementData aExplicitIncrement
;
360 switch( eScaleProperty
)
364 aRet
= aScaleData
.Maximum
;
365 if( !aRet
.hasValue() )
367 m_spChart2ModelContact
->getExplicitValuesForAxis(
368 xAxis
, aExplicitScale
, aExplicitIncrement
);
369 aRet
<<= aExplicitScale
.Maximum
;
375 aRet
= aScaleData
.Minimum
;
376 if( !aRet
.hasValue() )
378 m_spChart2ModelContact
->getExplicitValuesForAxis(
379 xAxis
, aExplicitScale
, aExplicitIncrement
);
380 aRet
<<= aExplicitScale
.Minimum
;
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
;
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
);
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
) &&
423 aRet
<<= fStepMain
/ static_cast< double >( nIntervalCount
);
424 bNeedToCalculateExplicitValues
= false;
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 )
444 if( rSubIncrements
.hasElements() )
446 sal_Int32 nIntervalCount
= aExplicitIncrement
.SubIncrements
[ 0 ].IntervalCount
;
447 aRet
<<= double(nIntervalCount
);
451 aRet
<<= aExplicitIncrement
.Distance
/
452 static_cast< double >(
453 aExplicitIncrement
.SubIncrements
[ 0 ].IntervalCount
);
460 aRet
<<= aExplicitIncrement
.Distance
;
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
;
484 case SCALE_PROP_AUTO_MAX
:
486 aRet
<<= !aScaleData
.Maximum
.hasValue();
489 case SCALE_PROP_AUTO_MIN
:
491 aRet
<<= !aScaleData
.Minimum
.hasValue();
494 case SCALE_PROP_AUTO_STEPMAIN
:
496 aRet
<<= !aScaleData
.IncrementData
.Distance
.hasValue();
499 case SCALE_PROP_AUTO_STEPHELP
:
501 Sequence
< chart2::SubIncrement
>& rSubIncrements( aScaleData
.IncrementData
.SubIncrements
);
502 if( rSubIncrements
.hasElements() )
503 aRet
<<= !rSubIncrements
[ 0 ].IntervalCount
.hasValue();
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
;
519 case SCALE_PROP_AUTO_ORIGIN
:
521 aRet
<<= !hasDoubleValue(aScaleData
.Origin
);
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
;
539 case SCALE_PROP_DATE_INCREMENT
:
541 if( aScaleData
.AxisType
== AxisType::DATE
|| aScaleData
.AutoDateAxis
)
542 aRet
<<= aScaleData
.TimeIncrement
;
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
;
559 aRet
<<= aScaleData
.TimeIncrement
;
564 case SCALE_PROP_LOGARITHMIC
:
566 aRet
<<= AxisHelper::isLogarithmic(aScaleData
.Scaling
);
569 case SCALE_PROP_REVERSEDIRECTION
:
571 aRet
<<= aScaleData
.Orientation
== AxisOrientation_REVERSE
;
576 OSL_FAIL("unknown scale property");
584 } // namespace wrapper
587 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */