bump product version to 4.1.6.2
[LibreOffice.git] / oox / source / drawingml / chart / chartconverter.cxx
blobe280f088da8d8ceba8936cace449b4e35416a880
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 "oox/drawingml/chart/chartconverter.hxx"
22 #include <com/sun/star/chart2/XChartDocument.hpp>
23 #include "oox/drawingml/chart/chartspaceconverter.hxx"
24 #include "oox/drawingml/chart/chartspacemodel.hxx"
25 #include "oox/helper/containerhelper.hxx"
26 #include "oox/core/xmlfilterbase.hxx"
28 using ::oox::drawingml::chart::DataSequenceModel;
29 using ::com::sun::star::uno::Any;
30 namespace oox {
31 namespace drawingml {
32 namespace chart {
34 // ============================================================================
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::chart2;
38 using namespace ::com::sun::star::chart2::data;
39 using namespace ::com::sun::star::drawing;
40 using namespace ::com::sun::star::uno;
42 using ::oox::core::XmlFilterBase;
44 // ============================================================================
46 static const sal_Unicode API_TOKEN_ARRAY_OPEN = '{';
47 static const sal_Unicode API_TOKEN_ARRAY_CLOSE = '}';
48 static const sal_Unicode API_TOKEN_ARRAY_ROWSEP = '|';
49 static const sal_Unicode API_TOKEN_ARRAY_COLSEP = ';';
51 // Code similar to oox/source/xls/formulabase.cxx
52 static OUString lclGenerateApiString( const OUString& rString )
54 OUString aRetString = rString;
55 sal_Int32 nQuotePos = aRetString.getLength();
56 while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
57 aRetString = aRetString.replaceAt( nQuotePos, 1, "\"\"" );
58 return OUStringBuffer().append( sal_Unicode( '"' ) ).append( aRetString ).append( sal_Unicode( '"' ) ).makeStringAndClear();
61 static OUString lclGenerateApiArray( const Matrix< Any >& rMatrix )
63 OSL_ENSURE( !rMatrix.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
64 OUStringBuffer aBuffer;
65 aBuffer.append( API_TOKEN_ARRAY_OPEN );
66 for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
68 if( nRow > 0 )
69 aBuffer.append( API_TOKEN_ARRAY_ROWSEP );
70 for( Matrix< Any >::const_iterator aBeg = rMatrix.row_begin( nRow ), aIt = aBeg, aEnd = rMatrix.row_end( nRow ); aIt != aEnd; ++aIt )
72 double fValue = 0.0;
73 OUString aString;
74 if( aIt != aBeg )
75 aBuffer.append( API_TOKEN_ARRAY_COLSEP );
76 if( *aIt >>= fValue )
77 aBuffer.append( fValue );
78 else if( *aIt >>= aString )
79 aBuffer.append( lclGenerateApiString( aString ) );
80 else
81 aBuffer.appendAscii( "\"\"" );
84 aBuffer.append( API_TOKEN_ARRAY_CLOSE );
85 return aBuffer.makeStringAndClear();
88 // ============================================================================
90 ChartConverter::ChartConverter()
94 ChartConverter::~ChartConverter()
98 void ChartConverter::convertFromModel( XmlFilterBase& rFilter,
99 ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
100 const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
102 OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
103 if( rxChartDoc.is() )
105 ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
106 ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
107 aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
111 void ChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
115 if( !rxChartDoc->hasInternalDataProvider() )
116 rxChartDoc->createInternalDataProvider( sal_False );
118 catch( Exception& )
123 Reference< XDataSequence > ChartConverter::createDataSequence( const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
125 Reference< XDataSequence > xDataSeq;
126 if( rxDataProvider.is() )
128 OUString aRangeRep;
129 if( !rDataSeq.maData.empty() )
131 // create a single-row array from constant source data
132 Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
133 Matrix< Any >::iterator aMIt = aMatrix.begin();
134 // TODO: how to handle missing values in the map?
135 for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
136 *aMIt = aDIt->second;
137 aRangeRep = lclGenerateApiArray( aMatrix );
140 if( !aRangeRep.isEmpty() ) try
142 // create the data sequence
143 xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
144 return xDataSeq;
146 catch( Exception& )
148 OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
152 return 0;
155 // ============================================================================
157 } // namespace chart
158 } // namespace drawingml
159 } // namespace oox
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */