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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 #include "SchXMLSeriesHelper.hxx"
28 #include <com/sun/star/chart2/XChartDocument.hpp>
29 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
30 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
31 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
32 #include <com/sun/star/lang/XInitialization.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 
35 // header for define RTL_CONSTASCII_USTRINGPARAM
36 #include <rtl/ustring.h>
37 // header for define DBG_ERROR1
38 #include <tools/debug.hxx>
39 
40 #include <typeinfo>
41 
42 using namespace ::com::sun::star;
43 using ::rtl::OUString;
44 using ::rtl::OUStringToOString;
45 
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::rtl::OUString;
49 
50 // ----------------------------------------
51 
52 ::std::vector< Reference< chart2::XDataSeries > >
getDataSeriesFromDiagram(const Reference<chart2::XDiagram> & xDiagram)53     SchXMLSeriesHelper::getDataSeriesFromDiagram(
54         const Reference< chart2::XDiagram > & xDiagram )
55 {
56     ::std::vector< Reference< chart2::XDataSeries > > aResult;
57 
58     try
59     {
60         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
61             xDiagram, uno::UNO_QUERY_THROW );
62         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
63             xCooSysCnt->getCoordinateSystems());
64         for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
65         {
66             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
67             Sequence< Reference< chart2::XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
68             for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
69             {
70                 Reference< chart2::XDataSeriesContainer > xDSCnt( aChartTypeSeq[j], uno::UNO_QUERY_THROW );
71                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xDSCnt->getDataSeries() );
72                 ::std::copy( aSeriesSeq.getConstArray(), aSeriesSeq.getConstArray() + aSeriesSeq.getLength(),
73                              ::std::back_inserter( aResult ));
74             }
75         }
76     }
77     catch( uno::Exception & ex )
78     {
79         (void)ex; // avoid warning for pro build
80 
81         OSL_ENSURE( false, OUStringToOString( OUString(
82                         OUString( RTL_CONSTASCII_USTRINGPARAM( "Exception caught. Type: " )) +
83                         OUString::createFromAscii( typeid( ex ).name()) +
84                         OUString( RTL_CONSTASCII_USTRINGPARAM( ", Message: " )) +
85                         ex.Message), RTL_TEXTENCODING_ASCII_US ).getStr());
86 
87     }
88 
89     return aResult;
90 }
91 
getDataSeriesIndexMapFromDiagram(const Reference<chart2::XDiagram> & xDiagram)92 ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > SchXMLSeriesHelper::getDataSeriesIndexMapFromDiagram(
93         const Reference< chart2::XDiagram > & xDiagram )
94 {
95     ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > aRet;
96 
97     sal_Int32 nIndex=0;
98 
99     ::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( SchXMLSeriesHelper::getDataSeriesFromDiagram( xDiagram ));
100     for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
101         ; aSeriesIt != aSeriesVector.end()
102         ; aSeriesIt++, nIndex++ )
103     {
104         Reference< chart2::XDataSeries > xSeries( *aSeriesIt );
105         if( xSeries.is() )
106         {
107             if( aRet.end() == aRet.find(xSeries) )
108                 aRet[xSeries]=nIndex;
109         }
110     }
111     return aRet;
112 }
113 
lcl_getChartTypeOfSeries(const uno::Reference<chart2::XDiagram> & xDiagram,const Reference<chart2::XDataSeries> & xSeries)114 uno::Reference< chart2::XChartType > lcl_getChartTypeOfSeries(
115 								const uno::Reference< chart2::XDiagram >&   xDiagram
116 						      , const Reference< chart2::XDataSeries >& xSeries )
117 {
118     if(!xDiagram.is())
119         return 0;
120 
121 	//iterate through the model to find the given xSeries
122 	//the found parent indicates the charttype
123 
124     //iterate through all coordinate systems
125     uno::Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
126     if( !xCooSysContainer.is())
127         return 0;
128 
129     uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
130     for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
131     {
132         uno::Reference< chart2::XCoordinateSystem > xCooSys( aCooSysList[nCS] );
133 
134         //iterate through all chart types in the current coordinate system
135         uno::Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
136         OSL_ASSERT( xChartTypeContainer.is());
137         if( !xChartTypeContainer.is() )
138             continue;
139         uno::Sequence< uno::Reference< chart2::XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
140         for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
141         {
142             uno::Reference< chart2::XChartType > xChartType( aChartTypeList[nT] );
143 
144             //iterate through all series in this chart type
145             uno::Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
146             OSL_ASSERT( xDataSeriesContainer.is());
147             if( !xDataSeriesContainer.is() )
148                 continue;
149 
150             uno::Sequence< uno::Reference< chart2::XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
151             for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
152             {
153                 Reference< chart2::XDataSeries > xCurrentSeries( aSeriesList[nS] );
154 
155                 if( xSeries == xCurrentSeries )
156                     return xChartType;
157             }
158         }
159     }
160 	return 0;
161 }
162 
isCandleStickSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<frame::XModel> & xChartModel)163 bool SchXMLSeriesHelper::isCandleStickSeries(
164                   const Reference< chart2::XDataSeries >& xSeries
165                 , const Reference< frame::XModel >& xChartModel )
166 {
167     bool bRet = false;
168 
169     uno::Reference< chart2::XChartDocument > xNewDoc( xChartModel, uno::UNO_QUERY );
170     if( xNewDoc.is() )
171     {
172         uno::Reference< chart2::XDiagram > xNewDiagram( xNewDoc->getFirstDiagram() );
173         if( xNewDiagram.is() )
174         {
175             uno::Reference< chart2::XChartType > xChartType( lcl_getChartTypeOfSeries(
176 								        xNewDiagram, xSeries ) );
177             if( xChartType.is() )
178             {
179                 rtl::OUString aServiceName( xChartType->getChartType() );
180                 if( aServiceName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.CandleStickChartType" ) ) ) )
181                     bRet = true;
182             }
183         }
184     }
185     return bRet;
186 }
187 
188 // static
getFirstCandleStickSeries(const Reference<chart2::XDiagram> & xDiagram)189 Reference< chart2::XDataSeries > SchXMLSeriesHelper::getFirstCandleStickSeries(
190     const Reference< chart2::XDiagram > & xDiagram  )
191 {
192     Reference< chart2::XDataSeries > xResult;
193 
194     try
195     {
196         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY_THROW );
197         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
198         for( sal_Int32 nCooSysIdx=0; !xResult.is() && nCooSysIdx<aCooSysSeq.getLength(); ++nCooSysIdx )
199         {
200             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[nCooSysIdx], uno::UNO_QUERY_THROW );
201             Sequence< Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
202             for( sal_Int32 nCTIdx=0; !xResult.is() && nCTIdx<aCTSeq.getLength(); ++nCTIdx )
203             {
204                 if( aCTSeq[nCTIdx]->getChartType().equals(
205                         ::rtl::OUString::createFromAscii("com.sun.star.chart2.CandleStickChartType")))
206                 {
207                     Reference< chart2::XDataSeriesContainer > xSeriesCnt( aCTSeq[nCTIdx], uno::UNO_QUERY_THROW );
208                     Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries() );
209                     if( aSeriesSeq.getLength())
210                         xResult.set( aSeriesSeq[0] );
211                     break;
212                 }
213             }
214         }
215     }
216     catch( const uno::Exception & )
217     {
218         OSL_ENSURE( false, "Exception caught" );
219     }
220     return xResult;
221 }
222 
223 //static
createOldAPISeriesPropertySet(const uno::Reference<chart2::XDataSeries> & xSeries,const uno::Reference<frame::XModel> & xChartModel)224 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPISeriesPropertySet(
225             const uno::Reference< chart2::XDataSeries >& xSeries
226             , const uno::Reference< frame::XModel >& xChartModel )
227 {
228     uno::Reference< beans::XPropertySet > xRet;
229 
230     if( xSeries.is() )
231     {
232         try
233         {
234             uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
235             if( xFactory.is() )
236             {
237                 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
238                     OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
239                 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
240                 if(xInit.is())
241                 {
242                     Sequence< uno::Any > aArguments(1);
243                     aArguments[0]=uno::makeAny(xSeries);
244                     xInit->initialize(aArguments);
245                 }
246             }
247         }
248         catch( uno::Exception & rEx )
249 	    {
250             (void)rEx; // avoid warning for pro build
251             DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPISeriesPropertySet: %s",
252                         OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
253         }
254     }
255 
256     return xRet;
257 }
258 
259 //static
createOldAPIDataPointPropertySet(const uno::Reference<chart2::XDataSeries> & xSeries,sal_Int32 nPointIndex,const uno::Reference<frame::XModel> & xChartModel)260 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPIDataPointPropertySet(
261             const uno::Reference< chart2::XDataSeries >& xSeries
262             , sal_Int32 nPointIndex
263             , const uno::Reference< frame::XModel >& xChartModel )
264 {
265     uno::Reference< beans::XPropertySet > xRet;
266 
267     if( xSeries.is() )
268     {
269         try
270         {
271             uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
272             if( xFactory.is() )
273             {
274                 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
275                     OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
276                 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
277                 if(xInit.is())
278                 {
279                     Sequence< uno::Any > aArguments(2);
280                     aArguments[0]=uno::makeAny(xSeries);
281                     aArguments[1]=uno::makeAny(nPointIndex);
282                     xInit->initialize(aArguments);
283                 }
284             }
285         }
286         catch( uno::Exception & rEx )
287 	    {
288             (void)rEx; // avoid warning for pro build
289 
290             DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPIDataPointPropertySet: %s",
291                         OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
292         }
293     }
294 
295     return xRet;
296 }
297 
298