fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / model / template / StockChartTypeTemplate.cxx
blobe1f5d9f0d7d8cb59feaee8b0e7525cb2847beed9
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 "StockChartTypeTemplate.hxx"
21 #include "macros.hxx"
22 #include "DataSeriesHelper.hxx"
23 #include "StockDataInterpreter.hxx"
24 #include "CartesianCoordinateSystem.hxx"
25 #include "AxisHelper.hxx"
26 #include "DiagramHelper.hxx"
27 #include "servicenames_charttypes.hxx"
28 #include "servicenames_coosystems.hxx"
29 #include "ContainerHelper.hxx"
30 #include "AxisIndexDefines.hxx"
31 #include <com/sun/star/chart2/AxisType.hpp>
32 #include <com/sun/star/chart2/data/XDataSource.hpp>
33 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
34 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
35 #include "PropertyHelper.hxx"
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/drawing/LineStyle.hpp>
39 #include <vector>
40 #include <algorithm>
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::chart2;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::beans::Property;
48 using ::com::sun::star::uno::Any;
49 using ::osl::MutexGuard;
51 namespace
54 enum
56 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
57 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
58 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
59 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE
62 void lcl_AddPropertiesToVector(
63 ::std::vector< Property > & rOutProperties )
65 rOutProperties.push_back(
66 Property( "Volume",
67 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
68 cppu::UnoType<bool>::get(),
69 beans::PropertyAttribute::BOUND
70 | beans::PropertyAttribute::MAYBEDEFAULT ));
71 rOutProperties.push_back(
72 Property( "Open",
73 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
74 cppu::UnoType<bool>::get(),
75 beans::PropertyAttribute::BOUND
76 | beans::PropertyAttribute::MAYBEDEFAULT ));
77 rOutProperties.push_back(
78 Property( "LowHigh",
79 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
80 cppu::UnoType<bool>::get(),
81 beans::PropertyAttribute::BOUND
82 | beans::PropertyAttribute::MAYBEDEFAULT ));
83 rOutProperties.push_back(
84 Property( "Japanese",
85 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
86 cppu::UnoType<bool>::get(),
87 beans::PropertyAttribute::BOUND
88 | beans::PropertyAttribute::MAYBEDEFAULT ));
91 struct StaticStockChartTypeTemplateDefaults_Initializer
93 ::chart::tPropertyValueMap* operator()()
95 static ::chart::tPropertyValueMap aStaticDefaults;
96 lcl_AddDefaultsToMap( aStaticDefaults );
97 return &aStaticDefaults;
99 private:
100 static void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
102 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME, false );
103 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_OPEN, false );
104 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH, true );
105 ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE, false );
109 struct StaticStockChartTypeTemplateDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticStockChartTypeTemplateDefaults_Initializer >
113 struct StaticStockChartTypeTemplateInfoHelper_Initializer
115 ::cppu::OPropertyArrayHelper* operator()()
117 static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
118 return &aPropHelper;
121 private:
122 static Sequence< Property > lcl_GetPropertySequence()
124 ::std::vector< ::com::sun::star::beans::Property > aProperties;
125 lcl_AddPropertiesToVector( aProperties );
127 ::std::sort( aProperties.begin(), aProperties.end(),
128 ::chart::PropertyNameLess() );
130 return ::chart::ContainerHelper::ContainerToSequence( aProperties );
134 struct StaticStockChartTypeTemplateInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticStockChartTypeTemplateInfoHelper_Initializer >
138 struct StaticStockChartTypeTemplateInfo_Initializer
140 uno::Reference< beans::XPropertySetInfo >* operator()()
142 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
143 ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticStockChartTypeTemplateInfoHelper::get() ) );
144 return &xPropertySetInfo;
148 struct StaticStockChartTypeTemplateInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticStockChartTypeTemplateInfo_Initializer >
152 } // anonymous namespace
154 namespace chart
157 StockChartTypeTemplate::StockChartTypeTemplate(
158 uno::Reference<
159 uno::XComponentContext > const & xContext,
160 const 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 const tPropertyValueMap& rStaticDefaults = *StaticStockChartTypeTemplateDefaults::get();
187 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
188 if( aFound == rStaticDefaults.end() )
189 return uno::Any();
190 return (*aFound).second;
193 ::cppu::IPropertyArrayHelper & SAL_CALL StockChartTypeTemplate::getInfoHelper()
195 return *StaticStockChartTypeTemplateInfoHelper::get();
198 // ____ XPropertySet ____
199 uno::Reference< beans::XPropertySetInfo > SAL_CALL StockChartTypeTemplate::getPropertySetInfo()
200 throw (uno::RuntimeException, std::exception)
202 return *StaticStockChartTypeTemplateInfo::get();
205 sal_Int32 StockChartTypeTemplate::getAxisCountByDimension( sal_Int32 nDimension )
207 // one x-axis
208 if( nDimension <= 0 )
209 return 1;
210 // no further axes
211 if( nDimension >= 2 )
212 return 0;
214 // one or two y-axes depending on volume
215 OSL_ASSERT( nDimension == 1 );
216 bool bHasVolume = false;
217 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
218 return bHasVolume ? 2 : 1;
221 void SAL_CALL StockChartTypeTemplate::applyStyle(
222 const Reference< chart2::XDataSeries >& xSeries,
223 ::sal_Int32 nChartTypeIndex,
224 ::sal_Int32 nSeriesIndex,
225 ::sal_Int32 nSeriesCount )
226 throw (uno::RuntimeException, std::exception)
228 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
231 sal_Int32 nNewAxisIndex = 0;
233 bool bHasVolume = false;
234 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
235 if( bHasVolume )
237 if( nChartTypeIndex != 0 )
238 nNewAxisIndex = 1;
241 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
242 if( xProp.is() )
243 xProp->setPropertyValue( "AttachedAxisIndex", uno::makeAny( nNewAxisIndex ) );
245 if( bHasVolume && nChartTypeIndex==0 )
247 //switch lines off for volume bars
248 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, "BorderStyle", uno::makeAny( drawing::LineStyle_NONE ) );
250 else
252 //ensure that lines are on
253 if( xProp.is() )
255 drawing::LineStyle eStyle = drawing::LineStyle_NONE;
256 xProp->getPropertyValue( "LineStyle" ) >>= eStyle;
257 if( eStyle == drawing::LineStyle_NONE )
258 xProp->setPropertyValue( "LineStyle", uno::makeAny( drawing::LineStyle_SOLID ));
263 catch( const uno::Exception & ex )
265 ASSERT_EXCEPTION( ex );
269 void SAL_CALL StockChartTypeTemplate::resetStyles(
270 const Reference< chart2::XDiagram >& xDiagram )
271 throw (uno::RuntimeException, std::exception)
273 ChartTypeTemplate::resetStyles( xDiagram );
274 if( getDimension() == 3 )
276 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
277 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
278 for( ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt( aSeriesVec.begin());
279 aIt != aSeriesVec.end(); ++aIt )
281 Reference< beans::XPropertySet > xProp( *aIt, uno::UNO_QUERY );
282 if( xProp.is() )
283 xProp->setPropertyValue( "AttachedAxisIndex", uno::makeAny( sal_Int32(0) ) );
287 DiagramHelper::setVertical( xDiagram, false );
290 Reference< XChartType > StockChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
292 Reference< XChartType > xCT;
293 Reference< lang::XMultiServiceFactory > xFact(
294 GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
295 if(xFact.is())
297 bool bHasVolume = false;
298 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
299 if( bHasVolume )
301 if( nChartTypeIndex == 0 )
302 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
303 else if( nChartTypeIndex == 1 )
304 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
305 else
306 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
308 else
310 if( nChartTypeIndex == 0 )
311 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY );
312 else
313 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
316 return xCT;
319 void StockChartTypeTemplate::createChartTypes(
320 const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
321 const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
322 const Sequence< Reference< XChartType > >& /* aOldChartTypesSeq */ )
324 if( rCoordSys.getLength() < 1 )
325 return;
329 Reference< lang::XMultiServiceFactory > xFact(
330 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
331 bool bHasVolume = false;
332 bool bShowFirst = false;
333 bool bJapaneseStyle = false;
334 bool bShowHighLow = true;
336 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
337 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bShowFirst;
338 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bJapaneseStyle;
339 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH ) >>= bShowHighLow;
341 sal_Int32 nSeriesIndex = 0;
343 std::vector< Reference< chart2::XChartType > > aChartTypeVec;
344 // Bars (Volume)
345 if( bHasVolume )
347 // chart type
348 Reference< XChartType > xCT(
349 xFact->createInstance(
350 CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
351 aChartTypeVec.push_back( xCT );
353 if( aSeriesSeq.getLength() > nSeriesIndex &&
354 aSeriesSeq[nSeriesIndex].getLength() > 0 )
356 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
357 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
359 ++nSeriesIndex;
362 Reference< XChartType > xCT(
363 xFact->createInstance(
364 CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK ), uno::UNO_QUERY_THROW );
365 aChartTypeVec.push_back( xCT );
367 Reference< beans::XPropertySet > xCTProp( xCT, uno::UNO_QUERY );
368 if( xCTProp.is())
370 xCTProp->setPropertyValue( "Japanese", uno::makeAny( bJapaneseStyle ));
371 xCTProp->setPropertyValue( "ShowFirst", uno::makeAny( bShowFirst ));
372 xCTProp->setPropertyValue( "ShowHighLow", uno::makeAny( bShowHighLow ));
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;
383 // Lines (remaining series)
384 if( aSeriesSeq.getLength() > nSeriesIndex &&
385 aSeriesSeq[ nSeriesIndex ].getLength() > 0 )
387 xCT.set(
388 xFact->createInstance(
389 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
390 aChartTypeVec.push_back( xCT );
392 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
393 xDSCnt->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
396 Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
397 xCTCnt->setChartTypes( ::chart::ContainerHelper::ContainerToSequence(aChartTypeVec) );
399 catch( const uno::Exception & ex )
401 ASSERT_EXCEPTION( ex );
405 // ____ XChartTypeTemplate ____
406 sal_Bool SAL_CALL StockChartTypeTemplate::matchesTemplate(
407 const uno::Reference< XDiagram >& xDiagram,
408 sal_Bool /* bAdaptProperties */ )
409 throw (uno::RuntimeException, std::exception)
411 bool bResult = false;
413 if( ! xDiagram.is())
414 return bResult;
418 bool bHasVolume = false, bHasOpenValue = false, bHasJapaneseStyle = false;
420 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
421 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bHasOpenValue;
422 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bHasJapaneseStyle;
424 Reference< chart2::XChartType > xVolumeChartType;
425 Reference< chart2::XChartType > xCandleStickChartType;
426 Reference< chart2::XChartType > xLineChartType;
427 sal_Int32 nNumberOfChartTypes = 0;
429 Reference< XCoordinateSystemContainer > xCooSysCnt(
430 xDiagram, uno::UNO_QUERY_THROW );
431 Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
432 xCooSysCnt->getCoordinateSystems());
433 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
435 Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
436 Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
437 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
439 if( aChartTypeSeq[j].is())
441 ++nNumberOfChartTypes;
442 if( nNumberOfChartTypes > 3 )
443 break;
444 OUString aCTService = aChartTypeSeq[j]->getChartType();
445 if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_COLUMN )
446 xVolumeChartType.set( aChartTypeSeq[j] );
447 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK )
448 xCandleStickChartType.set( aChartTypeSeq[j] );
449 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_LINE )
450 xLineChartType.set( aChartTypeSeq[j] );
453 if( nNumberOfChartTypes > 3 )
454 break;
457 if( xCandleStickChartType.is() &&
458 ( ( bHasVolume &&
459 xVolumeChartType.is() ) ||
460 ( ! bHasVolume &&
461 ! xVolumeChartType.is() )))
463 bResult = true;
465 // check for japanese style
466 Reference< beans::XPropertySet > xCTProp( xCandleStickChartType, uno::UNO_QUERY );
467 if( xCTProp.is())
469 bool bJapaneseProp = false;
470 xCTProp->getPropertyValue( "Japanese") >>= bJapaneseProp;
471 bResult = bResult && ( bHasJapaneseStyle == bJapaneseProp );
473 // in old chart japanese == showFirst
474 bool bShowFirstProp = false;
475 xCTProp->getPropertyValue( "ShowFirst") >>= bShowFirstProp;
476 bResult = bResult && ( bHasOpenValue == bShowFirstProp );
480 catch( const uno::Exception & ex )
482 ASSERT_EXCEPTION( ex );
485 return bResult;
488 Reference< XChartType > SAL_CALL StockChartTypeTemplate::getChartTypeForNewSeries(
489 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
490 throw (uno::RuntimeException, std::exception)
492 Reference< chart2::XChartType > xResult;
496 Reference< lang::XMultiServiceFactory > xFact(
497 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
498 xResult.set( xFact->createInstance(
499 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
500 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
502 catch( const uno::Exception & ex )
504 ASSERT_EXCEPTION( ex );
507 return xResult;
510 Reference< XDataInterpreter > SAL_CALL StockChartTypeTemplate::getDataInterpreter()
511 throw (uno::RuntimeException, std::exception)
513 if( ! m_xDataInterpreter.is())
514 m_xDataInterpreter.set( new StockDataInterpreter( m_eStockVariant, GetComponentContext() ) );
516 return m_xDataInterpreter;
519 IMPLEMENT_FORWARD_XINTERFACE2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
520 IMPLEMENT_FORWARD_XTYPEPROVIDER2( StockChartTypeTemplate, ChartTypeTemplate, OPropertySet )
522 } // namespace chart
524 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */