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