Update ooo320-m1
[ooovba.git] / chart2 / source / controller / chartapiwrapper / WrappedScaleProperty.cxx
blobf83f26ccf7c14a42517c6fb5bc79388c6fe95e2a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WrappedScaleProperty.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "WrappedScaleProperty.hxx"
35 #include "macros.hxx"
36 #include "CommonConverters.hxx"
37 #include "AxisHelper.hxx"
38 #include <com/sun/star/chart2/XAxis.hpp>
39 #include <com/sun/star/chart2/ExplicitIncrementData.hpp>
40 #include <com/sun/star/chart2/ExplicitScaleData.hpp>
41 #include <com/sun/star/chart2/AxisOrientation.hpp>
43 using namespace ::com::sun::star;
44 using ::com::sun::star::uno::Any;
45 using namespace ::com::sun::star::chart2;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::rtl::OUString;
50 //.............................................................................
51 namespace chart
53 namespace wrapper
56 WrappedScaleProperty::WrappedScaleProperty( tScaleProperty eScaleProperty
57 , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
58 : WrappedProperty(OUString(),OUString())
59 , m_spChart2ModelContact( spChart2ModelContact )
60 , m_eScaleProperty( eScaleProperty )
62 switch( m_eScaleProperty )
64 case SCALE_PROP_MAX:
65 m_aOuterName = C2U("Max");
66 break;
67 case SCALE_PROP_MIN:
68 m_aOuterName = C2U("Min");
69 break;
70 case SCALE_PROP_ORIGIN:
71 m_aOuterName = C2U("Origin");
72 break;
73 case SCALE_PROP_STEPMAIN:
74 m_aOuterName = C2U("StepMain");
75 break;
76 case SCALE_PROP_STEPHELP:
77 m_aOuterName = C2U("StepHelp");
78 break;
79 case SCALE_PROP_STEPHELP_COUNT:
80 m_aOuterName = C2U("StepHelpCount");
81 break;
82 case SCALE_PROP_AUTO_MAX:
83 m_aOuterName = C2U("AutoMax");
84 break;
85 case SCALE_PROP_AUTO_MIN:
86 m_aOuterName = C2U("AutoMin");
87 break;
88 case SCALE_PROP_AUTO_ORIGIN:
89 m_aOuterName = C2U("AutoOrigin");
90 break;
91 case SCALE_PROP_AUTO_STEPMAIN:
92 m_aOuterName = C2U("AutoStepMain");
93 break;
94 case SCALE_PROP_AUTO_STEPHELP:
95 m_aOuterName = C2U("AutoStepHelp");
96 break;
97 case SCALE_PROP_LOGARITHMIC:
98 m_aOuterName = C2U("Logarithmic");
99 break;
100 case SCALE_PROP_REVERSEDIRECTION:
101 m_aOuterName = C2U("ReverseDirection");
102 break;
103 default:
104 OSL_ENSURE(false,"unknown scale property");
105 break;
109 WrappedScaleProperty::~WrappedScaleProperty()
113 //static
114 void WrappedScaleProperty::addWrappedProperties( std::vector< WrappedProperty* >& rList
115 , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
117 rList.push_back( new WrappedScaleProperty( SCALE_PROP_MAX, spChart2ModelContact ) );
118 rList.push_back( new WrappedScaleProperty( SCALE_PROP_MIN, spChart2ModelContact ) );
119 rList.push_back( new WrappedScaleProperty( SCALE_PROP_ORIGIN, spChart2ModelContact ) );
120 rList.push_back( new WrappedScaleProperty( SCALE_PROP_STEPMAIN, spChart2ModelContact ) );
121 rList.push_back( new WrappedScaleProperty( SCALE_PROP_STEPHELP, spChart2ModelContact ) );
122 rList.push_back( new WrappedScaleProperty( SCALE_PROP_STEPHELP_COUNT, spChart2ModelContact ) );
123 rList.push_back( new WrappedScaleProperty( SCALE_PROP_AUTO_MAX, spChart2ModelContact ) );
124 rList.push_back( new WrappedScaleProperty( SCALE_PROP_AUTO_MIN, spChart2ModelContact ) );
125 rList.push_back( new WrappedScaleProperty( SCALE_PROP_AUTO_ORIGIN, spChart2ModelContact ) );
126 rList.push_back( new WrappedScaleProperty( SCALE_PROP_AUTO_STEPMAIN, spChart2ModelContact ) );
127 rList.push_back( new WrappedScaleProperty( SCALE_PROP_AUTO_STEPHELP, spChart2ModelContact ) );
128 rList.push_back( new WrappedScaleProperty( SCALE_PROP_LOGARITHMIC, spChart2ModelContact ) );
129 rList.push_back( new WrappedScaleProperty( SCALE_PROP_REVERSEDIRECTION, spChart2ModelContact ) );
132 void WrappedScaleProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
133 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
135 setPropertyValue( m_eScaleProperty, rOuterValue, xInnerPropertySet );
138 Any WrappedScaleProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
139 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
141 return getPropertyValue( m_eScaleProperty, xInnerPropertySet );
144 void WrappedScaleProperty::setPropertyValue( tScaleProperty eScaleProperty, const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
145 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
147 m_aOuterValue = rOuterValue;
149 Reference< chart2::XAxis > xAxis( xInnerPropertySet, uno::UNO_QUERY );
150 OSL_ENSURE(xAxis.is(),"need an XAxis");
151 if(!xAxis.is())
152 return;
154 bool bSetScaleData = false;
156 chart2::ScaleData aScaleData( xAxis->getScaleData() );
158 sal_Bool bBool = false;
159 switch( eScaleProperty )
161 case SCALE_PROP_MAX:
163 aScaleData.Maximum = rOuterValue;
164 bSetScaleData = true;
165 break;
167 case SCALE_PROP_MIN:
169 aScaleData.Minimum = rOuterValue;
170 bSetScaleData = true;
171 break;
173 case SCALE_PROP_STEPMAIN:
175 aScaleData.IncrementData.Distance = rOuterValue;
176 bSetScaleData = true;
177 break;
179 case SCALE_PROP_STEPHELP:
181 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
182 if( rSubIncrements.getLength() == 0 )
183 rSubIncrements.realloc( 1 );
185 double fStepMain = 0, fStepHelp = 0;
186 if( (rOuterValue >>= fStepHelp) )
188 if( AxisHelper::isLogarithmic(aScaleData.Scaling) )
190 sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepHelp);
191 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
193 else if( (fStepHelp != 0.0) &&
194 (aScaleData.IncrementData.Distance >>= fStepMain) )
196 // approximate interval count
197 sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepMain / fStepHelp);
198 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
201 bSetScaleData = true;
202 break;
204 case SCALE_PROP_STEPHELP_COUNT:
206 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
207 if( rSubIncrements.getLength() == 0 )
208 rSubIncrements.realloc( 1 );
209 sal_Int32 nIntervalCount=0;
210 if( rOuterValue>>=nIntervalCount )
211 rSubIncrements[ 0 ].IntervalCount <<= nIntervalCount;
212 else
213 rSubIncrements[ 0 ].IntervalCount = Any();
214 bSetScaleData = true;
215 break;
217 case SCALE_PROP_AUTO_MAX:
219 if( (rOuterValue >>= bBool) && bBool )
220 aScaleData.Maximum = Any();
221 else
222 aScaleData.Maximum = getPropertyValue( SCALE_PROP_MAX, xInnerPropertySet );
223 bSetScaleData = true;
224 break;
226 case SCALE_PROP_AUTO_MIN:
228 if( (rOuterValue >>= bBool) && bBool )
229 aScaleData.Minimum = Any();
230 else
231 aScaleData.Minimum = getPropertyValue( SCALE_PROP_MIN, xInnerPropertySet );
232 bSetScaleData = true;
233 break;
235 case SCALE_PROP_AUTO_STEPMAIN:
237 if( (rOuterValue >>= bBool) && bBool )
238 aScaleData.IncrementData.Distance = Any();
239 else
240 aScaleData.IncrementData.Distance = getPropertyValue( SCALE_PROP_STEPMAIN, xInnerPropertySet );
241 bSetScaleData = true;
242 break;
244 case SCALE_PROP_AUTO_STEPHELP:
246 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
247 if( rSubIncrements.getLength() == 0 )
248 rSubIncrements.realloc( 1 );
250 if( (rOuterValue >>= bBool) && bBool )
251 rSubIncrements[ 0 ].IntervalCount = Any();
252 else
253 rSubIncrements[ 0 ].IntervalCount = getPropertyValue( SCALE_PROP_STEPHELP_COUNT, xInnerPropertySet );
254 bSetScaleData = true;
255 break;
257 case SCALE_PROP_ORIGIN:
259 aScaleData.Origin = rOuterValue;
260 bSetScaleData = true;
261 break;
263 case SCALE_PROP_AUTO_ORIGIN:
265 if( (rOuterValue >>= bBool) && bBool )
266 aScaleData.Origin = Any();
267 else
268 aScaleData.Origin = getPropertyValue( SCALE_PROP_ORIGIN, xInnerPropertySet );
269 bSetScaleData = true;
270 break;
272 case SCALE_PROP_LOGARITHMIC:
274 if( rOuterValue >>= bBool )
276 bool bWasLogarithm = AxisHelper::isLogarithmic( aScaleData.Scaling );
278 // safe comparison between sal_Bool and bool
279 if( (!bBool) != (!bWasLogarithm) )
281 if( bBool )
282 aScaleData.Scaling = AxisHelper::createLogarithmicScaling( 10.0 );
283 else
284 aScaleData.Scaling = 0;
285 bSetScaleData = true;
288 break;
290 case SCALE_PROP_REVERSEDIRECTION:
292 if( rOuterValue >>= bBool )
294 bool bWasReverse = ( AxisOrientation_REVERSE == aScaleData.Orientation );
295 if( (!bBool) != (!bWasReverse) ) // safe comparison between sal_Bool and bool
297 aScaleData.Orientation = bBool ? AxisOrientation_REVERSE : AxisOrientation_MATHEMATICAL;
298 bSetScaleData = true;
301 break;
303 default:
305 OSL_ENSURE(false,"unknown scale property");
306 break;
310 if( bSetScaleData )
311 xAxis->setScaleData( aScaleData );
314 Any WrappedScaleProperty::getPropertyValue( tScaleProperty eScaleProperty, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
315 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
317 Any aRet( m_aOuterValue );
319 Reference< chart2::XAxis > xAxis( xInnerPropertySet, uno::UNO_QUERY );
320 OSL_ENSURE(xAxis.is(),"need an XAxis");
321 if(!xAxis.is())
322 return aRet;
324 chart2::ScaleData aScaleData( xAxis->getScaleData() );
326 chart2::ExplicitScaleData aExplicitScale;
327 chart2::ExplicitIncrementData aExplicitIncrement;
329 switch( eScaleProperty )
331 case SCALE_PROP_MAX:
333 aRet = aScaleData.Maximum;
334 if( !aRet.hasValue() )
336 m_spChart2ModelContact->getExplicitValuesForAxis(
337 xAxis, aExplicitScale, aExplicitIncrement );
338 aRet <<= aExplicitScale.Maximum;
340 break;
342 case SCALE_PROP_MIN:
344 aRet = aScaleData.Minimum;
345 if( !aRet.hasValue() )
347 m_spChart2ModelContact->getExplicitValuesForAxis(
348 xAxis, aExplicitScale, aExplicitIncrement );
349 aRet <<= aExplicitScale.Minimum;
351 break;
354 case SCALE_PROP_STEPMAIN:
356 aRet = aScaleData.IncrementData.Distance;
357 if( !aRet.hasValue() )
359 m_spChart2ModelContact->getExplicitValuesForAxis(
360 xAxis, aExplicitScale, aExplicitIncrement );
361 aRet <<= aExplicitIncrement.Distance;
363 break;
365 case SCALE_PROP_STEPHELP:
367 // todo: evaluate PostEquidistant
368 bool bNeedToCalculateExplicitValues = true;
370 bool bLogarithmic( AxisHelper::isLogarithmic(aScaleData.Scaling) );
371 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
372 if( bLogarithmic )
374 if( rSubIncrements.getLength() > 0 )
376 sal_Int32 nIntervalCount = 0;
377 rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount;
378 aRet = uno::makeAny( double(nIntervalCount) );
379 bNeedToCalculateExplicitValues = false;
382 else if( aScaleData.IncrementData.Distance.hasValue() )
384 if( rSubIncrements.getLength() > 0 )
386 double fStepMain = 0;
387 sal_Int32 nIntervalCount = 0;
388 if( (aScaleData.IncrementData.Distance >>= fStepMain) &&
389 (rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount) &&
390 nIntervalCount > 0 )
392 aRet <<= ( fStepMain / static_cast< double >( nIntervalCount ) );
393 bNeedToCalculateExplicitValues = false;
396 else
398 aRet = aScaleData.IncrementData.Distance;
399 bNeedToCalculateExplicitValues = false;
403 if( bNeedToCalculateExplicitValues )
405 m_spChart2ModelContact->getExplicitValuesForAxis(
406 xAxis, aExplicitScale, aExplicitIncrement );
407 if( aExplicitIncrement.SubIncrements.getLength() > 0 &&
408 aExplicitIncrement.SubIncrements[ 0 ].IntervalCount > 0 )
410 if( bLogarithmic )
412 if( rSubIncrements.getLength() > 0 )
414 sal_Int32 nIntervalCount = aExplicitIncrement.SubIncrements[ 0 ].IntervalCount;
415 aRet = uno::makeAny( double(nIntervalCount) );
418 else
419 aRet <<= ( aExplicitIncrement.Distance /
420 static_cast< double >(
421 aExplicitIncrement.SubIncrements[ 0 ].IntervalCount ));
423 else
425 if( bLogarithmic )
426 aRet <<= 5.0;
427 else
428 aRet <<= aExplicitIncrement.Distance;
431 break;
433 case SCALE_PROP_STEPHELP_COUNT:
435 sal_Int32 nIntervalCount = 0;
436 bool bNeedToCalculateExplicitValues = true;
437 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
438 if( rSubIncrements.getLength() > 0 )
440 if( (rSubIncrements[ 0 ].IntervalCount >>= nIntervalCount) && (nIntervalCount > 0) )
441 bNeedToCalculateExplicitValues = false;
443 if( bNeedToCalculateExplicitValues )
445 m_spChart2ModelContact->getExplicitValuesForAxis( xAxis, aExplicitScale, aExplicitIncrement );
446 if( aExplicitIncrement.SubIncrements.getLength() > 0 )
447 nIntervalCount = aExplicitIncrement.SubIncrements[ 0 ].IntervalCount;
449 aRet = uno::makeAny( nIntervalCount );
450 break;
452 case SCALE_PROP_AUTO_MAX:
454 aRet <<= (sal_Bool)( !aScaleData.Maximum.hasValue() );
455 break;
457 case SCALE_PROP_AUTO_MIN:
459 aRet <<= (sal_Bool)( !aScaleData.Minimum.hasValue() );
460 break;
462 case SCALE_PROP_AUTO_STEPMAIN:
464 aRet <<= (sal_Bool)( !aScaleData.IncrementData.Distance.hasValue() );
465 break;
467 case SCALE_PROP_AUTO_STEPHELP:
469 Sequence< chart2::SubIncrement >& rSubIncrements( aScaleData.IncrementData.SubIncrements );
470 if( rSubIncrements.getLength() > 0 )
471 aRet <<= (sal_Bool)( !rSubIncrements[ 0 ].IntervalCount.hasValue() );
472 else
473 aRet <<= sal_True;
474 break;
476 case SCALE_PROP_ORIGIN:
478 aRet = aScaleData.Origin;
479 if( !aRet.hasValue() )
481 m_spChart2ModelContact->getExplicitValuesForAxis(
482 xAxis, aExplicitScale, aExplicitIncrement );
483 aRet <<= aExplicitScale.Origin;
485 break;
487 case SCALE_PROP_AUTO_ORIGIN:
489 aRet <<= !hasDoubleValue(aScaleData.Origin);
490 break;
492 case SCALE_PROP_LOGARITHMIC:
494 aRet <<= static_cast< sal_Bool >( AxisHelper::isLogarithmic(aScaleData.Scaling) );
495 break;
497 case SCALE_PROP_REVERSEDIRECTION:
499 aRet <<= static_cast< sal_Bool >( AxisOrientation_REVERSE == aScaleData.Orientation );
500 break;
502 default:
504 OSL_ENSURE(false,"unknown scale property");
505 break;
509 return aRet;
512 } // namespace wrapper
513 } //namespace chart
514 //.............................................................................