Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "ColumnLineChartTypeTemplate.hxx"
21 : #include "macros.hxx"
22 : #include "CommonConverters.hxx"
23 : #include "DiagramHelper.hxx"
24 : #include "DataSeriesHelper.hxx"
25 : #include "servicenames_charttypes.hxx"
26 : #include "ColumnLineDataInterpreter.hxx"
27 : #include "ContainerHelper.hxx"
28 : #include "PropertyHelper.hxx"
29 : #include <com/sun/star/beans/PropertyAttribute.hpp>
30 : #include <com/sun/star/chart2/XChartTypeContainer.hpp>
31 : #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
32 : #include <com/sun/star/drawing/LineStyle.hpp>
33 :
34 : #include <algorithm>
35 :
36 : using namespace ::com::sun::star::chart2;
37 : using namespace ::com::sun::star;
38 :
39 : using ::com::sun::star::uno::Reference;
40 : using ::com::sun::star::uno::Sequence;
41 : using ::com::sun::star::beans::Property;
42 : using ::com::sun::star::uno::Any;
43 : using ::osl::MutexGuard;
44 :
45 : namespace
46 : {
47 :
48 11 : static const OUString lcl_aServiceName( "com.sun.star.chart2.ColumnLineChartTypeTemplate" );
49 :
50 : enum
51 : {
52 : PROP_COL_LINE_NUMBER_OF_LINES
53 : };
54 :
55 0 : void lcl_AddPropertiesToVector(
56 : ::std::vector< Property > & rOutProperties )
57 : {
58 : rOutProperties.push_back(
59 : Property( "NumberOfLines",
60 : PROP_COL_LINE_NUMBER_OF_LINES,
61 0 : ::getCppuType( reinterpret_cast< const sal_Int32 * >(0)),
62 : beans::PropertyAttribute::BOUND
63 0 : | beans::PropertyAttribute::MAYBEDEFAULT ));
64 0 : }
65 :
66 : struct StaticColumnLineChartTypeTemplateDefaults_Initializer
67 : {
68 3 : ::chart::tPropertyValueMap* operator()()
69 : {
70 3 : static ::chart::tPropertyValueMap aStaticDefaults;
71 3 : lcl_AddDefaultsToMap( aStaticDefaults );
72 3 : return &aStaticDefaults;
73 : }
74 : private:
75 3 : void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
76 : {
77 3 : ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( rOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
78 3 : }
79 : };
80 :
81 : struct StaticColumnLineChartTypeTemplateDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticColumnLineChartTypeTemplateDefaults_Initializer >
82 : {
83 : };
84 :
85 : struct StaticColumnLineChartTypeTemplateInfoHelper_Initializer
86 : {
87 0 : ::cppu::OPropertyArrayHelper* operator()()
88 : {
89 0 : static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
90 0 : return &aPropHelper;
91 : }
92 :
93 : private:
94 0 : uno::Sequence< Property > lcl_GetPropertySequence()
95 : {
96 0 : ::std::vector< ::com::sun::star::beans::Property > aProperties;
97 0 : lcl_AddPropertiesToVector( aProperties );
98 :
99 : ::std::sort( aProperties.begin(), aProperties.end(),
100 0 : ::chart::PropertyNameLess() );
101 :
102 0 : return ::chart::ContainerHelper::ContainerToSequence( aProperties );
103 : }
104 :
105 : };
106 :
107 : struct StaticColumnLineChartTypeTemplateInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticColumnLineChartTypeTemplateInfoHelper_Initializer >
108 : {
109 : };
110 :
111 : struct StaticColumnLineChartTypeTemplateInfo_Initializer
112 : {
113 0 : uno::Reference< beans::XPropertySetInfo >* operator()()
114 : {
115 : static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
116 0 : ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticColumnLineChartTypeTemplateInfoHelper::get() ) );
117 0 : return &xPropertySetInfo;
118 : }
119 : };
120 :
121 : struct StaticColumnLineChartTypeTemplateInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticColumnLineChartTypeTemplateInfo_Initializer >
122 : {
123 : };
124 :
125 : } // anonymous namespace
126 :
127 : namespace chart
128 : {
129 :
130 35 : ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
131 : Reference<
132 : uno::XComponentContext > const & xContext,
133 : const OUString & rServiceName,
134 : StackMode eStackMode,
135 : sal_Int32 nNumberOfLines ) :
136 : ChartTypeTemplate( xContext, rServiceName ),
137 : ::property::OPropertySet( m_aMutex ),
138 35 : m_eStackMode( eStackMode )
139 : {
140 35 : setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
141 35 : }
142 :
143 70 : ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
144 70 : {}
145 :
146 : // ____ OPropertySet ____
147 35 : uno::Any ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle ) const
148 : throw(beans::UnknownPropertyException)
149 : {
150 35 : const tPropertyValueMap& rStaticDefaults = *StaticColumnLineChartTypeTemplateDefaults::get();
151 35 : tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
152 35 : if( aFound == rStaticDefaults.end() )
153 0 : return uno::Any();
154 35 : return (*aFound).second;
155 : }
156 :
157 0 : ::cppu::IPropertyArrayHelper & SAL_CALL ColumnLineChartTypeTemplate::getInfoHelper()
158 : {
159 0 : return *StaticColumnLineChartTypeTemplateInfoHelper::get();
160 : }
161 :
162 : // ____ XPropertySet ____
163 0 : uno::Reference< beans::XPropertySetInfo > SAL_CALL ColumnLineChartTypeTemplate::getPropertySetInfo()
164 : throw (uno::RuntimeException)
165 : {
166 0 : return *StaticColumnLineChartTypeTemplateInfo::get();
167 : }
168 :
169 0 : void ColumnLineChartTypeTemplate::createChartTypes(
170 : const Sequence< Sequence< Reference< XDataSeries > > > & aSeriesSeq,
171 : const Sequence< Reference< XCoordinateSystem > > & rCoordSys,
172 : const Sequence< Reference< XChartType > >& aOldChartTypesSeq )
173 : {
174 0 : if( rCoordSys.getLength() == 0 ||
175 0 : ! rCoordSys[0].is() )
176 0 : return;
177 :
178 : try
179 : {
180 : Reference< lang::XMultiServiceFactory > xFact(
181 0 : GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
182 0 : Sequence< Reference< XDataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
183 0 : sal_Int32 nNumberOfSeries = aFlatSeriesSeq.getLength();
184 0 : sal_Int32 nNumberOfLines = 0;
185 0 : sal_Int32 nNumberOfColumns = 0;
186 :
187 0 : getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
188 : OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
189 0 : if( nNumberOfLines < 0 )
190 0 : nNumberOfLines = 0;
191 :
192 0 : if( nNumberOfLines >= nNumberOfSeries )
193 : {
194 0 : if( nNumberOfSeries > 0 )
195 : {
196 0 : nNumberOfLines = nNumberOfSeries - 1;
197 0 : nNumberOfColumns = 1;
198 : }
199 : else
200 0 : nNumberOfLines = 0;
201 : }
202 : else
203 0 : nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
204 :
205 : // Columns
206 : // -------
207 : Reference< XChartType > xCT(
208 0 : xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY_THROW );
209 :
210 0 : ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aOldChartTypesSeq, xCT );
211 :
212 0 : Reference< XChartTypeContainer > xCTCnt( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
213 0 : xCTCnt->setChartTypes( Sequence< Reference< chart2::XChartType > >( &xCT, 1 ));
214 :
215 0 : if( nNumberOfColumns > 0 )
216 : {
217 0 : Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
218 0 : Sequence< Reference< XDataSeries > > aColumnSeq( nNumberOfColumns );
219 : ::std::copy( aFlatSeriesSeq.getConstArray(),
220 0 : aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
221 0 : aColumnSeq.getArray());
222 0 : xDSCnt->setDataSeries( aColumnSeq );
223 : }
224 :
225 : // Lines
226 : // -----
227 0 : xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
228 0 : xCTCnt.set( rCoordSys[ 0 ], uno::UNO_QUERY_THROW );
229 0 : xCTCnt->addChartType( xCT );
230 :
231 0 : if( nNumberOfLines > 0 )
232 : {
233 0 : Reference< XDataSeriesContainer > xDSCnt( xCT, uno::UNO_QUERY_THROW );
234 0 : Sequence< Reference< XDataSeries > > aLineSeq( nNumberOfLines );
235 0 : ::std::copy( aFlatSeriesSeq.getConstArray() + nNumberOfColumns,
236 0 : aFlatSeriesSeq.getConstArray() + aFlatSeriesSeq.getLength(),
237 0 : aLineSeq.getArray());
238 0 : xDSCnt->setDataSeries( aLineSeq );
239 0 : }
240 : }
241 0 : catch( const uno::Exception & ex )
242 : {
243 : ASSERT_EXCEPTION( ex );
244 : }
245 : }
246 :
247 0 : void SAL_CALL ColumnLineChartTypeTemplate::applyStyle(
248 : const Reference< chart2::XDataSeries >& xSeries,
249 : ::sal_Int32 nChartTypeIndex,
250 : ::sal_Int32 nSeriesIndex,
251 : ::sal_Int32 nSeriesCount )
252 : throw (uno::RuntimeException)
253 : {
254 0 : ChartTypeTemplate::applyStyle( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
255 :
256 0 : if( nChartTypeIndex==0 ) // columns
257 : {
258 0 : DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, "BorderStyle", uno::makeAny( drawing::LineStyle_NONE ) );
259 : }
260 0 : else if( nChartTypeIndex==1 ) // lines
261 : {
262 0 : Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
263 0 : if( xProp.is() )
264 : {
265 0 : DataSeriesHelper::switchLinesOnOrOff( xProp, true );
266 0 : DataSeriesHelper::switchSymbolsOnOrOff( xProp, false, nSeriesIndex );
267 0 : DataSeriesHelper::makeLinesThickOrThin( xProp, true );
268 0 : }
269 : }
270 0 : }
271 :
272 0 : StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
273 : {
274 0 : if( nChartTypeIndex == 0 )
275 0 : return m_eStackMode;
276 0 : return StackMode_NONE;
277 : }
278 :
279 : // ____ XChartTypeTemplate ____
280 35 : sal_Bool SAL_CALL ColumnLineChartTypeTemplate::matchesTemplate(
281 : const uno::Reference< XDiagram >& xDiagram,
282 : sal_Bool bAdaptProperties )
283 : throw (uno::RuntimeException)
284 : {
285 35 : sal_Bool bResult = sal_False;
286 :
287 35 : if( ! xDiagram.is())
288 0 : return bResult;
289 :
290 : try
291 : {
292 35 : Reference< chart2::XChartType > xColumnChartType;
293 70 : Reference< XCoordinateSystem > xColumnChartCooSys;
294 70 : Reference< chart2::XChartType > xLineChartType;
295 35 : sal_Int32 nNumberOfChartTypes = 0;
296 :
297 : Reference< XCoordinateSystemContainer > xCooSysCnt(
298 70 : xDiagram, uno::UNO_QUERY_THROW );
299 : Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
300 70 : xCooSysCnt->getCoordinateSystems());
301 70 : for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
302 : {
303 35 : Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
304 70 : Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
305 70 : for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
306 : {
307 35 : if( aChartTypeSeq[j].is())
308 : {
309 35 : ++nNumberOfChartTypes;
310 35 : if( nNumberOfChartTypes > 2 )
311 0 : break;
312 35 : OUString aCTService = aChartTypeSeq[j]->getChartType();
313 35 : if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ))
314 : {
315 0 : xColumnChartType.set( aChartTypeSeq[j] );
316 0 : xColumnChartCooSys.set( aCooSysSeq[i] );
317 : }
318 35 : else if( aCTService.equals( CHART2_SERVICE_NAME_CHARTTYPE_LINE ))
319 12 : xLineChartType.set( aChartTypeSeq[j] );
320 : }
321 : }
322 35 : if( nNumberOfChartTypes > 2 )
323 0 : break;
324 35 : }
325 :
326 35 : if( nNumberOfChartTypes == 2 &&
327 35 : xColumnChartType.is() &&
328 0 : xLineChartType.is())
329 : {
330 : OSL_ASSERT( xColumnChartCooSys.is());
331 :
332 : // check stackmode of bars
333 0 : bResult = (xColumnChartCooSys->getDimension() == getDimension());
334 0 : if( bResult )
335 : {
336 0 : bool bFound=false;
337 0 : bool bAmbiguous=false;
338 : bResult = ( DiagramHelper::getStackModeFromChartType(
339 : xColumnChartType, bFound, bAmbiguous,
340 0 : xColumnChartCooSys )
341 0 : == getStackMode( 0 ) );
342 :
343 0 : if( bResult && bAdaptProperties )
344 : {
345 0 : Reference< XDataSeriesContainer > xSeriesContainer( xLineChartType, uno::UNO_QUERY );
346 0 : if( xSeriesContainer.is() )
347 : {
348 0 : sal_Int32 nNumberOfLines = xSeriesContainer->getDataSeries().getLength();
349 0 : setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::makeAny( nNumberOfLines ));
350 0 : }
351 : }
352 : }
353 35 : }
354 : }
355 0 : catch( const uno::Exception & ex )
356 : {
357 : ASSERT_EXCEPTION( ex );
358 : }
359 :
360 35 : return bResult;
361 : }
362 :
363 0 : Reference< chart2::XChartType > ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
364 : {
365 0 : Reference< chart2::XChartType > xCT;
366 : Reference< lang::XMultiServiceFactory > xFact(
367 0 : GetComponentContext()->getServiceManager(), uno::UNO_QUERY );
368 0 : if(xFact.is())
369 : {
370 0 : if( nChartTypeIndex == 0 )
371 0 : xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN ), uno::UNO_QUERY );
372 : else
373 0 : xCT.set( xFact->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY );
374 : }
375 0 : return xCT;
376 : }
377 :
378 0 : Reference< XChartType > SAL_CALL ColumnLineChartTypeTemplate::getChartTypeForNewSeries(
379 : const uno::Sequence< Reference< chart2::XChartType > >& aFormerlyUsedChartTypes )
380 : throw (uno::RuntimeException)
381 : {
382 0 : Reference< chart2::XChartType > xResult;
383 :
384 : try
385 : {
386 : Reference< lang::XMultiServiceFactory > xFact(
387 0 : GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW );
388 0 : xResult.set( xFact->createInstance(
389 0 : CHART2_SERVICE_NAME_CHARTTYPE_LINE ), uno::UNO_QUERY_THROW );
390 0 : ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes, xResult );
391 : }
392 0 : catch( const uno::Exception & ex )
393 : {
394 : ASSERT_EXCEPTION( ex );
395 : }
396 :
397 0 : return xResult;
398 : }
399 :
400 0 : Reference< XDataInterpreter > SAL_CALL ColumnLineChartTypeTemplate::getDataInterpreter()
401 : throw (uno::RuntimeException)
402 : {
403 0 : if( ! m_xDataInterpreter.is())
404 : {
405 0 : sal_Int32 nNumberOfLines = 1;
406 0 : getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
407 0 : m_xDataInterpreter.set( new ColumnLineDataInterpreter( nNumberOfLines, GetComponentContext() ) );
408 : }
409 : else
410 : {
411 : //todo...
412 : OSL_FAIL( "number of lines may not be valid anymore in the datainterpreter" );
413 :
414 : }
415 :
416 0 : return m_xDataInterpreter;
417 : }
418 :
419 : // ----------------------------------------
420 :
421 0 : uno::Sequence< OUString > ColumnLineChartTypeTemplate::getSupportedServiceNames_Static()
422 : {
423 0 : uno::Sequence< OUString > aServices( 2 );
424 0 : aServices[ 0 ] = lcl_aServiceName;
425 0 : aServices[ 1 ] = "com.sun.star.chart2.ChartTypeTemplate";
426 0 : return aServices;
427 : }
428 :
429 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
430 0 : APPHELPER_XSERVICEINFO_IMPL( ColumnLineChartTypeTemplate, lcl_aServiceName );
431 :
432 280 : IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
433 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
434 :
435 33 : } // namespace chart
436 :
437 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|