merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / chart / SchXMLSeriesHelper.cxx
blob5cae51aa995480eee9b9ec474924df7f9b9c784f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SchXMLSeriesHelper.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #include "SchXMLSeriesHelper.hxx"
35 #include <com/sun/star/chart2/XChartDocument.hpp>
36 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
37 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
38 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
39 #include <com/sun/star/lang/XInitialization.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 // header for define RTL_CONSTASCII_USTRINGPARAM
43 #include <rtl/ustring.h>
44 // header for define DBG_ERROR1
45 #include <tools/debug.hxx>
47 #include <typeinfo>
49 using namespace ::com::sun::star;
50 using ::rtl::OUString;
51 using ::rtl::OUStringToOString;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::Sequence;
55 using ::rtl::OUString;
57 // ----------------------------------------
59 ::std::vector< Reference< chart2::XDataSeries > >
60 SchXMLSeriesHelper::getDataSeriesFromDiagram(
61 const Reference< chart2::XDiagram > & xDiagram )
63 ::std::vector< Reference< chart2::XDataSeries > > aResult;
65 try
67 Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
68 xDiagram, uno::UNO_QUERY_THROW );
69 Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
70 xCooSysCnt->getCoordinateSystems());
71 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
73 Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
74 Sequence< Reference< chart2::XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
75 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
77 Reference< chart2::XDataSeriesContainer > xDSCnt( aChartTypeSeq[j], uno::UNO_QUERY_THROW );
78 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xDSCnt->getDataSeries() );
79 ::std::copy( aSeriesSeq.getConstArray(), aSeriesSeq.getConstArray() + aSeriesSeq.getLength(),
80 ::std::back_inserter( aResult ));
84 catch( uno::Exception & ex )
86 (void)ex; // avoid warning for pro build
88 OSL_ENSURE( false, OUStringToOString( OUString(
89 OUString( RTL_CONSTASCII_USTRINGPARAM( "Exception caught. Type: " )) +
90 OUString::createFromAscii( typeid( ex ).name()) +
91 OUString( RTL_CONSTASCII_USTRINGPARAM( ", Message: " )) +
92 ex.Message), RTL_TEXTENCODING_ASCII_US ).getStr());
96 return aResult;
99 ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > SchXMLSeriesHelper::getDataSeriesIndexMapFromDiagram(
100 const Reference< chart2::XDiagram > & xDiagram )
102 ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > aRet;
104 sal_Int32 nIndex=0;
106 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( SchXMLSeriesHelper::getDataSeriesFromDiagram( xDiagram ));
107 for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
108 ; aSeriesIt != aSeriesVector.end()
109 ; aSeriesIt++, nIndex++ )
111 Reference< chart2::XDataSeries > xSeries( *aSeriesIt );
112 if( xSeries.is() )
114 if( aRet.end() == aRet.find(xSeries) )
115 aRet[xSeries]=nIndex;
118 return aRet;
121 uno::Reference< chart2::XChartType > lcl_getChartTypeOfSeries(
122 const uno::Reference< chart2::XDiagram >& xDiagram
123 , const Reference< chart2::XDataSeries >& xSeries )
125 if(!xDiagram.is())
126 return 0;
128 //iterate through the model to find the given xSeries
129 //the found parent indicates the charttype
131 //iterate through all coordinate systems
132 uno::Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
133 if( !xCooSysContainer.is())
134 return 0;
136 uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
137 for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
139 uno::Reference< chart2::XCoordinateSystem > xCooSys( aCooSysList[nCS] );
141 //iterate through all chart types in the current coordinate system
142 uno::Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
143 OSL_ASSERT( xChartTypeContainer.is());
144 if( !xChartTypeContainer.is() )
145 continue;
146 uno::Sequence< uno::Reference< chart2::XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
147 for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
149 uno::Reference< chart2::XChartType > xChartType( aChartTypeList[nT] );
151 //iterate through all series in this chart type
152 uno::Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
153 OSL_ASSERT( xDataSeriesContainer.is());
154 if( !xDataSeriesContainer.is() )
155 continue;
157 uno::Sequence< uno::Reference< chart2::XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
158 for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
160 Reference< chart2::XDataSeries > xCurrentSeries( aSeriesList[nS] );
162 if( xSeries == xCurrentSeries )
163 return xChartType;
167 return 0;
170 bool SchXMLSeriesHelper::isCandleStickSeries(
171 const Reference< chart2::XDataSeries >& xSeries
172 , const Reference< frame::XModel >& xChartModel )
174 bool bRet = false;
176 uno::Reference< chart2::XChartDocument > xNewDoc( xChartModel, uno::UNO_QUERY );
177 if( xNewDoc.is() )
179 uno::Reference< chart2::XDiagram > xNewDiagram( xNewDoc->getFirstDiagram() );
180 if( xNewDiagram.is() )
182 uno::Reference< chart2::XChartType > xChartType( lcl_getChartTypeOfSeries(
183 xNewDiagram, xSeries ) );
184 if( xChartType.is() )
186 rtl::OUString aServiceName( xChartType->getChartType() );
187 if( aServiceName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.CandleStickChartType" ) ) ) )
188 bRet = true;
192 return bRet;
195 // static
196 Reference< chart2::XDataSeries > SchXMLSeriesHelper::getFirstCandleStickSeries(
197 const Reference< chart2::XDiagram > & xDiagram )
199 Reference< chart2::XDataSeries > xResult;
203 Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY_THROW );
204 Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
205 for( sal_Int32 nCooSysIdx=0; !xResult.is() && nCooSysIdx<aCooSysSeq.getLength(); ++nCooSysIdx )
207 Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[nCooSysIdx], uno::UNO_QUERY_THROW );
208 Sequence< Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
209 for( sal_Int32 nCTIdx=0; !xResult.is() && nCTIdx<aCTSeq.getLength(); ++nCTIdx )
211 if( aCTSeq[nCTIdx]->getChartType().equals(
212 ::rtl::OUString::createFromAscii("com.sun.star.chart2.CandleStickChartType")))
214 Reference< chart2::XDataSeriesContainer > xSeriesCnt( aCTSeq[nCTIdx], uno::UNO_QUERY_THROW );
215 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries() );
216 if( aSeriesSeq.getLength())
217 xResult.set( aSeriesSeq[0] );
218 break;
223 catch( const uno::Exception & )
225 OSL_ENSURE( false, "Exception caught" );
227 return xResult;
230 //static
231 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPISeriesPropertySet(
232 const uno::Reference< chart2::XDataSeries >& xSeries
233 , const uno::Reference< frame::XModel >& xChartModel )
235 uno::Reference< beans::XPropertySet > xRet;
237 if( xSeries.is() )
241 uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
242 if( xFactory.is() )
244 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
245 OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
246 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
247 if(xInit.is())
249 Sequence< uno::Any > aArguments(1);
250 aArguments[0]=uno::makeAny(xSeries);
251 xInit->initialize(aArguments);
255 catch( uno::Exception & rEx )
257 (void)rEx; // avoid warning for pro build
258 DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPISeriesPropertySet: %s",
259 OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
263 return xRet;
266 //static
267 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPIDataPointPropertySet(
268 const uno::Reference< chart2::XDataSeries >& xSeries
269 , sal_Int32 nPointIndex
270 , const uno::Reference< frame::XModel >& xChartModel )
272 uno::Reference< beans::XPropertySet > xRet;
274 if( xSeries.is() )
278 uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
279 if( xFactory.is() )
281 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
282 OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
283 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
284 if(xInit.is())
286 Sequence< uno::Any > aArguments(2);
287 aArguments[0]=uno::makeAny(xSeries);
288 aArguments[1]=uno::makeAny(nPointIndex);
289 xInit->initialize(aArguments);
293 catch( uno::Exception & rEx )
295 (void)rEx; // avoid warning for pro build
297 DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPIDataPointPropertySet: %s",
298 OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
302 return xRet;