merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / template / ColumnLineChartTypeTemplate.cxx
blob663cad4c2fe460558c3ea4a7ccb8bc9c59682650
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 "ColumnLineChartTypeTemplate.hxx"
31 #include "macros.hxx"
32 #include "CommonConverters.hxx"
33 #include "DiagramHelper.hxx"
34 #include "DataSeriesHelper.hxx"
35 #include "servicenames_charttypes.hxx"
36 #include "ColumnLineDataInterpreter.hxx"
37 #include "ContainerHelper.hxx"
38 #include "PropertyHelper.hxx"
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
41 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
42 #include <com/sun/star/drawing/LineStyle.hpp>
44 #include <algorithm>
46 using namespace ::com::sun::star::chart2;
47 using namespace ::com::sun::star;
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::uno::Sequence;
51 using ::rtl::OUString;
52 using ::com::sun::star::beans::Property;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::Any;
55 using ::osl::MutexGuard;
57 namespace
60 static const ::rtl::OUString lcl_aServiceName(
61 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.ColumnLineChartTypeTemplate" ));
63 enum
65 PROP_COL_LINE_NUMBER_OF_LINES
68 void lcl_AddPropertiesToVector(
69 ::std::vector< Property > & rOutProperties )
71 rOutProperties.push_back(
72 Property( C2U( "NumberOfLines" ),
73 PROP_COL_LINE_NUMBER_OF_LINES,
74 ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
75 beans::PropertyAttribute::BOUND
76 | beans::PropertyAttribute::MAYBEDEFAULT ));
79 void lcl_AddDefaultsToMap(
80 ::chart::tPropertyValueMap & rOutMap )
82 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
85 const uno::Sequence< Property > & lcl_GetPropertySequence()
87 static uno::Sequence< Property > aPropSeq;
89 // /--
90 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
91 if( 0 == aPropSeq.getLength() )
93 // get properties
94 ::std::vector< ::com::sun::star::beans::Property > aProperties;
95 lcl_AddPropertiesToVector( aProperties );
97 // and sort them for access via bsearch
98 ::std::sort( aProperties.begin(), aProperties.end(),
99 ::chart::PropertyNameLess() );
101 // transfer result to static Sequence
102 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
105 return aPropSeq;
108 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
110 static ::cppu::OPropertyArrayHelper aArrayHelper(
111 lcl_GetPropertySequence(),
112 /* bSorted = */ sal_True );
114 return aArrayHelper;
117 } // anonymous namespace
119 namespace chart
122 ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
123 Reference<
124 uno::XComponentContext > const & xContext,
125 const ::rtl::OUString & rServiceName,
126 StackMode eStackMode,
127 sal_Int32 nNumberOfLines ) :
128 ChartTypeTemplate( xContext, rServiceName ),
129 ::property::OPropertySet( m_aMutex ),
130 m_eStackMode( eStackMode )
132 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
135 ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
138 // ____ OPropertySet ____
139 uno::Any ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
140 throw(beans::UnknownPropertyException)
142 static tPropertyValueMap aStaticDefaults;
144 // /--
145 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
146 if( 0 == aStaticDefaults.size() )
148 // initialize defaults
149 lcl_AddDefaultsToMap( aStaticDefaults );
152 tPropertyValueMap::const_iterator aFound(
153 aStaticDefaults.find( nHandle ));
155 if( aFound == aStaticDefaults.end())
156 return uno::Any();
158 return (*aFound).second;
159 // \--
162 ::cppu::IPropertyArrayHelper & SAL_CALL ColumnLineChartTypeTemplate::getInfoHelper()
164 return lcl_getInfoHelper();
168 // ____ XPropertySet ____
169 uno::Reference< beans::XPropertySetInfo > SAL_CALL
170 ColumnLineChartTypeTemplate::getPropertySetInfo()
171 throw (uno::RuntimeException)
173 static uno::Reference< beans::XPropertySetInfo > xInfo;
175 // /--
176 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
177 if( !xInfo.is())
179 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
180 getInfoHelper());
183 return xInfo;
184 // \--
187 void ColumnLineChartTypeTemplate::createChartTypes(
188 const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
189 const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
190 const Sequence< Reference< XChartType > >& aOldChartTypesSeq )
192 if( rCoordSys.getLength() == 0 ||
193 ! rCoordSys[0].is() )
194 return;
198 Reference< lang::XMultiServiceFactory > xFact(
199 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
200 Sequence< Reference< XDataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
201 sal_Int32 nNumberOfSeries = aFlatSeriesSeq.getLength();
202 sal_Int32 nNumberOfLines = 0;
203 sal_Int32 nNumberOfColumns = 0;
205 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
206 OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
207 if( nNumberOfLines < 0 )
208 nNumberOfLines = 0;
210 if( nNumberOfLines >= nNumberOfSeries )
212 if( nNumberOfSeries > 0 )
214 nNumberOfLines = nNumberOfSeries - 1;
215 nNumberOfColumns = 1;
217 else
218 nNumberOfLines = 0;
220 else
221 nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
223 // Columns
224 // -------
225 Reference< XChartType > xCT(
226 xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
228 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aOldChartTypesSeq, xCT );
230 Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
231 xCTCnt->setChartTypes( Sequence< Reference< chart2::XChartType > >( &xCT, 1 ));
233 if( nNumberOfColumns > 0 )
235 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
236 Sequence< Reference< XDataSeries > > aColumnSeq( nNumberOfColumns );
237 ::std::copy( aFlatSeriesSeq.getConstArray(),
238 aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
239 aColumnSeq.getArray());
240 xDSCnt->setDataSeries( aColumnSeq );
243 // Lines
244 // -----
245 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
246 xCTCnt.set( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
247 xCTCnt->addChartType( xCT );
249 if( nNumberOfLines > 0 )
251 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
252 Sequence< Reference< XDataSeries > > aLineSeq( nNumberOfLines );
253 ::std::copy( aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
254 aFlatSeriesSeq.getConstArray() + aFlatSeriesSeq.getLength(),
255 aLineSeq.getArray());
256 xDSCnt->setDataSeries( aLineSeq );
259 catch( uno::Exception & ex )
261 ASSERT_EXCEPTION( ex );
265 void SAL_CALL ColumnLineChartTypeTemplate::applyStyle(
266 const Reference< chart2::XDataSeries >& xSeries,
267 ::sal_Int32 nChartTypeIndex,
268 ::sal_Int32 nSeriesIndex,
269 ::sal_Int32 nSeriesCount )
270 throw (uno::RuntimeException)
272 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
274 if( nChartTypeIndex==0 ) // columns
276 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, C2U( "BorderStyle" ), uno::makeAny( drawing::LineStyle_NONE ) );
278 else if( nChartTypeIndex==1 ) // lines
280 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
281 if( xProp.is() )
283 DataSeriesHelper::switchLinesOnOrOff( xProp, true );
284 DataSeriesHelper::switchSymbolsOnOrOff( xProp, false, nSeriesIndex );
285 DataSeriesHelper::makeLinesThickOrThin( xProp, true );
290 StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
292 if( nChartTypeIndex == 0 )
293 return m_eStackMode;
294 return StackMode_NONE;
297 // ____ XChartTypeTemplate ____
298 sal_Bool SAL_CALL ColumnLineChartTypeTemplate::matchesTemplate(
299 const uno::Reference< XDiagram >& xDiagram,
300 sal_Bool bAdaptProperties )
301 throw (uno::RuntimeException)
303 sal_Bool bResult = sal_False;
305 if( ! xDiagram.is())
306 return bResult;
310 Reference< chart2::XChartType > xColumnChartType;
311 Reference< XCoordinateSystem > xColumnChartCooSys;
312 Reference< chart2::XChartType > xLineChartType;
313 sal_Int32 nNumberOfChartTypes = 0;
315 Reference< XCoordinateSystemContainer > xCooSysCnt(
316 xDiagram, uno::UNO_QUERY_THROW );
317 Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
318 xCooSysCnt->getCoordinateSystems());
319 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
321 Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
322 Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
323 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
325 if( aChartTypeSeq[j].is())
327 ++nNumberOfChartTypes;
328 if( nNumberOfChartTypes > 2 )
329 break;
330 OUString aCTService = aChartTypeSeq[j]->getChartType();
331 if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ))
333 xColumnChartType.set( aChartTypeSeq[j] );
334 xColumnChartCooSys.set( aCooSysSeq[i] );
336 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_LINE ))
337 xLineChartType.set( aChartTypeSeq[j] );
340 if( nNumberOfChartTypes > 2 )
341 break;
344 if( nNumberOfChartTypes == 2 &&
345 xColumnChartType.is() &&
346 xLineChartType.is())
348 OSL_ASSERT( xColumnChartCooSys.is());
350 // check stackmode of bars
351 bResult = (xColumnChartCooSys->getDimension() == getDimension());
352 if( bResult )
354 bool bFound=false;
355 bool bAmbiguous=false;
356 bResult = ( DiagramHelper::getStackModeFromChartType(
357 xColumnChartType, bFound, bAmbiguous,
358 xColumnChartCooSys )
359 == getStackMode( 0 ) );
361 if( bResult && bAdaptProperties )
363 Reference< XDataSeriesContainer > xSeriesContainer( xLineChartType, uno::UNO_QUERY );
364 if( xSeriesContainer.is() )
366 sal_Int32 nNumberOfLines = xSeriesContainer->getDataSeries().getLength();
367 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
373 catch( uno::Exception & ex )
375 ASSERT_EXCEPTION( ex );
378 return bResult;
381 Reference< chart2::XChartType > ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
383 Reference< chart2::XChartType > xCT;
384 Reference< lang::XMultiServiceFactory > xFact(
385 GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
386 if(xFact.is())
388 if( nChartTypeIndex == 0 )
389 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
390 else
391 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
393 return xCT;
396 Reference< XChartType > SAL_CALL ColumnLineChartTypeTemplate::getChartTypeForNewSeries(
397 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
398 throw (uno::RuntimeException)
400 Reference< chart2::XChartType > xResult;
404 Reference< lang::XMultiServiceFactory > xFact(
405 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
406 xResult.set( xFact->createInstance(
407 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
408 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
410 catch( uno::Exception & ex )
412 ASSERT_EXCEPTION( ex );
415 return xResult;
418 Reference< XDataInterpreter > SAL_CALL ColumnLineChartTypeTemplate::getDataInterpreter()
419 throw (uno::RuntimeException)
421 if( ! m_xDataInterpreter.is())
423 sal_Int32 nNumberOfLines = 1;
424 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
425 m_xDataInterpreter.set( new ColumnLineDataInterpreter( nNumberOfLines, GetComponentContext() ) );
427 else
429 //todo...
430 OSL_ENSURE( false, "number of lines may not be valid anymore in the datainterpreter" );
434 return m_xDataInterpreter;
437 // ----------------------------------------
439 uno::Sequence< ::rtl::OUString > ColumnLineChartTypeTemplate::getSupportedServiceNames_Static()
441 uno::Sequence< ::rtl::OUString > aServices( 2 );
442 aServices[ 0 ] = lcl_aServiceName;
443 aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
444 return aServices;
447 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
448 APPHELPER_XSERVICEINFO_IMPL( ColumnLineChartTypeTemplate, lcl_aServiceName );
450 IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
451 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
453 } // namespace chart