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 "excelchartconverter.hxx"
21 :
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/chart2/data/XDataProvider.hpp>
24 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
25 : #include <com/sun/star/chart2/data/XSheetDataProvider.hpp>
26 :
27 : #include <oox/core/filterbase.hxx>
28 : #include <oox/drawingml/chart/datasourcemodel.hxx>
29 : #include <oox/helper/containerhelper.hxx>
30 : #include "formulaparser.hxx"
31 :
32 : namespace oox {
33 : namespace xls {
34 :
35 : using namespace ::com::sun::star::chart2;
36 : using namespace ::com::sun::star::chart2::data;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::table;
39 : using namespace ::com::sun::star::uno;
40 :
41 : using ::oox::drawingml::chart::DataSequenceModel;
42 :
43 128 : ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
44 128 : WorkbookHelper( rHelper )
45 : {
46 128 : }
47 :
48 256 : ExcelChartConverter::~ExcelChartConverter()
49 : {
50 256 : }
51 :
52 34 : void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
53 : {
54 : try
55 : {
56 34 : Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
57 34 : Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
58 68 : "com.sun.star.chart2.data.DataProvider" ), UNO_QUERY_THROW );
59 68 : xDataRec->attachDataProvider( xDataProv );
60 : }
61 0 : catch( Exception& )
62 : {
63 : }
64 34 : }
65 :
66 126 : Reference< XDataSequence > ExcelChartConverter::createDataSequence(
67 : const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
68 : const OUString& /*rRole*/ )
69 : {
70 126 : Reference< XDataSequence > xDataSeq;
71 126 : if (!rxDataProvider.is())
72 0 : return xDataSeq;
73 :
74 252 : Reference<XSheetDataProvider> xSheetProvider(rxDataProvider, UNO_QUERY);
75 126 : if (!xSheetProvider.is())
76 0 : return xDataSeq;
77 :
78 126 : if (!rDataSeq.maFormula.isEmpty())
79 : {
80 : // parse the formula string, create a token sequence
81 106 : FormulaParser& rParser = getFormulaParser();
82 106 : CellAddress aBaseAddr( getCurrentSheetIndex(), 0, 0 );
83 106 : ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
84 :
85 : try
86 : {
87 : // create the data sequence
88 106 : xDataSeq = xSheetProvider->createDataSequenceByFormulaTokens(aTokens);
89 : }
90 0 : catch (Exception&)
91 : {
92 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
93 106 : }
94 : }
95 20 : else if (!rDataSeq.maData.empty())
96 : {
97 : // create a single-row array from constant source data
98 20 : Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
99 20 : Matrix< Any >::iterator aMIt = aMatrix.begin();
100 : // TODO: how to handle missing values in the map?
101 98 : for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
102 78 : *aMIt = aDIt->second;
103 40 : OUString aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
104 :
105 20 : if (!aRangeRep.isEmpty())
106 : {
107 : try
108 : {
109 : // create the data sequence
110 20 : xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
111 : }
112 0 : catch (Exception&)
113 : {
114 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
115 : }
116 20 : }
117 : }
118 126 : return xDataSeq;
119 : }
120 :
121 : } // namespace xls
122 48 : } // namespace oox
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|