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 "BubbleDataInterpreter.hxx"
22 : #include "DataSeries.hxx"
23 : #include "macros.hxx"
24 : #include "DataSeriesHelper.hxx"
25 : #include "CommonConverters.hxx"
26 : #include "ContainerHelper.hxx"
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <com/sun/star/chart2/data/XDataSink.hpp>
29 : #include <com/sun/star/util/XCloneable.hpp>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::chart2;
33 : using namespace ::std;
34 :
35 : using ::com::sun::star::uno::Reference;
36 : using ::com::sun::star::uno::Sequence;
37 : using ::rtl::OUString;
38 :
39 : namespace chart
40 : {
41 :
42 0 : BubbleDataInterpreter::BubbleDataInterpreter(
43 : const uno::Reference< uno::XComponentContext > & xContext ) :
44 0 : DataInterpreter( xContext )
45 : {
46 0 : }
47 :
48 0 : BubbleDataInterpreter::~BubbleDataInterpreter()
49 : {
50 0 : }
51 :
52 : // ____ XDataInterpreter ____
53 0 : chart2::InterpretedData SAL_CALL BubbleDataInterpreter::interpretDataSource(
54 : const Reference< chart2::data::XDataSource >& xSource,
55 : const Sequence< beans::PropertyValue >& aArguments,
56 : const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
57 : throw (uno::RuntimeException)
58 : {
59 0 : if( ! xSource.is())
60 0 : return InterpretedData();
61 :
62 0 : Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
63 :
64 0 : Reference< data::XLabeledDataSequence > xValuesX;
65 0 : vector< Reference< data::XLabeledDataSequence > > aYValuesVector;
66 0 : vector< Reference< data::XLabeledDataSequence > > aSizeValuesVector;
67 :
68 0 : Reference< data::XLabeledDataSequence > xCategories;
69 0 : bool bHasCategories = HasCategories( aArguments, aData );
70 0 : bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
71 :
72 0 : bool bSetXValues = false;
73 0 : sal_Int32 nDataSeqCount = aData.getLength();
74 :
75 : bSetXValues = bHasCategories ? ( (nDataSeqCount-1) > 2 && (nDataSeqCount-1) % 2 != 0 )
76 0 : :( nDataSeqCount > 2 && nDataSeqCount % 2 != 0 );
77 :
78 0 : bool bCategoriesUsed = false;
79 0 : bool bNextIsYValues = bHasCategories ? nDataSeqCount>2 : nDataSeqCount>1;
80 0 : for( sal_Int32 nDataIdx = 0; nDataIdx < nDataSeqCount; ++nDataIdx )
81 : {
82 : try
83 : {
84 0 : if( bHasCategories && !bCategoriesUsed )
85 : {
86 0 : xCategories.set( aData[nDataIdx] );
87 0 : if( xCategories.is())
88 : {
89 0 : SetRole( xCategories->getValues(), C2U("categories"));
90 0 : if( bUseCategoriesAsX )
91 : {
92 0 : bSetXValues = false;
93 0 : bNextIsYValues = nDataSeqCount > 2;
94 : }
95 : }
96 0 : bCategoriesUsed = true;
97 : }
98 0 : else if( !xValuesX.is() && bSetXValues )
99 : {
100 0 : xValuesX.set( aData[nDataIdx] );
101 0 : if( xValuesX.is())
102 0 : SetRole( xValuesX->getValues(), C2U("values-x"));
103 : }
104 0 : else if( bNextIsYValues )
105 : {
106 0 : aYValuesVector.push_back( aData[nDataIdx] );
107 0 : if( aData[nDataIdx].is())
108 0 : SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
109 0 : bNextIsYValues = false;
110 : }
111 0 : else if( !bNextIsYValues )
112 : {
113 0 : aSizeValuesVector.push_back( aData[nDataIdx] );
114 0 : if( aData[nDataIdx].is())
115 0 : SetRole( aData[nDataIdx]->getValues(), C2U("values-size"));
116 0 : bNextIsYValues = (nDataSeqCount-(nDataIdx+1)) >= 2;//two or more left
117 : }
118 : }
119 0 : catch( const uno::Exception & ex )
120 : {
121 : ASSERT_EXCEPTION( ex );
122 : }
123 : }
124 :
125 : // create DataSeries
126 0 : sal_Int32 nSeriesIndex = 0;
127 0 : vector< Reference< XDataSeries > > aSeriesVec;
128 0 : aSeriesVec.reserve( aSizeValuesVector.size());
129 :
130 0 : Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
131 0 : Reference< util::XCloneable > xCloneableX( xValuesX, uno::UNO_QUERY );
132 :
133 0 : for( size_t nN = 0; nN < aSizeValuesVector.size(); ++nN, ++nSeriesIndex )
134 : {
135 0 : vector< Reference< data::XLabeledDataSequence > > aNewData;
136 0 : if( xValuesX.is() )
137 : {
138 0 : if( nN > 0 && xCloneableX.is() )
139 0 : xClonedXValues.set( xCloneableX->createClone(), uno::UNO_QUERY );
140 0 : aNewData.push_back( xClonedXValues );
141 : }
142 0 : if( aYValuesVector.size() > nN )
143 0 : aNewData.push_back( aYValuesVector[nN] );
144 0 : if( aSizeValuesVector.size() > nN )
145 0 : aNewData.push_back( aSizeValuesVector[nN] );
146 :
147 0 : Reference< XDataSeries > xSeries;
148 0 : if( nSeriesIndex < aSeriesToReUse.getLength())
149 0 : xSeries.set( aSeriesToReUse[nSeriesIndex] );
150 : else
151 0 : xSeries.set( new DataSeries( GetComponentContext() ) );
152 : OSL_ASSERT( xSeries.is() );
153 0 : Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
154 : OSL_ASSERT( xSink.is() );
155 0 : xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
156 :
157 0 : aSeriesVec.push_back( xSeries );
158 0 : }
159 :
160 0 : Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
161 0 : aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
162 0 : return InterpretedData( aSeries, xCategories );
163 : }
164 :
165 0 : chart2::InterpretedData SAL_CALL BubbleDataInterpreter::reinterpretDataSeries(
166 : const chart2::InterpretedData& aInterpretedData )
167 : throw (uno::RuntimeException)
168 : {
169 0 : InterpretedData aResult( aInterpretedData );
170 :
171 0 : sal_Int32 i=0;
172 0 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
173 0 : const sal_Int32 nCount = aSeries.getLength();
174 0 : for( ; i<nCount; ++i )
175 : {
176 : try
177 : {
178 0 : Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
179 0 : Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
180 :
181 : Reference< data::XLabeledDataSequence > xValuesSize(
182 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-size"), false ));
183 : Reference< data::XLabeledDataSequence > xValuesY(
184 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
185 : Reference< data::XLabeledDataSequence > xValuesX(
186 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
187 :
188 0 : if( ! xValuesX.is() ||
189 0 : ! xValuesY.is() ||
190 0 : ! xValuesSize.is() )
191 : {
192 : vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
193 : DataSeriesHelper::getAllDataSequencesByRole(
194 0 : xSeriesSource->getDataSequences(), C2U("values"), true ));
195 0 : if( xValuesX.is())
196 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
197 0 : if( xValuesY.is())
198 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
199 0 : if( xValuesSize.is())
200 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesSize ));
201 :
202 0 : size_t nIndex = 0;
203 :
204 0 : if( ! xValuesSize.is() &&
205 0 : aValueSeqVec.size() > nIndex )
206 : {
207 0 : xValuesSize.set( aValueSeqVec[nIndex++] );
208 0 : if( xValuesSize.is())
209 0 : SetRole( xValuesSize->getValues(), C2U("values-size"));
210 : }
211 :
212 0 : if( ! xValuesY.is() &&
213 0 : aValueSeqVec.size() > nIndex )
214 : {
215 0 : xValuesY.set( aValueSeqVec[nIndex++] );
216 0 : if( xValuesY.is())
217 0 : SetRole( xValuesY->getValues(), C2U("values-y"));
218 : }
219 :
220 0 : if( ! xValuesX.is() &&
221 0 : aValueSeqVec.size() > nIndex )
222 : {
223 0 : xValuesX.set( aValueSeqVec[nIndex++] );
224 0 : if( xValuesX.is())
225 0 : SetRole( xValuesY->getValues(), C2U("values-x"));
226 0 : }
227 : }
228 0 : if( xValuesSize.is())
229 : {
230 0 : if( xValuesY.is() )
231 : {
232 0 : if( xValuesX.is() )
233 : {
234 0 : aNewSequences.realloc(3);
235 0 : aNewSequences[0] = xValuesX;
236 0 : aNewSequences[1] = xValuesY;
237 0 : aNewSequences[2] = xValuesSize;
238 : }
239 : else
240 : {
241 0 : aNewSequences.realloc(2);
242 0 : aNewSequences[0] = xValuesY;
243 0 : aNewSequences[1] = xValuesSize;
244 : }
245 : }
246 : else
247 : {
248 0 : aNewSequences.realloc(1);
249 0 : aNewSequences[0] = xValuesSize;
250 : }
251 : }
252 :
253 0 : Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
254 0 : if( aSeqs.getLength() != aNewSequences.getLength() )
255 : {
256 : #if OSL_DEBUG_LEVEL > 1
257 : sal_Int32 j=0;
258 : for( ; j<aSeqs.getLength(); ++j )
259 : {
260 : OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX || aSeqs[j] == xValuesSize, "All sequences should be used" );
261 : }
262 : #endif
263 0 : Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
264 0 : xSink->setData( aNewSequences );
265 0 : }
266 : }
267 0 : catch( const uno::Exception & ex )
268 : {
269 : ASSERT_EXCEPTION( ex );
270 : }
271 : }
272 :
273 0 : return aResult;
274 : }
275 :
276 0 : sal_Bool SAL_CALL BubbleDataInterpreter::isDataCompatible(
277 : const chart2::InterpretedData& aInterpretedData )
278 : throw (uno::RuntimeException)
279 : {
280 0 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
281 0 : for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
282 : {
283 : try
284 : {
285 0 : Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
286 0 : Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
287 0 : if( aSeq.getLength() != 3 )
288 0 : return sal_False;
289 : }
290 0 : catch( const uno::Exception & ex )
291 : {
292 : ASSERT_EXCEPTION( ex );
293 : }
294 : }
295 :
296 0 : return sal_True;
297 : }
298 :
299 : } // namespace chart
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|