Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / model / template / ColumnLineChartTypeTemplate.cxx
blob69769bc08ff91c248257f6bf01afff1c032370c0
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 "ColumnLineChartTypeTemplate.hxx"
21 #include "ColumnChartType.hxx"
22 #include "LineChartType.hxx"
23 #include <CommonConverters.hxx>
24 #include <BaseCoordinateSystem.hxx>
25 #include <Diagram.hxx>
26 #include <DiagramHelper.hxx>
27 #include <DataSeries.hxx>
28 #include <DataSeriesHelper.hxx>
29 #include <servicenames_charttypes.hxx>
30 #include "ColumnLineDataInterpreter.hxx"
31 #include <PropertyHelper.hxx>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/drawing/LineStyle.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <comphelper/diagnose_ex.hxx>
37 #include <algorithm>
39 using namespace ::com::sun::star::chart2;
40 using namespace ::com::sun::star;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::beans::Property;
46 namespace
49 enum
51 PROP_COL_LINE_NUMBER_OF_LINES
54 ::chart::tPropertyValueMap& StaticColumnLineChartTypeTemplateDefaults()
56 static ::chart::tPropertyValueMap aStaticDefaults =
57 []()
59 ::chart::tPropertyValueMap aOutMap;
60 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( aOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
61 return aOutMap;
62 }();
63 return aStaticDefaults;
66 ::cppu::OPropertyArrayHelper& StaticColumnLineChartTypeTemplateInfoHelper()
68 static ::cppu::OPropertyArrayHelper aPropHelper(
69 []()
71 std::vector< css::beans::Property > aProperties {
72 { "NumberOfLines",
73 PROP_COL_LINE_NUMBER_OF_LINES,
74 cppu::UnoType<sal_Int32>::get(),
75 beans::PropertyAttribute::BOUND
76 | beans::PropertyAttribute::MAYBEDEFAULT } };
78 std::sort( aProperties.begin(), aProperties.end(),
79 ::chart::PropertyNameLess() );
81 return comphelper::containerToSequence( aProperties );
82 }());
83 return aPropHelper;
86 uno::Reference< beans::XPropertySetInfo >& StaticColumnLineChartTypeTemplateInfo()
88 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
89 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticColumnLineChartTypeTemplateInfoHelper() ) );
90 return xPropertySetInfo;
93 } // anonymous namespace
95 namespace chart
98 ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
99 Reference<
100 uno::XComponentContext > const & xContext,
101 const OUString & rServiceName,
102 StackMode eStackMode,
103 sal_Int32 nNumberOfLines ) :
104 ChartTypeTemplate( xContext, rServiceName ),
105 m_eStackMode( eStackMode )
107 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
110 ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
113 // ____ OPropertySet ____
114 void ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle, uno::Any& rAny ) const
116 const tPropertyValueMap& rStaticDefaults = StaticColumnLineChartTypeTemplateDefaults();
117 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
118 if( aFound == rStaticDefaults.end() )
119 rAny.clear();
120 else
121 rAny = (*aFound).second;
124 ::cppu::IPropertyArrayHelper & SAL_CALL ColumnLineChartTypeTemplate::getInfoHelper()
126 return StaticColumnLineChartTypeTemplateInfoHelper();
129 // ____ XPropertySet ____
130 uno::Reference< beans::XPropertySetInfo > SAL_CALL ColumnLineChartTypeTemplate::getPropertySetInfo()
132 return StaticColumnLineChartTypeTemplateInfo();
135 void ColumnLineChartTypeTemplate::createChartTypes(
136 const std::vector< std::vector< rtl::Reference< DataSeries > > > & aSeriesSeq,
137 const std::vector< rtl::Reference< BaseCoordinateSystem > > & rCoordSys,
138 const std::vector< rtl::Reference< ChartType > >& aOldChartTypesSeq )
140 if( rCoordSys.empty() )
141 return;
145 const std::vector< rtl::Reference< DataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
146 sal_Int32 nNumberOfSeries = aFlatSeriesSeq.size();
147 sal_Int32 nNumberOfLines = 0;
148 sal_Int32 nNumberOfColumns = 0;
150 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
151 OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
152 if( nNumberOfLines < 0 )
153 nNumberOfLines = 0;
155 if( nNumberOfLines >= nNumberOfSeries )
157 if( nNumberOfSeries > 0 )
159 nNumberOfLines = nNumberOfSeries - 1;
160 nNumberOfColumns = 1;
162 else
163 nNumberOfLines = 0;
165 else
166 nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
168 // Columns
170 rtl::Reference< ChartType > xCT = new ColumnChartType();
172 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aOldChartTypesSeq, xCT );
174 rCoordSys[ 0 ]->setChartTypes( std::vector{xCT} );
176 if( nNumberOfColumns > 0 )
178 std::vector< rtl::Reference< DataSeries > > aColumnSeq( nNumberOfColumns );
179 std::copy( aFlatSeriesSeq.begin(),
180 aFlatSeriesSeq.begin() + nNumberOfColumns,
181 aColumnSeq.begin());
182 xCT->setDataSeries( aColumnSeq );
185 // Lines
187 xCT = new LineChartType();
188 rCoordSys[ 0 ]->addChartType( xCT );
190 if( nNumberOfLines > 0 )
192 std::vector< rtl::Reference< DataSeries > > aLineSeq( nNumberOfLines );
193 std::copy( aFlatSeriesSeq.begin() + nNumberOfColumns,
194 aFlatSeriesSeq.end(),
195 aLineSeq.begin());
196 xCT->setDataSeries( aLineSeq );
199 catch( const uno::Exception & )
201 DBG_UNHANDLED_EXCEPTION("chart2");
205 void ColumnLineChartTypeTemplate::applyStyle2(
206 const rtl::Reference< DataSeries >& xSeries,
207 ::sal_Int32 nChartTypeIndex,
208 ::sal_Int32 nSeriesIndex,
209 ::sal_Int32 nSeriesCount )
211 ChartTypeTemplate::applyStyle2( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
213 if( nChartTypeIndex==0 ) // columns
215 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, "BorderStyle", uno::Any( drawing::LineStyle_NONE ) );
217 else if( nChartTypeIndex==1 ) // lines
219 DataSeriesHelper::switchLinesOnOrOff( xSeries, true );
220 DataSeriesHelper::switchSymbolsOnOrOff( xSeries, false, nSeriesIndex );
221 DataSeriesHelper::makeLinesThickOrThin( xSeries, true );
225 StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
227 if( nChartTypeIndex == 0 )
228 return m_eStackMode;
229 return StackMode::NONE;
232 // ____ XChartTypeTemplate ____
233 bool ColumnLineChartTypeTemplate::matchesTemplate2(
234 const rtl::Reference< ::chart::Diagram >& xDiagram,
235 bool bAdaptProperties )
237 bool bResult = false;
239 if( ! xDiagram.is())
240 return bResult;
244 rtl::Reference< ChartType > xColumnChartType;
245 rtl::Reference< BaseCoordinateSystem > xColumnChartCooSys;
246 rtl::Reference< ChartType > xLineChartType;
247 sal_Int32 nNumberOfChartTypes = 0;
249 for( rtl::Reference< BaseCoordinateSystem > const & coords : xDiagram->getBaseCoordinateSystems() )
251 const std::vector< rtl::Reference< ChartType > > aChartTypeSeq( coords->getChartTypes2());
252 for( rtl::Reference< ChartType > const & chartType : aChartTypeSeq )
254 ++nNumberOfChartTypes;
255 if( nNumberOfChartTypes > 2 )
256 break;
257 OUString aCTService = chartType->getChartType();
258 if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_COLUMN )
260 xColumnChartType = chartType;
261 xColumnChartCooSys = coords;
263 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_LINE )
264 xLineChartType = chartType;
266 if( nNumberOfChartTypes > 2 )
267 break;
270 if( nNumberOfChartTypes == 2 &&
271 xColumnChartType.is() &&
272 xLineChartType.is())
274 OSL_ASSERT( xColumnChartCooSys.is());
276 // check stackmode of bars
277 bResult = (xColumnChartCooSys->getDimension() == getDimension());
278 if( bResult )
280 bool bFound=false;
281 bool bAmbiguous=false;
282 bResult = ( DiagramHelper::getStackModeFromChartType(
283 xColumnChartType, bFound, bAmbiguous,
284 xColumnChartCooSys )
285 == getStackMode( 0 ) );
287 if( bResult && bAdaptProperties )
289 if( xLineChartType.is() )
291 sal_Int32 nNumberOfLines = xLineChartType->getDataSeries().getLength();
292 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
298 catch( const uno::Exception & )
300 DBG_UNHANDLED_EXCEPTION("chart2");
303 return bResult;
306 rtl::Reference< ChartType > ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
308 if( nChartTypeIndex == 0 )
309 return new ColumnChartType();
310 else
311 return new LineChartType();
314 rtl::Reference< ChartType > ColumnLineChartTypeTemplate::getChartTypeForNewSeries2(
315 const std::vector< rtl::Reference< ChartType > >& aFormerlyUsedChartTypes )
317 rtl::Reference< ChartType > xResult;
321 xResult = new LineChartType();
322 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aFormerlyUsedChartTypes, xResult );
324 catch( const uno::Exception & )
326 DBG_UNHANDLED_EXCEPTION("chart2");
329 return xResult;
332 rtl::Reference< DataInterpreter > ColumnLineChartTypeTemplate::getDataInterpreter2()
334 if( ! m_xDataInterpreter.is())
336 sal_Int32 nNumberOfLines = 1;
337 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
338 m_xDataInterpreter = new ColumnLineDataInterpreter( nNumberOfLines );
340 else
342 //todo...
343 OSL_FAIL( "number of lines may not be valid anymore in the datainterpreter" );
347 return m_xDataInterpreter;
350 IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
351 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
353 } // namespace chart
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */