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