Debian Codegraph @

chart2/source/controller/itemsetwrapper/SeriesOptionsItemConverter.cxx

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