merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / template / StockChartTypeTemplate.cxx
blob00ace6c330ad0eba473db07e9a8d90dfdc264000
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "StockChartTypeTemplate.hxx"
31 #include "macros.hxx"
32 #include "DataSeriesHelper.hxx"
33 #include "StockDataInterpreter.hxx"
34 #include "CartesianCoordinateSystem.hxx"
35 #include "AxisHelper.hxx"
36 #include "DiagramHelper.hxx"
37 #include "servicenames_charttypes.hxx"
38 #include "servicenames_coosystems.hxx"
39 #include "ContainerHelper.hxx"
40 #include "AxisIndexDefines.hxx"
41 #include <com/sun/star/chart2/AxisType.hpp>
42 #include <com/sun/star/chart2/SymbolStyle.hpp>
43 #include <com/sun/star/chart2/data/XDataSource.hpp>
44 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
45 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
46 #include "PropertyHelper.hxx"
47 #include <com/sun/star/beans/PropertyAttribute.hpp>
48 #include <com/sun/star/drawing/LineStyle.hpp>
50 #include <vector>
51 #include <algorithm>
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::chart2;
56 using ::com::sun::star::uno::Reference;
57 using ::com::sun::star::uno::Sequence;
58 using ::rtl::OUString;
59 using ::com::sun::star::beans::Property;
60 using ::com::sun::star::uno::Any;
61 using ::osl::MutexGuard;
63 // ----------------------------------------
64 namespace
67 static const OUString lcl_aServiceName(
68 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.StockChartTypeTemplate" ));
70 enum
72 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
73 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
74 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
75 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE
78 void lcl_AddPropertiesToVector(
79 ::std::vector< Property > & rOutProperties )
81 rOutProperties.push_back(
82 Property( C2U( "Volume" ),
83 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
84 ::getBooleanCppuType(),
85 beans::PropertyAttribute::BOUND
86 | beans::PropertyAttribute::MAYBEDEFAULT ));
87 rOutProperties.push_back(
88 Property( C2U( "Open" ),
89 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
90 ::getBooleanCppuType(),
91 beans::PropertyAttribute::BOUND
92 | beans::PropertyAttribute::MAYBEDEFAULT ));
93 rOutProperties.push_back(
94 Property( C2U( "LowHigh" ),
95 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
96 ::getBooleanCppuType(),
97 beans::PropertyAttribute::BOUND
98 | beans::PropertyAttribute::MAYBEDEFAULT ));
99 rOutProperties.push_back(
100 Property( C2U( "Japanese" ),
101 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
102 ::getBooleanCppuType(),
103 beans::PropertyAttribute::BOUND
104 | beans::PropertyAttribute::MAYBEDEFAULT ));
107 void lcl_AddDefaultsToMap(
108 ::chart::tPropertyValueMap & rOutMap )
110 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME, false );
111 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_OPEN, false );
112 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH, true );
113 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE, false );
116 const Sequence< Property > & lcl_GetPropertySequence()
118 static Sequence< Property > aPropSeq;
120 // /--
121 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
122 if( 0 == aPropSeq.getLength() )
124 // get properties
125 ::std::vector< ::com::sun::star::beans::Property > aProperties;
126 lcl_AddPropertiesToVector( aProperties );
128 // and sort them for access via bsearch
129 ::std::sort( aProperties.begin(), aProperties.end(),
130 ::chart::PropertyNameLess() );
132 // transfer result to static Sequence
133 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
136 return aPropSeq;
139 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
141 static ::cppu::OPropertyArrayHelper aArrayHelper(
142 lcl_GetPropertySequence(),
143 /* bSorted = */ sal_True );
145 return aArrayHelper;
148 } // anonymous namespace
149 // ----------------------------------------
151 namespace chart
154 StockChartTypeTemplate::StockChartTypeTemplate(
155 uno::Reference<
156 uno::XComponentContext > const & xContext,
157 const ::rtl::OUString & rServiceName,
158 StockVariant eVariant,
159 bool bJapaneseStyle ) :
160 ChartTypeTemplate( xContext, rServiceName ),
161 ::property::OPropertySet( m_aMutex ),
162 m_eStockVariant( eVariant )
164 setFastPropertyValue_NoBroadcast(
165 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
166 uno::makeAny( ( eVariant == OPEN_LOW_HI_CLOSE ||
167 eVariant == VOL_OPEN_LOW_HI_CLOSE )));
168 setFastPropertyValue_NoBroadcast(
169 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
170 uno::makeAny( ( eVariant == VOL_LOW_HI_CLOSE ||
171 eVariant == VOL_OPEN_LOW_HI_CLOSE )));
172 setFastPropertyValue_NoBroadcast(
173 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
174 uno::makeAny( bJapaneseStyle ));
177 StockChartTypeTemplate::~StockChartTypeTemplate()
179 // ____ OPropertySet ____
180 uno::Any StockChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
181 throw(beans::UnknownPropertyException)
183 static tPropertyValueMap aStaticDefaults;
185 // /--
186 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
187 if( 0 == aStaticDefaults.size() )
189 // initialize defaults
190 lcl_AddDefaultsToMap( aStaticDefaults );
193 tPropertyValueMap::const_iterator aFound(
194 aStaticDefaults.find( nHandle ));
196 if( aFound == aStaticDefaults.end())
197 return uno::Any();
199 return (*aFound).second;
200 // \--
203 ::cppu::IPropertyArrayHelper & SAL_CALL StockChartTypeTemplate::getInfoHelper()
205 return lcl_getInfoHelper();
209 // ____ XPropertySet ____
210 uno::Reference< beans::XPropertySetInfo > SAL_CALL
211 StockChartTypeTemplate::getPropertySetInfo()
212 throw (uno::RuntimeException)
214 static uno::Reference< beans::XPropertySetInfo > xInfo;
216 // /--
217 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
218 if( !xInfo.is())
220 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
221 getInfoHelper());
224 return xInfo;
225 // \--
228 sal_Int32 StockChartTypeTemplate::getAxisCountByDimension( sal_Int32 nDimension )
230 // one x-axis
231 if( nDimension <= 0 )
232 return 1;
233 // no further axes
234 if( nDimension >= 2 )
235 return 0;
237 // one or two y-axes depending on volume
238 OSL_ASSERT( nDimension == 1 );
239 bool bHasVolume = false;
240 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
241 return bHasVolume ? 2 : 1;
244 void SAL_CALL StockChartTypeTemplate::applyStyle(
245 const Reference< chart2::XDataSeries >& xSeries,
246 ::sal_Int32 nChartTypeIndex,
247 ::sal_Int32 nSeriesIndex,
248 ::sal_Int32 nSeriesCount )
249 throw (uno::RuntimeException)
251 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
254 sal_Int32 nNewAxisIndex = 0;
256 bool bHasVolume = false;
257 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
258 if( bHasVolume )
260 if( nChartTypeIndex != 0 )
261 nNewAxisIndex = 1;
264 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
265 if( xProp.is() )
266 xProp->setPropertyValue( C2U("AttachedAxisIndex"), uno::makeAny( nNewAxisIndex ) );
268 if( bHasVolume && nChartTypeIndex==0 )
270 //switch lines off for volume bars
271 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, C2U( "BorderStyle" ), uno::makeAny( drawing::LineStyle_NONE ) );
273 else
275 //ensure that lines are on
276 if( xProp.is() )
278 drawing::LineStyle eStyle = drawing::LineStyle_NONE;
279 xProp->getPropertyValue( C2U("LineStyle") ) >>= eStyle;
280 if( eStyle == drawing::LineStyle_NONE )
281 xProp->setPropertyValue( C2U("LineStyle"), uno::makeAny( drawing::LineStyle_SOLID ));
286 catch( uno::Exception & ex )
288 ASSERT_EXCEPTION( ex );
292 void SAL_CALL StockChartTypeTemplate::resetStyles(
293 const Reference< chart2::XDiagram >& xDiagram )
294 throw (uno::RuntimeException)
296 ChartTypeTemplate::resetStyles( xDiagram );
297 if( getDimension() == 3 )
299 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
300 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
301 for( ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt( aSeriesVec.begin());
302 aIt != aSeriesVec.end(); ++aIt )
304 Reference< beans::XPropertySet > xProp( *aIt, uno::UNO_QUERY );
305 if( xProp.is() )
306 xProp->setPropertyValue( C2U("AttachedAxisIndex"), uno::makeAny( sal_Int32(0) ) );
310 DiagramHelper::setVertical( xDiagram, false );
313 Reference< XChartType > StockChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
315 Reference< XChartType > xCT;
316 Reference< lang::XMultiServiceFactory > xFact(
317 GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
318 if(xFact.is())
320 bool bHasVolume = false;
321 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
322 if( bHasVolume )
324 if( nChartTypeIndex == 0 )
325 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
326 else if( nChartTypeIndex == 1 )
327 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
328 else
329 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
331 else
333 if( nChartTypeIndex == 0 )
334 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
335 else
336 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
339 return xCT;
342 void StockChartTypeTemplate::createChartTypes(
343 const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
344 const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
345 const Sequence< Reference< XChartType > >& /* aOldChartTypesSeq */ )
347 if( rCoordSys.getLength() < 1 )
348 return;
352 Reference< lang::XMultiServiceFactory > xFact(
353 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
354 bool bHasVolume = false;
355 bool bShowFirst = false;
356 bool bJapaneseStyle = false;
357 bool bShowHighLow = true;
359 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
360 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bShowFirst;
361 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bJapaneseStyle;
362 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH ) >>= bShowHighLow;
364 sal_Int32 nSeriesIndex = 0;
366 std::vector< Reference< chart2::XChartType > > aChartTypeVec;
367 // Bars (Volume)
368 // -------------
369 if( bHasVolume )
371 // chart type
372 Reference< XChartType > xCT(
373 xFact->createInstance(
374 CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
375 aChartTypeVec.push_back( xCT );
377 if( aSeriesSeq.getLength() > nSeriesIndex &&
378 aSeriesSeq[nSeriesIndex].getLength() > 0 )
380 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
381 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
383 ++nSeriesIndex;
386 Reference< XChartType > xCT(
387 xFact->createInstance(
388 CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY_THROW );
389 aChartTypeVec.push_back( xCT );
391 Reference< beans::XPropertySet > xCTProp( xCT, uno::UNO_QUERY );
392 if( xCTProp.is())
394 xCTProp->setPropertyValue( C2U("Japanese"), uno::makeAny( bJapaneseStyle ));
395 xCTProp->setPropertyValue( C2U("ShowFirst"), uno::makeAny( bShowFirst ));
396 xCTProp->setPropertyValue( C2U("ShowHighLow"), uno::makeAny( bShowHighLow ));
399 if( aSeriesSeq.getLength() > nSeriesIndex &&
400 aSeriesSeq[ nSeriesIndex ].getLength() > 0 )
402 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
403 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
405 ++nSeriesIndex;
407 // Lines (remaining series)
408 // ------------------------
409 if( aSeriesSeq.getLength() > nSeriesIndex &&
410 aSeriesSeq[ nSeriesIndex ].getLength() > 0 )
412 xCT.set(
413 xFact->createInstance(
414 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
415 aChartTypeVec.push_back( xCT );
417 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
418 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
421 Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
422 xCTCnt->setChartTypes( ::chart::ContainerHelper::ContainerToSequence(aChartTypeVec) );
424 catch( uno::Exception & ex )
426 ASSERT_EXCEPTION( ex );
430 // ____ XChartTypeTemplate ____
431 sal_Bool SAL_CALL StockChartTypeTemplate::matchesTemplate(
432 const uno::Reference< XDiagram >& xDiagram,
433 sal_Bool /* bAdaptProperties */ )
434 throw (uno::RuntimeException)
436 sal_Bool bResult = sal_False;
438 if( ! xDiagram.is())
439 return bResult;
443 sal_Bool bHasVolume = false, bHasOpenValue = false, bHasJapaneseStyle = false;
445 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
446 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bHasOpenValue;
447 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bHasJapaneseStyle;
449 Reference< chart2::XChartType > xVolumeChartType;
450 Reference< chart2::XChartType > xCandleStickChartType;
451 Reference< chart2::XChartType > xLineChartType;
452 sal_Int32 nNumberOfChartTypes = 0;
454 Reference< XCoordinateSystemContainer > xCooSysCnt(
455 xDiagram, uno::UNO_QUERY_THROW );
456 Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
457 xCooSysCnt->getCoordinateSystems());
458 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
460 Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
461 Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
462 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
464 if( aChartTypeSeq[j].is())
466 ++nNumberOfChartTypes;
467 if( nNumberOfChartTypes > 3 )
468 break;
469 OUString aCTService = aChartTypeSeq[j]->getChartType();
470 if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ))
471 xVolumeChartType.set( aChartTypeSeq[j] );
472 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ))
473 xCandleStickChartType.set( aChartTypeSeq[j] );
474 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_LINE ))
475 xLineChartType.set( aChartTypeSeq[j] );
478 if( nNumberOfChartTypes > 3 )
479 break;
482 if( xCandleStickChartType.is() &&
483 ( ( bHasVolume &&
484 xVolumeChartType.is() ) ||
485 ( ! bHasVolume &&
486 ! xVolumeChartType.is() )))
488 bResult = true;
490 // check for japanese style
491 Reference< beans::XPropertySet > xCTProp( xCandleStickChartType, uno::UNO_QUERY );
492 if( xCTProp.is())
494 sal_Bool bJapaneseProp = sal_False;
495 xCTProp->getPropertyValue( C2U("Japanese")) >>= bJapaneseProp;
496 bResult = bResult && ( bHasJapaneseStyle == bJapaneseProp );
498 // in old chart japanese == showFirst
499 sal_Bool bShowFirstProp = sal_False;
500 xCTProp->getPropertyValue( C2U("ShowFirst")) >>= bShowFirstProp;
501 bResult = bResult && ( bHasOpenValue == bShowFirstProp );
505 catch( uno::Exception & ex )
507 ASSERT_EXCEPTION( ex );
510 return bResult;
513 Reference< XChartType > SAL_CALL StockChartTypeTemplate::getChartTypeForNewSeries(
514 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
515 throw (uno::RuntimeException)
517 Reference< chart2::XChartType > xResult;
521 Reference< lang::XMultiServiceFactory > xFact(
522 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
523 xResult.set( xFact->createInstance(
524 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
525 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
527 catch( uno::Exception & ex )
529 ASSERT_EXCEPTION( ex );
532 return xResult;
535 Reference< XDataInterpreter > SAL_CALL StockChartTypeTemplate::getDataInterpreter()
536 throw (uno::RuntimeException)
538 if( ! m_xDataInterpreter.is())
539 m_xDataInterpreter.set( new StockDataInterpreter( m_eStockVariant, GetComponentContext() ) );
541 return m_xDataInterpreter;
544 // ----------------------------------------
546 Sequence< OUString > StockChartTypeTemplate::getSupportedServiceNames_Static()
548 Sequence< OUString > aServices( 2 );
549 aServices[ 0 ] = lcl_aServiceName;
550 aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
551 return aServices;
554 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
555 APPHELPER_XSERVICEINFO_IMPL( StockChartTypeTemplate, lcl_aServiceName );
557 IMPLEMENT_FORWARD_XINTERFACE2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
558 IMPLEMENT_FORWARD_XTYPEPROVIDER2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
560 } // namespace chart