Debian Codegraph @

chart2/source/controller/dialogs/DataBrowserModel.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 "DialogModel.hxx"
21 #include <DataBrowserModel.hxx>
22 #include <ChartType.hxx>
23 #include <ChartTypeManager.hxx>
24 #include <Diagram.hxx>
25 #include <DataSeries.hxx>
26 #include <DataSeriesHelper.hxx>
27 #include <ControllerLockGuard.hxx>
28 #include <StatisticsHelper.hxx>
29 #include <ChartTypeHelper.hxx>
30 #include <ExplicitCategoriesProvider.hxx>
31 #include <BaseCoordinateSystem.hxx>
32 #include <ChartModel.hxx>
33 #include <unonames.hxx>
34
35 #include <com/sun/star/container/XIndexReplace.hpp>
36 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
37 #include <com/sun/star/chart2/data/XDataSource.hpp>
38 #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
39 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
40 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
41 #include <o3tl/safeint.hxx>
42 #include <comphelper/diagnose_ex.hxx>
43 #include <comphelper/property.hxx>
44
45 #include <algorithm>
46 #include <cstddef>
47 #include <limits>
48 #include <utility>
49
50 using namespace ::com::sun::star;
51
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::Sequence;
54
55 namespace chart {
56
57 namespace {
58
59 OUString lcl_getRole(
60 const Reference< chart2::data::XDataSequence > & xSeq )
61 {
62 OUString aResult;
63 Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY );
64 if( xProp.is())
65 {
66 try
67 {
68 xProp->getPropertyValue( u"Role"_ustr ) >>= aResult;
69 }
70 catch( const uno::Exception & )
71 {
72 DBG_UNHANDLED_EXCEPTION("chart2");
73 }
74 }
75 return aResult;
76 }
77
78 OUString lcl_getUIRoleName(
79 const Reference< chart2::data::XLabeledDataSequence > & xLSeq )
80 {
81 OUString aResult = DataSeriesHelper::getRole(xLSeq);
82 if( !aResult.isEmpty())
83 aResult = DialogModel::ConvertRoleFromInternalToUI(aResult);
84 return aResult;
85 }
86
87 void lcl_copyDataSequenceProperties(
88 const Reference< chart2::data::XDataSequence > & xOldSequence,
89 const Reference< chart2::data::XDataSequence > & xNewSequence )
90 {
91 Reference< beans::XPropertySet > xOldSeqProp( xOldSequence, uno::UNO_QUERY );
92 Reference< beans::XPropertySet > xNewSeqProp( xNewSequence, uno::UNO_QUERY );
93 comphelper::copyProperties( xOldSeqProp, xNewSeqProp );
94 }
95
96 bool lcl_SequenceOfSeriesIsShared(
97 const rtl::Reference< ::chart::DataSeries > & xSeries,
98 const Reference< chart2::data::XDataSequence > & xValues )
99 {
100 bool bResult = false;
101 if( !xValues.is())
102 return bResult;
103 try
104 {
105 OUString aValuesRole( lcl_getRole( xValues ));
106 OUString aValuesRep( xValues->getSourceRangeRepresentation());
107 const std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > & aLSeq( xSeries->getDataSequences2());
108 for( uno::Reference< chart2::data::XLabeledDataSequence > const & labeledDataSeq : aLSeq )
109 if (labeledDataSeq.is() && DataSeriesHelper::getRole(labeledDataSeq) == aValuesRole)
110 {
111 // getValues().is(), because lcl_getRole checked that already
112 bResult = (aValuesRep == labeledDataSeq->getValues()->getSourceRangeRepresentation());
113 // assumption: a role appears only once in a series
114 break;
115 }
116 }
117 catch( const uno::Exception & )
118 {
119 DBG_UNHANDLED_EXCEPTION("chart2");
120 }
121 return bResult;
122 }
123
124 typedef std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > lcl_tSharedSeqVec;
125
126 lcl_tSharedSeqVec lcl_getSharedSequences( const std::vector< rtl::Reference< DataSeries > > & rSeries )
127 {
128 // @todo: if only some series share a sequence, those have to be duplicated
129 // and made unshared for all series
130 lcl_tSharedSeqVec aResult;
131 // if we have only one series, we don't want any shared sequences
132 if( rSeries.size() <= 1 )
133 return aResult;
134
135 for( uno::Reference< chart2::data::XLabeledDataSequence > const & labeledDataSeq : rSeries[0]->getDataSequences2() )
136 {
137 Reference< chart2::data::XDataSequence > xValues( labeledDataSeq->getValues());
138 bool bShared = true;
139 for( std::size_t nSeriesIdx=1; nSeriesIdx<rSeries.size(); ++nSeriesIdx )
140 {
141 bShared = lcl_SequenceOfSeriesIsShared( rSeries[nSeriesIdx], xValues );
142 if( !bShared )
143 break;
144 }
145 if( bShared )
146 aResult.push_back( labeledDataSeq );
147 }
148
149 return aResult;
150 }
151
152 sal_Int32 lcl_getValuesRepresentationIndex(
153 const Reference< chart2::data::XLabeledDataSequence > & xLSeq )
154 {
155 sal_Int32 nResult = -1;
156 if( xLSeq.is())
157 {
158 Reference< chart2::data::XDataSequence > xSeq( xLSeq->getValues());
159 if( xSeq.is())
160 {
161 OUString aRep( xSeq->getSourceRangeRepresentation());
162 nResult = aRep.toInt32();
163 }
164 }
165 return nResult;
166 }
167
168 struct lcl_RepresentationsOfLSeqMatch
169 {
170 explicit lcl_RepresentationsOfLSeqMatch( const Reference< chart2::data::XLabeledDataSequence > & xLSeq ) :
171 m_aValuesRep( xLSeq.is() ?
172 (xLSeq->getValues().is() ? xLSeq->getValues()->getSourceRangeRepresentation() : OUString())
173 : OUString() )
174 {}
175 bool operator() ( const Reference< chart2::data::XLabeledDataSequence > & xLSeq )
176 {
177 if (!xLSeq.is() || !xLSeq->getValues().is())
178 return false;
179
180 return xLSeq->getValues()->getSourceRangeRepresentation() == m_aValuesRep;
181 }
182 private:
183 OUString m_aValuesRep;
184 };
185
186 struct lcl_RolesOfLSeqMatch
187 {
188 explicit lcl_RolesOfLSeqMatch( const uno::Reference< chart2::data::XLabeledDataSequence > & xLSeq ) :
189 m_aRole(DataSeriesHelper::getRole(xLSeq)) {}
190
191 bool operator() ( const uno::Reference< chart2::data::XLabeledDataSequence > & xLSeq )
192 {
193 return DataSeriesHelper::getRole(xLSeq) == m_aRole;
194 }
195 private:
196 OUString m_aRole;
197 };
198
199 bool lcl_ShowCategoriesAsDataLabel( const rtl::Reference< ::chart::Diagram > & xDiagram )
200 {
201 return !xDiagram->isCategory();
202 }
203
204 } // anonymous namespace
205
206 struct DataBrowserModel::tDataColumn
207 {
208 rtl::Reference<DataSeries> m_xDataSeries;
209 OUString m_aUIRoleName;
210 uno::Reference<chart2::data::XLabeledDataSequence> m_xLabeledDataSequence;
211 eCellType m_eCellType;
212 sal_Int32 m_nNumberFormatKey;
213
214 // default CTOR
215 tDataColumn() : m_eCellType( TEXT ), m_nNumberFormatKey( 0 ) {}
216 // "full" CTOR
217 tDataColumn(
218 rtl::Reference<DataSeries> xDataSeries,
219 OUString aUIRoleName,
220 uno::Reference<chart2::data::XLabeledDataSequence> xLabeledDataSequence,
221 eCellType aCellType,
222 sal_Int32 nNumberFormatKey ) :
223 m_xDataSeries(std::move( xDataSeries )),
224 m_aUIRoleName(std::move( aUIRoleName )),
225 m_xLabeledDataSequence(std::move( xLabeledDataSequence )),
226 m_eCellType( aCellType ),
227 m_nNumberFormatKey( nNumberFormatKey )
228 {}
229 };
230
231 struct DataBrowserModel::implColumnLess
232 {
233 bool operator() ( const DataBrowserModel::tDataColumn & rLeft, const DataBrowserModel::tDataColumn & rRight )
234 {
235 if( rLeft.m_xLabeledDataSequence.is() && rRight.m_xLabeledDataSequence.is())
236 {
237 return DialogModel::GetRoleIndexForSorting(DataSeriesHelper::getRole(rLeft.m_xLabeledDataSequence)) <
238 DialogModel::GetRoleIndexForSorting(DataSeriesHelper::getRole(rRight.m_xLabeledDataSequence));
239 }
240 return true;
241 }
242 };
243
244 DataBrowserModel::DataBrowserModel(
245 const rtl::Reference<::chart::ChartModel> & xChartDoc ) :
246 m_xChartDocument( xChartDoc ),
247 m_apDialogModel( new DialogModel( xChartDoc ))
248 {
249 updateFromModel();
250 }
251
252 DataBrowserModel::~DataBrowserModel()
253 {}
254
255 namespace
256 {
257 struct lcl_DataSeriesOfHeaderMatches
258 {
259 explicit lcl_DataSeriesOfHeaderMatches(
260 rtl::Reference< ::chart::DataSeries > xSeriesToCompareWith ) :
261 m_xSeries(std::move( xSeriesToCompareWith ))
262 {}
263 bool operator() ( const ::chart::DataBrowserModel::tDataHeader & rHeader )
264 {
265 return (m_xSeries == rHeader.m_xDataSeries);
266 }
267 private:
268 rtl::Reference< ::chart::DataSeries > m_xSeries;
269 };
270 }
271
272 void DataBrowserModel::insertDataSeries( sal_Int32 nAfterColumnIndex )
273 {
274 OSL_ASSERT(m_apDialogModel);
275 Reference< chart2::XInternalDataProvider > xDataProvider(
276 m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
277
278 if (!xDataProvider.is())
279 return;
280
281 if( isCategoriesColumn(nAfterColumnIndex) )
282 // Move to the last category column.
283 nAfterColumnIndex = getCategoryColumnCount()-1;
284
285 sal_Int32 nStartCol = 0;
286 rtl::Reference< Diagram > xDiagram = m_xChartDocument->getFirstChartDiagram();
287 rtl::Reference<ChartType> xChartType;
288 rtl::Reference<DataSeries> xSeries;
289 if (o3tl::make_unsigned(nAfterColumnIndex) < m_aColumns.size())
290 // Get the data series at specific column position (if available).
291 xSeries = m_aColumns[nAfterColumnIndex].m_xDataSeries;
292
293 sal_Int32 nSeriesNumberFormat = 0;
294 if( xSeries.is())
295 {
296 // Use the chart type of the currently selected data series.
297 xChartType = xDiagram->getChartTypeOfSeries( xSeries );
298
299 // Find the corresponding header and determine the last column of this
300 // data series.
301 tDataHeaderVector::const_iterator aIt(
302 std::find_if( m_aHeaders.begin(), m_aHeaders.end(),
303 lcl_DataSeriesOfHeaderMatches( xSeries )));
304 if( aIt != m_aHeaders.end())
305 nStartCol = aIt->m_nEndColumn;
306
307 // Get the number format too.
308 if( xSeries.is() )
309 xSeries->getPropertyValue(CHART_UNONAME_NUMFMT) >>= nSeriesNumberFormat;
310 }
311 else
312 {
313 // No data series at specified column position. Use the first chart type.
314 xChartType = xDiagram->getChartTypeByIndex( 0 );
315 nStartCol = nAfterColumnIndex;
316 }
317
318 if (!xChartType.is())
319 return;
320
321 // Get shared sequences of current series. Normally multiple data series
322 // only share "values-x" sequences. (TODO: simplify this logic).
323 lcl_tSharedSeqVec aSharedSequences = lcl_getSharedSequences( xChartType->getDataSeries2());
324
325 rtl::Reference<::chart::DataSeries> xNewSeries =
326 m_apDialogModel->insertSeriesAfter(xSeries, xChartType, true);
327
328 if (!xNewSeries.is())
329 // Failed to insert new data series to the model. Bail out.
330 return;
331
332 const std::vector<uno::Reference<chart2::data::XLabeledDataSequence> > & aLSequences = xNewSeries->getDataSequences2();
333 sal_Int32 nSeqIdx = 0;
334 sal_Int32 nSeqSize = aLSequences.size();
335 for (sal_Int32 nIndex = nStartCol; nSeqIdx < nSeqSize; ++nSeqIdx)
336 {
337 lcl_tSharedSeqVec::const_iterator aSharedIt(
338 std::find_if( aSharedSequences.begin(), aSharedSequences.end(),
339 lcl_RolesOfLSeqMatch( aLSequences[nSeqIdx] )));
340
341 if( aSharedIt != aSharedSequences.end())
342 {
343 // Shared sequence. Most likely "values-x" sequence. Copy it from existing sequence.
344 aLSequences[nSeqIdx]->setValues( (*aSharedIt)->getValues());
345 aLSequences[nSeqIdx]->setLabel( (*aSharedIt)->getLabel());
346 }
347 else
348 {
349 // Insert a new column in the internal data for the new sequence.
350 xDataProvider->insertSequence( nIndex - 1 );
351
352 // values
353 Reference< chart2::data::XDataSequence > xNewSeq(
354 xDataProvider->createDataSequenceByRangeRepresentation(
355 OUString::number( nIndex )));
356 lcl_copyDataSequenceProperties(
357 aLSequences[nSeqIdx]->getValues(), xNewSeq );
358 aLSequences[nSeqIdx]->setValues( xNewSeq );
359
360 // labels
361 Reference< chart2::data::XDataSequence > xNewLabelSeq(
362 xDataProvider->createDataSequenceByRangeRepresentation(
363 "label " +
364 OUString::number( nIndex )));
365 lcl_copyDataSequenceProperties(
366 aLSequences[nSeqIdx]->getLabel(), xNewLabelSeq );
367 aLSequences[nSeqIdx]->setLabel( xNewLabelSeq );
368 ++nIndex;
369 }
370 }
371
372 if( nSeriesNumberFormat != 0 )
373 {
374 //give the new series the same number format as the former series especially for bubble charts thus the bubble size values can be edited with same format immediately
375 xNewSeries->setPropertyValue(CHART_UNONAME_NUMFMT , uno::Any(nSeriesNumberFormat));
376 }
377
378 updateFromModel();
379 }
380
381 void DataBrowserModel::insertComplexCategoryLevel( sal_Int32 nAfterColumnIndex )
382 {
383 //create a new text column for complex categories
384
385 OSL_ASSERT(m_apDialogModel);
386 Reference< chart2::XInternalDataProvider > xDataProvider( m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
387 if (!xDataProvider.is())
388 return;
389
390 if( !isCategoriesColumn(nAfterColumnIndex) )
391 nAfterColumnIndex = getCategoryColumnCount()-1;
392
393 if(nAfterColumnIndex<0)
394 {
395 OSL_FAIL( "wrong index for category level insertion" );
396 return;
397 }
398
399 m_apDialogModel->startControllerLockTimer();
400 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
401 xDataProvider->insertComplexCategoryLevel( nAfterColumnIndex+1 );
402 updateFromModel();
403 }
404
405 void DataBrowserModel::removeComplexCategoryLevel( sal_Int32 nAtColumnIndex )
406 {
407 //delete a category column if there is more than one level (in case of a single column we do not get here)
408 OSL_ENSURE(nAtColumnIndex>0, "wrong index for categories deletion" );
409
410 Reference< chart2::XInternalDataProvider > xDataProvider( m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
411 if (!xDataProvider.is())
412 return;
413
414 m_apDialogModel->startControllerLockTimer();
415 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
416 xDataProvider->deleteComplexCategoryLevel( nAtColumnIndex );
417
418 updateFromModel();
419 }
420
421 void DataBrowserModel::removeDataSeriesOrComplexCategoryLevel( sal_Int32 nAtColumnIndex )
422 {
423 OSL_ASSERT(m_apDialogModel);
424 if (nAtColumnIndex < 0 || o3tl::make_unsigned(nAtColumnIndex) >= m_aColumns.size())
425 // Out of bound.
426 return;
427
428 if (isCategoriesColumn(nAtColumnIndex))
429 {
430 removeComplexCategoryLevel(nAtColumnIndex);
431 return;
432 }
433
434 const rtl::Reference<DataSeries>& xSeries = m_aColumns[nAtColumnIndex].m_xDataSeries;
435
436 m_apDialogModel->deleteSeries(xSeries, getHeaderForSeries(xSeries).m_xChartType);
437
438 //delete sequences from internal data provider that are not used anymore
439 //but do not delete sequences that are still in use by the remaining series
440
441 Reference< chart2::XInternalDataProvider > xDataProvider( m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
442 if (!xDataProvider.is() || !xSeries.is())
443 {
444 // Something went wrong. Bail out.
445 updateFromModel();
446 return;
447 }
448
449 rtl::Reference<ChartType> xSeriesCnt(getHeaderForSeries(xSeries).m_xChartType);
450 if (!xSeriesCnt.is())
451 {
452 // Unexpected happened. Bail out.
453 updateFromModel();
454 return;
455 }
456
457 // Collect all the remaining data sequences in the same chart type. The
458 // deleted data series is already gone by this point.
459 std::vector<uno::Reference<chart2::data::XLabeledDataSequence> > aAllDataSeqs =
460 DataSeriesHelper::getAllDataSequences(xSeriesCnt->getDataSeries2());
461
462 // Check if the sequences to be deleted are still referenced by any of
463 // the other data series. If not, mark them for deletion.
464 std::vector<sal_Int32> aSequenceIndexesToDelete;
465 const std::vector<uno::Reference<chart2::data::XLabeledDataSequence> > & aSequencesOfDeleted = xSeries->getDataSequences2();
466 for (auto const & labeledDataSeq : aSequencesOfDeleted)
467 {
468 // if not used by the remaining series this sequence can be deleted
469 if( std::none_of( aAllDataSeqs.begin(), aAllDataSeqs.end(),
470 lcl_RepresentationsOfLSeqMatch( labeledDataSeq )) )
471 aSequenceIndexesToDelete.push_back( lcl_getValuesRepresentationIndex( labeledDataSeq ) );
472 }
473
474 // delete unnecessary sequences of the internal data
475 // iterate using greatest index first, so that deletion does not
476 // shift other sequences that will be deleted later
477 std::sort( aSequenceIndexesToDelete.begin(), aSequenceIndexesToDelete.end());
478 for( std::vector< sal_Int32 >::reverse_iterator aIt(
479 aSequenceIndexesToDelete.rbegin()); aIt != aSequenceIndexesToDelete.rend(); ++aIt )
480 {
481 if( *aIt != -1 )
482 xDataProvider->deleteSequence( *aIt );
483 }
484
485 updateFromModel();
486 }
487
488 void DataBrowserModel::swapDataSeries( sal_Int32 nFirstColumnIndex )
489 {
490 OSL_ASSERT(m_apDialogModel);
491 if( o3tl::make_unsigned( nFirstColumnIndex ) < m_aColumns.size() - 1 )
492 {
493 rtl::Reference< DataSeries > xSeries( m_aColumns[nFirstColumnIndex].m_xDataSeries );
494 if( xSeries.is())
495 {
496 m_apDialogModel->moveSeries( xSeries, DialogModel::MoveDirection::Down );
497 updateFromModel();
498 }
499 }
500 }
501
502 void DataBrowserModel::swapDataPointForAllSeries( sal_Int32 nFirstIndex )
503 {
504 OSL_ASSERT(m_apDialogModel);
505 Reference< chart2::XInternalDataProvider > xDataProvider(
506 m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
507 // lockControllers
508 ControllerLockGuardUNO aGuard( m_apDialogModel->getChartModel());
509 if( xDataProvider.is())
510 xDataProvider->swapDataPointWithNextOneForAllSequences( nFirstIndex );
511 // unlockControllers
512 }
513
514 void DataBrowserModel::insertDataPointForAllSeries( sal_Int32 nAfterIndex )
515 {
516 Reference< chart2::XInternalDataProvider > xDataProvider(
517 m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
518 // lockControllers
519 ControllerLockGuardUNO aGuard( m_apDialogModel->getChartModel());
520 if( xDataProvider.is())
521 xDataProvider->insertDataPointForAllSequences( nAfterIndex );
522 // unlockControllers
523 }
524
525 void DataBrowserModel::removeDataPointForAllSeries( sal_Int32 nAtIndex )
526 {
527 Reference< chart2::XInternalDataProvider > xDataProvider(
528 m_apDialogModel->getDataProvider(), uno::UNO_QUERY );
529 // lockControllers
530 ControllerLockGuardUNO aGuard( m_apDialogModel->getChartModel());
531 if( xDataProvider.is())
532 xDataProvider->deleteDataPointForAllSequences( nAtIndex );
533 // unlockControllers
534 }
535
536 DataBrowserModel::tDataHeader DataBrowserModel::getHeaderForSeries(
537 const Reference< chart2::XDataSeries > & xSeries ) const
538 {
539 rtl::Reference<DataSeries> pSeries = dynamic_cast<DataSeries*>(xSeries.get());
540 assert(!xSeries || pSeries);
541 for (auto const& elemHeader : m_aHeaders)
542 {
543 if( elemHeader.m_xDataSeries == pSeries )
544 return elemHeader;
545 }
546 return tDataHeader();
547 }
548
549 rtl::Reference< DataSeries >
550 DataBrowserModel::getDataSeriesByColumn( sal_Int32 nColumn ) const
551 {
552 tDataColumnVector::size_type nIndex( nColumn );
553 if( nIndex < m_aColumns.size())
554 return m_aColumns[nIndex].m_xDataSeries;
555 return nullptr;
556 }
557
558 DataBrowserModel::eCellType DataBrowserModel::getCellType( sal_Int32 nAtColumn ) const
559 {
560 eCellType eResult = TEXT;
561 tDataColumnVector::size_type nIndex( nAtColumn );
562 if( nIndex < m_aColumns.size())
563 eResult = m_aColumns[nIndex].m_eCellType;
564 return eResult;
565 }
566
567 double DataBrowserModel::getCellNumber( sal_Int32 nAtColumn, sal_Int32 nAtRow )
568 {
569 tDataColumnVector::size_type nIndex( nAtColumn );
570 if( nIndex < m_aColumns.size() &&
571 m_aColumns[ nIndex ].m_xLabeledDataSequence.is())
572 {
573 Reference< chart2::data::XNumericalDataSequence > xData(
574 m_aColumns[ nIndex ].m_xLabeledDataSequence->getValues(), uno::UNO_QUERY );
575 if( xData.is())
576 {
577 Sequence< double > aValues( xData->getNumericalData());
578 if( nAtRow < aValues.getLength())
579 return aValues[nAtRow];
580 }
581 }
582 return std::numeric_limits<double>::quiet_NaN();
583 }
584
585 uno::Any DataBrowserModel::getCellAny( sal_Int32 nAtColumn, sal_Int32 nAtRow )
586 {
587 uno::Any aResult;
588
589 tDataColumnVector::size_type nIndex( nAtColumn );
590 if( nIndex < m_aColumns.size() &&
591 m_aColumns[ nIndex ].m_xLabeledDataSequence.is())
592 {
593 Reference< chart2::data::XDataSequence > xData(
594 m_aColumns[ nIndex ].m_xLabeledDataSequence->getValues() );
595 if( xData.is() )
596 {
597 Sequence< uno::Any > aValues( xData->getData());
598 if( nAtRow < aValues.getLength())
599 aResult = aValues[nAtRow];
600 }
601 }
602 return aResult;
603 }
604
605 OUString DataBrowserModel::getCellText( sal_Int32 nAtColumn, sal_Int32 nAtRow )
606 {
607 OUString aResult;
608
609 tDataColumnVector::size_type nIndex( nAtColumn );
610 if( nIndex < m_aColumns.size() &&
611 m_aColumns[ nIndex ].m_xLabeledDataSequence.is())
612 {
613 Reference< chart2::data::XTextualDataSequence > xData(
614 m_aColumns[ nIndex ].m_xLabeledDataSequence->getValues(), uno::UNO_QUERY );
615 if( xData.is())
616 {
617 Sequence< OUString > aValues( xData->getTextualData());
618 if( nAtRow < aValues.getLength())
619 aResult = aValues[nAtRow];
620 }
621 }
622 return aResult;
623 }
624
625 sal_uInt32 DataBrowserModel::getNumberFormatKey( sal_Int32 nAtColumn )
626 {
627 tDataColumnVector::size_type nIndex( nAtColumn );
628 if( nIndex < m_aColumns.size())
629 return m_aColumns[ nIndex ].m_nNumberFormatKey;
630 return 0;
631 }
632
633 bool DataBrowserModel::setCellAny( sal_Int32 nAtColumn, sal_Int32 nAtRow, const uno::Any & rValue )
634 {
635 bool bResult = false;
636 tDataColumnVector::size_type nIndex( nAtColumn );
637 if( nIndex < m_aColumns.size() &&
638 m_aColumns[ nIndex ].m_xLabeledDataSequence.is())
639 {
640 bResult = true;
641 try
642 {
643 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
644
645 // label
646 if( nAtRow == -1 )
647 {
648 Reference< container::XIndexReplace > xIndexReplace(
649 m_aColumns[ nIndex ].m_xLabeledDataSequence->getLabel(), uno::UNO_QUERY_THROW );
650 xIndexReplace->replaceByIndex( 0, rValue );
651 }
652 else
653 {
654 Reference< container::XIndexReplace > xIndexReplace(
655 m_aColumns[ nIndex ].m_xLabeledDataSequence->getValues(), uno::UNO_QUERY_THROW );
656 xIndexReplace->replaceByIndex( nAtRow, rValue );
657 }
658
659 m_apDialogModel->startControllerLockTimer();
660 //notify change directly to the model (this is necessary here as sequences for complex categories not known directly to the chart model so they do not notify their changes) (for complex categories see issue #i82971#)
661 if( m_xChartDocument.is() )
662 m_xChartDocument->setModified(true);
663 }
664 catch( const uno::Exception & )
665 {
666 bResult = false;
667 }
668 }
669 return bResult;
670 }
671
672 bool DataBrowserModel::setCellNumber( sal_Int32 nAtColumn, sal_Int32 nAtRow, double fValue )
673 {
674 return (getCellType( nAtColumn ) == NUMBER) &&
675 setCellAny( nAtColumn, nAtRow, uno::Any( fValue ));
676 }
677
678 bool DataBrowserModel::setCellText( sal_Int32 nAtColumn, sal_Int32 nAtRow, const OUString & rText )
679 {
680 return (getCellType( nAtColumn ) == TEXT) &&
681 setCellAny( nAtColumn, nAtRow, uno::Any( rText ));
682 }
683
684 sal_Int32 DataBrowserModel::getColumnCount() const
685 {
686 return static_cast< sal_Int32 >( m_aColumns.size());
687 }
688
689 sal_Int32 DataBrowserModel::getMaxRowCount() const
690 {
691 sal_Int32 nResult = 0;
692 for (auto const& column : m_aColumns)
693 {
694 if( column.m_xLabeledDataSequence.is())
695 {
696 Reference< chart2::data::XDataSequence > xSeq(
697 column.m_xLabeledDataSequence->getValues());
698 if( !xSeq.is())
699 continue;
700 sal_Int32 nLength( xSeq->getData().getLength());
701 if( nLength > nResult )
702 nResult = nLength;
703 }
704 }
705
706 return nResult;
707 }
708
709 const OUString & DataBrowserModel::getRoleOfColumn( sal_Int32 nColumnIndex ) const
710 {
711 if( nColumnIndex != -1 &&
712 o3tl::make_unsigned( nColumnIndex ) < m_aColumns.size())
713 return m_aColumns[ nColumnIndex ].m_aUIRoleName;
714 return EMPTY_OUSTRING;
715 }
716
717 bool DataBrowserModel::isCategoriesColumn( sal_Int32 nColumnIndex ) const
718 {
719 if (nColumnIndex < 0)
720 return false;
721
722 if (o3tl::make_unsigned(nColumnIndex) >= m_aColumns.size())
723 return false;
724
725 // A column is a category when it doesn't have an associated data series.
726 return !m_aColumns[nColumnIndex].m_xDataSeries.is();
727 }
728
729 sal_Int32 DataBrowserModel::getCategoryColumnCount()
730 {
731 sal_Int32 nLastTextColumnIndex = -1;
732 for (auto const& column : m_aColumns)
733 {
734 if( !column.m_xDataSeries.is() )
735 nLastTextColumnIndex++;
736 else
737 break;
738 }
739 return nLastTextColumnIndex+1;
740 }
741
742 void DataBrowserModel::updateFromModel()
743 {
744 if( !m_xChartDocument.is())
745 return;
746 m_aColumns.clear();
747 m_aHeaders.clear();
748
749 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
750 if( !xDiagram.is())
751 return;
752
753 // set template at DialogModel
754 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = m_xChartDocument->getTypeManager();
755 Diagram::tTemplateWithServiceName aTemplateAndService =
756 xDiagram->getTemplate( xChartTypeManager );
757 if( aTemplateAndService.xChartTypeTemplate.is())
758 m_apDialogModel->setTemplate( aTemplateAndService.xChartTypeTemplate );
759
760 sal_Int32 nHeaderStart = 0;
761 sal_Int32 nHeaderEnd = 0;
762 {
763 ExplicitCategoriesProvider aExplicitCategoriesProvider( m_xChartDocument->getFirstCoordinateSystem(), *m_xChartDocument );
764
765 const std::vector< Reference< chart2::data::XLabeledDataSequence> >& rSplitCategoriesList = aExplicitCategoriesProvider.getSplitCategoriesList();
766 sal_Int32 nLevelCount = rSplitCategoriesList.size();
767 for( sal_Int32 nL = 0; nL<nLevelCount; nL++ )
768 {
769 const Reference< chart2::data::XLabeledDataSequence >& xCategories( rSplitCategoriesList[nL] );
770 if( !xCategories.is() )
771 continue;
772
773 tDataColumn aCategories;
774 aCategories.m_xLabeledDataSequence = xCategories;
775 if( lcl_ShowCategoriesAsDataLabel( xDiagram ))
776 aCategories.m_aUIRoleName = DialogModel::GetRoleDataLabel();
777 else
778 aCategories.m_aUIRoleName = lcl_getUIRoleName( xCategories );
779 aCategories.m_eCellType = TEXTORDATE;
780 m_aColumns.push_back( aCategories );
781 ++nHeaderStart;
782 }
783 }
784
785 if( !xDiagram.is())
786 return;
787 const std::vector< rtl::Reference< BaseCoordinateSystem > > aCooSysSeq( xDiagram->getBaseCoordinateSystems());
788 for( rtl::Reference< BaseCoordinateSystem > const & coords : aCooSysSeq )
789 {
790 const std::vector< rtl::Reference< ChartType > > aChartTypes( coords->getChartTypes2());
791 sal_Int32 nXAxisNumberFormat = DataSeriesHelper::getNumberFormatKeyFromAxis( nullptr, coords, 0, 0 );
792
793 for( auto const & CT: aChartTypes )
794 {
795 const rtl::Reference< ChartType >& xSeriesCnt( CT );
796 OUString aRoleForDataLabelNumberFormat = ChartTypeHelper::getRoleOfSequenceForDataLabelNumberFormatDetection( CT );
797
798 const std::vector< rtl::Reference< DataSeries > > & aSeries( xSeriesCnt->getDataSeries2());
799 lcl_tSharedSeqVec aSharedSequences( lcl_getSharedSequences( aSeries ));
800 for (auto const& sharedSequence : aSharedSequences)
801 {
802 tDataColumn aSharedSequence;
803 aSharedSequence.m_xLabeledDataSequence = sharedSequence;
804 aSharedSequence.m_aUIRoleName = lcl_getUIRoleName(sharedSequence);
805 aSharedSequence.m_eCellType = NUMBER;
806 // as the sequences are shared it should be ok to take the first series
807 // @todo: dimension index 0 for x-values used here. This is just a guess.
808 // Also, the axis index is 0, as there is usually only one x-axis
809 aSharedSequence.m_nNumberFormatKey = nXAxisNumberFormat;
810 m_aColumns.push_back( aSharedSequence );
811 ++nHeaderStart;
812 }
813 for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
814 {
815 tDataColumnVector::size_type nStartColIndex = m_aColumns.size();
816 const rtl::Reference< DataSeries >& xSeries( dataSeries );
817 if( xSeries.is())
818 {
819 const std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > & aLSeqs( xSeries->getDataSequences2());
820 if( aLSeqs.empty() )
821 continue;
822 nHeaderEnd = nHeaderStart;
823
824 // @todo: dimension index 1 for y-values used here. This is just a guess
825 sal_Int32 nYAxisNumberFormatKey =
826 DataSeriesHelper::getNumberFormatKeyFromAxis(
827 dataSeries, coords, 1 );
828
829 sal_Int32 nSeqIdx=0;
830 for( ; nSeqIdx<static_cast<sal_Int32>(aLSeqs.size()); ++nSeqIdx )
831 {
832 sal_Int32 nSequenceNumberFormatKey = nYAxisNumberFormatKey;
833 OUString aRole = DataSeriesHelper::getRole(aLSeqs[nSeqIdx]);
834
835 if( aRole == aRoleForDataLabelNumberFormat )
836 {
837 nSequenceNumberFormatKey = xSeries->getExplicitNumberFormatKeyForDataLabel();
838 }
839 else if( aRole == "values-x" )
840 nSequenceNumberFormatKey = nXAxisNumberFormat;
841
842 if( std::none_of( aSharedSequences.begin(), aSharedSequences.end(),
843 lcl_RepresentationsOfLSeqMatch( aLSeqs[nSeqIdx] )) )
844 {
845 // no shared sequence
846 m_aColumns.emplace_back(
847 dataSeries,
848 lcl_getUIRoleName( aLSeqs[nSeqIdx] ),
849 aLSeqs[nSeqIdx],
850 NUMBER,
851 nSequenceNumberFormatKey );
852 ++nHeaderEnd;
853 }
854 // else skip
855 }
856 bool bSwapXAndYAxis = false;
857 try
858 {
859 coords->getPropertyValue( u"SwapXAndYAxis"_ustr ) >>= bSwapXAndYAxis;
860 }
861 catch( const beans::UnknownPropertyException & ) {}
862
863 // add ranges for error bars if present for a series
864 if( StatisticsHelper::usesErrorBarRanges( dataSeries ))
865 addErrorBarRanges( dataSeries, nYAxisNumberFormatKey, nSeqIdx, nHeaderEnd, true );
866
867 if( StatisticsHelper::usesErrorBarRanges( dataSeries, /* bYError = */ false ))
868 addErrorBarRanges( dataSeries, nYAxisNumberFormatKey, nSeqIdx, nHeaderEnd, false );
869
870 m_aHeaders.emplace_back(
871 dataSeries,
872 CT,
873 bSwapXAndYAxis,
874 nHeaderStart,
875 nHeaderEnd - 1 );
876
877 nHeaderStart = nHeaderEnd;
878
879 std::sort( m_aColumns.begin() + nStartColIndex, m_aColumns.end(), implColumnLess() );
880 }
881 }
882 }
883 }
884 }
885
886 void DataBrowserModel::addErrorBarRanges(
887 const rtl::Reference< DataSeries > & xDataSeries,
888 sal_Int32 nNumberFormatKey,
889 sal_Int32 & rInOutSequenceIndex,
890 sal_Int32 & rInOutHeaderEnd, bool bYError )
891 {
892 try
893 {
894 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aSequences;
895
896 Reference< chart2::data::XDataSource > xErrorSource(
897 StatisticsHelper::getErrorBars( xDataSeries, bYError ), uno::UNO_QUERY );
898
899 uno::Reference< chart2::data::XLabeledDataSequence > xErrorLSequence =
900 StatisticsHelper::getErrorLabeledDataSequenceFromDataSource(
901 xErrorSource,
902 /* bPositiveValue = */ true,
903 bYError );
904 if( xErrorLSequence.is())
905 aSequences.push_back( xErrorLSequence );
906
907 xErrorLSequence =
908 StatisticsHelper::getErrorLabeledDataSequenceFromDataSource(
909 xErrorSource,
910 /* bPositiveValue = */ false,
911 bYError );
912 if( xErrorLSequence.is())
913 aSequences.push_back( xErrorLSequence );
914
915 for (uno::Reference<chart2::data::XLabeledDataSequence> const & rDataSequence : aSequences)
916 {
917 m_aColumns.emplace_back(xDataSeries, lcl_getUIRoleName(rDataSequence),
918 rDataSequence, NUMBER, nNumberFormatKey);
919 ++rInOutSequenceIndex;
920 ++rInOutHeaderEnd;
921 }
922 }
923 catch( const uno::Exception & )
924 {
925 DBG_UNHANDLED_EXCEPTION("chart2");
926 }
927 }
928
929 } // namespace chart
930
931 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */