Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / itemsetwrapper / SeriesOptionsItemConverter.cxx
blob4c7c9071e251d34dba3426c2b000b8128388c371
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 <SeriesOptionsItemConverter.hxx>
21 #include "SchWhichPairs.hxx"
23 #include <ItemPropertyMap.hxx>
24 #include <GraphicPropertyItemConverter.hxx>
25 #include <MultipleItemConverter.hxx>
26 #include <ChartModelHelper.hxx>
27 #include <AxisHelper.hxx>
28 #include <DiagramHelper.hxx>
29 #include <ChartTypeHelper.hxx>
30 #include <DataSeriesHelper.hxx>
32 #include <com/sun/star/chart/MissingValueTreatment.hpp>
33 #include <com/sun/star/chart2/XDataSeries.hpp>
35 #include <svl/eitem.hxx>
36 #include <svl/intitem.hxx>
37 #include <svl/ilstitem.hxx>
38 #include <rtl/math.hxx>
39 #include <tools/diagnose_ex.h>
41 #include <functional>
42 #include <algorithm>
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::chart2;
47 namespace chart
49 namespace wrapper
52 SeriesOptionsItemConverter::SeriesOptionsItemConverter(
53 const uno::Reference< frame::XModel >& xChartModel
54 , const uno::Reference< uno::XComponentContext > & xContext
55 , const uno::Reference< beans::XPropertySet >& xPropertySet
56 , SfxItemPool& rItemPool )
57 : ItemConverter( xPropertySet, rItemPool )
58 , m_xChartModel(xChartModel)
59 , m_xCC(xContext)
60 , m_bAttachToMainAxis(true)
61 , m_bSupportingOverlapAndGapWidthProperties(false)
62 , m_bSupportingBarConnectors(false)
63 , m_nBarOverlap(0)
64 , m_nGapWidth(100)
65 , m_bConnectBars(false)
66 , m_bSupportingAxisSideBySide(false)
67 , m_bGroupBarsPerAxis(true)
68 , m_nAllSeriesAxisIndex(-1)
69 , m_bSupportingStartingAngle(false)
70 , m_nStartingAngle(90)
71 , m_bClockwise(false)
72 , m_aSupportedMissingValueTreatments()
73 , m_nMissingValueTreatment(0)
74 , m_bSupportingPlottingOfHiddenCells(false)
75 , m_bIncludeHiddenCells(true)
77 try
79 uno::Reference< XDataSeries > xDataSeries( xPropertySet, uno::UNO_QUERY );
81 m_bAttachToMainAxis = DiagramHelper::isSeriesAttachedToMainAxis( xDataSeries );
83 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
84 uno::Reference< beans::XPropertySet > xDiagramProperties( xDiagram, uno::UNO_QUERY );
85 uno::Reference< XChartType > xChartType( DiagramHelper::getChartTypeOfSeries( xDiagram , xDataSeries ) );
87 m_xCooSys = DataSeriesHelper::getCoordinateSystemOfSeries( xDataSeries, xDiagram );
88 if( m_xCooSys.is() )
90 uno::Reference< chart2::XAxis > xAxis( AxisHelper::getAxis( 1, 0, m_xCooSys ) );
91 chart2::ScaleData aScale( xAxis->getScaleData() );
92 m_bClockwise = (aScale.Orientation == chart2::AxisOrientation_REVERSE);
95 sal_Int32 nDimensionCount = DiagramHelper::getDimension( xDiagram );
96 m_bSupportingOverlapAndGapWidthProperties = ChartTypeHelper::isSupportingOverlapAndGapWidthProperties( xChartType, nDimensionCount );
98 if( m_bSupportingOverlapAndGapWidthProperties )
101 sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
103 uno::Sequence< sal_Int32 > aBarPositionSequence;
104 uno::Reference< beans::XPropertySet > xChartTypeProps( xChartType, uno::UNO_QUERY );
105 if( xChartTypeProps.is() )
107 if( xChartTypeProps->getPropertyValue( "OverlapSequence" ) >>= aBarPositionSequence )
109 if( nAxisIndex >= 0 && nAxisIndex < aBarPositionSequence.getLength() )
110 m_nBarOverlap = aBarPositionSequence[nAxisIndex];
112 if( xChartTypeProps->getPropertyValue( "GapwidthSequence" ) >>= aBarPositionSequence )
114 if( nAxisIndex >= 0 && nAxisIndex < aBarPositionSequence.getLength() )
115 m_nGapWidth = aBarPositionSequence[nAxisIndex];
120 m_bSupportingBarConnectors = ChartTypeHelper::isSupportingBarConnectors( xChartType, nDimensionCount );
121 if( m_bSupportingBarConnectors && xDiagramProperties.is() )
123 xDiagramProperties->getPropertyValue( "ConnectBars" ) >>= m_bConnectBars;
126 m_bSupportingAxisSideBySide = ChartTypeHelper::isSupportingAxisSideBySide( xChartType, nDimensionCount );
127 if( m_bSupportingAxisSideBySide && xDiagramProperties.is() )
129 xDiagramProperties->getPropertyValue( "GroupBarsPerAxis" ) >>= m_bGroupBarsPerAxis;
132 m_bSupportingStartingAngle = ChartTypeHelper::isSupportingStartingAngle( xChartType );
133 if( m_bSupportingStartingAngle )
135 xDiagramProperties->getPropertyValue( "StartingAngle" ) >>= m_nStartingAngle;
138 m_aSupportedMissingValueTreatments = ChartTypeHelper::getSupportedMissingValueTreatments( xChartType );
139 m_nMissingValueTreatment = DiagramHelper::getCorrectedMissingValueTreatment(
140 ChartModelHelper::findDiagram(m_xChartModel), xChartType );
142 uno::Reference< XChartDocument > xChartDoc( m_xChartModel, uno::UNO_QUERY );
143 uno::Reference< beans::XPropertySet > xProp( xChartDoc->getDataProvider(), uno::UNO_QUERY );
144 if( xProp.is() )
148 //test whether the data provider offers this property
149 xProp->getPropertyValue( "IncludeHiddenCells" );
150 //if not exception is thrown the property is offered
151 m_bSupportingPlottingOfHiddenCells = true;
152 xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= m_bIncludeHiddenCells;
154 catch( const beans::UnknownPropertyException& )
159 catch( const uno::Exception & )
161 DBG_UNHANDLED_EXCEPTION("chart2");
165 SeriesOptionsItemConverter::~SeriesOptionsItemConverter()
169 const sal_uInt16 * SeriesOptionsItemConverter::GetWhichPairs() const
171 // must span all used items!
172 return nSeriesOptionsWhichPairs;
175 bool SeriesOptionsItemConverter::GetItemProperty( tWhichIdType /*nWhichId*/, tPropertyNameWithMemberId & /*rOutProperty*/ ) const
177 return false;
180 bool SeriesOptionsItemConverter::ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
182 bool bChanged = false;
183 switch( nWhichId )
185 case SCHATTR_AXIS:
187 sal_Int32 nItemValue = static_cast< const SfxInt32Item & >(
188 rItemSet.Get( nWhichId )).GetValue();
189 bool bAttachToMainAxis = nItemValue == CHART_AXIS_PRIMARY_Y;
190 if( bAttachToMainAxis != m_bAttachToMainAxis )
192 //change model:
193 bChanged = DiagramHelper::attachSeriesToAxis( bAttachToMainAxis, uno::Reference< XDataSeries >::query( GetPropertySet() )
194 , ChartModelHelper::findDiagram(m_xChartModel), m_xCC );
196 if( bChanged )
197 m_bAttachToMainAxis = bAttachToMainAxis;
200 break;
202 case SCHATTR_BAR_OVERLAP:
203 case SCHATTR_BAR_GAPWIDTH:
205 if( m_bSupportingOverlapAndGapWidthProperties )
207 sal_Int32& rBarPosition = ( nWhichId == SCHATTR_BAR_OVERLAP ) ? m_nBarOverlap : m_nGapWidth;
208 rBarPosition = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
210 OUString aPropName("GapwidthSequence" );
211 if( nWhichId == SCHATTR_BAR_OVERLAP )
212 aPropName = "OverlapSequence";
214 uno::Reference< XDataSeries > xDataSeries( GetPropertySet(), uno::UNO_QUERY );
215 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram(m_xChartModel) );
216 uno::Reference< beans::XPropertySet > xChartTypeProps( DiagramHelper::getChartTypeOfSeries( xDiagram , xDataSeries ), uno::UNO_QUERY );
217 if( xChartTypeProps.is() )
219 sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
220 uno::Sequence< sal_Int32 > aBarPositionSequence;
221 if( xChartTypeProps.is() )
223 if( xChartTypeProps->getPropertyValue( aPropName ) >>= aBarPositionSequence )
225 bool bGroupBarsPerAxis = rItemSet.Get( SCHATTR_GROUP_BARS_PER_AXIS ).GetValue();
226 if(!bGroupBarsPerAxis)
228 //set the same value for all axes
229 for( sal_Int32 nN = 0; nN < aBarPositionSequence.getLength(); nN++ )
230 aBarPositionSequence[nN] = rBarPosition;
232 else if( nAxisIndex >= 0 && nAxisIndex < aBarPositionSequence.getLength() )
233 aBarPositionSequence[nAxisIndex] = rBarPosition;
235 xChartTypeProps->setPropertyValue( aPropName, uno::Any(aBarPositionSequence) );
236 bChanged = true;
242 break;
244 case SCHATTR_BAR_CONNECT:
246 m_bConnectBars = static_cast< const SfxBoolItem & >(
247 rItemSet.Get( nWhichId )).GetValue();
248 if( m_bSupportingBarConnectors )
250 bool bOldConnectBars = false;
251 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
252 if( xDiagramProperties.is() &&
253 (xDiagramProperties->getPropertyValue( "ConnectBars" ) >>= bOldConnectBars) &&
254 bOldConnectBars != m_bConnectBars )
256 xDiagramProperties->setPropertyValue( "ConnectBars" , uno::Any(m_bConnectBars) );
257 bChanged = true;
261 break;
263 case SCHATTR_GROUP_BARS_PER_AXIS:
265 m_bGroupBarsPerAxis = static_cast< const SfxBoolItem & >(
266 rItemSet.Get( nWhichId )).GetValue();
267 if( m_bSupportingAxisSideBySide )
269 bool bOldGroupBarsPerAxis = true;
270 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
271 if( xDiagramProperties.is() &&
272 (xDiagramProperties->getPropertyValue( "GroupBarsPerAxis" ) >>= bOldGroupBarsPerAxis) &&
273 bOldGroupBarsPerAxis != m_bGroupBarsPerAxis )
275 xDiagramProperties->setPropertyValue( "GroupBarsPerAxis" , uno::Any(m_bGroupBarsPerAxis) );
276 bChanged = true;
280 break;
282 case SCHATTR_STARTING_ANGLE:
284 if( m_bSupportingStartingAngle )
286 m_nStartingAngle = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
287 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
288 if( xDiagramProperties.is() )
290 xDiagramProperties->setPropertyValue( "StartingAngle" , uno::Any(m_nStartingAngle) );
291 bChanged = true;
295 break;
297 case SCHATTR_CLOCKWISE:
299 bool bClockwise = static_cast< const SfxBoolItem & >(
300 rItemSet.Get( nWhichId )).GetValue();
301 if( m_xCooSys.is() )
303 uno::Reference< chart2::XAxis > xAxis( AxisHelper::getAxis( 1, 0, m_xCooSys ) );
304 if( xAxis.is() )
306 chart2::ScaleData aScaleData( xAxis->getScaleData() );
307 aScaleData.Orientation = bClockwise ? chart2::AxisOrientation_REVERSE : chart2::AxisOrientation_MATHEMATICAL;
308 xAxis->setScaleData( aScaleData );
309 bChanged = true;
313 break;
315 case SCHATTR_MISSING_VALUE_TREATMENT:
317 if( m_aSupportedMissingValueTreatments.getLength() )
319 sal_Int32 nNew = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
320 if( m_nMissingValueTreatment != nNew )
324 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
325 if( xDiagramProperties.is() )
327 xDiagramProperties->setPropertyValue( "MissingValueTreatment" , uno::Any( nNew ));
328 bChanged = true;
331 catch( const uno::Exception& e )
333 SAL_WARN("chart2", "Exception caught. " << e );
338 break;
339 case SCHATTR_INCLUDE_HIDDEN_CELLS:
341 if( m_bSupportingPlottingOfHiddenCells )
343 bool bIncludeHiddenCells = static_cast<const SfxBoolItem &>(rItemSet.Get(nWhichId)).GetValue();
344 if (bIncludeHiddenCells != m_bIncludeHiddenCells)
346 ChartModel* pModel = dynamic_cast<ChartModel*>(m_xChartModel.get());
347 if (pModel)
348 bChanged = ChartModelHelper::setIncludeHiddenCells( bIncludeHiddenCells, *pModel );
352 break;
354 return bChanged;
357 void SeriesOptionsItemConverter::FillSpecialItem(
358 sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
360 switch( nWhichId )
362 case SCHATTR_AXIS:
364 sal_Int32 nItemValue = m_bAttachToMainAxis ? CHART_AXIS_PRIMARY_Y : CHART_AXIS_SECONDARY_Y;
365 rOutItemSet.Put( SfxInt32Item(nWhichId,nItemValue ) );
366 break;
368 case SCHATTR_BAR_OVERLAP:
370 if( m_bSupportingOverlapAndGapWidthProperties )
371 rOutItemSet.Put( SfxInt32Item(nWhichId,m_nBarOverlap) );
372 break;
374 case SCHATTR_BAR_GAPWIDTH:
376 if( m_bSupportingOverlapAndGapWidthProperties )
377 rOutItemSet.Put( SfxInt32Item(nWhichId,m_nGapWidth) );
378 break;
380 case SCHATTR_BAR_CONNECT:
382 if( m_bSupportingBarConnectors )
383 rOutItemSet.Put( SfxBoolItem(nWhichId,m_bConnectBars));
384 break;
386 case SCHATTR_GROUP_BARS_PER_AXIS:
388 if( m_bSupportingAxisSideBySide )
389 rOutItemSet.Put( SfxBoolItem(nWhichId,m_bGroupBarsPerAxis) );
390 break;
392 case SCHATTR_AXIS_FOR_ALL_SERIES:
394 if( m_nAllSeriesAxisIndex != - 1)
395 rOutItemSet.Put( SfxInt32Item(nWhichId, m_nAllSeriesAxisIndex));
396 break;
398 case SCHATTR_STARTING_ANGLE:
400 if( m_bSupportingStartingAngle )
401 rOutItemSet.Put( SfxInt32Item(nWhichId,m_nStartingAngle));
402 break;
404 case SCHATTR_CLOCKWISE:
406 rOutItemSet.Put( SfxBoolItem(nWhichId,m_bClockwise) );
407 break;
409 case SCHATTR_MISSING_VALUE_TREATMENT:
411 if( m_aSupportedMissingValueTreatments.getLength() )
412 rOutItemSet.Put( SfxInt32Item( nWhichId, m_nMissingValueTreatment ));
413 break;
415 case SCHATTR_AVAILABLE_MISSING_VALUE_TREATMENTS:
417 rOutItemSet.Put( SfxIntegerListItem( nWhichId, m_aSupportedMissingValueTreatments ) );
418 break;
420 case SCHATTR_INCLUDE_HIDDEN_CELLS:
422 if( m_bSupportingPlottingOfHiddenCells )
423 rOutItemSet.Put( SfxBoolItem(nWhichId, m_bIncludeHiddenCells) );
424 break;
426 default:
427 break;
431 } // namespace wrapper
432 } // namespace chart
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */