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