1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <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>
31 using ::oox::drawingml::chart::DataSequenceModel
;
32 using ::com::sun::star::uno::Any
;
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
;
43 using ::oox::core::XmlFilterBase
;
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
= ';';
50 // Code similar to oox/source/xls/formulabase.cxx
51 static OUString
lclGenerateApiString( const OUString
& rString
)
53 OUString aRetString
= rString
;
54 sal_Int32 nQuotePos
= aRetString
.getLength();
55 while( (nQuotePos
= aRetString
.lastIndexOf( '"', nQuotePos
)) >= 0 )
56 aRetString
= aRetString
.replaceAt( nQuotePos
, 1, "\"\"" );
57 return "\"" + aRetString
+ "\"";
60 static OUString
lclGenerateApiArray( const Matrix
< Any
>& rMatrix
)
62 OSL_ENSURE( !rMatrix
.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
63 OUStringBuffer aBuffer
;
64 aBuffer
.append( API_TOKEN_ARRAY_OPEN
);
65 for( size_t nRow
= 0, nHeight
= rMatrix
.height(); nRow
< nHeight
; ++nRow
)
68 aBuffer
.append( API_TOKEN_ARRAY_ROWSEP
);
69 for( Matrix
< Any
>::const_iterator aBeg
= rMatrix
.row_begin( nRow
), aIt
= aBeg
, aEnd
= rMatrix
.row_end( nRow
); aIt
!= aEnd
; ++aIt
)
74 aBuffer
.append( API_TOKEN_ARRAY_COLSEP
);
76 aBuffer
.append( fValue
);
77 else if( *aIt
>>= aString
)
78 aBuffer
.append( lclGenerateApiString( aString
) );
80 aBuffer
.appendAscii( "\"\"" );
83 aBuffer
.append( API_TOKEN_ARRAY_CLOSE
);
84 return aBuffer
.makeStringAndClear();
87 ChartConverter::ChartConverter()
91 ChartConverter::~ChartConverter()
95 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
)
99 OSL_ENSURE( rxChartDoc
.is(), "ChartConverter::convertFromModel - missing chart document" );
100 if( rxChartDoc
.is() )
102 Reference
< data::XDataReceiver
> xDataReceiver( rxChartDoc
, uno::UNO_QUERY_THROW
);
103 Reference
< util::XNumberFormatsSupplier
> xNumberFormatsSupplier( rFilter
.getModel(), uno::UNO_QUERY
);
104 if (xNumberFormatsSupplier
.is())
105 xDataReceiver
->attachNumberFormatsSupplier( xNumberFormatsSupplier
);
107 ConverterRoot
aConvBase( rFilter
, *this, rChartModel
, rxChartDoc
, rChartSize
);
108 ChartSpaceConverter
aSpaceConv( aConvBase
, rChartModel
);
109 aSpaceConv
.convertFromModel( rxExternalPage
, rChartPos
);
113 void ChartConverter::createDataProvider( const Reference
< XChartDocument
>& rxChartDoc
)
117 if( !rxChartDoc
->hasInternalDataProvider() )
118 rxChartDoc
->createInternalDataProvider( sal_False
);
125 Reference
< XDataSequence
> ChartConverter::createDataSequence(
126 const Reference
< XDataProvider
>& rxDataProvider
, const DataSequenceModel
& rDataSeq
,
127 const OUString
& rRole
)
129 Reference
< XDataSequence
> xDataSeq
;
130 if( rxDataProvider
.is() )
133 if( !rDataSeq
.maData
.empty() )
135 // create a single-row array from constant source data
136 Matrix
< Any
> aMatrix( rDataSeq
.maData
.size(), 1 );
137 Matrix
< Any
>::iterator aMIt
= aMatrix
.begin();
138 // TODO: how to handle missing values in the map?
139 for( DataSequenceModel::AnyMap::const_iterator aDIt
= rDataSeq
.maData
.begin(), aDEnd
= rDataSeq
.maData
.end(); aDIt
!= aDEnd
; ++aDIt
, ++aMIt
)
140 *aMIt
= aDIt
->second
;
141 aRangeRep
= lclGenerateApiArray( aMatrix
);
144 if( !aRangeRep
.isEmpty() ) try
146 // create the data sequence
147 xDataSeq
= rxDataProvider
->createDataSequenceByValueArray(rRole
, aRangeRep
);
152 OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
160 } // namespace drawingml
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */