Resave with newer Glade version
[LibreOffice.git] / chart2 / source / controller / dialogs / DialogModel.cxx
blob3abf2b6b0a6a058bbe1448446d84f45a9ce05216
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "DialogModel.hxx"
21 #include <RangeSelectionHelper.hxx>
22 #include <DataInterpreter.hxx>
23 #include <DataSeries.hxx>
24 #include <DataSeriesHelper.hxx>
25 #include <DataSourceHelper.hxx>
26 #include <Diagram.hxx>
27 #include <strings.hrc>
28 #include <ResId.hxx>
29 #include <ControllerLockGuard.hxx>
30 #include <ChartType.hxx>
31 #include <ChartTypeHelper.hxx>
32 #include <ChartTypeTemplate.hxx>
33 #include <ThreeDHelper.hxx>
34 #include <ChartModel.hxx>
35 #include <BaseCoordinateSystem.hxx>
36 #include <LabeledDataSequence.hxx>
38 #include <com/sun/star/chart2/AxisType.hpp>
39 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
40 #include <comphelper/diagnose_ex.hxx>
42 #include <rtl/ustring.hxx>
44 #include <utility>
45 #include <algorithm>
46 #include <cstddef>
47 #include <iterator>
48 #include <numeric>
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::chart2;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::Sequence;
56 namespace
58 constexpr OUString lcl_aLabelRole( u"label"_ustr );
61 OUString lcl_ConvertRole( const OUString & rRoleString )
63 OUString aResult( rRoleString );
65 typedef std::map< OUString, OUString > tTranslationMap;
66 static const tTranslationMap aTranslationMap =
68 { "categories", ::chart::SchResId( STR_DATA_ROLE_CATEGORIES ) },
69 { "error-bars-x", ::chart::SchResId( STR_DATA_ROLE_X_ERROR ) },
70 { "error-bars-x-positive", ::chart::SchResId( STR_DATA_ROLE_X_ERROR_POSITIVE ) },
71 { "error-bars-x-negative", ::chart::SchResId( STR_DATA_ROLE_X_ERROR_NEGATIVE ) },
72 { "error-bars-y", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR ) },
73 { "error-bars-y-positive", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR_POSITIVE ) },
74 { "error-bars-y-negative", ::chart::SchResId( STR_DATA_ROLE_Y_ERROR_NEGATIVE ) },
75 { "label", ::chart::SchResId( STR_DATA_ROLE_LABEL ) },
76 { "values-first", ::chart::SchResId( STR_DATA_ROLE_FIRST ) },
77 { "values-last", ::chart::SchResId( STR_DATA_ROLE_LAST ) },
78 { "values-max", ::chart::SchResId( STR_DATA_ROLE_MAX ) },
79 { "values-min", ::chart::SchResId( STR_DATA_ROLE_MIN ) },
80 { "values-x", ::chart::SchResId( STR_DATA_ROLE_X ) },
81 { "values-y", ::chart::SchResId( STR_DATA_ROLE_Y ) },
82 { "values-size", ::chart::SchResId( STR_DATA_ROLE_SIZE ) },
83 { "FillColor", ::chart::SchResId( STR_PROPERTY_ROLE_FILLCOLOR ) },
84 { "BorderColor", ::chart::SchResId( STR_PROPERTY_ROLE_BORDERCOLOR ) },
87 tTranslationMap::const_iterator aIt( aTranslationMap.find( rRoleString ));
88 if( aIt != aTranslationMap.end())
90 aResult = (*aIt).second;
92 return aResult;
95 typedef std::map< OUString, sal_Int32 > lcl_tRoleIndexMap;
97 lcl_tRoleIndexMap lcl_createRoleIndexMap()
99 lcl_tRoleIndexMap aMap;
100 sal_Int32 nIndex = 0;
102 aMap[ u"label"_ustr ] = ++nIndex;
103 aMap[ u"categories"_ustr ] = ++nIndex;
104 aMap[ u"values-x"_ustr ] = ++nIndex;
105 aMap[ u"values-y"_ustr ] = ++nIndex;
106 aMap[ u"error-bars-x"_ustr ] = ++nIndex;
107 aMap[ u"error-bars-x-positive"_ustr ] = ++nIndex;
108 aMap[ u"error-bars-x-negative"_ustr ] = ++nIndex;
109 aMap[ u"error-bars-y"_ustr ] = ++nIndex;
110 aMap[ u"error-bars-y-positive"_ustr ] = ++nIndex;
111 aMap[ u"error-bars-y-negative"_ustr ] = ++nIndex;
112 aMap[ u"values-first"_ustr ] = ++nIndex;
113 aMap[ u"values-min"_ustr ] = ++nIndex;
114 aMap[ u"values-max"_ustr ] = ++nIndex;
115 aMap[ u"values-last"_ustr ] = ++nIndex;
116 aMap[ u"values-size"_ustr ] = ++nIndex;
118 return aMap;
122 struct lcl_RolesWithRangeAppend
124 typedef Reference< data::XLabeledDataSequence > value_type;
125 typedef ::chart::DialogModel::tRolesWithRanges tContainerType;
127 explicit lcl_RolesWithRangeAppend( tContainerType * rCnt,
128 OUString aLabelRole )
129 : m_rDestCnt( rCnt ),
130 m_aRoleForLabelSeq(std::move( aLabelRole ))
133 lcl_RolesWithRangeAppend & operator= ( const value_type & xVal )
137 if( xVal.is())
139 // data sequence
140 Reference< data::XDataSequence > xSeq( xVal->getValues());
141 if( xSeq.is())
143 OUString aRole;
144 Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY_THROW );
145 if( xProp->getPropertyValue( u"Role"_ustr ) >>= aRole )
147 m_rDestCnt->emplace(aRole, xSeq->getSourceRangeRepresentation());
148 // label
149 if( aRole == m_aRoleForLabelSeq )
151 Reference< data::XDataSequence > xLabelSeq( xVal->getLabel());
152 if( xLabelSeq.is())
154 m_rDestCnt->emplace(
155 lcl_aLabelRole, xLabelSeq->getSourceRangeRepresentation());
162 catch( const uno::Exception & )
164 DBG_UNHANDLED_EXCEPTION("chart2");
166 return *this;
169 // Implement output operator requirements as required by std::copy (and
170 // implement prefix increment in terms of postfix increment to avoid unused
171 // member function warnings for the latter in the common case where
172 // std::copy would not actually need it):
173 lcl_RolesWithRangeAppend & operator* () { return *this; }
174 lcl_RolesWithRangeAppend & operator++ () { return operator++(0); }
175 lcl_RolesWithRangeAppend & operator++ (int) { return *this; }
177 private:
178 tContainerType * m_rDestCnt;
179 OUString m_aRoleForLabelSeq;
184 namespace std
186 template<> struct iterator_traits<lcl_RolesWithRangeAppend>
188 typedef std::output_iterator_tag iterator_category;
189 typedef Reference< data::XLabeledDataSequence > value_type;
190 typedef value_type& reference;
194 namespace {
196 void lcl_SetSequenceRole(
197 const Reference< data::XDataSequence > & xSeq,
198 const OUString & rRole )
200 Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY );
201 if( xProp.is())
202 xProp->setPropertyValue( u"Role"_ustr , uno::Any( rRole ));
205 Sequence< OUString > lcl_CopyExcludingValuesFirst(
206 Sequence< OUString > const & i_aInput )
208 Sequence< OUString > aOutput( i_aInput.getLength());
209 auto pOutput = aOutput.getArray();
210 int nSourceIndex, nDestIndex;
211 for( nSourceIndex = nDestIndex = 0; nSourceIndex < i_aInput.getLength(); nSourceIndex++ )
213 if( i_aInput[nSourceIndex] == "values-first" )
215 aOutput.realloc( aOutput.getLength() - 1 );
216 pOutput = aOutput.getArray();
218 else
220 pOutput[nDestIndex] = i_aInput[nSourceIndex];
221 nDestIndex++;
224 return aOutput;
227 rtl::Reference< ::chart::DataSeries > lcl_CreateNewSeries(
228 const rtl::Reference< ::chart::ChartType > & xChartType,
229 sal_Int32 nNewSeriesIndex,
230 sal_Int32 nTotalNumberOfSeriesInCTGroup,
231 const rtl::Reference< ::chart::Diagram > & xDiagram,
232 const rtl::Reference< ::chart::ChartTypeTemplate > & xTemplate,
233 bool bCreateDataCachedSequences )
235 // create plain series
236 rtl::Reference< ::chart::DataSeries > xResult = new ::chart::DataSeries();
237 if( xTemplate.is())
239 // @deprecated: correct default color should be found by view
240 // without setting it as hard attribute
241 Reference< XColorScheme > xColorScheme( xDiagram->getDefaultColorScheme());
242 if( xColorScheme.is())
243 xResult->setPropertyValue( u"Color"_ustr , uno::Any( xColorScheme->getColorByIndex( nNewSeriesIndex )));
244 std::size_t nGroupIndex=0;
245 if( xChartType.is())
247 std::vector< rtl::Reference< ::chart::ChartType > > aCTs =
248 xDiagram->getChartTypes();
249 for( ; nGroupIndex < aCTs.size(); ++nGroupIndex)
250 if( aCTs[nGroupIndex] == xChartType )
251 break;
252 if( nGroupIndex == aCTs.size())
253 nGroupIndex = 0;
255 xTemplate->applyStyle2( xResult, nGroupIndex, nNewSeriesIndex, nTotalNumberOfSeriesInCTGroup );
258 if( bCreateDataCachedSequences )
260 // set chart type specific roles
261 if( xChartType.is() )
263 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewSequences;
264 const OUString aRoleOfSeqForSeriesLabel = xChartType->getRoleOfSequenceForSeriesLabel();
265 const OUString aLabel(::chart::SchResId(STR_DATA_UNNAMED_SERIES));
266 Sequence< OUString > aPossibleRoles( xChartType->getSupportedMandatoryRoles());
267 Sequence< OUString > aPossibleOptRoles( xChartType->getSupportedOptionalRoles());
269 //special handling for candlestick type
270 if( xTemplate.is())
272 rtl::Reference< ::chart::DataInterpreter > xInterpreter( xTemplate->getDataInterpreter2());
273 if( xInterpreter.is())
275 sal_Int32 nStockVariant;
276 if( xInterpreter->getChartTypeSpecificData(u"stock variant"_ustr) >>= nStockVariant )
278 if( nStockVariant == 0 || nStockVariant == 2) {
279 //delete "values-first" role
280 aPossibleRoles = lcl_CopyExcludingValuesFirst(aPossibleRoles);
281 aPossibleOptRoles = lcl_CopyExcludingValuesFirst(aPossibleOptRoles);
287 const Sequence< OUString > aRoles( aPossibleRoles );
288 const Sequence< OUString > aOptRoles( aPossibleOptRoles );
290 for(OUString const & role : aRoles)
292 if( role == lcl_aLabelRole )
293 continue;
294 Reference< data::XDataSequence > xSeq( ::chart::DataSourceHelper::createCachedDataSequence() );
295 lcl_SetSequenceRole( xSeq, role );
296 // assert that aRoleOfSeqForSeriesLabel is part of the mandatory roles
297 if( role == aRoleOfSeqForSeriesLabel )
299 Reference< data::XDataSequence > xLabel( ::chart::DataSourceHelper::createCachedDataSequence( aLabel ));
300 lcl_SetSequenceRole( xLabel, lcl_aLabelRole );
301 aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq, xLabel ));
303 else
304 aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq ));
307 for(OUString const & role : aOptRoles)
309 if( role == lcl_aLabelRole )
310 continue;
311 Reference< data::XDataSequence > xSeq( ::chart::DataSourceHelper::createCachedDataSequence());
312 lcl_SetSequenceRole( xSeq, role );
313 aNewSequences.push_back( ::chart::DataSourceHelper::createLabeledDataSequence( xSeq ));
316 xResult->setData( aNewSequences );
320 return xResult;
323 struct lcl_addSeriesNumber
325 sal_Int32 operator() ( sal_Int32 nCurrentNumber, const Reference< XDataSeriesContainer > & xCnt ) const
327 if( xCnt.is())
328 return nCurrentNumber + (xCnt->getDataSeries().getLength());
329 return nCurrentNumber;
333 } // anonymous namespace
335 namespace chart
338 DialogModelTimeBasedInfo::DialogModelTimeBasedInfo():
339 bTimeBased(false),
340 nStart(0),
341 nEnd(0)
345 DialogModel::DialogModel(
346 rtl::Reference<::chart::ChartModel> xChartDocument ) :
347 m_xChartDocument(std::move( xChartDocument )),
348 m_aTimerTriggeredControllerLock( m_xChartDocument )
352 DialogModel::~DialogModel()
354 if(maTimeBasedInfo.bTimeBased)
356 getModel().setTimeBasedRange(maTimeBasedInfo.nStart, maTimeBasedInfo.nEnd);
360 void DialogModel::setTemplate(
361 const rtl::Reference< ChartTypeTemplate > & xTemplate )
363 m_xTemplate = xTemplate;
366 std::shared_ptr< RangeSelectionHelper > const &
367 DialogModel::getRangeSelectionHelper() const
369 if( ! m_spRangeSelectionHelper)
370 m_spRangeSelectionHelper =
371 std::make_shared<RangeSelectionHelper>( m_xChartDocument );
373 return m_spRangeSelectionHelper;
376 const rtl::Reference<::chart::ChartModel> & DialogModel::getChartModel() const
378 return m_xChartDocument;
381 Reference< data::XDataProvider > DialogModel::getDataProvider() const
383 Reference< data::XDataProvider > xResult;
384 if( m_xChartDocument.is())
385 xResult.set( m_xChartDocument->getDataProvider());
386 return xResult;
389 std::vector< rtl::Reference< ChartType > >
390 DialogModel::getAllDataSeriesContainers() const
392 std::vector< rtl::Reference< ChartType > > aResult;
396 if( !m_xChartDocument )
397 return {};
398 rtl::Reference< Diagram > xDiagram = m_xChartDocument->getFirstChartDiagram();
399 if( xDiagram.is())
401 const std::vector< rtl::Reference< BaseCoordinateSystem > > & aCooSysSeq(
402 xDiagram->getBaseCoordinateSystems());
403 for( rtl::Reference< BaseCoordinateSystem > const & coords : aCooSysSeq )
406 for (const auto & rxChartType : coords->getChartTypes2())
407 aResult.push_back(rxChartType);
411 catch( const uno::Exception & )
413 DBG_UNHANDLED_EXCEPTION("chart2");
416 return aResult;
419 std::vector< DialogModel::tSeriesWithChartTypeByName >
420 DialogModel::getAllDataSeriesWithLabel() const
422 std::vector< tSeriesWithChartTypeByName > aResult;
423 std::vector< rtl::Reference< ChartType > > aContainers(
424 getAllDataSeriesContainers());
426 for (const auto & rxChartType : aContainers )
430 const std::vector< rtl::Reference< DataSeries > > & aSeq = rxChartType->getDataSeries2();
431 OUString aRole = rxChartType->getRoleOfSequenceForSeriesLabel();
432 for( rtl::Reference< DataSeries > const & dataSeries : aSeq )
434 aResult.push_back(
435 ::chart::DialogModel::tSeriesWithChartTypeByName(
436 dataSeries->getLabelForRole( aRole ),
437 std::make_pair( dataSeries, rxChartType )));
440 catch( const uno::Exception & )
442 DBG_UNHANDLED_EXCEPTION("chart2");
446 return aResult;
449 namespace {
451 void addMissingRoles(DialogModel::tRolesWithRanges& rResult, const uno::Sequence<OUString>& rRoles)
453 for(OUString const & role : rRoles)
455 if(rResult.find(role) == rResult.end())
456 rResult.emplace(role, OUString());
461 * Insert a new data series to chart type at position after specified series
462 * position.
464 * @param xChartType chart type that contains data series.
465 * @param xSeries insertion position. The new series will be inserted after
466 * this one.
467 * @param xNewSeries new data series to insert.
469 void addNewSeriesToContainer(
470 const rtl::Reference<ChartType>& xChartType,
471 const rtl::Reference<DataSeries>& xSeries,
472 const rtl::Reference<DataSeries>& xNewSeries )
474 auto aSeries = xChartType->getDataSeries2();
476 auto aIt = std::find( aSeries.begin(), aSeries.end(), xSeries);
478 if( aIt == aSeries.end())
479 // if we have no series we insert at the first position.
480 aIt = aSeries.begin();
481 else
482 // vector::insert inserts before, so we have to advance
483 ++aIt;
485 aSeries.insert(aIt, xNewSeries);
486 xChartType->setDataSeries(aSeries);
491 DialogModel::tRolesWithRanges DialogModel::getRolesWithRanges(
492 const Reference< XDataSeries > & xSeries,
493 const OUString & aRoleOfSequenceForLabel,
494 const rtl::Reference< ::chart::ChartType > & xChartType )
496 DialogModel::tRolesWithRanges aResult;
499 Reference< data::XDataSource > xSource( xSeries, uno::UNO_QUERY_THROW );
500 const Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSource->getDataSequences());
501 std::copy( aSeq.begin(), aSeq.end(),
502 lcl_RolesWithRangeAppend( &aResult, aRoleOfSequenceForLabel ));
503 if( xChartType.is())
505 // add missing mandatory roles
506 Sequence< OUString > aRoles( xChartType->getSupportedMandatoryRoles());
507 addMissingRoles(aResult, aRoles);
509 // add missing optional roles
510 aRoles = xChartType->getSupportedOptionalRoles();
511 addMissingRoles(aResult, aRoles);
513 // add missing property roles
514 aRoles = xChartType->getSupportedPropertyRoles();
515 addMissingRoles(aResult, aRoles);
518 catch( const uno::Exception & )
520 DBG_UNHANDLED_EXCEPTION("chart2");
522 return aResult;
525 void DialogModel::moveSeries(
526 const rtl::Reference< DataSeries > & xSeries,
527 MoveDirection eDirection )
529 m_aTimerTriggeredControllerLock.startTimer();
530 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
532 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
533 xDiagram->moveSeries( xSeries, eDirection==MoveDirection::Down );
536 rtl::Reference< ::chart::DataSeries > DialogModel::insertSeriesAfter(
537 const Reference< XDataSeries > & xUnoSeries,
538 const rtl::Reference< ::chart::ChartType > & xChartType,
539 bool bCreateDataCachedSequences /* = false */ )
541 m_aTimerTriggeredControllerLock.startTimer();
542 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
543 rtl::Reference< ::chart::DataSeries > xNewSeries;
544 rtl::Reference<DataSeries> xSeries = dynamic_cast<DataSeries*>(xUnoSeries.get());
545 assert(xSeries || !xUnoSeries);
549 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram() );
550 ThreeDLookScheme e3DScheme = xDiagram->detectScheme();
552 sal_Int32 nSeriesInChartType = 0;
553 const sal_Int32 nTotalSeries = countSeries();
554 if( xChartType.is())
556 nSeriesInChartType = xChartType->getDataSeries().getLength();
559 // create new series
560 xNewSeries =
561 lcl_CreateNewSeries(
562 xChartType,
563 nTotalSeries, // new series' index
564 nSeriesInChartType,
565 xDiagram,
566 m_xTemplate,
567 bCreateDataCachedSequences );
569 // add new series to container
570 if( xNewSeries.is())
571 addNewSeriesToContainer(xChartType, xSeries, xNewSeries);
573 xDiagram->setScheme( e3DScheme );
575 catch( const uno::Exception & )
577 DBG_UNHANDLED_EXCEPTION("chart2");
579 return xNewSeries;
582 void DialogModel::deleteSeries(
583 const rtl::Reference< DataSeries > & xSeries,
584 const rtl::Reference< ChartType > & xChartType )
586 m_aTimerTriggeredControllerLock.startTimer();
587 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
589 DataSeriesHelper::deleteSeries( xSeries, xChartType );
592 uno::Reference< chart2::data::XLabeledDataSequence > DialogModel::getCategories() const
594 uno::Reference< chart2::data::XLabeledDataSequence > xResult;
597 if( m_xChartDocument.is())
599 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
600 if (xDiagram.is())
601 xResult = xDiagram->getCategories();
604 catch( const uno::Exception & )
606 DBG_UNHANDLED_EXCEPTION("chart2");
608 return xResult;
611 void DialogModel::setCategories( const Reference< chart2::data::XLabeledDataSequence > & xCategories )
613 if( !m_xChartDocument.is())
614 return;
616 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
617 if( !xDiagram.is())
618 return;
620 // categories
621 bool bSupportsCategories = true;
623 rtl::Reference< ChartType > xFirstChartType( xDiagram->getChartTypeByIndex( 0 ) );
624 if( xFirstChartType.is() )
626 sal_Int32 nAxisType = ChartTypeHelper::getAxisType( xFirstChartType, 0 ); // x-axis
627 bSupportsCategories = (nAxisType == AxisType::CATEGORY);
629 xDiagram->setCategories( xCategories, true, bSupportsCategories );
632 OUString DialogModel::getCategoriesRange() const
634 OUString aRange;
637 uno::Reference< chart2::data::XLabeledDataSequence > xLSeq( getCategories());
638 if( xLSeq.is())
640 Reference< data::XDataSequence > xSeq( xLSeq->getValues());
641 if( xSeq.is())
642 aRange = xSeq->getSourceRangeRepresentation();
645 catch (const lang::DisposedException&)
647 TOOLS_WARN_EXCEPTION( "chart2", "unexpected exception caught" );
649 return aRange;
652 bool DialogModel::isCategoryDiagram() const
654 bool bRet = false;
655 if( m_xChartDocument.is() && m_xChartDocument->getFirstChartDiagram())
656 bRet = m_xChartDocument->getFirstChartDiagram()->isCategory();
657 return bRet;
660 void DialogModel::detectArguments(
661 OUString & rOutRangeString,
662 bool & rOutUseColumns,
663 bool & rOutFirstCellAsLabel,
664 bool & rOutHasCategories ) const
668 uno::Sequence< sal_Int32 > aSequenceMapping;//todo YYYX
670 // Note: unused data is currently not supported in being passed to detectRangeSegmentation
671 if( m_xChartDocument.is())
673 (void)DataSourceHelper::detectRangeSegmentation(
674 m_xChartDocument,
675 rOutRangeString, aSequenceMapping, rOutUseColumns, rOutFirstCellAsLabel, rOutHasCategories );
678 catch( const uno::Exception & )
680 DBG_UNHANDLED_EXCEPTION("chart2");
684 bool DialogModel::allArgumentsForRectRangeDetected() const
686 return DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument );
689 void DialogModel::startControllerLockTimer()
691 m_aTimerTriggeredControllerLock.startTimer();
694 void DialogModel::setData(
695 const Sequence< beans::PropertyValue > & rArguments )
697 m_aTimerTriggeredControllerLock.startTimer();
698 ControllerLockGuardUNO aLockedControllers( m_xChartDocument );
700 Reference< data::XDataProvider > xDataProvider( getDataProvider());
701 if( ! xDataProvider.is() ||
702 ! m_xTemplate.is() )
704 OSL_FAIL( "Model objects missing" );
705 return;
710 Reference< chart2::data::XDataSource > xDataSource(
711 xDataProvider->createDataSource( rArguments ) );
713 rtl::Reference< ::chart::DataInterpreter > xInterpreter(
714 m_xTemplate->getDataInterpreter2());
715 if( xInterpreter.is())
717 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram() );
718 ThreeDLookScheme e3DScheme = xDiagram->detectScheme();
720 std::vector< rtl::Reference< DataSeries > > aSeriesToReUse =
721 xDiagram->getDataSeries();
722 applyInterpretedData(
723 xInterpreter->interpretDataSource(
724 xDataSource, rArguments,
725 aSeriesToReUse ),
726 aSeriesToReUse);
728 xDiagram->setScheme( e3DScheme );
731 catch( const uno::Exception & )
733 DBG_UNHANDLED_EXCEPTION("chart2");
737 void DialogModel::setTimeBasedRange( bool bTimeBased, sal_Int32 nStart, sal_Int32 nEnd) const
739 maTimeBasedInfo.nStart = nStart;
740 maTimeBasedInfo.nEnd = nEnd;
741 maTimeBasedInfo.bTimeBased = bTimeBased;
744 OUString DialogModel::ConvertRoleFromInternalToUI( const OUString & rRoleString )
746 return lcl_ConvertRole( rRoleString );
749 OUString DialogModel::GetRoleDataLabel()
751 return ::chart::SchResId(STR_OBJECT_DATALABELS);
754 sal_Int32 DialogModel::GetRoleIndexForSorting( const OUString & rInternalRoleString )
756 static lcl_tRoleIndexMap aRoleIndexMap = lcl_createRoleIndexMap();
758 lcl_tRoleIndexMap::const_iterator aIt( aRoleIndexMap.find( rInternalRoleString ));
759 if( aIt != aRoleIndexMap.end())
760 return aIt->second;
762 return 0;
765 // private methods
767 void DialogModel::applyInterpretedData(
768 const InterpretedData & rNewData,
769 const std::vector< rtl::Reference< DataSeries > > & rSeriesToReUse )
771 if( ! m_xChartDocument.is())
772 return;
774 m_aTimerTriggeredControllerLock.startTimer();
775 rtl::Reference< Diagram > xDiagram( m_xChartDocument->getFirstChartDiagram());
776 if( !xDiagram.is())
777 return;
779 // styles
780 if( m_xTemplate.is() )
782 sal_Int32 nGroup = 0;
783 sal_Int32 nSeriesCounter = 0;
784 sal_Int32 nNewSeriesIndex = static_cast< sal_Int32 >( rSeriesToReUse.size());
785 const sal_Int32 nOuterSize=rNewData.Series.size();
787 for(; nGroup < nOuterSize; ++nGroup)
789 const std::vector< rtl::Reference< DataSeries > > & aSeries( rNewData.Series[ nGroup ] );
790 const sal_Int32 nSeriesInGroup = aSeries.size();
791 for( sal_Int32 nSeries=0; nSeries<nSeriesInGroup; ++nSeries, ++nSeriesCounter )
793 if( std::find( rSeriesToReUse.begin(), rSeriesToReUse.end(), aSeries[nSeries] )
794 == rSeriesToReUse.end())
796 if( aSeries[nSeries].is())
798 // @deprecated: correct default color should be found by view
799 // without setting it as hard attribute
800 Reference< XColorScheme > xColorScheme( xDiagram->getDefaultColorScheme());
801 if( xColorScheme.is())
802 aSeries[nSeries]->setPropertyValue( u"Color"_ustr ,
803 uno::Any( xColorScheme->getColorByIndex( nSeriesCounter )));
805 m_xTemplate->applyStyle2( aSeries[nSeries], nGroup, nNewSeriesIndex++, nSeriesInGroup );
811 // data series
812 std::vector< rtl::Reference< ChartType > > aSeriesCnt = getAllDataSeriesContainers();
814 OSL_ASSERT( aSeriesCnt.size() == rNewData.Series.size());
816 auto aSrcIt = rNewData.Series.begin();
817 auto aDestIt = aSeriesCnt.begin();
818 for(; aSrcIt != rNewData.Series.end() && aDestIt != aSeriesCnt.end();
819 ++aSrcIt, ++aDestIt )
823 OSL_ASSERT( (*aDestIt).is());
824 (*aDestIt)->setDataSeries( *aSrcIt );
826 catch( const uno::Exception & )
828 DBG_UNHANDLED_EXCEPTION("chart2");
832 DialogModel::setCategories(rNewData.Categories);
835 sal_Int32 DialogModel::countSeries() const
837 std::vector< rtl::Reference< ChartType > > aCnt( getAllDataSeriesContainers());
838 return std::accumulate( aCnt.begin(), aCnt.end(), 0, lcl_addSeriesNumber());
841 ChartModel& DialogModel::getModel() const
843 return *m_xChartDocument;
846 } // namespace chart
848 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */