tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / model / template / ColumnLineChartTypeTemplate.cxx
blob30eb42d2ce037ba40d71a69f4ce9a9a2799f4ddd
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;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::beans::Property;
45 namespace
48 enum
50 PROP_COL_LINE_NUMBER_OF_LINES
53 ::chart::tPropertyValueMap& StaticColumnLineChartTypeTemplateDefaults()
55 static ::chart::tPropertyValueMap aStaticDefaults =
56 []()
58 ::chart::tPropertyValueMap aOutMap;
59 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( aOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
60 return aOutMap;
61 }();
62 return aStaticDefaults;
65 ::cppu::OPropertyArrayHelper& StaticColumnLineChartTypeTemplateInfoHelper()
67 static ::cppu::OPropertyArrayHelper aPropHelper(
68 []()
70 std::vector< css::beans::Property > aProperties {
71 { u"NumberOfLines"_ustr,
72 PROP_COL_LINE_NUMBER_OF_LINES,
73 cppu::UnoType<sal_Int32>::get(),
74 beans::PropertyAttribute::BOUND
75 | beans::PropertyAttribute::MAYBEDEFAULT } };
77 std::sort( aProperties.begin(), aProperties.end(),
78 ::chart::PropertyNameLess() );
80 return comphelper::containerToSequence( aProperties );
81 }());
82 return aPropHelper;
85 uno::Reference< beans::XPropertySetInfo >& StaticColumnLineChartTypeTemplateInfo()
87 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
88 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticColumnLineChartTypeTemplateInfoHelper() ) );
89 return xPropertySetInfo;
92 } // anonymous namespace
94 namespace chart
97 ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
98 Reference<
99 uno::XComponentContext > const & xContext,
100 const OUString & rServiceName,
101 StackMode eStackMode,
102 sal_Int32 nNumberOfLines ) :
103 ChartTypeTemplate( xContext, rServiceName ),
104 m_eStackMode( eStackMode )
106 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
109 ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
112 // ____ OPropertySet ____
113 void ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle, uno::Any& rAny ) const
115 const tPropertyValueMap& rStaticDefaults = StaticColumnLineChartTypeTemplateDefaults();
116 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
117 if( aFound == rStaticDefaults.end() )
118 rAny.clear();
119 else
120 rAny = (*aFound).second;
123 ::cppu::IPropertyArrayHelper & SAL_CALL ColumnLineChartTypeTemplate::getInfoHelper()
125 return StaticColumnLineChartTypeTemplateInfoHelper();
128 // ____ XPropertySet ____
129 uno::Reference< beans::XPropertySetInfo > SAL_CALL ColumnLineChartTypeTemplate::getPropertySetInfo()
131 return StaticColumnLineChartTypeTemplateInfo();
134 void ColumnLineChartTypeTemplate::createChartTypes(
135 const std::vector< std::vector< rtl::Reference< DataSeries > > > & aSeriesSeq,
136 const std::vector< rtl::Reference< BaseCoordinateSystem > > & rCoordSys,
137 const std::vector< rtl::Reference< ChartType > >& aOldChartTypesSeq )
139 if( rCoordSys.empty() )
140 return;
144 const std::vector< rtl::Reference< DataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
145 sal_Int32 nNumberOfSeries = aFlatSeriesSeq.size();
146 sal_Int32 nNumberOfLines = 0;
147 sal_Int32 nNumberOfColumns = 0;
149 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
150 OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
151 if( nNumberOfLines < 0 )
152 nNumberOfLines = 0;
154 if( nNumberOfLines >= nNumberOfSeries )
156 if( nNumberOfSeries > 0 )
158 nNumberOfLines = nNumberOfSeries - 1;
159 nNumberOfColumns = 1;
161 else
162 nNumberOfLines = 0;
164 else
165 nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
167 // Columns
169 rtl::Reference< ChartType > xCT = new ColumnChartType();
171 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aOldChartTypesSeq, xCT );
173 rCoordSys[ 0 ]->setChartTypes( std::vector{xCT} );
175 if( nNumberOfColumns > 0 )
177 std::vector< rtl::Reference< DataSeries > > aColumnSeq( nNumberOfColumns );
178 std::copy( aFlatSeriesSeq.begin(),
179 aFlatSeriesSeq.begin() + nNumberOfColumns,
180 aColumnSeq.begin());
181 xCT->setDataSeries( aColumnSeq );
184 // Lines
186 xCT = new LineChartType();
187 rCoordSys[ 0 ]->addChartType( xCT );
189 if( nNumberOfLines > 0 )
191 std::vector< rtl::Reference< DataSeries > > aLineSeq( nNumberOfLines );
192 std::copy( aFlatSeriesSeq.begin() + nNumberOfColumns,
193 aFlatSeriesSeq.end(),
194 aLineSeq.begin());
195 xCT->setDataSeries( aLineSeq );
198 catch( const uno::Exception & )
200 DBG_UNHANDLED_EXCEPTION("chart2");
204 void ColumnLineChartTypeTemplate::applyStyle2(
205 const rtl::Reference< DataSeries >& xSeries,
206 ::sal_Int32 nChartTypeIndex,
207 ::sal_Int32 nSeriesIndex,
208 ::sal_Int32 nSeriesCount )
210 ChartTypeTemplate::applyStyle2( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
212 if( nChartTypeIndex==0 ) // columns
214 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, u"BorderStyle"_ustr, uno::Any( drawing::LineStyle_NONE ) );
216 else if( nChartTypeIndex==1 ) // lines
218 DataSeriesHelper::switchLinesOnOrOff( xSeries, true );
219 DataSeriesHelper::switchSymbolsOnOrOff( xSeries, false, nSeriesIndex );
220 DataSeriesHelper::makeLinesThickOrThin( xSeries, true );
224 StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
226 if( nChartTypeIndex == 0 )
227 return m_eStackMode;
228 return StackMode::NONE;
231 // ____ XChartTypeTemplate ____
232 bool ColumnLineChartTypeTemplate::matchesTemplate2(
233 const rtl::Reference< ::chart::Diagram >& xDiagram,
234 bool bAdaptProperties )
236 bool bResult = false;
238 if( ! xDiagram.is())
239 return bResult;
243 rtl::Reference< ChartType > xColumnChartType;
244 rtl::Reference< BaseCoordinateSystem > xColumnChartCooSys;
245 rtl::Reference< ChartType > xLineChartType;
246 sal_Int32 nNumberOfChartTypes = 0;
248 for( rtl::Reference< BaseCoordinateSystem > const & coords : xDiagram->getBaseCoordinateSystems() )
250 const std::vector< rtl::Reference< ChartType > > aChartTypeSeq( coords->getChartTypes2());
251 for( rtl::Reference< ChartType > const & chartType : aChartTypeSeq )
253 ++nNumberOfChartTypes;
254 if( nNumberOfChartTypes > 2 )
255 break;
256 OUString aCTService = chartType->getChartType();
257 if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_COLUMN )
259 xColumnChartType = chartType;
260 xColumnChartCooSys = coords;
262 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_LINE )
263 xLineChartType = chartType;
265 if( nNumberOfChartTypes > 2 )
266 break;
269 if( nNumberOfChartTypes == 2 &&
270 xColumnChartType.is() &&
271 xLineChartType.is())
273 OSL_ASSERT( xColumnChartCooSys.is());
275 // check stackmode of bars
276 bResult = (xColumnChartCooSys->getDimension() == getDimension());
277 if( bResult )
279 bool bFound=false;
280 bool bAmbiguous=false;
281 bResult = ( DiagramHelper::getStackModeFromChartType(
282 xColumnChartType, bFound, bAmbiguous,
283 xColumnChartCooSys )
284 == getStackMode( 0 ) );
286 if( bResult && bAdaptProperties )
288 if( xLineChartType.is() )
290 sal_Int32 nNumberOfLines = xLineChartType->getDataSeries().getLength();
291 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
297 catch( const uno::Exception & )
299 DBG_UNHANDLED_EXCEPTION("chart2");
302 return bResult;
305 rtl::Reference< ChartType > ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex )
307 if( nChartTypeIndex == 0 )
308 return new ColumnChartType();
309 else
310 return new LineChartType();
313 rtl::Reference< ChartType > ColumnLineChartTypeTemplate::getChartTypeForNewSeries2(
314 const std::vector< rtl::Reference< ChartType > >& aFormerlyUsedChartTypes )
316 rtl::Reference< ChartType > xResult;
320 xResult = new LineChartType();
321 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aFormerlyUsedChartTypes, xResult );
323 catch( const uno::Exception & )
325 DBG_UNHANDLED_EXCEPTION("chart2");
328 return xResult;
331 rtl::Reference< DataInterpreter > ColumnLineChartTypeTemplate::getDataInterpreter2()
333 if( ! m_xDataInterpreter.is())
335 sal_Int32 nNumberOfLines = 1;
336 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
337 m_xDataInterpreter = new ColumnLineDataInterpreter( nNumberOfLines );
339 else
341 //todo...
342 OSL_FAIL( "number of lines may not be valid anymore in the datainterpreter" );
346 return m_xDataInterpreter;
349 IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
350 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate, ChartTypeTemplate, OPropertySet )
352 } // namespace chart
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */