xref: /trunk/main/chart2/source/tools/InternalDataProvider.cxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_charttools.hxx"
27 #include <rtl/math.hxx>
28 
29 #include <valarray>
30 
31 #include "InternalDataProvider.hxx"
32 #include "LabeledDataSequence.hxx"
33 #include "DataSource.hxx"
34 #include "PropertyHelper.hxx"
35 #include "macros.hxx"
36 #include "XMLRangeHelper.hxx"
37 #include "ContainerHelper.hxx"
38 #include "CommonConverters.hxx"
39 #include "CommonFunctors.hxx"
40 #include "UncachedDataSequence.hxx"
41 #include "DataSourceHelper.hxx"
42 #include "ChartModelHelper.hxx"
43 #include "DiagramHelper.hxx"
44 #include "ExplicitCategoriesProvider.hxx"
45 
46 #include <com/sun/star/chart2/XChartDocument.hpp>
47 #include <com/sun/star/chart2/data/XDataSequence.hpp>
48 #include <com/sun/star/chart/ChartDataRowSource.hpp>
49 #include <rtl/ustrbuf.hxx>
50 #include <unotools/charclass.hxx>
51 #include <comphelper/sequenceashashmap.hxx>
52 
53 #include <vector>
54 #include <algorithm>
55 #include <iterator>
56 
57 using namespace ::com::sun::star;
58 using namespace ::std;
59 
60 using ::com::sun::star::uno::Reference;
61 using ::com::sun::star::uno::Sequence;
62 using ::rtl::OUString;
63 using ::rtl::OUStringBuffer;
64 
65 namespace chart
66 {
67 
68 // ================================================================================
69 
70 namespace
71 {
72 
73 // note: in xmloff this name is used to indicate usage of own data
74 static const ::rtl::OUString lcl_aServiceName(
75     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.InternalDataProvider" ));
76 
77 static const ::rtl::OUString lcl_aCategoriesRangeName(
78     RTL_CONSTASCII_USTRINGPARAM( "categories" ));
79 static const ::rtl::OUString lcl_aCategoriesLevelRangeNamePrefix(
80     RTL_CONSTASCII_USTRINGPARAM( "categoriesL " )); //L <-> level
81 static const ::rtl::OUString lcl_aCategoriesPointRangeNamePrefix(
82     RTL_CONSTASCII_USTRINGPARAM( "categoriesP " )); //P <-> point
83 static const ::rtl::OUString lcl_aCategoriesRoleName(
84     RTL_CONSTASCII_USTRINGPARAM( "categories" ));
85 static const ::rtl::OUString lcl_aLabelRangePrefix(
86     RTL_CONSTASCII_USTRINGPARAM( "label " ));
87 static const ::rtl::OUString lcl_aCompleteRange(
88     RTL_CONSTASCII_USTRINGPARAM( "all" ));
89 
90 typedef ::std::multimap< OUString, uno::WeakReference< chart2::data::XDataSequence > >
91     lcl_tSequenceMap;
92 
lcl_AnyToStringSequence(const Sequence<uno::Any> & aAnySeq)93 Sequence< OUString > lcl_AnyToStringSequence( const Sequence< uno::Any >& aAnySeq )
94 {
95     Sequence< OUString > aResult;
96     aResult.realloc( aAnySeq.getLength() );
97     transform( aAnySeq.getConstArray(), aAnySeq.getConstArray() + aAnySeq.getLength(),
98                aResult.getArray(), CommonFunctors::AnyToString() );
99     return aResult;
100 }
101 
lcl_StringToAnySequence(const Sequence<OUString> & aStringSeq)102 Sequence< uno::Any > lcl_StringToAnySequence( const Sequence< OUString >& aStringSeq )
103 {
104     Sequence< uno::Any > aResult;
105     aResult.realloc( aStringSeq.getLength() );
106     transform( aStringSeq.getConstArray(), aStringSeq.getConstArray() + aStringSeq.getLength(),
107                aResult.getArray(), CommonFunctors::makeAny< OUString >() );
108     return aResult;
109 }
110 
111 struct lcl_setModified
112 {
113     typedef lcl_tSequenceMap argument_type;
114     typedef void result_type;
operator ()chart::__anon1831b16c0111::lcl_setModified115     void operator() ( const lcl_tSequenceMap::value_type & rMapEntry )
116     {
117         // convert weak reference to reference
118         Reference< chart2::data::XDataSequence > xSeq( rMapEntry.second );
119         if( xSeq.is())
120         {
121             Reference< util::XModifiable > xMod( xSeq, uno::UNO_QUERY );
122             if( xMod.is())
123                 xMod->setModified( sal_True );
124         }
125     }
126 };
127 
128 struct lcl_internalizeSeries
129 {
130     typedef Reference< chart2::XDataSeries > argument_type;
131     typedef void result_type;
lcl_internalizeSerieschart::__anon1831b16c0111::lcl_internalizeSeries132     lcl_internalizeSeries( InternalData & rInternalData,
133                            InternalDataProvider & rProvider,
134                            bool bConnectToModel, bool bDataInColumns ) :
135             m_rInternalData( rInternalData ),
136             m_rProvider( rProvider ),
137             m_bConnectToModel( bConnectToModel ),
138             m_bDataInColumns( bDataInColumns )
139     {}
operator ()chart::__anon1831b16c0111::lcl_internalizeSeries140     void operator() ( const Reference< chart2::XDataSeries > & xSeries )
141     {
142         Reference< chart2::data::XDataSource > xSource( xSeries, uno::UNO_QUERY );
143         Reference< chart2::data::XDataSink >   xSink(   xSeries, uno::UNO_QUERY );
144         if( xSource.is() && xSink.is() )
145         {
146             Sequence< Reference< chart2::data::XLabeledDataSequence > > aOldSeriesData = xSource->getDataSequences();
147             Sequence< Reference< chart2::data::XLabeledDataSequence > > aNewSeriesData( aOldSeriesData.getLength() );
148             for( sal_Int32 i=0; i<aOldSeriesData.getLength(); ++i )
149             {
150                 sal_Int32 nNewIndex( m_bDataInColumns ? m_rInternalData.appendColumn() : m_rInternalData.appendRow() );
151                 OUString aIdentifier( OUString::valueOf( nNewIndex ));
152                 //@todo: deal also with genericXDataSequence
153                 Reference< chart2::data::XNumericalDataSequence > xValues( aOldSeriesData[i]->getValues(), uno::UNO_QUERY );
154                 Reference< chart2::data::XTextualDataSequence > xLabel( aOldSeriesData[i]->getLabel(), uno::UNO_QUERY );
155                 Reference< chart2::data::XDataSequence > xNewValues;
156 
157                 if( xValues.is() )
158                 {
159                     ::std::vector< double > aValues( ContainerHelper::SequenceToVector( xValues->getNumericalData()));
160                     if( m_bDataInColumns )
161                         m_rInternalData.setColumnValues( nNewIndex, aValues );
162                     else
163                         m_rInternalData.setRowValues( nNewIndex, aValues );
164                     if( m_bConnectToModel )
165                     {
166                         xNewValues.set( m_rProvider.createDataSequenceByRangeRepresentation( aIdentifier ));
167                         comphelper::copyProperties(
168                             Reference< beans::XPropertySet >( xValues, uno::UNO_QUERY ),
169                             Reference< beans::XPropertySet >( xNewValues, uno::UNO_QUERY ));
170                     }
171                 }
172 
173                 if( xLabel.is() )
174                 {
175                     if( m_bDataInColumns )
176                         m_rInternalData.setComplexColumnLabel( nNewIndex, ContainerHelper::SequenceToVector( lcl_StringToAnySequence( xLabel->getTextualData() ) ) );
177                     else
178                         m_rInternalData.setComplexRowLabel( nNewIndex, ContainerHelper::SequenceToVector( lcl_StringToAnySequence( xLabel->getTextualData() ) ) );
179                     if( m_bConnectToModel )
180                     {
181                         Reference< chart2::data::XDataSequence > xNewLabel(
182                             m_rProvider.createDataSequenceByRangeRepresentation( lcl_aLabelRangePrefix + aIdentifier ));
183                         comphelper::copyProperties(
184                             Reference< beans::XPropertySet >( xLabel, uno::UNO_QUERY ),
185                             Reference< beans::XPropertySet >( xNewLabel, uno::UNO_QUERY ));
186                         aNewSeriesData[i] = Reference< chart2::data::XLabeledDataSequence >(
187                                 new LabeledDataSequence( xNewValues, xNewLabel ));
188                     }
189                 }
190                 else
191                 {
192                     if( m_bConnectToModel )
193                         aNewSeriesData[i] = Reference< chart2::data::XLabeledDataSequence >(
194                             new LabeledDataSequence( xNewValues ));
195                 }
196             }
197             if( m_bConnectToModel )
198                 xSink->setData( aNewSeriesData );
199         }
200      }
201 
202 private:
203     InternalData &          m_rInternalData;
204     InternalDataProvider &  m_rProvider;
205     bool                    m_bConnectToModel;
206     bool                    m_bDataInColumns;
207 };
208 
209 struct lcl_copyFromLevel
210 {
211     typedef vector< uno::Any > argument_type;
212     typedef uno::Any result_type;
213 public:
214 
lcl_copyFromLevelchart::__anon1831b16c0111::lcl_copyFromLevel215     explicit lcl_copyFromLevel( sal_Int32 nLevel ) : m_nLevel( nLevel )
216     {}
217 
operator ()chart::__anon1831b16c0111::lcl_copyFromLevel218     uno::Any operator() ( const vector< uno::Any >& rVector )
219     {
220         uno::Any aRet;
221         if( m_nLevel <  static_cast< sal_Int32 >(rVector.size()) )
222             aRet = rVector[m_nLevel];
223         return aRet;
224     }
225 
226 private:
227     sal_Int32 m_nLevel;
228 };
229 
230 struct lcl_getStringFromLevelVector
231 {
232     typedef vector< uno::Any > argument_type;
233     typedef OUString result_type;
234 public:
235 
lcl_getStringFromLevelVectorchart::__anon1831b16c0111::lcl_getStringFromLevelVector236     explicit lcl_getStringFromLevelVector( sal_Int32 nLevel ) : m_nLevel( nLevel )
237     {}
238 
operator ()chart::__anon1831b16c0111::lcl_getStringFromLevelVector239     OUString operator() ( const vector< uno::Any >& rVector )
240     {
241         OUString aString;
242         if( m_nLevel < static_cast< sal_Int32 >(rVector.size()) )
243             aString = CommonFunctors::AnyToString()(rVector[m_nLevel]);
244         return aString;
245     }
246 
247 private:
248     sal_Int32 m_nLevel;
249 };
250 
251 
252 struct lcl_setAnyAtLevel
253 {
254     typedef vector< uno::Any > first_argument_type;
255     typedef uno::Any second_argument_type;
256     typedef vector< uno::Any > result_type;
257 public:
258 
lcl_setAnyAtLevelchart::__anon1831b16c0111::lcl_setAnyAtLevel259     explicit lcl_setAnyAtLevel( sal_Int32 nLevel ) : m_nLevel( nLevel )
260     {}
261 
operator ()chart::__anon1831b16c0111::lcl_setAnyAtLevel262     vector< uno::Any > operator() ( const vector< uno::Any >& rVector, const uno::Any& rNewValue )
263     {
264         vector< uno::Any > aRet( rVector );
265         if( m_nLevel >= static_cast< sal_Int32 >(aRet.size()) )
266             aRet.resize( m_nLevel+1 );
267         aRet[ m_nLevel ]=rNewValue;
268         return aRet;
269     }
270 
271 private:
272     sal_Int32 m_nLevel;
273 };
274 
275 struct lcl_setAnyAtLevelFromStringSequence
276 {
277     typedef vector< uno::Any > first_argument_type;
278     typedef OUString second_argument_type;
279     typedef vector< uno::Any > result_type;
280 public:
281 
lcl_setAnyAtLevelFromStringSequencechart::__anon1831b16c0111::lcl_setAnyAtLevelFromStringSequence282     explicit lcl_setAnyAtLevelFromStringSequence( sal_Int32 nLevel ) : m_nLevel( nLevel )
283     {}
284 
operator ()chart::__anon1831b16c0111::lcl_setAnyAtLevelFromStringSequence285     vector< uno::Any > operator() ( const vector< uno::Any >& rVector, const OUString& rNewValue )
286     {
287         vector< uno::Any > aRet( rVector );
288         if( m_nLevel >= static_cast< sal_Int32 >(aRet.size()) )
289             aRet.resize( m_nLevel+1 );
290         aRet[ m_nLevel ]=uno::makeAny(rNewValue);
291         return aRet;
292     }
293 
294 private:
295     sal_Int32 m_nLevel;
296 };
297 
298 struct lcl_insertAnyAtLevel
299 {
300     typedef vector< uno::Any > argument_type;
301     typedef void result_type;
302 public:
303 
lcl_insertAnyAtLevelchart::__anon1831b16c0111::lcl_insertAnyAtLevel304     explicit lcl_insertAnyAtLevel( sal_Int32 nLevel ) : m_nLevel( nLevel )
305     {}
306 
operator ()chart::__anon1831b16c0111::lcl_insertAnyAtLevel307     void operator() ( vector< uno::Any >& rVector )
308     {
309         if( m_nLevel > static_cast< sal_Int32 >(rVector.size()) )
310             rVector.resize( m_nLevel );
311 
312         vector< uno::Any >::iterator aIt( rVector.begin() );
313         for( sal_Int32 nN=0; aIt<rVector.end(); aIt++, nN++)
314         {
315             if( nN==m_nLevel )
316                 break;
317         }
318         rVector.insert( aIt, uno::Any() );
319     }
320 
321 private:
322     sal_Int32 m_nLevel;
323 };
324 
325 struct lcl_removeAnyAtLevel
326 {
327     typedef vector< uno::Any > argument_type;
328     typedef void result_type;
329 public:
330 
lcl_removeAnyAtLevelchart::__anon1831b16c0111::lcl_removeAnyAtLevel331     explicit lcl_removeAnyAtLevel( sal_Int32 nLevel ) : m_nLevel( nLevel )
332     {}
333 
operator ()chart::__anon1831b16c0111::lcl_removeAnyAtLevel334     void operator() ( vector< uno::Any >& rVector )
335     {
336         vector< uno::Any >::iterator aIt( rVector.begin() );
337         for( sal_Int32 nN=0; aIt<rVector.end(); aIt++, nN++)
338         {
339             if( nN==m_nLevel )
340             {
341                 rVector.erase( aIt );
342                 break;
343             }
344         }
345     }
346 
347 private:
348     sal_Int32 m_nLevel;
349 };
350 
351 } // anonymous namespace
352 
353 // ================================================================================
354 
InternalDataProvider(const Reference<uno::XComponentContext> &)355 InternalDataProvider::InternalDataProvider( const Reference< uno::XComponentContext > & /*_xContext*/)
356     : m_bDataInColumns( true )
357 {}
358 
InternalDataProvider(const Reference<chart2::XChartDocument> & xChartDoc,bool bConnectToModel,bool bDefaultDataInColumns)359 InternalDataProvider::InternalDataProvider(
360     const Reference< chart2::XChartDocument > & xChartDoc,
361     bool bConnectToModel,
362     bool bDefaultDataInColumns)
363 :   m_bDataInColumns( bDefaultDataInColumns )
364 {
365     try
366     {
367         Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ) );
368         if( xDiagram.is())
369         {
370             Reference< frame::XModel > xChartModel( xChartDoc, uno::UNO_QUERY );
371 
372             //data in columns?
373             {
374                 ::rtl::OUString aRangeString;
375                 bool bFirstCellAsLabel = true;
376                 bool bHasCategories = true;
377                 uno::Sequence< sal_Int32 > aSequenceMapping;
378                 const bool bSomethingDetected(
379                     DataSourceHelper::detectRangeSegmentation(
380                         xChartModel, aRangeString, aSequenceMapping, m_bDataInColumns, bFirstCellAsLabel, bHasCategories ));
381 
382                 // #120559# if no data was available, restore default
383                 if(!bSomethingDetected && m_bDataInColumns != bDefaultDataInColumns)
384                 {
385                     m_bDataInColumns = bDefaultDataInColumns;
386                 }
387             }
388 
389             // categories
390             {
391                 vector< vector< uno::Any > > aNewCategories;//inner count is level
392                 {
393                     ExplicitCategoriesProvider aExplicitCategoriesProvider( ChartModelHelper::getFirstCoordinateSystem(xChartModel), xChartModel );
394 
395                     const Sequence< Reference< chart2::data::XLabeledDataSequence> >& rSplitCategoriesList( aExplicitCategoriesProvider.getSplitCategoriesList() );
396                     sal_Int32 nLevelCount = rSplitCategoriesList.getLength();
397                     for( sal_Int32 nL = 0; nL<nLevelCount; nL++ )
398                     {
399                         Reference< chart2::data::XLabeledDataSequence > xLDS( rSplitCategoriesList[nL] );
400                         if( !xLDS.is() )
401                             continue;
402                         Sequence< uno::Any > aDataSeq;
403                         Reference< chart2::data::XDataSequence > xSeq( xLDS->getValues() );
404                         if( xSeq.is() )
405                             aDataSeq = xSeq->getData();
406                         sal_Int32 nLength = aDataSeq.getLength();
407                         sal_Int32 nCatLength = static_cast< sal_Int32 >(aNewCategories.size());
408                         if( nCatLength < nLength )
409                             aNewCategories.resize( nLength );
410                         else if( nLength < nCatLength )
411                             aDataSeq.realloc( nCatLength );
412                         transform( aNewCategories.begin(), aNewCategories.end(), aDataSeq.getConstArray(),
413                             aNewCategories.begin(), lcl_setAnyAtLevel(nL) );
414                     }
415                     if( !nLevelCount )
416                     {
417                         Sequence< OUString > aSimplecategories = aExplicitCategoriesProvider.getSimpleCategories();
418                         sal_Int32 nLength = aSimplecategories.getLength();
419                         aNewCategories.reserve( nLength );
420                         for( sal_Int32 nN=0; nN<nLength; nN++)
421                         {
422                             vector< uno::Any > aVector(1);
423                             aVector[0] = uno::makeAny( aSimplecategories[nN] );
424                             aNewCategories.push_back( aVector );
425                         }
426                     }
427                 }
428 
429                 if( m_bDataInColumns )
430                     m_aInternalData.setComplexRowLabels( aNewCategories );
431                 else
432                     m_aInternalData.setComplexColumnLabels( aNewCategories );
433                 if( bConnectToModel )
434                     DiagramHelper::setCategoriesToDiagram( new LabeledDataSequence(
435                         createDataSequenceByRangeRepresentation( lcl_aCategoriesRangeName )), xDiagram );
436             }
437 
438             // data series
439             ::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( ChartModelHelper::getDataSeries( xChartDoc ));
440             ::std::for_each( aSeriesVector.begin(), aSeriesVector.end(), lcl_internalizeSeries( m_aInternalData, *this, bConnectToModel, m_bDataInColumns ) );
441         }
442     }
443     catch( const uno::Exception & ex )
444     {
445         ASSERT_EXCEPTION( ex );
446     }
447 }
448 
449 // copy-CTOR
InternalDataProvider(const InternalDataProvider & rOther)450 InternalDataProvider::InternalDataProvider( const InternalDataProvider & rOther ) :
451         impl::InternalDataProvider_Base(),
452         m_aSequenceMap( rOther.m_aSequenceMap ),
453         m_aInternalData( rOther.m_aInternalData ),
454         m_bDataInColumns( rOther.m_bDataInColumns )
455 {}
456 
~InternalDataProvider()457 InternalDataProvider::~InternalDataProvider()
458 {}
459 
lcl_addDataSequenceToMap(const OUString & rRangeRepresentation,const Reference<chart2::data::XDataSequence> & xSequence)460 void InternalDataProvider::lcl_addDataSequenceToMap(
461     const OUString & rRangeRepresentation,
462     const Reference< chart2::data::XDataSequence > & xSequence )
463 {
464     m_aSequenceMap.insert(
465         tSequenceMap::value_type(
466             rRangeRepresentation,
467             uno::WeakReference< chart2::data::XDataSequence >( xSequence )));
468 }
469 
lcl_deleteMapReferences(const OUString & rRangeRepresentation)470 void InternalDataProvider::lcl_deleteMapReferences( const OUString & rRangeRepresentation )
471 {
472     // set sequence to deleted by setting its range to an empty string
473     tSequenceMapRange aRange( m_aSequenceMap.equal_range( rRangeRepresentation ));
474     for( tSequenceMap::iterator aIt( aRange.first ); aIt != aRange.second; ++aIt )
475     {
476         Reference< chart2::data::XDataSequence > xSeq( aIt->second );
477         if( xSeq.is())
478         {
479             Reference< container::XNamed > xNamed( xSeq, uno::UNO_QUERY );
480             if( xNamed.is())
481                 xNamed->setName( OUString());
482         }
483     }
484     // remove from map
485     m_aSequenceMap.erase( aRange.first, aRange.second );
486 }
487 
lcl_adaptMapReferences(const OUString & rOldRangeRepresentation,const OUString & rNewRangeRepresentation)488 void InternalDataProvider::lcl_adaptMapReferences(
489     const OUString & rOldRangeRepresentation,
490     const OUString & rNewRangeRepresentation )
491 {
492     tSequenceMapRange aRange( m_aSequenceMap.equal_range( rOldRangeRepresentation ));
493     tSequenceMap aNewElements;
494     for( tSequenceMap::iterator aIt( aRange.first ); aIt != aRange.second; ++aIt )
495     {
496         Reference< chart2::data::XDataSequence > xSeq( aIt->second );
497         if( xSeq.is())
498         {
499             Reference< container::XNamed > xNamed( xSeq, uno::UNO_QUERY );
500             if( xNamed.is())
501                 xNamed->setName( rNewRangeRepresentation );
502         }
503         aNewElements.insert( tSequenceMap::value_type( rNewRangeRepresentation, aIt->second ));
504     }
505     // erase map values for old index
506     m_aSequenceMap.erase( aRange.first, aRange.second );
507     // add new entries for values with new index
508     ::std::copy( aNewElements.begin(), aNewElements.end(),
509                  ::std::inserter( m_aSequenceMap,
510                                   m_aSequenceMap.upper_bound( rNewRangeRepresentation )));
511 }
512 
lcl_increaseMapReferences(sal_Int32 nBegin,sal_Int32 nEnd)513 void InternalDataProvider::lcl_increaseMapReferences(
514     sal_Int32 nBegin, sal_Int32 nEnd )
515 {
516     for( sal_Int32 nIndex = nEnd - 1; nIndex >= nBegin; --nIndex )
517     {
518         lcl_adaptMapReferences( OUString::valueOf( nIndex ),
519                             OUString::valueOf( nIndex + 1 ));
520         lcl_adaptMapReferences( lcl_aLabelRangePrefix + OUString::valueOf( nIndex ),
521                             lcl_aLabelRangePrefix + OUString::valueOf( nIndex + 1 ));
522     }
523 }
524 
lcl_decreaseMapReferences(sal_Int32 nBegin,sal_Int32 nEnd)525 void InternalDataProvider::lcl_decreaseMapReferences(
526     sal_Int32 nBegin, sal_Int32 nEnd )
527 {
528     for( sal_Int32 nIndex = nBegin; nIndex < nEnd; ++nIndex )
529     {
530         lcl_adaptMapReferences( OUString::valueOf( nIndex ),
531                             OUString::valueOf( nIndex - 1 ));
532         lcl_adaptMapReferences( lcl_aLabelRangePrefix + OUString::valueOf( nIndex ),
533                             lcl_aLabelRangePrefix + OUString::valueOf( nIndex - 1 ));
534     }
535 }
536 
lcl_createDataSequenceAndAddToMap(const OUString & rRangeRepresentation)537 Reference< chart2::data::XDataSequence > InternalDataProvider::lcl_createDataSequenceAndAddToMap(
538     const OUString & rRangeRepresentation )
539 {
540     Reference< chart2::data::XDataSequence > xSeq(
541         new UncachedDataSequence( this, rRangeRepresentation ));
542     lcl_addDataSequenceToMap( rRangeRepresentation, xSeq );
543     return xSeq;
544 }
545 
lcl_createDataSequenceAndAddToMap(const OUString & rRangeRepresentation,const OUString & rRole)546 Reference< chart2::data::XDataSequence > InternalDataProvider::lcl_createDataSequenceAndAddToMap(
547     const OUString & rRangeRepresentation,
548     const OUString & rRole )
549 {
550     Reference< chart2::data::XDataSequence > xSeq(
551         new UncachedDataSequence( this, rRangeRepresentation, rRole ));
552     lcl_addDataSequenceToMap( rRangeRepresentation, xSeq );
553     return xSeq;
554 }
555 
createDefaultData()556 void InternalDataProvider::createDefaultData()
557 {
558     m_aInternalData.createDefaultData();
559 }
560 
561 // ____ XDataProvider ____
createDataSourcePossible(const Sequence<beans::PropertyValue> &)562 ::sal_Bool SAL_CALL InternalDataProvider::createDataSourcePossible( const Sequence< beans::PropertyValue >& /* aArguments */ )
563 {
564     return true;
565 }
566 
567 namespace
568 {
569 
lcl_getInnerLevelCount(const vector<vector<uno::Any>> & rLabels)570 sal_Int32 lcl_getInnerLevelCount( const vector< vector< uno::Any > >& rLabels )
571 {
572     sal_Int32 nCount = 1;//minimum is 1!
573     vector< vector< uno::Any > >::const_iterator aLevelIt( rLabels.begin() );
574     vector< vector< uno::Any > >::const_iterator aLevelEnd( rLabels.end() );
575     for( ;aLevelIt!=aLevelEnd; ++aLevelIt )
576     {
577         const vector< uno::Any >& rCurrentLevelLabels = *aLevelIt;
578         nCount = std::max<sal_Int32>( rCurrentLevelLabels.size(), nCount );
579     }
580     return nCount;
581 }
582 
583 }//end anonymous namespace
584 
createDataSource(const Sequence<beans::PropertyValue> & aArguments)585 Reference< chart2::data::XDataSource > SAL_CALL InternalDataProvider::createDataSource(
586     const Sequence< beans::PropertyValue >& aArguments )
587 {
588     OUString aRangeRepresentation;
589     bool bUseColumns = true;
590     bool bFirstCellAsLabel = true;
591     bool bHasCategories = true;
592     uno::Sequence< sal_Int32 > aSequenceMapping;
593     DataSourceHelper::readArguments( aArguments, aRangeRepresentation, aSequenceMapping, bUseColumns, bFirstCellAsLabel, bHasCategories );
594 
595     if( aRangeRepresentation.equals( lcl_aCategoriesRangeName ) )
596     {
597         //return split complex categories if we have any:
598         ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aComplexCategories;
599         vector< vector< uno::Any > > aCategories( m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels());
600         if( bUseColumns==m_bDataInColumns )
601         {
602             sal_Int32 nLevelCount = lcl_getInnerLevelCount( aCategories );
603             for( sal_Int32 nL=0; nL<nLevelCount; nL++ )
604                 aComplexCategories.push_back( new LabeledDataSequence(
605                     new UncachedDataSequence( this
606                         , lcl_aCategoriesLevelRangeNamePrefix + OUString::valueOf( nL )
607                         , lcl_aCategoriesRoleName ) ) );
608         }
609         else
610         {
611             sal_Int32 nPointCount = m_bDataInColumns ? m_aInternalData.getRowCount() : m_aInternalData.getColumnCount();
612             for( sal_Int32 nP=0; nP<nPointCount; nP++ )
613                 aComplexCategories.push_back( new LabeledDataSequence(
614                     new UncachedDataSequence( this
615                         , lcl_aCategoriesPointRangeNamePrefix + OUString::valueOf( nP )
616                         , lcl_aCategoriesRoleName ) ) );
617         }
618         //don't add the created sequences to the map as they are used temporarily only ...
619         return new DataSource( ContainerHelper::ContainerToSequence(aComplexCategories) );
620     }
621 
622     OSL_ASSERT( aRangeRepresentation.equals( lcl_aCompleteRange ));
623 
624     ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aResultLSeqVec;
625 
626     // categories
627     if( bHasCategories )
628         aResultLSeqVec.push_back(
629             new LabeledDataSequence( lcl_createDataSequenceAndAddToMap( lcl_aCategoriesRangeName, lcl_aCategoriesRoleName ) ) );
630 
631     // data with labels
632     ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aDataVec;
633     const sal_Int32 nCount = (bUseColumns ? m_aInternalData.getColumnCount() : m_aInternalData.getRowCount());
634     for( sal_Int32 nIdx=0; nIdx<nCount; ++nIdx )
635     {
636         aDataVec.push_back(
637             new LabeledDataSequence(
638                 lcl_createDataSequenceAndAddToMap( OUString::valueOf( nIdx )),
639                 lcl_createDataSequenceAndAddToMap( lcl_aLabelRangePrefix + OUString::valueOf( nIdx ))));
640     }
641 
642     // attention: this data provider has the limitation that it stores
643     // internally if data comes from columns or rows. It is intended for
644     // creating only one used data source.
645     // @todo: add this information in the range representation strings
646     m_bDataInColumns = bUseColumns;
647 
648     //reorder labeled sequences according to aSequenceMapping; ignore categories
649     for( sal_Int32 nNewIndex = 0; nNewIndex < aSequenceMapping.getLength(); nNewIndex++ )
650     {
651         std::vector< LabeledDataSequence* >::size_type nOldIndex = aSequenceMapping[nNewIndex];
652         if( nOldIndex < aDataVec.size() )
653         {
654             if( aDataVec[nOldIndex].is() )
655             {
656                 aResultLSeqVec.push_back( aDataVec[nOldIndex] );
657                 aDataVec[nOldIndex] = 0;
658             }
659         }
660     }
661 
662     //add left over data sequences to result
663     ::std::vector< Reference< chart2::data::XLabeledDataSequence > >::iterator aIt(aDataVec.begin());
664     const ::std::vector< Reference< chart2::data::XLabeledDataSequence > >::const_iterator aEndIt(aDataVec.end());
665     for( ;aIt!=aEndIt; ++aIt)
666     {
667         if( aIt->is() )
668             aResultLSeqVec.push_back( *aIt );
669     }
670 
671     return new DataSource( ContainerHelper::ContainerToSequence(aResultLSeqVec) );
672 }
673 
detectArguments(const Reference<chart2::data::XDataSource> &)674 Sequence< beans::PropertyValue > SAL_CALL InternalDataProvider::detectArguments(
675     const Reference< chart2::data::XDataSource >& /* xDataSource */ )
676 {
677     Sequence< beans::PropertyValue > aArguments( 4 );
678     aArguments[0] = beans::PropertyValue(
679         C2U("CellRangeRepresentation"), -1, uno::makeAny( lcl_aCompleteRange ),
680         beans::PropertyState_DIRECT_VALUE );
681     aArguments[1] = beans::PropertyValue(
682         C2U("DataRowSource"), -1, uno::makeAny(
683             m_bDataInColumns
684             ? ::com::sun::star::chart::ChartDataRowSource_COLUMNS
685             : ::com::sun::star::chart::ChartDataRowSource_ROWS ),
686         beans::PropertyState_DIRECT_VALUE );
687     // internal data always contains labels and categories
688     aArguments[2] = beans::PropertyValue(
689         C2U("FirstCellAsLabel"), -1, uno::makeAny( true ), beans::PropertyState_DIRECT_VALUE );
690     aArguments[3] = beans::PropertyValue(
691         C2U("HasCategories"), -1, uno::makeAny( true ), beans::PropertyState_DIRECT_VALUE );
692 
693     // #i85913# Sequence Mapping is not needed for internal data, as it is
694     // applied to the data when the data source is created.
695 
696     return aArguments;
697 }
698 
createDataSequenceByRangeRepresentationPossible(const OUString &)699 ::sal_Bool SAL_CALL InternalDataProvider::createDataSequenceByRangeRepresentationPossible( const OUString& /* aRangeRepresentation */ )
700 {
701     return true;
702 }
703 
createDataSequenceByRangeRepresentation(const OUString & aRangeRepresentation)704 Reference< chart2::data::XDataSequence > SAL_CALL InternalDataProvider::createDataSequenceByRangeRepresentation(
705     const OUString& aRangeRepresentation )
706 {
707     if( aRangeRepresentation.match( lcl_aCategoriesRangeName ))
708     {
709         OSL_ASSERT( aRangeRepresentation.equals( lcl_aCategoriesRangeName ) );//it is not expected nor implemented that only parts of the categories are really requested
710 
711         // categories
712         return lcl_createDataSequenceAndAddToMap( lcl_aCategoriesRangeName, lcl_aCategoriesRoleName );
713     }
714     else if( aRangeRepresentation.match( lcl_aLabelRangePrefix ))
715     {
716         // label
717         sal_Int32 nIndex = aRangeRepresentation.copy( lcl_aLabelRangePrefix.getLength()).toInt32();
718         return lcl_createDataSequenceAndAddToMap( lcl_aLabelRangePrefix + OUString::valueOf( nIndex ));
719     }
720     else if( aRangeRepresentation.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "last" )))
721     {
722         sal_Int32 nIndex = (m_bDataInColumns
723                             ? m_aInternalData.getColumnCount()
724                             : m_aInternalData.getRowCount()) - 1;
725         return lcl_createDataSequenceAndAddToMap( OUString::valueOf( nIndex ));
726     }
727     else if( !aRangeRepresentation.isEmpty() )
728     {
729         // data
730         sal_Int32 nIndex = aRangeRepresentation.toInt32();
731         return lcl_createDataSequenceAndAddToMap( OUString::valueOf( nIndex ));
732     }
733 
734     return Reference< chart2::data::XDataSequence >();
735 }
736 
getRangeSelection()737 Reference< sheet::XRangeSelection > SAL_CALL InternalDataProvider::getRangeSelection()
738 {
739     // there is no range selection component
740     return Reference< sheet::XRangeSelection >();
741 }
742 
743 // ____ XInternalDataProvider ____
hasDataByRangeRepresentation(const OUString & aRange)744 ::sal_Bool SAL_CALL InternalDataProvider::hasDataByRangeRepresentation( const OUString& aRange )
745 {
746     sal_Bool bResult = false;
747 
748     if( aRange.match( lcl_aCategoriesRangeName ))
749     {
750         OSL_ASSERT( aRange.equals( lcl_aCategoriesRangeName ) );//it is not expected nor implemented that only parts of the categories are really requested
751         bResult = true;
752     }
753     else if( aRange.match( lcl_aLabelRangePrefix ))
754     {
755         sal_Int32 nIndex = aRange.copy( lcl_aLabelRangePrefix.getLength()).toInt32();
756         bResult = (nIndex < (m_bDataInColumns ? m_aInternalData.getColumnCount(): m_aInternalData.getRowCount()));
757     }
758     else
759     {
760         sal_Int32 nIndex = aRange.toInt32();
761         bResult = (nIndex < (m_bDataInColumns ? m_aInternalData.getColumnCount(): m_aInternalData.getRowCount()));
762     }
763 
764     return bResult;
765 }
766 
getDataByRangeRepresentation(const OUString & aRange)767 Sequence< uno::Any > SAL_CALL InternalDataProvider::getDataByRangeRepresentation( const OUString& aRange )
768 {
769     Sequence< uno::Any > aResult;
770 
771     if( aRange.match( lcl_aLabelRangePrefix ) )
772     {
773         sal_Int32 nIndex = aRange.copy( lcl_aLabelRangePrefix.getLength()).toInt32();
774         vector< uno::Any > aComplexLabel = m_bDataInColumns
775             ? m_aInternalData.getComplexColumnLabel( nIndex )
776             : m_aInternalData.getComplexRowLabel( nIndex );
777         if( !aComplexLabel.empty() )
778             aResult = ContainerHelper::ContainerToSequence(aComplexLabel);
779     }
780     else if( aRange.match( lcl_aCategoriesPointRangeNamePrefix ) )
781     {
782         sal_Int32 nPointIndex = aRange.copy( lcl_aCategoriesPointRangeNamePrefix.getLength() ).toInt32();
783         vector< uno::Any > aComplexCategory = m_bDataInColumns
784             ? m_aInternalData.getComplexRowLabel( nPointIndex )
785             : m_aInternalData.getComplexColumnLabel( nPointIndex );
786         if( !aComplexCategory.empty() )
787             aResult = ContainerHelper::ContainerToSequence(aComplexCategory);
788     }
789     else if( aRange.match( lcl_aCategoriesLevelRangeNamePrefix ) )
790     {
791         sal_Int32 nLevel = aRange.copy( lcl_aCategoriesLevelRangeNamePrefix.getLength() ).toInt32();
792         vector< vector< uno::Any > > aCategories( m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels());
793         if( nLevel < lcl_getInnerLevelCount( aCategories ) )
794         {
795             aResult.realloc( aCategories.size() );
796             transform( aCategories.begin(), aCategories.end(),
797                        aResult.getArray(), lcl_copyFromLevel(nLevel) );
798         }
799     }
800     else if( aRange.equals( lcl_aCategoriesRangeName ) )
801     {
802         vector< vector< uno::Any > > aCategories( m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels());
803         sal_Int32 nLevelCount = lcl_getInnerLevelCount( aCategories );
804         if( nLevelCount == 1 )
805         {
806             sal_Int32 nL=0;
807             aResult = this->getDataByRangeRepresentation( lcl_aCategoriesLevelRangeNamePrefix + OUString::valueOf( nL ) );
808         }
809         else
810         {
811             Sequence< OUString > aLabels = m_bDataInColumns ? this->getRowDescriptions() : this->getColumnDescriptions();
812             aResult.realloc( aLabels.getLength() );
813             transform( aLabels.getConstArray(), aLabels.getConstArray() + aLabels.getLength(),
814                        aResult.getArray(), CommonFunctors::makeAny< OUString >() );
815         }
816     }
817     else
818     {
819         sal_Int32 nIndex = aRange.toInt32();
820         if( nIndex >= 0 )
821         {
822             Sequence< double > aData;
823             if( m_bDataInColumns )
824                 aData = m_aInternalData.getColumnValues(nIndex);
825             else
826                 aData = m_aInternalData.getRowValues(nIndex);
827             if( aData.getLength() )
828             {
829                 aResult.realloc( aData.getLength());
830                 transform( aData.getConstArray(), aData.getConstArray() + aData.getLength(),
831                            aResult.getArray(), CommonFunctors::makeAny< double >());
832             }
833         }
834     }
835 
836     return aResult;
837 }
838 
setDataByRangeRepresentation(const OUString & aRange,const Sequence<uno::Any> & aNewData)839 void SAL_CALL InternalDataProvider::setDataByRangeRepresentation(
840     const OUString& aRange, const Sequence< uno::Any >& aNewData )
841 {
842     vector< uno::Any > aNewVector( ContainerHelper::SequenceToVector(aNewData) );
843     if( aRange.match( lcl_aLabelRangePrefix ) )
844     {
845         sal_uInt32 nIndex = aRange.copy( lcl_aLabelRangePrefix.getLength()).toInt32();
846         if( m_bDataInColumns )
847             m_aInternalData.setComplexColumnLabel( nIndex, aNewVector );
848         else
849             m_aInternalData.setComplexRowLabel( nIndex, aNewVector );
850     }
851     else if( aRange.match( lcl_aCategoriesPointRangeNamePrefix ) )
852     {
853         sal_Int32 nPointIndex = aRange.copy( lcl_aCategoriesLevelRangeNamePrefix.getLength()).toInt32();
854         if( m_bDataInColumns )
855             m_aInternalData.setComplexRowLabel( nPointIndex, aNewVector );
856         else
857             m_aInternalData.setComplexColumnLabel( nPointIndex, aNewVector );
858     }
859     else if( aRange.match( lcl_aCategoriesLevelRangeNamePrefix ) )
860     {
861         sal_Int32 nLevel = aRange.copy( lcl_aCategoriesLevelRangeNamePrefix.getLength()).toInt32();
862         vector< vector< uno::Any > > aComplexCategories = m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels();
863 
864         //ensure equal length
865         if( aNewVector.size() > aComplexCategories.size() )
866             aComplexCategories.resize( aNewVector.size() );
867         else if( aNewVector.size() < aComplexCategories.size() )
868             aNewVector.resize( aComplexCategories.size() );
869 
870         transform( aComplexCategories.begin(), aComplexCategories.end(), aNewVector.begin(),
871                    aComplexCategories.begin(), lcl_setAnyAtLevel(nLevel) );
872 
873         if( m_bDataInColumns )
874             m_aInternalData.setComplexRowLabels( aComplexCategories );
875         else
876             m_aInternalData.setComplexColumnLabels( aComplexCategories );
877     }
878     else if( aRange.equals( lcl_aCategoriesRangeName ) )
879     {
880         vector< vector< uno::Any > > aComplexCategories;
881         aComplexCategories.resize( aNewVector.size() );
882         transform( aComplexCategories.begin(), aComplexCategories.end(), aNewVector.begin(),
883                             aComplexCategories.begin(), lcl_setAnyAtLevel(0) );
884         if( m_bDataInColumns )
885             m_aInternalData.setComplexRowLabels( aComplexCategories );
886         else
887             m_aInternalData.setComplexColumnLabels( aComplexCategories );
888     }
889     else
890     {
891         sal_Int32 nIndex = aRange.toInt32();
892         if( nIndex>=0 )
893         {
894             vector< double > aNewDataVec;
895             transform( aNewData.getConstArray(), aNewData.getConstArray() + aNewData.getLength(),
896                        back_inserter( aNewDataVec ), CommonFunctors::AnyToDouble());
897             if( m_bDataInColumns )
898                 m_aInternalData.setColumnValues( nIndex, aNewDataVec );
899             else
900                 m_aInternalData.setRowValues( nIndex, aNewDataVec );
901         }
902     }
903 }
904 
insertSequence(::sal_Int32 nAfterIndex)905 void SAL_CALL InternalDataProvider::insertSequence( ::sal_Int32 nAfterIndex )
906 {
907     if( m_bDataInColumns )
908     {
909         lcl_increaseMapReferences( nAfterIndex + 1, m_aInternalData.getColumnCount());
910         m_aInternalData.insertColumn( nAfterIndex );
911     }
912     else
913     {
914         lcl_increaseMapReferences( nAfterIndex + 1, m_aInternalData.getRowCount());
915         m_aInternalData.insertRow( nAfterIndex );
916     }
917 }
918 
deleteSequence(::sal_Int32 nAtIndex)919 void SAL_CALL InternalDataProvider::deleteSequence( ::sal_Int32 nAtIndex )
920 {
921     lcl_deleteMapReferences( OUString::valueOf( nAtIndex ));
922     lcl_deleteMapReferences( lcl_aLabelRangePrefix + OUString::valueOf( nAtIndex ));
923     if( m_bDataInColumns )
924     {
925         lcl_decreaseMapReferences( nAtIndex + 1, m_aInternalData.getColumnCount());
926         m_aInternalData.deleteColumn( nAtIndex );
927     }
928     else
929     {
930         lcl_decreaseMapReferences( nAtIndex + 1, m_aInternalData.getRowCount());
931         m_aInternalData.deleteRow( nAtIndex );
932     }
933 }
934 
appendSequence()935 void SAL_CALL InternalDataProvider::appendSequence()
936 {
937     if( m_bDataInColumns )
938         m_aInternalData.appendColumn();
939     else
940         m_aInternalData.appendRow();
941 }
942 
insertComplexCategoryLevel(sal_Int32 nLevel)943 void SAL_CALL InternalDataProvider::insertComplexCategoryLevel( sal_Int32 nLevel )
944 {
945     OSL_ENSURE( nLevel> 0, "you can only insert category levels > 0" );//the first categories level cannot be deleted, check the calling code for error
946     if( nLevel>0 )
947     {
948         vector< vector< uno::Any > > aComplexCategories = m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels();
949         ::std::for_each( aComplexCategories.begin(), aComplexCategories.end(), lcl_insertAnyAtLevel(nLevel) );
950         if( m_bDataInColumns )
951             m_aInternalData.setComplexRowLabels( aComplexCategories );
952         else
953             m_aInternalData.setComplexColumnLabels( aComplexCategories );
954 
955         tSequenceMapRange aRange( m_aSequenceMap.equal_range( lcl_aCategoriesRangeName ));
956         ::std::for_each( aRange.first, aRange.second, lcl_setModified());
957     }
958 }
deleteComplexCategoryLevel(sal_Int32 nLevel)959 void SAL_CALL InternalDataProvider::deleteComplexCategoryLevel( sal_Int32 nLevel )
960 {
961     OSL_ENSURE( nLevel>0, "you can only delete category levels > 0" );//the first categories level cannot be deleted, check the calling code for error
962     if( nLevel>0 )
963     {
964         vector< vector< uno::Any > > aComplexCategories = m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels();
965         ::std::for_each( aComplexCategories.begin(), aComplexCategories.end(), lcl_removeAnyAtLevel(nLevel) );
966         if( m_bDataInColumns )
967             m_aInternalData.setComplexRowLabels( aComplexCategories );
968         else
969             m_aInternalData.setComplexColumnLabels( aComplexCategories );
970 
971         tSequenceMapRange aRange( m_aSequenceMap.equal_range( lcl_aCategoriesRangeName ));
972         ::std::for_each( aRange.first, aRange.second, lcl_setModified());
973     }
974 }
975 
insertDataPointForAllSequences(::sal_Int32 nAfterIndex)976 void SAL_CALL InternalDataProvider::insertDataPointForAllSequences( ::sal_Int32 nAfterIndex )
977 {
978     sal_Int32 nMaxRep = 0;
979     if( m_bDataInColumns )
980     {
981         m_aInternalData.insertRow( nAfterIndex );
982         nMaxRep = m_aInternalData.getColumnCount();
983     }
984     else
985     {
986         m_aInternalData.insertColumn( nAfterIndex );
987         nMaxRep = m_aInternalData.getRowCount();
988     }
989 
990     // notify change to all affected ranges
991     tSequenceMap::const_iterator aBegin( m_aSequenceMap.lower_bound( C2U("0")));
992     tSequenceMap::const_iterator aEnd( m_aSequenceMap.upper_bound( OUString::valueOf( nMaxRep )));
993     ::std::for_each( aBegin, aEnd, lcl_setModified());
994 
995     tSequenceMapRange aRange( m_aSequenceMap.equal_range( lcl_aCategoriesRangeName ));
996     ::std::for_each( aRange.first, aRange.second, lcl_setModified());
997 }
998 
deleteDataPointForAllSequences(::sal_Int32 nAtIndex)999 void SAL_CALL InternalDataProvider::deleteDataPointForAllSequences( ::sal_Int32 nAtIndex )
1000 {
1001     sal_Int32 nMaxRep = 0;
1002     if( m_bDataInColumns )
1003     {
1004         m_aInternalData.deleteRow( nAtIndex );
1005         nMaxRep = m_aInternalData.getColumnCount();
1006     }
1007     else
1008     {
1009         m_aInternalData.deleteColumn( nAtIndex );
1010         nMaxRep = m_aInternalData.getRowCount();
1011     }
1012 
1013     // notify change to all affected ranges
1014     tSequenceMap::const_iterator aBegin( m_aSequenceMap.lower_bound( C2U("0")));
1015     tSequenceMap::const_iterator aEnd( m_aSequenceMap.upper_bound( OUString::valueOf( nMaxRep )));
1016     ::std::for_each( aBegin, aEnd, lcl_setModified());
1017 
1018     tSequenceMapRange aRange( m_aSequenceMap.equal_range( lcl_aCategoriesRangeName ));
1019     ::std::for_each( aRange.first, aRange.second, lcl_setModified());
1020 }
1021 
swapDataPointWithNextOneForAllSequences(::sal_Int32 nAtIndex)1022 void SAL_CALL InternalDataProvider::swapDataPointWithNextOneForAllSequences( ::sal_Int32 nAtIndex )
1023 {
1024     if( m_bDataInColumns )
1025         m_aInternalData.swapRowWithNext( nAtIndex );
1026     else
1027         m_aInternalData.swapColumnWithNext( nAtIndex );
1028     sal_Int32 nMaxRep = (m_bDataInColumns
1029                          ? m_aInternalData.getColumnCount()
1030                          : m_aInternalData.getRowCount());
1031 
1032     // notify change to all affected ranges
1033     tSequenceMap::const_iterator aBegin( m_aSequenceMap.lower_bound( C2U("0")));
1034     tSequenceMap::const_iterator aEnd( m_aSequenceMap.upper_bound( OUString::valueOf( nMaxRep )));
1035     ::std::for_each( aBegin, aEnd, lcl_setModified());
1036 
1037     tSequenceMapRange aRange( m_aSequenceMap.equal_range( lcl_aCategoriesRangeName ));
1038     ::std::for_each( aRange.first, aRange.second, lcl_setModified());
1039 }
1040 
registerDataSequenceForChanges(const Reference<chart2::data::XDataSequence> & xSeq)1041 void SAL_CALL InternalDataProvider::registerDataSequenceForChanges( const Reference< chart2::data::XDataSequence >& xSeq )
1042 {
1043     if( xSeq.is())
1044         lcl_addDataSequenceToMap( xSeq->getSourceRangeRepresentation(), xSeq );
1045 }
1046 
1047 
1048 // ____ XRangeXMLConversion ____
convertRangeToXML(const OUString & aRangeRepresentation)1049 OUString SAL_CALL InternalDataProvider::convertRangeToXML( const OUString& aRangeRepresentation )
1050 {
1051     XMLRangeHelper::CellRange aRange;
1052     aRange.aTableName = OUString(RTL_CONSTASCII_USTRINGPARAM("local-table"));
1053 
1054     // attention: this data provider has the limitation that it stores
1055     // internally if data comes from columns or rows. It is intended for
1056     // creating only one used data source.
1057     // @todo: add this information in the range representation strings
1058     if( aRangeRepresentation.match( lcl_aCategoriesRangeName ))
1059     {
1060         OSL_ASSERT( aRangeRepresentation.equals( lcl_aCategoriesRangeName ) );//it is not expected nor implemented that only parts of the categories are really requested
1061         aRange.aUpperLeft.bIsEmpty = false;
1062         if( m_bDataInColumns )
1063         {
1064             aRange.aUpperLeft.nColumn = 0;
1065             aRange.aUpperLeft.nRow = 1;
1066             aRange.aLowerRight = aRange.aUpperLeft;
1067             aRange.aLowerRight.nRow = m_aInternalData.getRowCount();
1068         }
1069         else
1070         {
1071             aRange.aUpperLeft.nColumn = 1;
1072             aRange.aUpperLeft.nRow = 0;
1073             aRange.aLowerRight = aRange.aUpperLeft;
1074             aRange.aLowerRight.nColumn = m_aInternalData.getColumnCount();
1075         }
1076     }
1077     else if( aRangeRepresentation.match( lcl_aLabelRangePrefix ))
1078     {
1079         sal_Int32 nIndex = aRangeRepresentation.copy( lcl_aLabelRangePrefix.getLength()).toInt32();
1080         aRange.aUpperLeft.bIsEmpty = false;
1081         aRange.aLowerRight.bIsEmpty = true;
1082         if( m_bDataInColumns )
1083         {
1084             aRange.aUpperLeft.nColumn = nIndex + 1;
1085             aRange.aUpperLeft.nRow = 0;
1086         }
1087         else
1088         {
1089             aRange.aUpperLeft.nColumn = 0;
1090             aRange.aUpperLeft.nRow = nIndex + 1;
1091         }
1092     }
1093     else if( aRangeRepresentation.equals( lcl_aCompleteRange ))
1094     {
1095         aRange.aUpperLeft.bIsEmpty = false;
1096         aRange.aLowerRight.bIsEmpty = false;
1097         aRange.aUpperLeft.nColumn = 0;
1098         aRange.aUpperLeft.nRow = 0;
1099         aRange.aLowerRight.nColumn = m_aInternalData.getColumnCount();
1100         aRange.aLowerRight.nRow = m_aInternalData.getRowCount();
1101     }
1102     else
1103     {
1104         sal_Int32 nIndex = aRangeRepresentation.toInt32();
1105         aRange.aUpperLeft.bIsEmpty = false;
1106         if( m_bDataInColumns )
1107         {
1108             aRange.aUpperLeft.nColumn = nIndex + 1;
1109             aRange.aUpperLeft.nRow = 1;
1110             aRange.aLowerRight = aRange.aUpperLeft;
1111             aRange.aLowerRight.nRow = m_aInternalData.getRowCount();
1112         }
1113         else
1114         {
1115             aRange.aUpperLeft.nColumn = 1;
1116             aRange.aUpperLeft.nRow = nIndex + 1;
1117             aRange.aLowerRight = aRange.aUpperLeft;
1118             aRange.aLowerRight.nColumn = m_aInternalData.getColumnCount();
1119         }
1120     }
1121 
1122     return XMLRangeHelper::getXMLStringFromCellRange( aRange );
1123 }
1124 
convertRangeFromXML(const OUString & aXMLRange)1125 OUString SAL_CALL InternalDataProvider::convertRangeFromXML( const OUString& aXMLRange )
1126 {
1127     XMLRangeHelper::CellRange aRange( XMLRangeHelper::getCellRangeFromXMLString( aXMLRange ));
1128     if( aRange.aUpperLeft.bIsEmpty )
1129     {
1130         OSL_ENSURE( aRange.aLowerRight.bIsEmpty, "Weird Range" );
1131         return OUString();
1132     }
1133 
1134     // "all"
1135     if( !aRange.aLowerRight.bIsEmpty &&
1136         ( aRange.aUpperLeft.nColumn != aRange.aLowerRight.nColumn ) &&
1137         ( aRange.aUpperLeft.nRow != aRange.aLowerRight.nRow ) )
1138         return lcl_aCompleteRange;
1139 
1140     // attention: this data provider has the limitation that it stores
1141     // internally if data comes from columns or rows. It is intended for
1142     // creating only one used data source.
1143     // @todo: add this information in the range representation strings
1144 
1145     // data in columns
1146     if( m_bDataInColumns )
1147     {
1148         if( aRange.aUpperLeft.nColumn == 0 )
1149             return lcl_aCategoriesRangeName;
1150         if( aRange.aUpperLeft.nRow == 0 )
1151             return lcl_aLabelRangePrefix + OUString::valueOf( aRange.aUpperLeft.nColumn - 1 );
1152 
1153         return OUString::valueOf( aRange.aUpperLeft.nColumn - 1 );
1154     }
1155 
1156     // data in rows
1157     if( aRange.aUpperLeft.nRow == 0 )
1158         return lcl_aCategoriesRangeName;
1159     if( aRange.aUpperLeft.nColumn == 0 )
1160         return lcl_aLabelRangePrefix + OUString::valueOf( aRange.aUpperLeft.nRow - 1 );
1161 
1162     return OUString::valueOf( aRange.aUpperLeft.nRow - 1 );
1163 }
1164 
1165 namespace
1166 {
1167 
1168 template< class Type >
lcl_convertVectorVectorToSequenceSequence(const vector<vector<Type>> & rIn)1169 Sequence< Sequence< Type > > lcl_convertVectorVectorToSequenceSequence( const vector< vector< Type > >& rIn )
1170 {
1171     Sequence< Sequence< Type > > aRet;
1172     sal_Int32 nOuterCount = rIn.size();
1173     if( nOuterCount )
1174     {
1175         aRet.realloc(nOuterCount);
1176         for( sal_Int32 nN=0; nN<nOuterCount; nN++)
1177             aRet[nN]= ContainerHelper::ContainerToSequence( rIn[nN] );
1178     }
1179     return aRet;
1180 }
1181 
1182 template< class Type >
lcl_convertSequenceSequenceToVectorVector(const Sequence<Sequence<Type>> & rIn)1183 vector< vector< Type > > lcl_convertSequenceSequenceToVectorVector( const Sequence< Sequence< Type > >& rIn )
1184 {
1185     vector< vector< Type > > aRet;
1186     sal_Int32 nOuterCount = rIn.getLength();
1187     if( nOuterCount )
1188     {
1189         aRet.resize(nOuterCount);
1190         for( sal_Int32 nN=0; nN<nOuterCount; nN++)
1191             aRet[nN]= ContainerHelper::SequenceToVector( rIn[nN] );
1192     }
1193     return aRet;
1194 }
1195 
lcl_convertComplexAnyVectorToStringSequence(const vector<vector<uno::Any>> & rIn)1196 Sequence< Sequence< OUString > > lcl_convertComplexAnyVectorToStringSequence( const vector< vector< uno::Any > >& rIn )
1197 {
1198     Sequence< Sequence< OUString > > aRet;
1199     sal_Int32 nOuterCount = rIn.size();
1200     if( nOuterCount )
1201     {
1202         aRet.realloc(nOuterCount);
1203         for( sal_Int32 nN=0; nN<nOuterCount; nN++)
1204             aRet[nN]= lcl_AnyToStringSequence( ContainerHelper::ContainerToSequence( rIn[nN] ) );
1205     }
1206     return aRet;
1207 }
1208 
lcl_convertComplexStringSequenceToAnyVector(const Sequence<Sequence<OUString>> & rIn)1209 vector< vector< uno::Any > > lcl_convertComplexStringSequenceToAnyVector( const Sequence< Sequence< OUString > >& rIn )
1210 {
1211     vector< vector< uno::Any > > aRet;
1212     sal_Int32 nOuterCount = rIn.getLength();
1213     for( sal_Int32 nN=0; nN<nOuterCount; nN++)
1214         aRet.push_back( ContainerHelper::SequenceToVector( lcl_StringToAnySequence( rIn[nN] ) ) );
1215     return aRet;
1216 }
1217 
1218 class SplitCategoriesProvider_ForComplexDescriptions : public SplitCategoriesProvider
1219 {
1220 public:
1221 
SplitCategoriesProvider_ForComplexDescriptions(const::std::vector<::std::vector<uno::Any>> & rComplexDescriptions)1222     explicit SplitCategoriesProvider_ForComplexDescriptions( const ::std::vector< ::std::vector< uno::Any > >& rComplexDescriptions )
1223         : m_rComplexDescriptions( rComplexDescriptions )
1224     {}
~SplitCategoriesProvider_ForComplexDescriptions()1225     virtual ~SplitCategoriesProvider_ForComplexDescriptions()
1226     {}
1227 
1228     virtual sal_Int32 getLevelCount() const;
1229     virtual uno::Sequence< rtl::OUString > getStringsForLevel( sal_Int32 nIndex ) const;
1230 
1231 private:
1232     const ::std::vector< ::std::vector< uno::Any > >& m_rComplexDescriptions;
1233 };
1234 
getLevelCount() const1235 sal_Int32 SplitCategoriesProvider_ForComplexDescriptions::getLevelCount() const
1236 {
1237     return lcl_getInnerLevelCount( m_rComplexDescriptions );
1238 }
getStringsForLevel(sal_Int32 nLevel) const1239 uno::Sequence< rtl::OUString > SplitCategoriesProvider_ForComplexDescriptions::getStringsForLevel( sal_Int32 nLevel ) const
1240 {
1241     uno::Sequence< rtl::OUString > aResult;
1242     if( nLevel < lcl_getInnerLevelCount( m_rComplexDescriptions ) )
1243     {
1244         aResult.realloc( m_rComplexDescriptions.size() );
1245         transform( m_rComplexDescriptions.begin(), m_rComplexDescriptions.end(),
1246                    aResult.getArray(), lcl_getStringFromLevelVector(nLevel) );
1247     }
1248     return aResult;
1249 }
1250 
1251 }//anonymous namespace
1252 
1253 // ____ XDateCategories ____
getDateCategories()1254 Sequence< double > SAL_CALL InternalDataProvider::getDateCategories()
1255 {
1256     double fNan = InternalDataProvider::getNotANumber();
1257     double fValue = fNan;
1258     vector< vector< uno::Any > > aCategories( m_bDataInColumns ? m_aInternalData.getComplexRowLabels() : m_aInternalData.getComplexColumnLabels());
1259     sal_Int32 nCount = aCategories.size();
1260     Sequence< double > aDoubles( nCount );
1261     vector< vector< uno::Any > >::iterator aIt( aCategories.begin() );
1262     vector< vector< uno::Any > >::const_iterator aEnd( aCategories.end() );
1263     for(sal_Int32 nN=0; nN<nCount && aIt!=aEnd; ++nN, ++aIt )
1264     {
1265         if( !( !aIt->empty() && ((*aIt)[0]>>=fValue) ) )
1266             fValue = fNan;
1267         aDoubles[nN]=fValue;
1268     }
1269     return aDoubles;
1270 }
1271 
setDateCategories(const Sequence<double> & rDates)1272 void SAL_CALL InternalDataProvider::setDateCategories( const Sequence< double >& rDates )
1273 {
1274     sal_Int32 nCount = rDates.getLength();
1275     vector< vector< uno::Any > > aNewCategories;
1276     aNewCategories.reserve(nCount);
1277     vector< uno::Any > aSingleLabel(1);
1278 
1279     for(sal_Int32 nN=0; nN<nCount; ++nN )
1280     {
1281         aSingleLabel[0]=uno::makeAny(rDates[nN]);
1282         aNewCategories.push_back(aSingleLabel);
1283     }
1284 
1285     if( m_bDataInColumns )
1286         m_aInternalData.setComplexRowLabels( aNewCategories );
1287     else
1288         m_aInternalData.setComplexColumnLabels( aNewCategories );
1289 }
1290 
1291 // ____ XAnyDescriptionAccess ____
getAnyRowDescriptions()1292 Sequence< Sequence< uno::Any > > SAL_CALL InternalDataProvider::getAnyRowDescriptions()
1293 {
1294     return lcl_convertVectorVectorToSequenceSequence( m_aInternalData.getComplexRowLabels() );
1295 }
setAnyRowDescriptions(const Sequence<Sequence<uno::Any>> & aRowDescriptions)1296 void SAL_CALL InternalDataProvider::setAnyRowDescriptions( const Sequence< Sequence< uno::Any > >& aRowDescriptions )
1297 {
1298     m_aInternalData.setComplexRowLabels( lcl_convertSequenceSequenceToVectorVector( aRowDescriptions ) );
1299 }
getAnyColumnDescriptions()1300 Sequence< Sequence< uno::Any > > SAL_CALL InternalDataProvider::getAnyColumnDescriptions()
1301 {
1302     return lcl_convertVectorVectorToSequenceSequence( m_aInternalData.getComplexColumnLabels() );
1303 }
setAnyColumnDescriptions(const Sequence<Sequence<uno::Any>> & aColumnDescriptions)1304 void SAL_CALL InternalDataProvider::setAnyColumnDescriptions( const Sequence< Sequence< uno::Any > >& aColumnDescriptions )
1305 {
1306     m_aInternalData.setComplexColumnLabels( lcl_convertSequenceSequenceToVectorVector( aColumnDescriptions ) );
1307 }
1308 
1309 // ____ XComplexDescriptionAccess ____
getComplexRowDescriptions()1310 Sequence< Sequence< OUString > > SAL_CALL InternalDataProvider::getComplexRowDescriptions()
1311 {
1312     return lcl_convertComplexAnyVectorToStringSequence( m_aInternalData.getComplexRowLabels() );
1313 }
setComplexRowDescriptions(const Sequence<Sequence<::rtl::OUString>> & aRowDescriptions)1314 void SAL_CALL InternalDataProvider::setComplexRowDescriptions( const Sequence< Sequence< ::rtl::OUString > >& aRowDescriptions )
1315 {
1316     m_aInternalData.setComplexRowLabels( lcl_convertComplexStringSequenceToAnyVector(aRowDescriptions) );
1317 }
getComplexColumnDescriptions()1318 Sequence< Sequence< ::rtl::OUString > > SAL_CALL InternalDataProvider::getComplexColumnDescriptions()
1319 {
1320     return lcl_convertComplexAnyVectorToStringSequence( m_aInternalData.getComplexColumnLabels() );
1321 }
setComplexColumnDescriptions(const Sequence<Sequence<::rtl::OUString>> & aColumnDescriptions)1322 void SAL_CALL InternalDataProvider::setComplexColumnDescriptions( const Sequence< Sequence< ::rtl::OUString > >& aColumnDescriptions )
1323 {
1324     m_aInternalData.setComplexColumnLabels( lcl_convertComplexStringSequenceToAnyVector(aColumnDescriptions) );
1325 }
1326 
1327 // ____ XChartDataArray ____
getData()1328 Sequence< Sequence< double > > SAL_CALL InternalDataProvider::getData()
1329 {
1330     return m_aInternalData.getData();
1331 }
1332 
setData(const Sequence<Sequence<double>> & rDataInRows)1333 void SAL_CALL InternalDataProvider::setData( const Sequence< Sequence< double > >& rDataInRows )
1334 {
1335     return m_aInternalData.setData( rDataInRows );
1336 }
1337 
setRowDescriptions(const Sequence<OUString> & aRowDescriptions)1338 void SAL_CALL InternalDataProvider::setRowDescriptions( const Sequence< OUString >& aRowDescriptions )
1339 {
1340     vector< vector< uno::Any > > aComplexDescriptions( aRowDescriptions.getLength() );
1341     transform( aComplexDescriptions.begin(), aComplexDescriptions.end(), aRowDescriptions.getConstArray(),
1342                aComplexDescriptions.begin(), lcl_setAnyAtLevelFromStringSequence(0) );
1343     m_aInternalData.setComplexRowLabels( aComplexDescriptions );
1344 }
1345 
setColumnDescriptions(const Sequence<OUString> & aColumnDescriptions)1346 void SAL_CALL InternalDataProvider::setColumnDescriptions( const Sequence< OUString >& aColumnDescriptions )
1347 {
1348     vector< vector< uno::Any > > aComplexDescriptions( aColumnDescriptions.getLength() );
1349     transform( aComplexDescriptions.begin(), aComplexDescriptions.end(), aColumnDescriptions.getConstArray(),
1350                aComplexDescriptions.begin(), lcl_setAnyAtLevelFromStringSequence(0) );
1351     m_aInternalData.setComplexColumnLabels( aComplexDescriptions );
1352 }
1353 
getRowDescriptions()1354 Sequence< OUString > SAL_CALL InternalDataProvider::getRowDescriptions()
1355 {
1356     vector< vector< uno::Any > > aComplexLabels( m_aInternalData.getComplexRowLabels() );
1357     SplitCategoriesProvider_ForComplexDescriptions aProvider( aComplexLabels );
1358     return ExplicitCategoriesProvider::getExplicitSimpleCategories( aProvider );
1359 }
1360 
getColumnDescriptions()1361 Sequence< OUString > SAL_CALL InternalDataProvider::getColumnDescriptions()
1362 {
1363     vector< vector< uno::Any > > aComplexLabels( m_aInternalData.getComplexColumnLabels() );
1364     SplitCategoriesProvider_ForComplexDescriptions aProvider( aComplexLabels );
1365     return ExplicitCategoriesProvider::getExplicitSimpleCategories( aProvider );
1366 }
1367 
1368 // ____ XChartData (base of XChartDataArray) ____
addChartDataChangeEventListener(const Reference<::com::sun::star::chart::XChartDataChangeEventListener> &)1369 void SAL_CALL InternalDataProvider::addChartDataChangeEventListener(
1370     const Reference< ::com::sun::star::chart::XChartDataChangeEventListener >& )
1371 {
1372 }
1373 
removeChartDataChangeEventListener(const Reference<::com::sun::star::chart::XChartDataChangeEventListener> &)1374 void SAL_CALL InternalDataProvider::removeChartDataChangeEventListener(
1375     const Reference< ::com::sun::star::chart::XChartDataChangeEventListener >& )
1376 {
1377 }
1378 
getNotANumber()1379 double SAL_CALL InternalDataProvider::getNotANumber()
1380 {
1381     double fNan;
1382     ::rtl::math::setNan( & fNan );
1383     return fNan;
1384 }
1385 
isNotANumber(double nNumber)1386 ::sal_Bool SAL_CALL InternalDataProvider::isNotANumber( double nNumber )
1387 {
1388     return ::rtl::math::isNan( nNumber )
1389         || ::rtl::math::isInf( nNumber );
1390 }
1391 // lang::XInitialization:
initialize(const uno::Sequence<uno::Any> & _aArguments)1392 void SAL_CALL InternalDataProvider::initialize(const uno::Sequence< uno::Any > & _aArguments)
1393 {
1394     comphelper::SequenceAsHashMap aArgs(_aArguments);
1395     if ( aArgs.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CreateDefaultData")),sal_False) )
1396         createDefaultData();
1397 }
1398 // ____ XCloneable ____
createClone()1399 Reference< util::XCloneable > SAL_CALL InternalDataProvider::createClone()
1400 {
1401     return Reference< util::XCloneable >( new InternalDataProvider( *this ));
1402 }
1403 
1404 
1405 // ================================================================================
1406 
getSupportedServiceNames_Static()1407 Sequence< OUString > InternalDataProvider::getSupportedServiceNames_Static()
1408 {
1409     Sequence< OUString > aServices( 1 );
1410     aServices[ 0 ] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.data.DataProvider" ));
1411     return aServices;
1412 }
1413 
1414 // ================================================================================
1415 
1416 APPHELPER_XSERVICEINFO_IMPL( InternalDataProvider, lcl_aServiceName );
1417 
1418 } //  namespace chart
1419