merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / model / template / ColumnLineChartTypeTemplate.cxx
blob4d921760c1b5795c27420cd590cbdf43805ee623
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: ColumnLineChartTypeTemplate.cxx,v $
10 * $Revision: 1.13 $
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 "ColumnLineChartTypeTemplate.hxx"
34 #include "macros.hxx"
35 #include "CommonConverters.hxx"
36 #include "DiagramHelper.hxx"
37 #include "DataSeriesHelper.hxx"
38 #include "servicenames_charttypes.hxx"
39 #include "ColumnLineDataInterpreter.hxx"
40 #include "ContainerHelper.hxx"
41 #include "PropertyHelper.hxx"
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
43 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
44 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
46 #include <algorithm>
48 using namespace ::com::sun::star::chart2;
49 using namespace ::com::sun::star;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 using ::rtl::OUString;
54 using ::com::sun::star::beans::Property;
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::uno::Any;
57 using ::osl::MutexGuard;
59 namespace
62 static const ::rtl::OUString lcl_aServiceName(
63 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.ColumnLineChartTypeTemplate" ));
65 enum
67 PROP_COL_LINE_NUMBER_OF_LINES
70 void lcl_AddPropertiesToVector(
71 ::std::vector< Property > & rOutProperties )
73 rOutProperties.push_back(
74 Property( C2U( "NumberOfLines" ),
75 PROP_COL_LINE_NUMBER_OF_LINES,
76 ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
77 beans::PropertyAttribute::BOUND
78 | beans::PropertyAttribute::MAYBEDEFAULT ));
81 void lcl_AddDefaultsToMap(
82 ::chart::tPropertyValueMap & rOutMap )
84 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
87 const uno::Sequence< Property > & lcl_GetPropertySequence()
89 static uno::Sequence< Property > aPropSeq;
91 // /--
92 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
93 if( 0 == aPropSeq.getLength() )
95 // get properties
96 ::std::vector< ::com::sun::star::beans::Property > aProperties;
97 lcl_AddPropertiesToVector( aProperties );
99 // and sort them for access via bsearch
100 ::std::sort( aProperties.begin(), aProperties.end(),
101 ::chart::PropertyNameLess() );
103 // transfer result to static Sequence
104 aPropSeq = ::chart::ContainerHelper::ContainerToSequence( aProperties );
107 return aPropSeq;
110 ::cppu::IPropertyArrayHelper & lcl_getInfoHelper()
112 static ::cppu::OPropertyArrayHelper aArrayHelper(
113 lcl_GetPropertySequence(),
114 /* bSorted = */ sal_True );
116 return aArrayHelper;
119 } // anonymous namespace
121 namespace chart
124 ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
125 Reference<
126 uno::XComponentContext > const & xContext,
127 const ::rtl::OUString & rServiceName,
128 StackMode eStackMode,
129 sal_Int32 nNumberOfLines ) :
130 ChartTypeTemplate( xContext, rServiceName ),
131 ::property::OPropertySet( m_aMutex ),
132 m_eStackMode( eStackMode )
134 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
137 ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
140 // ____ OPropertySet ____
141 uno::Any ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
142 throw(beans::UnknownPropertyException)
144 static tPropertyValueMap aStaticDefaults;
146 // /--
147 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
148 if( 0 == aStaticDefaults.size() )
150 // initialize defaults
151 lcl_AddDefaultsToMap( aStaticDefaults );
154 tPropertyValueMap::const_iterator aFound(
155 aStaticDefaults.find( nHandle ));
157 if( aFound == aStaticDefaults.end())
158 return uno::Any();
160 return (*aFound).second;
161 // \--
164 ::cppu::IPropertyArrayHelper & SAL_CALL ColumnLineChartTypeTemplate::getInfoHelper()
166 return lcl_getInfoHelper();
170 // ____ XPropertySet ____
171 uno::Reference< beans::XPropertySetInfo > SAL_CALL
172 ColumnLineChartTypeTemplate::getPropertySetInfo()
173 throw (uno::RuntimeException)
175 static uno::Reference< beans::XPropertySetInfo > xInfo;
177 // /--
178 MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
179 if( !xInfo.is())
181 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo(
182 getInfoHelper());
185 return xInfo;
186 // \--
189 void ColumnLineChartTypeTemplate::createChartTypes(
190 const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
191 const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
192 const Sequence< Reference< XChartType > >& aOldChartTypesSeq )
194 if( rCoordSys.getLength() == 0 ||
195 ! rCoordSys[0].is() )
196 return;
200 Reference< lang::XMultiServiceFactory > xFact(
201 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
202 Sequence< Reference< XDataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
203 sal_Int32 nNumberOfSeries = aFlatSeriesSeq.getLength();
204 sal_Int32 nNumberOfLines = 0;
205 sal_Int32 nNumberOfColumns = 0;
207 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
208 OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
209 if( nNumberOfLines < 0 )
210 nNumberOfLines = 0;
212 if( nNumberOfLines >= nNumberOfSeries )
214 if( nNumberOfSeries > 0 )
216 nNumberOfLines = nNumberOfSeries - 1;
217 nNumberOfColumns = 1;
219 else
220 nNumberOfLines = 0;
222 else
223 nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
225 // Columns
226 // -------
227 Reference< XChartType > xCT(
228 xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
230 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aOldChartTypesSeq, xCT );
232 Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
233 xCTCnt->setChartTypes( Sequence< Reference< chart2::XChartType > >( &xCT, 1 ));
235 if( nNumberOfColumns > 0 )
237 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
238 Sequence< Reference< XDataSeries > > aColumnSeq( nNumberOfColumns );
239 ::std::copy( aFlatSeriesSeq.getConstArray(),
240 aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
241 aColumnSeq.getArray());
242 xDSCnt->setDataSeries( aColumnSeq );
245 // Lines
246 // -----
247 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
248 xCTCnt.set( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
249 xCTCnt->addChartType( xCT );
251 if( nNumberOfLines > 0 )
253 Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
254 Sequence< Reference< XDataSeries > > aLineSeq( nNumberOfLines );
255 ::std::copy( aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
256 aFlatSeriesSeq.getConstArray() + aFlatSeriesSeq.getLength(),
257 aLineSeq.getArray());
258 xDSCnt->setDataSeries( aLineSeq );
261 catch( uno::Exception & ex )
263 ASSERT_EXCEPTION( ex );
267 void SAL_CALL ColumnLineChartTypeTemplate::applyStyle(
268 const Reference< chart2::XDataSeries >& xSeries,
269 ::sal_Int32 nChartTypeIndex,
270 ::sal_Int32 nSeriesIndex,
271 ::sal_Int32 nSeriesCount )
272 throw (uno::RuntimeException)
274 ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
276 if( nChartTypeIndex==1 ) // lines
278 Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
279 if( xProp.is() )
281 DataSeriesHelper::switchLinesOnOrOff( xProp, true );
282 DataSeriesHelper::switchSymbolsOnOrOff( xProp, false, nSeriesIndex );
283 DataSeriesHelper::makeLinesThickOrThin( xProp, true );
288 StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
290 if( nChartTypeIndex == 0 )
291 return m_eStackMode;
292 return StackMode_NONE;
295 // ____ XChartTypeTemplate ____
296 sal_Bool SAL_CALL ColumnLineChartTypeTemplate::matchesTemplate(
297 const uno::Reference< XDiagram >& xDiagram,
298 sal_Bool bAdaptProperties )
299 throw (uno::RuntimeException)
301 sal_Bool bResult = sal_False;
303 if( ! xDiagram.is())
304 return bResult;
308 Reference< chart2::XChartType > xColumnChartType;
309 Reference< XCoordinateSystem > xColumnChartCooSys;
310 Reference< chart2::XChartType > xLineChartType;
311 sal_Int32 nNumberOfChartTypes = 0;
313 Reference< XCoordinateSystemContainer > xCooSysCnt(
314 xDiagram, uno::UNO_QUERY_THROW );
315 Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
316 xCooSysCnt->getCoordinateSystems());
317 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
319 Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
320 Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
321 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
323 if( aChartTypeSeq[j].is())
325 ++nNumberOfChartTypes;
326 if( nNumberOfChartTypes > 2 )
327 break;
328 OUString aCTService = aChartTypeSeq[j]->getChartType();
329 if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ))
331 xColumnChartType.set( aChartTypeSeq[j] );
332 xColumnChartCooSys.set( aCooSysSeq[i] );
334 else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_LINE ))
335 xLineChartType.set( aChartTypeSeq[j] );
338 if( nNumberOfChartTypes > 2 )
339 break;
342 if( nNumberOfChartTypes == 2 &&
343 xColumnChartType.is() &&
344 xLineChartType.is())
346 OSL_ASSERT( xColumnChartCooSys.is());
348 // check stackmode of bars
349 bResult = (xColumnChartCooSys->getDimension() == getDimension());
350 if( bResult )
352 bool bFound=false;
353 bool bAmbiguous=false;
354 bResult = ( DiagramHelper::getStackModeFromChartType(
355 xColumnChartType, bFound, bAmbiguous,
356 xColumnChartCooSys )
357 == getStackMode( 0 ) );
359 if( bResult && bAdaptProperties )
361 Reference< XDataSeriesContainer > xSeriesContainer( xLineChartType, uno::UNO_QUERY );
362 if( xSeriesContainer.is() )
364 sal_Int32 nNumberOfLines = xSeriesContainer->getDataSeries().getLength();
365 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
371 catch( uno::Exception & ex )
373 ASSERT_EXCEPTION( ex );
376 return bResult;
379 Reference< chart2::XChartType > ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
381 Reference< chart2::XChartType > xCT;
382 Reference< lang::XMultiServiceFactory > xFact(
383 GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
384 if(xFact.is())
386 if( nChartTypeIndex == 0 )
387 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
388 else
389 xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
391 return xCT;
394 Reference< XChartType > SAL_CALL ColumnLineChartTypeTemplate::getChartTypeForNewSeries(
395 const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
396 throw (uno::RuntimeException)
398 Reference< chart2::XChartType > xResult;
402 Reference< lang::XMultiServiceFactory > xFact(
403 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
404 xResult.set( xFact->createInstance(
405 CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
406 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
408 catch( uno::Exception & ex )
410 ASSERT_EXCEPTION( ex );
413 setStackModePropertiesToChartType(xResult);
414 return xResult;
417 Reference< XDataInterpreter > SAL_CALL ColumnLineChartTypeTemplate::getDataInterpreter()
418 throw (uno::RuntimeException)
420 if( ! m_xDataInterpreter.is())
422 sal_Int32 nNumberOfLines = 1;
423 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
424 m_xDataInterpreter.set( new ColumnLineDataInterpreter( nNumberOfLines, GetComponentContext() ) );
426 else
428 //todo...
429 OSL_ENSURE( false, "number of lines may not be valid anymore in the datainterpreter" );
433 return m_xDataInterpreter;
436 // ----------------------------------------
438 uno::Sequence< ::rtl::OUString > ColumnLineChartTypeTemplate::getSupportedServiceNames_Static()
440 uno::Sequence< ::rtl::OUString > aServices( 2 );
441 aServices[ 0 ] = lcl_aServiceName;
442 aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartTypeTemplate" );
443 return aServices;
446 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
447 APPHELPER_XSERVICEINFO_IMPL( ColumnLineChartTypeTemplate, lcl_aServiceName );
449 IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
450 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
452 } // namespace chart