merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / model / template / StockChartTypeTemplate.cxx
blobfc9538a875d377c2dbb230aab9e7a576d34f8b92
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: StockChartTypeTemplate.cxx,v $
10 * $Revision: 1.12 $
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"
33 #include "StockChartTypeTemplate.hxx"
34 #include "macros.hxx"
35 #include "DataSeriesHelper.hxx"
36 #include "StockDataInterpreter.hxx"
37 #include "CartesianCoordinateSystem.hxx"
38 #include "AxisHelper.hxx"
39 #include "DiagramHelper.hxx"
40 #include "servicenames_charttypes.hxx"
41 #include "servicenames_coosystems.hxx"
42 #include "ContainerHelper.hxx"
43 #include "AxisIndexDefines.hxx"
44 #include <com/sun/star/chart2/AxisType.hpp>
45 #include <com/sun/star/chart2/SymbolStyle.hpp>
46 #include <com/sun/star/chart2/data/XDataSource.hpp>
47 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
48 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
49 #include "PropertyHelper.hxx"
50 #include <com/sun/star/beans/PropertyAttribute.hpp>
51 #include <com/sun/star/drawing/LineStyle.hpp>
53 #include <vector>
54 #include <algorithm>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::chart2;
59 using ::com::sun::star::uno::Reference;
60 using ::com::sun::star::uno::Sequence;
61 using ::rtl::OUString;
62 using ::com::sun::star::beans::Property;
63 using ::com::sun::star::uno::Any;
64 using ::osl::MutexGuard;
66 // ----------------------------------------
67 namespace
70 static const OUString lcl_aServiceName(
71 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.StockChartTypeTemplate" ));
73 enum
75 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
76 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
77 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
78 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE
81 void lcl_AddPropertiesToVector(
82 ::std::vector< Property > & rOutProperties )
84 rOutProperties.push_back(
85 Property( C2U( "Volume" ),
86 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
87 ::getBooleanCppuType(),
88 beans::PropertyAttribute::BOUND
89 | beans::PropertyAttribute::MAYBEDEFAULT ));
90 rOutProperties.push_back(
91 Property( C2U( "Open" ),
92 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
93 ::getBooleanCppuType(),
94 beans::PropertyAttribute::BOUND
95 | beans::PropertyAttribute::MAYBEDEFAULT ));
96 rOutProperties.push_back(
97 Property( C2U( "LowHigh" ),
98 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
99 ::getBooleanCppuType(),
100 beans::PropertyAttribute::BOUND
101 | beans::PropertyAttribute::MAYBEDEFAULT ));
102 rOutProperties.push_back(
103 Property( C2U( "Japanese" ),
104 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
105 ::getBooleanCppuType(),
106 beans::PropertyAttribute::BOUND
107 | beans::PropertyAttribute::MAYBEDEFAULT ));
110 void lcl_AddDefaultsToMap(
111 ::chart::tPropertyValueMap & rOutMap )
113 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME, false );
114 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_OPEN, false );
115 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH, true );
116 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE, false );
119 const Sequence< Property > & lcl_GetPropertySequence()
121 static Sequence< Property > aPropSeq;
123 // /--
124 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
125 if( 0 == aPropSeq.getLength() )
127 // get properties
128 ::std::vector< ::com::sun::star::beans::Property > aProperties;
129 lcl_AddPropertiesToVector( aProperties );
131 // and sort them for access via bsearch
132 ::std::sort( aProperties.begin(), aProperties.end(),
133 ::chart::PropertyNameLess() );
135 // transfer result to static Sequence
136 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
139 return aPropSeq;
142 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
144 static ::cppu::OPropertyArrayHelper aArrayHelper(
145 lcl_GetPropertySequence(),
146 /* bSorted = */ sal_True );
148 return aArrayHelper;
151 } // anonymous namespace
152 // ----------------------------------------
154 namespace chart
157 StockChartTypeTemplate::StockChartTypeTemplate(
158 uno::Reference<
159 uno::XComponentContext > const & xContext,
160 const ::rtl::OUString & rServiceName,
161 StockVariant eVariant,
162 bool bJapaneseStyle ) :
163 ChartTypeTemplate( xContext, rServiceName ),
164 ::property::OPropertySet( m_aMutex ),
165 m_eStockVariant( eVariant )
167 setFastPropertyValue_NoBroadcast(
168 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
169 uno::makeAny( ( eVariant == OPEN_LOW_HI_CLOSE ||
170 eVariant == VOL_OPEN_LOW_HI_CLOSE )));
171 setFastPropertyValue_NoBroadcast(
172 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
173 uno::makeAny( ( eVariant == VOL_LOW_HI_CLOSE ||
174 eVariant == VOL_OPEN_LOW_HI_CLOSE )));
175 setFastPropertyValue_NoBroadcast(
176 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
177 uno::makeAny( bJapaneseStyle ));
180 StockChartTypeTemplate::~StockChartTypeTemplate()
182 // ____ OPropertySet ____
183 uno::Any StockChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
184 throw(beans::UnknownPropertyException)
186 static tPropertyValueMap aStaticDefaults;
188 // /--
189 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
190 if( 0 == aStaticDefaults.size() )
192 // initialize defaults
193 lcl_AddDefaultsToMap( aStaticDefaults );
196 tPropertyValueMap::const_iterator aFound(
197 aStaticDefaults.find( nHandle ));
199 if( aFound == aStaticDefaults.end())
200 return uno::Any();
202 return (*aFound).second;
203 // \--
206 ::cppu::IPropertyArrayHelper & SAL_CALL StockChartTypeTemplate::getInfoHelper()
208 return lcl_getInfoHelper();
212 // ____ XPropertySet ____
213 uno::Reference< beans::XPropertySetInfo > SAL_CALL
214 StockChartTypeTemplate::getPropertySetInfo()
215 throw (uno::RuntimeException)
217 static uno::Reference< beans::XPropertySetInfo > xInfo;
219 // /--
220 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
221 if( !xInfo.is())
223 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
224 getInfoHelper());
227 return xInfo;
228 // \--
231 sal_Int32 StockChartTypeTemplate::getAxisCountByDimension( sal_Int32 nDimension )
233 // one x-axis
234 if( nDimension <= 0 )
235 return 1;
236 // no further axes
237 if( nDimension >= 2 )
238 return 0;
240 // one or two y-axes depending on volume
241 OSL_ASSERT( nDimension == 1 );
242 bool bHasVolume = false;
243 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
244 return bHasVolume ? 2 : 1;
247 void SAL_CALL StockChartTypeTemplate::applyStyle(
248 const Reference< chart2::XDataSeries >& xSeries,
249 ::sal_Int32 nChartTypeIndex,
250 ::sal_Int32 nSeriesIndex,
251 ::sal_Int32 nSeriesCount )
252 throw (uno::RuntimeException)
254 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
257 sal_Int32 nNewAxisIndex = 0;
259 bool bHasVolume = false;
260 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
261 if( bHasVolume )
263 if( nChartTypeIndex != 0 )
264 nNewAxisIndex = 1;
267 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
268 if( xProp.is() )
269 xProp->setPropertyValue( C2U("AttachedAxisIndex"), uno::makeAny( nNewAxisIndex ) );
272 //ensure that lines are on
273 if( !bHasVolume || nChartTypeIndex==0 )
275 if( xProp.is() )
277 drawing::LineStyle eStyle = drawing::LineStyle_NONE;
278 xProp->getPropertyValue( C2U("LineStyle") ) >>= eStyle;
279 if( eStyle == drawing::LineStyle_NONE )
280 xProp->setPropertyValue( C2U("LineStyle"), uno::makeAny( drawing::LineStyle_SOLID ));
284 catch( uno::Exception & ex )
286 ASSERT_EXCEPTION( ex );
290 void SAL_CALL StockChartTypeTemplate::resetStyles(
291 const Reference< chart2::XDiagram >& xDiagram )
292 throw (uno::RuntimeException)
294 ChartTypeTemplate::resetStyles( xDiagram );
295 if( getDimension() == 3 )
297 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
298 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
299 for( ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt( aSeriesVec.begin());
300 aIt != aSeriesVec.end(); ++aIt )
302 Reference< beans::XPropertySet > xProp( *aIt, uno::UNO_QUERY );
303 if( xProp.is() )
304 xProp->setPropertyValue( C2U("AttachedAxisIndex"), uno::makeAny( sal_Int32(0) ) );
308 DiagramHelper::setVertical( xDiagram, false );
311 Reference< XChartType > StockChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
313 Reference< XChartType > xCT;
314 Reference< lang::XMultiServiceFactory > xFact(
315 GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
316 if(xFact.is())
318 bool bHasVolume = false;
319 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
320 if( bHasVolume )
322 if( nChartTypeIndex == 0 )
323 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
324 else if( nChartTypeIndex == 1 )
325 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
326 else
327 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
329 else
331 if( nChartTypeIndex == 0 )
332 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
333 else
334 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
337 return xCT;
340 void StockChartTypeTemplate::createChartTypes(
341 const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
342 const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
343 const Sequence< Reference< XChartType > >& /* aOldChartTypesSeq */ )
345 if( rCoordSys.getLength() < 1 )
346 return;
350 Reference< lang::XMultiServiceFactory > xFact(
351 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
352 bool bHasVolume = false;
353 bool bShowFirst = false;
354 bool bJapaneseStyle = false;
355 bool bShowHighLow = true;
357 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
358 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bShowFirst;
359 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bJapaneseStyle;
360 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH ) >>= bShowHighLow;
362 sal_Int32 nSeriesIndex = 0;
364 std::vector< Reference< chart2::XChartType > > aChartTypeVec;
365 // Bars (Volume)
366 // -------------
367 if( bHasVolume )
369 // chart type
370 Reference< XChartType > xCT(
371 xFact->createInstance(
372 CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
373 aChartTypeVec.push_back( xCT );
375 if( aSeriesSeq.getLength() > nSeriesIndex &&
376 aSeriesSeq[nSeriesIndex].getLength() > 0 )
378 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
379 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
381 ++nSeriesIndex;
384 Reference< XChartType > xCT(
385 xFact->createInstance(
386 CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY_THROW );
387 aChartTypeVec.push_back( xCT );
389 Reference< beans::XPropertySet > xCTProp( xCT, uno::UNO_QUERY );
390 if( xCTProp.is())
392 xCTProp->setPropertyValue( C2U("Japanese"), uno::makeAny( bJapaneseStyle ));
393 xCTProp->setPropertyValue( C2U("ShowFirst"), uno::makeAny( bShowFirst ));
394 xCTProp->setPropertyValue( C2U("ShowHighLow"), uno::makeAny( bShowHighLow ));
397 if( aSeriesSeq.getLength() > nSeriesIndex &&
398 aSeriesSeq[ nSeriesIndex ].getLength() > 0 )
400 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
401 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
403 ++nSeriesIndex;
405 // Lines (remaining series)
406 // ------------------------
407 if( aSeriesSeq.getLength() > nSeriesIndex &&
408 aSeriesSeq[ nSeriesIndex ].getLength() > 0 )
410 xCT.set(
411 xFact->createInstance(
412 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
413 aChartTypeVec.push_back( xCT );
415 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
416 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
419 Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
420 xCTCnt->setChartTypes( ::chart::ContainerHelper::ContainerToSequence(aChartTypeVec) );
422 catch( uno::Exception & ex )
424 ASSERT_EXCEPTION( ex );
428 // ____ XChartTypeTemplate ____
429 sal_Bool SAL_CALL StockChartTypeTemplate::matchesTemplate(
430 const uno::Reference< XDiagram >& xDiagram,
431 sal_Bool /* bAdaptProperties */ )
432 throw (uno::RuntimeException)
434 sal_Bool bResult = sal_False;
436 if( ! xDiagram.is())
437 return bResult;
441 sal_Bool bHasVolume = false, bHasOpenValue = false, bHasJapaneseStyle = false;
443 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
444 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bHasOpenValue;
445 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bHasJapaneseStyle;
447 Reference< chart2::XChartType > xVolumeChartType;
448 Reference< chart2::XChartType > xCandleStickChartType;
449 Reference< chart2::XChartType > xLineChartType;
450 sal_Int32 nNumberOfChartTypes = 0;
452 Reference< XCoordinateSystemContainer > xCooSysCnt(
453 xDiagram, uno::UNO_QUERY_THROW );
454 Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
455 xCooSysCnt->getCoordinateSystems());
456 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
458 Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
459 Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
460 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
462 if( aChartTypeSeq[j].is())
464 ++nNumberOfChartTypes;
465 if( nNumberOfChartTypes > 3 )
466 break;
467 OUString aCTService = aChartTypeSeq[j]->getChartType();
468 if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ))
469 xVolumeChartType.set( aChartTypeSeq[j] );
470 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ))
471 xCandleStickChartType.set( aChartTypeSeq[j] );
472 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_LINE ))
473 xLineChartType.set( aChartTypeSeq[j] );
476 if( nNumberOfChartTypes > 3 )
477 break;
480 if( xCandleStickChartType.is() &&
481 ( ( bHasVolume &&
482 xVolumeChartType.is() ) ||
483 ( ! bHasVolume &&
484 ! xVolumeChartType.is() )))
486 bResult = true;
488 // check for japanese style
489 Reference< beans::XPropertySet > xCTProp( xCandleStickChartType, uno::UNO_QUERY );
490 if( xCTProp.is())
492 sal_Bool bJapaneseProp = sal_False;
493 xCTProp->getPropertyValue( C2U("Japanese")) >>= bJapaneseProp;
494 bResult = bResult && ( bHasJapaneseStyle == bJapaneseProp );
496 // in old chart japanese == showFirst
497 sal_Bool bShowFirstProp = sal_False;
498 xCTProp->getPropertyValue( C2U("ShowFirst")) >>= bShowFirstProp;
499 bResult = bResult && ( bHasOpenValue == bShowFirstProp );
503 catch( uno::Exception & ex )
505 ASSERT_EXCEPTION( ex );
508 return bResult;
511 Reference< XChartType > SAL_CALL StockChartTypeTemplate::getChartTypeForNewSeries(
512 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
513 throw (uno::RuntimeException)
515 Reference< chart2::XChartType > xResult;
519 Reference< lang::XMultiServiceFactory > xFact(
520 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
521 xResult.set( xFact->createInstance(
522 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
523 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
525 catch( uno::Exception & ex )
527 ASSERT_EXCEPTION( ex );
530 setStackModePropertiesToChartType(xResult);
531 return xResult;
534 Reference< XDataInterpreter > SAL_CALL StockChartTypeTemplate::getDataInterpreter()
535 throw (uno::RuntimeException)
537 if( ! m_xDataInterpreter.is())
538 m_xDataInterpreter.set( new StockDataInterpreter( m_eStockVariant, GetComponentContext() ) );
540 return m_xDataInterpreter;
543 // ----------------------------------------
545 Sequence< OUString > StockChartTypeTemplate::getSupportedServiceNames_Static()
547 Sequence< OUString > aServices( 2 );
548 aServices[ 0 ] = lcl_aServiceName;
549 aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
550 return aServices;
553 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
554 APPHELPER_XSERVICEINFO_IMPL( StockChartTypeTemplate, lcl_aServiceName );
556 IMPLEMENT_FORWARD_XINTERFACE2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
557 IMPLEMENT_FORWARD_XTYPEPROVIDER2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
559 } // namespace chart