1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/drawingml/chart/chartspaceconverter.hxx"
29 
30 #include <com/sun/star/chart/MissingValueTreatment.hpp>
31 #include <com/sun/star/chart/XChartDocument.hpp>
32 #include <com/sun/star/chart2/XChartDocument.hpp>
33 #include <com/sun/star/chart2/XTitled.hpp>
34 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
35 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
36 #include "oox/core/xmlfilterbase.hxx"
37 #include "oox/drawingml/chart/chartconverter.hxx"
38 #include "oox/drawingml/chart/chartdrawingfragment.hxx"
39 #include "oox/drawingml/chart/chartspacemodel.hxx"
40 #include "oox/drawingml/chart/plotareaconverter.hxx"
41 #include "oox/drawingml/chart/titleconverter.hxx"
42 
43 using ::rtl::OUString;
44 using ::com::sun::star::awt::Point;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::UNO_QUERY;
48 using ::com::sun::star::uno::UNO_QUERY_THROW;
49 using ::com::sun::star::uno::makeAny;
50 using ::com::sun::star::util::XNumberFormatsSupplier;
51 using ::com::sun::star::drawing::XDrawPageSupplier;
52 using ::com::sun::star::drawing::XShapes;
53 using ::com::sun::star::chart2::XDiagram;
54 using ::com::sun::star::chart2::XTitled;
55 using ::com::sun::star::chart2::data::XDataReceiver;
56 using ::com::sun::star::beans::XPropertySet;
57 
58 namespace oox {
59 namespace drawingml {
60 namespace chart {
61 
62 // ============================================================================
63 
64 using namespace ::com::sun::star::awt;
65 using namespace ::com::sun::star::chart2;
66 using namespace ::com::sun::star::chart2::data;
67 using namespace ::com::sun::star::drawing;
68 using namespace ::com::sun::star::uno;
69 using namespace ::com::sun::star::util;
70 
71 using ::rtl::OUString;
72 
73 // ============================================================================
74 
75 ChartSpaceConverter::ChartSpaceConverter( const ConverterRoot& rParent, ChartSpaceModel& rModel ) :
76     ConverterBase< ChartSpaceModel >( rParent, rModel )
77 {
78 }
79 
80 ChartSpaceConverter::~ChartSpaceConverter()
81 {
82 }
83 
84 void ChartSpaceConverter::convertFromModel( const Reference< XShapes >& rxExternalPage, const Point& rChartPos )
85 {
86     /*  create data provider (virtual function in the ChartConverter class,
87         derived converters may create an external data provider) */
88     getChartConverter().createDataProvider( getChartDocument() );
89 
90     // attach number formatter of container document to data receiver
91     try
92     {
93         Reference< XDataReceiver > xDataRec( getChartDocument(), UNO_QUERY_THROW );
94         Reference< XNumberFormatsSupplier > xNumFmtSupp( getFilter().getModel(), UNO_QUERY_THROW );
95         xDataRec->attachNumberFormatsSupplier( xNumFmtSupp );
96     }
97     catch( Exception& )
98     {
99     }
100 
101     // formatting of the chart background
102     PropertySet aBackPropSet( getChartDocument()->getPageBackground() );
103     getFormatter().convertFrameFormatting( aBackPropSet, mrModel.mxShapeProp, OBJECTTYPE_CHARTSPACE );
104 
105     // convert plot area (container of all chart type groups)
106     PlotAreaConverter aPlotAreaConv( *this, mrModel.mxPlotArea.getOrCreate() );
107     aPlotAreaConv.convertFromModel( mrModel.mxView3D.getOrCreate() );
108 
109     // plot area converter has created the diagram object
110     Reference< XDiagram > xDiagram = getChartDocument()->getFirstDiagram();
111 
112     // convert wall and floor formatting in 3D charts
113     if( xDiagram.is() && aPlotAreaConv.isWall3dChart() )
114     {
115         WallFloorConverter aFloorConv( *this, mrModel.mxFloor.getOrCreate() );
116         aFloorConv.convertFromModel( xDiagram, OBJECTTYPE_FLOOR );
117 
118         WallFloorConverter aWallConv( *this, mrModel.mxBackWall.getOrCreate() );
119         aWallConv.convertFromModel( xDiagram, OBJECTTYPE_WALL );
120     }
121 
122     // chart title
123     if( !mrModel.mbAutoTitleDel ) try
124     {
125         /*  If the title model is missing, but the chart shows exactly one
126             series, the series title is shown as chart title. */
127         OUString aAutoTitle = aPlotAreaConv.getAutomaticTitle();
128         if( mrModel.mxTitle.is() || (aAutoTitle.getLength() > 0) )
129         {
130             if( aAutoTitle.getLength() == 0 )
131                 aAutoTitle = CREATE_OUSTRING( "Chart Title" );
132             Reference< XTitled > xTitled( getChartDocument(), UNO_QUERY_THROW );
133             TitleConverter aTitleConv( *this, mrModel.mxTitle.getOrCreate() );
134             aTitleConv.convertFromModel( xTitled, aAutoTitle, OBJECTTYPE_CHARTTITLE );
135         }
136     }
137     catch( Exception& )
138     {
139     }
140 
141     // legend
142     if( xDiagram.is() && mrModel.mxLegend.is() )
143     {
144         LegendConverter aLegendConv( *this, *mrModel.mxLegend );
145         aLegendConv.convertFromModel( xDiagram );
146     }
147 
148     // treatment of missing values
149     if( xDiagram.is() )
150     {
151         using namespace ::com::sun::star::chart::MissingValueTreatment;
152         sal_Int32 nMissingValues = LEAVE_GAP;
153         switch( mrModel.mnDispBlanksAs )
154         {
155             case XML_gap:   nMissingValues = LEAVE_GAP; break;
156             case XML_zero:  nMissingValues = USE_ZERO;  break;
157             case XML_span:  nMissingValues = CONTINUE;  break;
158         }
159         PropertySet aDiaProp( xDiagram );
160         aDiaProp.setProperty( PROP_MissingValueTreatment, nMissingValues );
161     }
162 
163     /*  Following all conversions needing the old Chart1 API that involves full
164         initialization of the chart view. */
165     namespace cssc = ::com::sun::star::chart;
166     Reference< cssc::XChartDocument > xChart1Doc( getChartDocument(), UNO_QUERY );
167     if( xChart1Doc.is() )
168     {
169         /*  Set the IncludeHiddenCells property via the old API as only this
170             ensures that the data provider and all created sequences get this
171             flag correctly. */
172         PropertySet aDiaProp( xChart1Doc->getDiagram() );
173         aDiaProp.setProperty( PROP_IncludeHiddenCells, !mrModel.mbPlotVisOnly );
174 
175         // plot area position and size
176         aPlotAreaConv.convertPositionFromModel();
177 
178         // positions of main title and all axis titles
179         convertTitlePositions();
180     }
181 
182     // embedded drawing shapes
183     if( mrModel.maDrawingPath.getLength() > 0 ) try
184     {
185         /*  Get the internal draw page of the chart document, if no external
186             drawing page has been passed. */
187         Reference< XShapes > xShapes;
188         Point aShapesOffset( 0, 0 );
189         if( rxExternalPage.is() )
190         {
191             xShapes = rxExternalPage;
192             // offset for embedded shapes to move them inside the chart area
193             aShapesOffset = rChartPos;
194         }
195         else
196         {
197             Reference< XDrawPageSupplier > xDrawPageSupp( getChartDocument(), UNO_QUERY_THROW );
198             xShapes.set( xDrawPageSupp->getDrawPage(), UNO_QUERY_THROW );
199         }
200 
201         /*  If an external drawing page is passed, all embedded shapes will be
202             inserted there (used e.g. with 'chart sheets' in spreadsheet
203             documents). In this case, all types of shapes including OLE objects
204             are supported. If the shapes are inserted into the internal chart
205             drawing page instead, it is not possible to embed OLE objects. */
206         bool bOleSupport = rxExternalPage.is();
207 
208         // now, xShapes is not null anymore
209         getFilter().importFragment( new ChartDrawingFragment(
210             getFilter(), mrModel.maDrawingPath, xShapes, getChartSize(), aShapesOffset, bOleSupport ) );
211     }
212     catch( Exception& )
213     {
214     }
215 
216     // pivot chart
217     if ( mrModel.mbPivotChart )
218     {
219         PropertySet aProps( getChartDocument() );
220         aProps.setProperty( PROP_DisableDataTableDialog , true );
221         aProps.setProperty( PROP_DisableComplexChartTypes , true );
222     }
223 }
224 
225 // ============================================================================
226 
227 } // namespace chart
228 } // namespace drawingml
229 } // namespace oox
230