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 "XYDataInterpreter.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 : XYDataInterpreter::XYDataInterpreter(
43 : const uno::Reference< uno::XComponentContext > & xContext ) :
44 0 : DataInterpreter( xContext )
45 : {
46 0 : }
47 :
48 0 : XYDataInterpreter::~XYDataInterpreter()
49 : {
50 0 : }
51 :
52 : // ____ XDataInterpreter ____
53 0 : chart2::InterpretedData SAL_CALL XYDataInterpreter::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 > > aSequencesVec;
66 :
67 0 : Reference< data::XLabeledDataSequence > xCategories;
68 0 : bool bHasCategories = HasCategories( aArguments, aData );
69 0 : bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
70 :
71 : // parse data
72 0 : bool bCategoriesUsed = false;
73 0 : bool bSetXValues = aData.getLength()>(bCategoriesUsed?2:1);
74 0 : for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
75 : {
76 : try
77 : {
78 0 : if( bHasCategories && ! bCategoriesUsed )
79 : {
80 0 : xCategories.set( aData[nDataIdx] );
81 0 : if( xCategories.is())
82 : {
83 0 : SetRole( xCategories->getValues(), C2U("categories"));
84 0 : if( bUseCategoriesAsX )
85 0 : bSetXValues = false;
86 : }
87 0 : bCategoriesUsed = true;
88 : }
89 0 : else if( !xValuesX.is() && bSetXValues )
90 : {
91 0 : xValuesX.set( aData[nDataIdx] );
92 0 : if( xValuesX.is())
93 0 : SetRole( xValuesX->getValues(), C2U("values-x"));
94 : }
95 : else
96 : {
97 0 : aSequencesVec.push_back( aData[nDataIdx] );
98 0 : if( aData[nDataIdx].is())
99 0 : SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
100 : }
101 : }
102 0 : catch( const uno::Exception & ex )
103 : {
104 : ASSERT_EXCEPTION( ex );
105 : }
106 : }
107 :
108 : // create DataSeries
109 : vector< Reference< data::XLabeledDataSequence > >::const_iterator
110 0 : aSequencesVecIt = aSequencesVec.begin();
111 :
112 0 : sal_Int32 nSeriesIndex = 0;
113 0 : vector< Reference< XDataSeries > > aSeriesVec;
114 0 : aSeriesVec.reserve( aSequencesVec.size());
115 :
116 0 : Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
117 0 : Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
118 :
119 0 : for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
120 : {
121 0 : vector< Reference< data::XLabeledDataSequence > > aNewData;
122 :
123 0 : if( aSequencesVecIt != aSequencesVec.begin() && xCloneable.is() )
124 0 : xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
125 0 : if( xValuesX.is() )
126 0 : aNewData.push_back( xClonedXValues );
127 :
128 0 : aNewData.push_back( *aSequencesVecIt );
129 :
130 0 : Reference< XDataSeries > xSeries;
131 0 : if( nSeriesIndex < aSeriesToReUse.getLength())
132 0 : xSeries.set( aSeriesToReUse[nSeriesIndex] );
133 : else
134 0 : xSeries.set( new DataSeries( GetComponentContext() ) );
135 : OSL_ASSERT( xSeries.is() );
136 0 : Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
137 : OSL_ASSERT( xSink.is() );
138 0 : xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
139 :
140 0 : aSeriesVec.push_back( xSeries );
141 0 : }
142 :
143 0 : Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
144 0 : aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
145 0 : return InterpretedData( aSeries, xCategories );
146 : }
147 :
148 0 : chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
149 : const chart2::InterpretedData& aInterpretedData )
150 : throw (uno::RuntimeException)
151 : {
152 0 : InterpretedData aResult( aInterpretedData );
153 :
154 0 : sal_Int32 i=0;
155 0 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
156 0 : const sal_Int32 nCount = aSeries.getLength();
157 0 : for( ; i<nCount; ++i )
158 : {
159 : try
160 : {
161 0 : Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
162 0 : Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
163 :
164 : // values-y
165 : Reference< data::XLabeledDataSequence > xValuesY(
166 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
167 : Reference< data::XLabeledDataSequence > xValuesX(
168 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
169 : // re-use values-... as values-x/values-y
170 0 : if( ! xValuesX.is() ||
171 0 : ! xValuesY.is())
172 : {
173 : vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
174 : DataSeriesHelper::getAllDataSequencesByRole(
175 0 : xSeriesSource->getDataSequences(), C2U("values"), true ));
176 0 : if( xValuesX.is())
177 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
178 0 : if( xValuesY.is())
179 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
180 :
181 0 : size_t nIndex = 0;
182 0 : if( ! xValuesY.is() &&
183 0 : aValueSeqVec.size() > nIndex )
184 : {
185 0 : xValuesY.set( aValueSeqVec[nIndex++] );
186 0 : if( xValuesY.is())
187 0 : SetRole( xValuesY->getValues(), C2U("values-y"));
188 : }
189 :
190 0 : if( ! xValuesX.is() &&
191 0 : aValueSeqVec.size() > nIndex )
192 : {
193 0 : xValuesX.set( aValueSeqVec[nIndex++] );
194 0 : if( xValuesX.is())
195 0 : SetRole( xValuesY->getValues(), C2U("values-x"));
196 0 : }
197 : }
198 0 : if( xValuesY.is())
199 : {
200 0 : if( xValuesX.is())
201 : {
202 0 : aNewSequences.realloc(2);
203 0 : aNewSequences[0] = xValuesX;
204 0 : aNewSequences[1] = xValuesY;
205 : }
206 : else
207 : {
208 0 : aNewSequences.realloc(1);
209 0 : aNewSequences[0] = xValuesY;
210 : }
211 : }
212 :
213 0 : Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
214 0 : if( aSeqs.getLength() != aNewSequences.getLength() )
215 : {
216 : #if OSL_DEBUG_LEVEL > 1
217 : sal_Int32 j=0;
218 : for( ; j<aSeqs.getLength(); ++j )
219 : {
220 : OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX, "All sequences should be used" );
221 : }
222 : #endif
223 0 : Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
224 0 : xSink->setData( aNewSequences );
225 0 : }
226 : }
227 0 : catch( const uno::Exception & ex )
228 : {
229 : ASSERT_EXCEPTION( ex );
230 : }
231 : }
232 :
233 0 : return aResult;
234 : }
235 :
236 : // criterion: all series must have exactly two data::XLabeledDataSequences
237 0 : sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
238 : const chart2::InterpretedData& aInterpretedData )
239 : throw (uno::RuntimeException)
240 : {
241 0 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
242 0 : for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
243 : {
244 : try
245 : {
246 0 : Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
247 0 : Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
248 0 : if( aSeq.getLength() != 2 )
249 0 : return sal_False;
250 : }
251 0 : catch( const uno::Exception & ex )
252 : {
253 : ASSERT_EXCEPTION( ex );
254 : }
255 : }
256 :
257 0 : return sal_True;
258 : }
259 :
260 : } // namespace chart
261 :
262 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|