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 : #include "oox/drawingml/chart/chartconverter.hxx"
21 :
22 : #include <com/sun/star/chart2/XChartDocument.hpp>
23 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
24 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
25 : #include "drawingml/chart/chartspaceconverter.hxx"
26 : #include "drawingml/chart/chartspacemodel.hxx"
27 : #include "oox/helper/containerhelper.hxx"
28 : #include "oox/core/xmlfilterbase.hxx"
29 : #include <osl/diagnose.h>
30 :
31 : using ::oox::drawingml::chart::DataSequenceModel;
32 : using ::com::sun::star::uno::Any;
33 : namespace oox {
34 : namespace drawingml {
35 : namespace chart {
36 :
37 : using namespace ::com::sun::star;
38 : using namespace ::com::sun::star::chart2;
39 : using namespace ::com::sun::star::chart2::data;
40 : using namespace ::com::sun::star::drawing;
41 : using namespace ::com::sun::star::uno;
42 :
43 : using ::oox::core::XmlFilterBase;
44 :
45 : static const sal_Unicode API_TOKEN_ARRAY_OPEN = '{';
46 : static const sal_Unicode API_TOKEN_ARRAY_CLOSE = '}';
47 : static const sal_Unicode API_TOKEN_ARRAY_ROWSEP = '|';
48 : static const sal_Unicode API_TOKEN_ARRAY_COLSEP = ';';
49 :
50 : // Code similar to oox/source/xls/formulabase.cxx
51 743 : static OUString lclGenerateApiString( const OUString& rString )
52 : {
53 743 : OUString aRetString = rString;
54 743 : sal_Int32 nQuotePos = aRetString.getLength();
55 1494 : while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
56 8 : aRetString = aRetString.replaceAt( nQuotePos, 1, "\"\"" );
57 743 : return "\"" + aRetString + "\"";
58 : }
59 :
60 533 : static OUString lclGenerateApiArray( const Matrix< Any >& rMatrix )
61 : {
62 : OSL_ENSURE( !rMatrix.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
63 533 : OUStringBuffer aBuffer;
64 533 : aBuffer.append( API_TOKEN_ARRAY_OPEN );
65 1066 : for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
66 : {
67 533 : if( nRow > 0 )
68 0 : aBuffer.append( API_TOKEN_ARRAY_ROWSEP );
69 2343 : for( Matrix< Any >::const_iterator aBeg = rMatrix.row_begin( nRow ), aIt = aBeg, aEnd = rMatrix.row_end( nRow ); aIt != aEnd; ++aIt )
70 : {
71 1810 : double fValue = 0.0;
72 1810 : OUString aString;
73 1810 : if( aIt != aBeg )
74 1277 : aBuffer.append( API_TOKEN_ARRAY_COLSEP );
75 1810 : if( *aIt >>= fValue )
76 1067 : aBuffer.append( fValue );
77 743 : else if( *aIt >>= aString )
78 743 : aBuffer.append( lclGenerateApiString( aString ) );
79 : else
80 0 : aBuffer.appendAscii( "\"\"" );
81 1810 : }
82 : }
83 533 : aBuffer.append( API_TOKEN_ARRAY_CLOSE );
84 533 : return aBuffer.makeStringAndClear();
85 : }
86 :
87 1056 : ChartConverter::ChartConverter()
88 : {
89 1056 : }
90 :
91 1967 : ChartConverter::~ChartConverter()
92 : {
93 1967 : }
94 :
95 165 : void ChartConverter::convertFromModel( XmlFilterBase& rFilter,
96 : ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
97 : const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
98 : {
99 : OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
100 165 : if( rxChartDoc.is() )
101 : {
102 165 : Reference< data::XDataReceiver > xDataReceiver( rxChartDoc, uno::UNO_QUERY_THROW );
103 330 : Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( rFilter.getModel(), uno::UNO_QUERY );
104 165 : if (xNumberFormatsSupplier.is())
105 156 : xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
106 :
107 330 : ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
108 330 : ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
109 330 : aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
110 : }
111 165 : }
112 :
113 95 : void ChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
114 : {
115 : try
116 : {
117 95 : if( !rxChartDoc->hasInternalDataProvider() )
118 95 : rxChartDoc->createInternalDataProvider( sal_False );
119 : }
120 0 : catch( Exception& )
121 : {
122 : }
123 95 : }
124 :
125 537 : Reference< XDataSequence > ChartConverter::createDataSequence(
126 : const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
127 : const OUString& rRole )
128 : {
129 537 : Reference< XDataSequence > xDataSeq;
130 537 : if( rxDataProvider.is() )
131 : {
132 537 : OUString aRangeRep;
133 537 : if( !rDataSeq.maData.empty() )
134 : {
135 : // create a single-row array from constant source data
136 533 : Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
137 533 : Matrix< Any >::iterator aMIt = aMatrix.begin();
138 : // TODO: how to handle missing values in the map?
139 2343 : for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
140 1810 : *aMIt = aDIt->second;
141 533 : aRangeRep = lclGenerateApiArray( aMatrix );
142 : }
143 :
144 537 : if( !aRangeRep.isEmpty() ) try
145 : {
146 : // create the data sequence
147 533 : xDataSeq = rxDataProvider->createDataSequenceByValueArray(rRole, aRangeRep);
148 533 : return xDataSeq;
149 : }
150 0 : catch( Exception& )
151 : {
152 : OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
153 4 : }
154 : }
155 :
156 4 : return 0;
157 : }
158 :
159 : } // namespace chart
160 : } // namespace drawingml
161 246 : } // namespace oox
162 :
163 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|