bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / chart / SchXMLSeriesHelper.cxx
blob61f7ee1c241a674d1a408f9f9cae9da83718673a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmloff/SchXMLSeriesHelper.hxx>
21 #include <com/sun/star/chart2/XChartDocument.hpp>
22 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
23 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
24 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
25 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <rtl/ustring.h>
29 // header for define DBG_ERROR1
30 #include <tools/debug.hxx>
32 #include <typeinfo>
34 using namespace ::com::sun::star;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Sequence;
39 // ----------------------------------------
41 ::std::vector< Reference< chart2::XDataSeries > >
42 SchXMLSeriesHelper::getDataSeriesFromDiagram(
43 const Reference< chart2::XDiagram > & xDiagram )
45 ::std::vector< Reference< chart2::XDataSeries > > aResult;
47 try
49 Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
50 xDiagram, uno::UNO_QUERY_THROW );
51 Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
52 xCooSysCnt->getCoordinateSystems());
53 for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
55 Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
56 Sequence< Reference< chart2::XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
57 for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
59 Reference< chart2::XDataSeriesContainer > xDSCnt( aChartTypeSeq[j], uno::UNO_QUERY_THROW );
60 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xDSCnt->getDataSeries() );
61 ::std::copy( aSeriesSeq.getConstArray(), aSeriesSeq.getConstArray() + aSeriesSeq.getLength(),
62 ::std::back_inserter( aResult ));
66 catch( const uno::Exception & ex )
68 SAL_WARN("xmloff.chart", "Exception caught. Type: " << OUString::createFromAscii( typeid( ex ).name() ) << ", Message: " << ex.Message );
71 return aResult;
74 ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > SchXMLSeriesHelper::getDataSeriesIndexMapFromDiagram(
75 const Reference< chart2::XDiagram > & xDiagram )
77 ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > aRet;
79 sal_Int32 nIndex=0;
81 ::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( SchXMLSeriesHelper::getDataSeriesFromDiagram( xDiagram ));
82 const ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aSeriesEnd( aSeriesVector.end() );
83 for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
84 ; aSeriesIt != aSeriesEnd
85 ; ++aSeriesIt, nIndex++ )
87 Reference< chart2::XDataSeries > xSeries( *aSeriesIt );
88 if( xSeries.is() )
90 if( aRet.end() == aRet.find(xSeries) )
91 aRet[xSeries]=nIndex;
94 return aRet;
97 namespace {
98 uno::Reference< chart2::XChartType > lcl_getChartTypeOfSeries(
99 const uno::Reference< chart2::XDiagram >& xDiagram
100 , const Reference< chart2::XDataSeries >& xSeries )
102 if(!xDiagram.is())
103 return 0;
105 //iterate through the model to find the given xSeries
106 //the found parent indicates the charttype
108 //iterate through all coordinate systems
109 uno::Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
110 if( !xCooSysContainer.is())
111 return 0;
113 uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
114 for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
116 uno::Reference< chart2::XCoordinateSystem > xCooSys( aCooSysList[nCS] );
118 //iterate through all chart types in the current coordinate system
119 uno::Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
120 SAL_WARN_IF( !xChartTypeContainer.is(), "xmloff.chart", "xChartTypeContainer is NULL");
121 if( !xChartTypeContainer.is() )
122 continue;
123 uno::Sequence< uno::Reference< chart2::XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
124 for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
126 uno::Reference< chart2::XChartType > xChartType( aChartTypeList[nT] );
128 //iterate through all series in this chart type
129 uno::Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
130 SAL_WARN_IF( !xDataSeriesContainer.is(), "xmloff.chart", "xDataSeriesContainer is NULL");
131 if( !xDataSeriesContainer.is() )
132 continue;
134 uno::Sequence< uno::Reference< chart2::XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
135 for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
137 Reference< chart2::XDataSeries > xCurrentSeries( aSeriesList[nS] );
139 if( xSeries == xCurrentSeries )
140 return xChartType;
144 return 0;
148 bool SchXMLSeriesHelper::isCandleStickSeries(
149 const Reference< chart2::XDataSeries >& xSeries
150 , const Reference< frame::XModel >& xChartModel )
152 bool bRet = false;
154 uno::Reference< chart2::XChartDocument > xNewDoc( xChartModel, uno::UNO_QUERY );
155 if( xNewDoc.is() )
157 uno::Reference< chart2::XDiagram > xNewDiagram( xNewDoc->getFirstDiagram() );
158 if( xNewDiagram.is() )
160 uno::Reference< chart2::XChartType > xChartType( lcl_getChartTypeOfSeries(
161 xNewDiagram, xSeries ) );
162 if( xChartType.is() )
164 OUString aServiceName( xChartType->getChartType() );
165 if( aServiceName == "com.sun.star.chart2.CandleStickChartType" )
166 bRet = true;
170 return bRet;
173 //static
174 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPISeriesPropertySet(
175 const uno::Reference< chart2::XDataSeries >& xSeries
176 , const uno::Reference< frame::XModel >& xChartModel )
178 uno::Reference< beans::XPropertySet > xRet;
180 if( xSeries.is() )
184 uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
185 if( xFactory.is() )
187 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
188 "com.sun.star.comp.chart2.DataSeriesWrapper" ), uno::UNO_QUERY );
189 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
190 if(xInit.is())
192 Sequence< uno::Any > aArguments(1);
193 aArguments[0]=uno::makeAny(xSeries);
194 xInit->initialize(aArguments);
198 catch( const uno::Exception & rEx )
200 SAL_INFO("xmloff.chart", "Exception caught SchXMLSeriesHelper::createOldAPISeriesPropertySet: " << rEx.Message );
204 return xRet;
207 //static
208 uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPIDataPointPropertySet(
209 const uno::Reference< chart2::XDataSeries >& xSeries
210 , sal_Int32 nPointIndex
211 , const uno::Reference< frame::XModel >& xChartModel )
213 uno::Reference< beans::XPropertySet > xRet;
215 if( xSeries.is() )
219 uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
220 if( xFactory.is() )
222 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
223 "com.sun.star.comp.chart2.DataSeriesWrapper" ), uno::UNO_QUERY );
224 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
225 if(xInit.is())
227 Sequence< uno::Any > aArguments(2);
228 aArguments[0]=uno::makeAny(xSeries);
229 aArguments[1]=uno::makeAny(nPointIndex);
230 xInit->initialize(aArguments);
234 catch( const uno::Exception & rEx )
236 SAL_INFO("xmloff.chart", "Exception caught SchXMLSeriesHelper::createOldAPIDataPointPropertySet: " << rEx.Message );
240 return xRet;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */