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 "SeriesOptionsItemConverter.hxx"
21 : #include "SchWhichPairs.hxx"
22 :
23 : #include "macros.hxx"
24 : #include "ItemPropertyMap.hxx"
25 : #include "GraphicPropertyItemConverter.hxx"
26 : #include "MultipleItemConverter.hxx"
27 : #include "ChartModelHelper.hxx"
28 : #include "AxisHelper.hxx"
29 : #include "DiagramHelper.hxx"
30 : #include "ChartTypeHelper.hxx"
31 : #include "DataSeriesHelper.hxx"
32 :
33 : #include <com/sun/star/chart/MissingValueTreatment.hpp>
34 : #include <com/sun/star/chart2/XDataSeries.hpp>
35 :
36 : #include <svl/eitem.hxx>
37 : #include <svl/intitem.hxx>
38 : #include <svl/ilstitem.hxx>
39 : #include <rtl/math.hxx>
40 :
41 : #include <functional>
42 : #include <algorithm>
43 :
44 : using namespace ::com::sun::star;
45 : using namespace ::com::sun::star::chart2;
46 :
47 : namespace chart
48 : {
49 : namespace wrapper
50 : {
51 :
52 0 : 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_bAllSeriesAttachedToSameAxis(true)
69 : , m_nAllSeriesAxisIndex(-1)
70 : , m_bSupportingStartingAngle(false)
71 : , m_nStartingAngle(90)
72 : , m_bClockwise(false)
73 : , m_aSupportedMissingValueTreatments()
74 : , m_nMissingValueTreatment(0)
75 : , m_bSupportingPlottingOfHiddenCells(false)
76 0 : , m_bIncludeHiddenCells(true)
77 : {
78 : try
79 : {
80 0 : uno::Reference< XDataSeries > xDataSeries( xPropertySet, uno::UNO_QUERY );
81 :
82 0 : m_bAttachToMainAxis = DiagramHelper::isSeriesAttachedToMainAxis( xDataSeries );
83 :
84 0 : uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
85 0 : uno::Reference< beans::XPropertySet > xDiagramProperties( xDiagram, uno::UNO_QUERY );
86 0 : uno::Reference< XChartType > xChartType( DiagramHelper::getChartTypeOfSeries( xDiagram , xDataSeries ) );
87 :
88 0 : m_xCooSys = DataSeriesHelper::getCoordinateSystemOfSeries( xDataSeries, xDiagram );
89 0 : if( m_xCooSys.is() )
90 : {
91 0 : uno::Reference< chart2::XAxis > xAxis( AxisHelper::getAxis( 1, 0, m_xCooSys ) );
92 0 : chart2::ScaleData aScale( xAxis->getScaleData() );
93 0 : m_bClockwise = (aScale.Orientation == chart2::AxisOrientation_REVERSE);
94 : }
95 :
96 0 : sal_Int32 nDimensionCount = DiagramHelper::getDimension( xDiagram );
97 0 : m_bSupportingOverlapAndGapWidthProperties = ChartTypeHelper::isSupportingOverlapAndGapWidthProperties( xChartType, nDimensionCount );
98 :
99 0 : if( m_bSupportingOverlapAndGapWidthProperties )
100 : {
101 :
102 0 : sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
103 :
104 0 : uno::Sequence< sal_Int32 > m_aBarPositionSequence;
105 0 : uno::Reference< beans::XPropertySet > xChartTypeProps( xChartType, uno::UNO_QUERY );
106 0 : if( xChartTypeProps.is() )
107 : {
108 0 : if( xChartTypeProps->getPropertyValue( "OverlapSequence" ) >>= m_aBarPositionSequence )
109 : {
110 0 : if( nAxisIndex >= 0 && nAxisIndex < m_aBarPositionSequence.getLength() )
111 0 : m_nBarOverlap = m_aBarPositionSequence[nAxisIndex];
112 : }
113 0 : if( xChartTypeProps->getPropertyValue( "GapwidthSequence" ) >>= m_aBarPositionSequence )
114 : {
115 0 : if( nAxisIndex >= 0 && nAxisIndex < m_aBarPositionSequence.getLength() )
116 0 : m_nGapWidth = m_aBarPositionSequence[nAxisIndex];
117 : }
118 0 : }
119 : }
120 :
121 0 : m_bSupportingBarConnectors = ChartTypeHelper::isSupportingBarConnectors( xChartType, nDimensionCount );
122 0 : if( m_bSupportingBarConnectors && xDiagramProperties.is() )
123 : {
124 0 : xDiagramProperties->getPropertyValue( "ConnectBars" ) >>= m_bConnectBars;
125 : }
126 :
127 0 : m_bSupportingAxisSideBySide = ChartTypeHelper::isSupportingAxisSideBySide( xChartType, nDimensionCount );
128 0 : if( m_bSupportingAxisSideBySide && xDiagramProperties.is() )
129 : {
130 0 : xDiagramProperties->getPropertyValue( "GroupBarsPerAxis" ) >>= m_bGroupBarsPerAxis;
131 0 : m_bAllSeriesAttachedToSameAxis = DataSeriesHelper::areAllSeriesAttachedToSameAxis( xChartType, m_nAllSeriesAxisIndex );
132 : }
133 :
134 0 : m_bSupportingStartingAngle = ChartTypeHelper::isSupportingStartingAngle( xChartType );
135 0 : if( m_bSupportingStartingAngle )
136 : {
137 0 : xDiagramProperties->getPropertyValue( "StartingAngle" ) >>= m_nStartingAngle;
138 : }
139 :
140 0 : m_aSupportedMissingValueTreatments = ChartTypeHelper::getSupportedMissingValueTreatments( xChartType );
141 : m_nMissingValueTreatment = DiagramHelper::getCorrectedMissingValueTreatment(
142 0 : ChartModelHelper::findDiagram(m_xChartModel), xChartType );
143 :
144 0 : uno::Reference< XChartDocument > xChartDoc( m_xChartModel, uno::UNO_QUERY );
145 0 : uno::Reference< beans::XPropertySet > xProp( xChartDoc->getDataProvider(), uno::UNO_QUERY );
146 0 : if( xProp.is() )
147 : {
148 : try
149 : {
150 : //test whether the data provider offers this property
151 0 : xProp->getPropertyValue( "IncludeHiddenCells" );
152 : //if not exception is thrown the property is offered
153 0 : m_bSupportingPlottingOfHiddenCells = true;
154 0 : xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= m_bIncludeHiddenCells;
155 : }
156 0 : catch( const beans::UnknownPropertyException& )
157 : {
158 : }
159 0 : }
160 : }
161 0 : catch( const uno::Exception &ex )
162 : {
163 : ASSERT_EXCEPTION( ex );
164 : }
165 0 : }
166 :
167 0 : SeriesOptionsItemConverter::~SeriesOptionsItemConverter()
168 : {
169 0 : }
170 :
171 0 : const sal_uInt16 * SeriesOptionsItemConverter::GetWhichPairs() const
172 : {
173 : // must span all used items!
174 0 : return nSeriesOptionsWhichPairs;
175 : }
176 :
177 0 : bool SeriesOptionsItemConverter::GetItemProperty( tWhichIdType /*nWhichId*/, tPropertyNameWithMemberId & /*rOutProperty*/ ) const
178 : {
179 0 : return false;
180 : }
181 :
182 0 : bool SeriesOptionsItemConverter::ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
183 : throw( uno::Exception )
184 : {
185 0 : bool bChanged = false;
186 0 : switch( nWhichId )
187 : {
188 : case SCHATTR_AXIS:
189 : {
190 : sal_Int32 nItemValue = static_cast< const SfxInt32Item & >(
191 0 : rItemSet.Get( nWhichId )).GetValue();
192 0 : bool bAttachToMainAxis = nItemValue == CHART_AXIS_PRIMARY_Y;
193 0 : if( bAttachToMainAxis != m_bAttachToMainAxis )
194 : {
195 : //change model:
196 : bChanged = DiagramHelper::attachSeriesToAxis( bAttachToMainAxis, uno::Reference< XDataSeries >::query( GetPropertySet() )
197 0 : , ChartModelHelper::findDiagram(m_xChartModel), m_xCC );
198 :
199 0 : if( bChanged )
200 0 : m_bAttachToMainAxis = bAttachToMainAxis;
201 : }
202 : }
203 0 : break;
204 :
205 : case SCHATTR_BAR_OVERLAP:
206 : case SCHATTR_BAR_GAPWIDTH:
207 : {
208 0 : if( m_bSupportingOverlapAndGapWidthProperties )
209 : {
210 0 : sal_Int32& rBarPosition = ( SCHATTR_BAR_OVERLAP == nWhichId ) ? m_nBarOverlap : m_nGapWidth;
211 0 : rBarPosition = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
212 :
213 0 : OUString aPropName("GapwidthSequence" );
214 0 : if( SCHATTR_BAR_OVERLAP == nWhichId )
215 0 : aPropName = "OverlapSequence";
216 :
217 0 : uno::Reference< XDataSeries > xDataSeries( GetPropertySet(), uno::UNO_QUERY );
218 0 : uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram(m_xChartModel) );
219 0 : uno::Reference< beans::XPropertySet > xChartTypeProps( DiagramHelper::getChartTypeOfSeries( xDiagram , xDataSeries ), uno::UNO_QUERY );
220 0 : if( xChartTypeProps.is() )
221 : {
222 0 : sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
223 0 : uno::Sequence< sal_Int32 > m_aBarPositionSequence;
224 0 : if( xChartTypeProps.is() )
225 : {
226 0 : if( xChartTypeProps->getPropertyValue( aPropName ) >>= m_aBarPositionSequence )
227 : {
228 0 : bool bGroupBarsPerAxis = static_cast< const SfxBoolItem & >(rItemSet.Get( SCHATTR_GROUP_BARS_PER_AXIS )).GetValue();
229 0 : if(!bGroupBarsPerAxis)
230 : {
231 : //set the same value for all axes
232 0 : for( sal_Int32 nN = 0; nN < m_aBarPositionSequence.getLength(); nN++ )
233 0 : m_aBarPositionSequence[nN] = rBarPosition;
234 : }
235 0 : else if( nAxisIndex >= 0 && nAxisIndex < m_aBarPositionSequence.getLength() )
236 0 : m_aBarPositionSequence[nAxisIndex] = rBarPosition;
237 :
238 0 : xChartTypeProps->setPropertyValue( aPropName, uno::makeAny(m_aBarPositionSequence) );
239 0 : bChanged = true;
240 : }
241 0 : }
242 0 : }
243 : }
244 : }
245 0 : break;
246 :
247 : case SCHATTR_BAR_CONNECT:
248 : {
249 0 : bool bOldConnectBars = false;
250 : m_bConnectBars = static_cast< const SfxBoolItem & >(
251 0 : rItemSet.Get( nWhichId )).GetValue();
252 0 : if( m_bSupportingBarConnectors )
253 : {
254 0 : uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
255 0 : if( xDiagramProperties.is() &&
256 0 : (xDiagramProperties->getPropertyValue( "ConnectBars" ) >>= bOldConnectBars) &&
257 0 : bOldConnectBars != m_bConnectBars )
258 : {
259 0 : xDiagramProperties->setPropertyValue( "ConnectBars" , uno::makeAny(m_bConnectBars) );
260 0 : bChanged = true;
261 0 : }
262 : }
263 : }
264 0 : break;
265 :
266 : case SCHATTR_GROUP_BARS_PER_AXIS:
267 : {
268 0 : bool bOldGroupBarsPerAxis = true;
269 : m_bGroupBarsPerAxis = static_cast< const SfxBoolItem & >(
270 0 : rItemSet.Get( nWhichId )).GetValue();
271 0 : if( m_bSupportingAxisSideBySide )
272 : {
273 0 : uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
274 0 : if( xDiagramProperties.is() &&
275 0 : (xDiagramProperties->getPropertyValue( "GroupBarsPerAxis" ) >>= bOldGroupBarsPerAxis) &&
276 0 : bOldGroupBarsPerAxis != m_bGroupBarsPerAxis )
277 : {
278 0 : xDiagramProperties->setPropertyValue( "GroupBarsPerAxis" , uno::makeAny(m_bGroupBarsPerAxis) );
279 0 : bChanged = true;
280 0 : }
281 : }
282 : }
283 0 : break;
284 :
285 : case SCHATTR_STARTING_ANGLE:
286 : {
287 0 : if( m_bSupportingStartingAngle )
288 : {
289 0 : m_nStartingAngle = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
290 0 : uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
291 0 : if( xDiagramProperties.is() )
292 : {
293 0 : xDiagramProperties->setPropertyValue( "StartingAngle" , uno::makeAny(m_nStartingAngle) );
294 0 : bChanged = true;
295 0 : }
296 : }
297 : }
298 0 : break;
299 :
300 : case SCHATTR_CLOCKWISE:
301 : {
302 : bool bClockwise = (static_cast< const SfxBoolItem & >(
303 0 : rItemSet.Get( nWhichId )).GetValue() );
304 0 : if( m_xCooSys.is() )
305 : {
306 0 : uno::Reference< chart2::XAxis > xAxis( AxisHelper::getAxis( 1, 0, m_xCooSys ) );
307 0 : if( xAxis.is() )
308 : {
309 0 : chart2::ScaleData aScaleData( xAxis->getScaleData() );
310 0 : aScaleData.Orientation = bClockwise ? chart2::AxisOrientation_REVERSE : chart2::AxisOrientation_MATHEMATICAL;
311 0 : xAxis->setScaleData( aScaleData );
312 0 : bChanged = true;
313 0 : }
314 : }
315 : }
316 0 : break;
317 :
318 : case SCHATTR_MISSING_VALUE_TREATMENT:
319 : {
320 0 : if( m_aSupportedMissingValueTreatments.getLength() )
321 : {
322 0 : sal_Int32 nNew = static_cast< const SfxInt32Item & >( rItemSet.Get( nWhichId )).GetValue();
323 0 : if( m_nMissingValueTreatment != nNew )
324 : {
325 : try
326 : {
327 0 : uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(m_xChartModel), uno::UNO_QUERY );
328 0 : if( xDiagramProperties.is() )
329 : {
330 0 : xDiagramProperties->setPropertyValue( "MissingValueTreatment" , uno::makeAny( nNew ));
331 0 : bChanged = true;
332 0 : }
333 : }
334 0 : catch( const uno::Exception& e )
335 : {
336 : ASSERT_EXCEPTION( e );
337 : }
338 : }
339 : }
340 : }
341 0 : break;
342 : case SCHATTR_INCLUDE_HIDDEN_CELLS:
343 : {
344 0 : if( m_bSupportingPlottingOfHiddenCells )
345 : {
346 0 : bool bIncludeHiddenCells = static_cast<const SfxBoolItem &>(rItemSet.Get(nWhichId)).GetValue();
347 0 : if (bIncludeHiddenCells != m_bIncludeHiddenCells)
348 : {
349 0 : ChartModel* pModel = dynamic_cast<ChartModel*>(m_xChartModel.get());
350 0 : if (pModel)
351 0 : bChanged = ChartModelHelper::setIncludeHiddenCells( bIncludeHiddenCells, *pModel );
352 : }
353 : }
354 : }
355 0 : break;
356 : }
357 0 : return bChanged;
358 : }
359 :
360 0 : void SeriesOptionsItemConverter::FillSpecialItem(
361 : sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
362 : throw( uno::Exception )
363 : {
364 0 : switch( nWhichId )
365 : {
366 : case SCHATTR_AXIS:
367 : {
368 0 : sal_Int32 nItemValue = m_bAttachToMainAxis ? CHART_AXIS_PRIMARY_Y : CHART_AXIS_SECONDARY_Y;
369 0 : rOutItemSet.Put( SfxInt32Item(nWhichId,nItemValue ) );
370 0 : break;
371 : }
372 : case SCHATTR_BAR_OVERLAP:
373 : {
374 0 : if( m_bSupportingOverlapAndGapWidthProperties )
375 0 : rOutItemSet.Put( SfxInt32Item(nWhichId,m_nBarOverlap) );
376 0 : break;
377 : }
378 : case SCHATTR_BAR_GAPWIDTH:
379 : {
380 0 : if( m_bSupportingOverlapAndGapWidthProperties )
381 0 : rOutItemSet.Put( SfxInt32Item(nWhichId,m_nGapWidth) );
382 0 : break;
383 : }
384 : case SCHATTR_BAR_CONNECT:
385 : {
386 0 : if( m_bSupportingBarConnectors )
387 0 : rOutItemSet.Put( SfxBoolItem(nWhichId,m_bConnectBars));
388 0 : break;
389 : }
390 : case SCHATTR_GROUP_BARS_PER_AXIS:
391 : {
392 0 : if( m_bSupportingAxisSideBySide )
393 0 : rOutItemSet.Put( SfxBoolItem(nWhichId,m_bGroupBarsPerAxis) );
394 0 : break;
395 : }
396 : case SCHATTR_AXIS_FOR_ALL_SERIES:
397 : {
398 0 : if( m_nAllSeriesAxisIndex != - 1)
399 0 : rOutItemSet.Put( SfxInt32Item(nWhichId, m_nAllSeriesAxisIndex));
400 0 : break;
401 : }
402 : case SCHATTR_STARTING_ANGLE:
403 : {
404 0 : if( m_bSupportingStartingAngle )
405 0 : rOutItemSet.Put( SfxInt32Item(nWhichId,m_nStartingAngle));
406 0 : break;
407 : }
408 : case SCHATTR_CLOCKWISE:
409 : {
410 0 : rOutItemSet.Put( SfxBoolItem(nWhichId,m_bClockwise) );
411 0 : break;
412 : }
413 : case SCHATTR_MISSING_VALUE_TREATMENT:
414 : {
415 0 : if( m_aSupportedMissingValueTreatments.getLength() )
416 0 : rOutItemSet.Put( SfxInt32Item( nWhichId, m_nMissingValueTreatment ));
417 0 : break;
418 : }
419 : case SCHATTR_AVAILABLE_MISSING_VALUE_TREATMENTS:
420 : {
421 0 : rOutItemSet.Put( SfxIntegerListItem( nWhichId, m_aSupportedMissingValueTreatments ) );
422 0 : break;
423 : }
424 : case SCHATTR_INCLUDE_HIDDEN_CELLS:
425 : {
426 0 : if( m_bSupportingPlottingOfHiddenCells )
427 0 : rOutItemSet.Put( SfxBoolItem(nWhichId, m_bIncludeHiddenCells) );
428 0 : break;
429 : }
430 : default:
431 0 : break;
432 : }
433 0 : }
434 :
435 : } // namespace wrapper
436 102 : } // namespace chart
437 :
438 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|