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