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/converterbase.hxx"
21 :
22 : #include <com/sun/star/chart/XAxisXSupplier.hpp>
23 : #include <com/sun/star/chart/XAxisYSupplier.hpp>
24 : #include <com/sun/star/chart/XAxisZSupplier.hpp>
25 : #include <com/sun/star/chart/XChartDocument.hpp>
26 : #include <com/sun/star/chart/XSecondAxisTitleSupplier.hpp>
27 : #include <com/sun/star/chart2/XChartDocument.hpp>
28 : #include <com/sun/star/chart2/RelativePosition.hpp>
29 : #include <com/sun/star/chart2/RelativeSize.hpp>
30 : #include <com/sun/star/drawing/FillStyle.hpp>
31 : #include <com/sun/star/drawing/LineStyle.hpp>
32 : #include <com/sun/star/frame/XModel.hpp>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include "basegfx/numeric/ftools.hxx"
35 : #include "oox/core/xmlfilterbase.hxx"
36 : #include "oox/drawingml/theme.hxx"
37 : #include <comphelper/processfactory.hxx>
38 :
39 : namespace oox {
40 : namespace drawingml {
41 : namespace chart {
42 :
43 :
44 :
45 : namespace cssc = ::com::sun::star::chart;
46 :
47 : using namespace ::com::sun::star;
48 : using namespace ::com::sun::star::chart2;
49 : using namespace ::com::sun::star::drawing;
50 : using namespace ::com::sun::star::frame;
51 : using namespace ::com::sun::star::lang;
52 : using namespace ::com::sun::star::uno;
53 :
54 : using ::oox::core::XmlFilterBase;
55 :
56 :
57 :
58 : namespace {
59 :
60 : struct TitleKey : public ::std::pair< ObjectType, ::std::pair< sal_Int32, sal_Int32 > >
61 : {
62 0 : inline explicit TitleKey( ObjectType eObjType, sal_Int32 nMainIdx = -1, sal_Int32 nSubIdx = -1 )
63 0 : { first = eObjType; second.first = nMainIdx; second.second = nSubIdx; }
64 : };
65 :
66 :
67 :
68 : /** A helper structure to store all data related to title objects. Needed for
69 : the conversion of manual title positions that needs the old Chart1 API.
70 : */
71 0 : struct TitleLayoutInfo
72 : {
73 : typedef Reference< XShape > (*GetShapeFunc)( const Reference< cssc::XChartDocument >& );
74 :
75 : Reference< XTitle > mxTitle; /// The API title object.
76 : ModelRef< LayoutModel > mxLayout; /// The layout model, if existing.
77 : GetShapeFunc mpGetShape; /// Helper function to receive the title shape.
78 :
79 0 : inline explicit TitleLayoutInfo() : mpGetShape( 0 ) {}
80 :
81 : void convertTitlePos(
82 : ConverterRoot& rRoot,
83 : const Reference< cssc::XChartDocument >& rxChart1Doc );
84 : };
85 :
86 0 : void TitleLayoutInfo::convertTitlePos( ConverterRoot& rRoot, const Reference< cssc::XChartDocument >& rxChart1Doc )
87 : {
88 0 : if( mxTitle.is() && mpGetShape ) try
89 : {
90 : // try to get the title shape
91 0 : Reference< XShape > xTitleShape = mpGetShape( rxChart1Doc );
92 : // get title rotation angle, needed for correction of position of top-left edge
93 0 : double fAngle = 0.0;
94 0 : PropertySet aTitleProp( mxTitle );
95 0 : aTitleProp.getProperty( fAngle, PROP_TextRotation );
96 : // convert the position
97 0 : LayoutModel& rLayout = mxLayout.getOrCreate();
98 0 : LayoutConverter aLayoutConv( rRoot, rLayout );
99 0 : aLayoutConv.convertFromModel( xTitleShape, fAngle );
100 : }
101 0 : catch( Exception& )
102 : {
103 : }
104 0 : }
105 :
106 :
107 :
108 : /* The following local functions implement getting the XShape interface of all
109 : supported title objects (chart and axes). This needs some effort due to the
110 : design of the old Chart1 API used to access these objects. */
111 :
112 : /** A code fragment that returns a shape object from the passed shape supplier
113 : using the specified interface function. Checks a boolean property first. */
114 : #define OOX_FRAGMENT_GETTITLESHAPE( shape_supplier, supplier_func, property_name ) \
115 : PropertySet aPropSet( shape_supplier ); \
116 : if( shape_supplier.is() && aPropSet.getBoolProperty( PROP_##property_name ) ) \
117 : return shape_supplier->supplier_func(); \
118 : return Reference< XShape >(); \
119 :
120 : /** Implements a function returning the drawing shape of an axis title, if
121 : existing, using the specified API interface and its function. */
122 : #define OOX_DEFINEFUNC_GETAXISTITLESHAPE( func_name, interface_type, supplier_func, property_name ) \
123 : Reference< XShape > func_name( const Reference< cssc::XChartDocument >& rxChart1Doc ) \
124 : { \
125 : Reference< cssc::interface_type > xAxisSupp( rxChart1Doc->getDiagram(), UNO_QUERY ); \
126 : OOX_FRAGMENT_GETTITLESHAPE( xAxisSupp, supplier_func, property_name ) \
127 : }
128 :
129 : /** Returns the drawing shape of the main title, if existing. */
130 0 : Reference< XShape > lclGetMainTitleShape( const Reference< cssc::XChartDocument >& rxChart1Doc )
131 : {
132 0 : OOX_FRAGMENT_GETTITLESHAPE( rxChart1Doc, getTitle, HasMainTitle )
133 : }
134 :
135 0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetXAxisTitleShape, XAxisXSupplier, getXAxisTitle, HasXAxisTitle )
136 0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetYAxisTitleShape, XAxisYSupplier, getYAxisTitle, HasYAxisTitle )
137 0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetZAxisTitleShape, XAxisZSupplier, getZAxisTitle, HasZAxisTitle )
138 0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetSecXAxisTitleShape, XSecondAxisTitleSupplier, getSecondXAxisTitle, HasSecondaryXAxisTitle )
139 0 : OOX_DEFINEFUNC_GETAXISTITLESHAPE( lclGetSecYAxisTitleShape, XSecondAxisTitleSupplier, getSecondYAxisTitle, HasSecondaryYAxisTitle )
140 :
141 : #undef OOX_DEFINEFUNC_GETAXISTITLESHAPE
142 : #undef OOX_IMPLEMENT_GETTITLESHAPE
143 :
144 : } // namespace
145 :
146 :
147 :
148 : struct ConverterData
149 : {
150 : typedef ::std::map< TitleKey, TitleLayoutInfo > TitleMap;
151 :
152 : ObjectFormatter maFormatter;
153 : TitleMap maTitles;
154 : XmlFilterBase& mrFilter;
155 : ChartConverter& mrConverter;
156 : Reference< XChartDocument > mxDoc;
157 : awt::Size maSize;
158 :
159 : explicit ConverterData(
160 : XmlFilterBase& rFilter,
161 : ChartConverter& rChartConverter,
162 : const ChartSpaceModel& rChartModel,
163 : const Reference< XChartDocument >& rxChartDoc,
164 : const awt::Size& rChartSize );
165 : ~ConverterData();
166 : };
167 :
168 :
169 :
170 0 : ConverterData::ConverterData(
171 : XmlFilterBase& rFilter,
172 : ChartConverter& rChartConverter,
173 : const ChartSpaceModel& rChartModel,
174 : const Reference< XChartDocument >& rxChartDoc,
175 : const awt::Size& rChartSize ) :
176 : maFormatter( rFilter, rxChartDoc, rChartModel ),
177 : mrFilter( rFilter ),
178 : mrConverter( rChartConverter ),
179 : mxDoc( rxChartDoc ),
180 0 : maSize( rChartSize )
181 : {
182 : OSL_ENSURE( mxDoc.is(), "ConverterData::ConverterData - missing chart document" );
183 : // lock the model to suppress internal updates during conversion
184 : try
185 : {
186 0 : mxDoc->lockControllers();
187 : }
188 0 : catch( Exception& )
189 : {
190 : }
191 :
192 : // prepare conversion of title positions
193 0 : maTitles[ TitleKey( OBJECTTYPE_CHARTTITLE ) ].mpGetShape = lclGetMainTitleShape;
194 0 : maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_X_AXIS ) ].mpGetShape = lclGetXAxisTitleShape;
195 0 : maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_Y_AXIS ) ].mpGetShape = lclGetYAxisTitleShape;
196 0 : maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_PRIM_AXESSET, API_Z_AXIS ) ].mpGetShape = lclGetZAxisTitleShape;
197 0 : maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_SECN_AXESSET, API_X_AXIS ) ].mpGetShape = lclGetSecXAxisTitleShape;
198 0 : maTitles[ TitleKey( OBJECTTYPE_AXISTITLE, API_SECN_AXESSET, API_Y_AXIS ) ].mpGetShape = lclGetSecYAxisTitleShape;
199 0 : }
200 :
201 0 : ConverterData::~ConverterData()
202 : {
203 : // unlock the model
204 : try
205 : {
206 0 : mxDoc->unlockControllers();
207 : }
208 0 : catch( Exception& )
209 : {
210 : }
211 0 : }
212 :
213 :
214 :
215 0 : ConverterRoot::ConverterRoot(
216 : XmlFilterBase& rFilter,
217 : ChartConverter& rChartConverter,
218 : const ChartSpaceModel& rChartModel,
219 : const Reference< XChartDocument >& rxChartDoc,
220 : const awt::Size& rChartSize ) :
221 0 : mxData( new ConverterData( rFilter, rChartConverter, rChartModel, rxChartDoc, rChartSize ) )
222 : {
223 0 : }
224 :
225 0 : ConverterRoot::~ConverterRoot()
226 : {
227 0 : }
228 :
229 0 : Reference< XInterface > ConverterRoot::createInstance( const OUString& rServiceName ) const
230 : {
231 0 : Reference< XInterface > xInt;
232 : try
233 : {
234 0 : Reference<XMultiServiceFactory> xMSF = Reference<XMultiServiceFactory>(getComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW);
235 :
236 0 : xInt = xMSF->createInstance( rServiceName );
237 : }
238 0 : catch( Exception& )
239 : {
240 : }
241 : OSL_ENSURE( xInt.is(), "ConverterRoot::createInstance - cannot create instance" );
242 0 : return xInt;
243 : }
244 :
245 0 : Reference< XComponentContext > ConverterRoot::getComponentContext() const
246 : {
247 0 : return mxData->mrFilter.getComponentContext();
248 : }
249 :
250 0 : XmlFilterBase& ConverterRoot::getFilter() const
251 : {
252 0 : return mxData->mrFilter;
253 : }
254 :
255 0 : ChartConverter* ConverterRoot::getChartConverter() const
256 : {
257 0 : return &mxData->mrConverter;
258 : }
259 :
260 0 : Reference< XChartDocument > ConverterRoot::getChartDocument() const
261 : {
262 0 : return mxData->mxDoc;
263 : }
264 :
265 0 : const awt::Size& ConverterRoot::getChartSize() const
266 : {
267 0 : return mxData->maSize;
268 : }
269 :
270 0 : ObjectFormatter& ConverterRoot::getFormatter() const
271 : {
272 0 : return mxData->maFormatter;
273 : }
274 :
275 0 : void ConverterRoot::registerTitleLayout( const Reference< XTitle >& rxTitle,
276 : const ModelRef< LayoutModel >& rxLayout, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
277 : {
278 : OSL_ENSURE( rxTitle.is(), "ConverterRoot::registerTitleLayout - missing title object" );
279 0 : TitleLayoutInfo& rTitleInfo = mxData->maTitles[ TitleKey( eObjType, nMainIdx, nSubIdx ) ];
280 : OSL_ENSURE( rTitleInfo.mpGetShape, "ConverterRoot::registerTitleLayout - invalid title key" );
281 0 : rTitleInfo.mxTitle = rxTitle;
282 0 : rTitleInfo.mxLayout = rxLayout;
283 0 : }
284 :
285 0 : void ConverterRoot::convertTitlePositions()
286 : {
287 : try
288 : {
289 0 : Reference< cssc::XChartDocument > xChart1Doc( mxData->mxDoc, UNO_QUERY_THROW );
290 0 : for( ConverterData::TitleMap::iterator aIt = mxData->maTitles.begin(), aEnd = mxData->maTitles.end(); aIt != aEnd; ++aIt )
291 0 : aIt->second.convertTitlePos( *this, xChart1Doc );
292 : }
293 0 : catch( Exception& )
294 : {
295 : }
296 0 : }
297 :
298 :
299 :
300 : namespace {
301 :
302 : /** Returns a position value in the chart area in 1/100 mm. */
303 0 : sal_Int32 lclCalcPosition( sal_Int32 nChartSize, double fPos, sal_Int32 nPosMode )
304 : {
305 0 : switch( nPosMode )
306 : {
307 : case XML_edge: // absolute start position as factor of chart size
308 0 : return getLimitedValue< sal_Int32, double >( nChartSize * fPos + 0.5, 0, nChartSize );
309 : case XML_factor: // position relative to object default position
310 : OSL_FAIL( "lclCalcPosition - relative positioning not supported" );
311 0 : return -1;
312 : };
313 :
314 : OSL_FAIL( "lclCalcPosition - unknown positioning mode" );
315 0 : return -1;
316 : }
317 :
318 : /** Returns a size value in the chart area in 1/100 mm. */
319 0 : sal_Int32 lclCalcSize( sal_Int32 nPos, sal_Int32 nChartSize, double fSize, sal_Int32 nSizeMode )
320 : {
321 0 : sal_Int32 nValue = getLimitedValue< sal_Int32, double >( nChartSize * fSize + 0.5, 0, nChartSize );
322 0 : switch( nSizeMode )
323 : {
324 : case XML_factor: // passed value is width/height
325 0 : return nValue;
326 : case XML_edge: // passed value is right/bottom position
327 0 : return nValue - nPos + 1;
328 : };
329 :
330 : OSL_FAIL( "lclCalcSize - unknown size mode" );
331 0 : return -1;
332 : }
333 :
334 : /** Returns a relative size value in the chart area. */
335 0 : double lclCalcRelSize( double fPos, double fSize, sal_Int32 nSizeMode )
336 : {
337 0 : switch( nSizeMode )
338 : {
339 : case XML_factor: // passed value is width/height
340 0 : break;
341 : case XML_edge: // passed value is right/bottom position
342 0 : fSize -= fPos;
343 0 : break;
344 : default:
345 : OSL_ENSURE( false, "lclCalcRelSize - unknown size mode" );
346 0 : fSize = 0.0;
347 : };
348 0 : return getLimitedValue< double, double >( fSize, 0.0, 1.0 - fPos );
349 : }
350 :
351 : } // namespace
352 :
353 :
354 :
355 0 : LayoutConverter::LayoutConverter( const ConverterRoot& rParent, LayoutModel& rModel ) :
356 0 : ConverterBase< LayoutModel >( rParent, rModel )
357 : {
358 0 : }
359 :
360 0 : LayoutConverter::~LayoutConverter()
361 : {
362 0 : }
363 :
364 0 : bool LayoutConverter::calcAbsRectangle( awt::Rectangle& orRect ) const
365 : {
366 0 : if( !mrModel.mbAutoLayout )
367 : {
368 0 : const awt::Size& rChartSize = getChartSize();
369 0 : orRect.X = lclCalcPosition( rChartSize.Width, mrModel.mfX, mrModel.mnXMode );
370 0 : orRect.Y = lclCalcPosition( rChartSize.Height, mrModel.mfY, mrModel.mnYMode );
371 0 : if( (orRect.X >= 0) && (orRect.Y >= 0) )
372 : {
373 0 : orRect.Width = lclCalcSize( orRect.X, rChartSize.Width, mrModel.mfW, mrModel.mnWMode );
374 0 : orRect.Height = lclCalcSize( orRect.Y, rChartSize.Height, mrModel.mfH, mrModel.mnHMode );
375 0 : return (orRect.Width > 0) && (orRect.Height > 0);
376 : }
377 : }
378 0 : return false;
379 : }
380 :
381 0 : bool LayoutConverter::convertFromModel( PropertySet& rPropSet )
382 : {
383 0 : if( !mrModel.mbAutoLayout &&
384 0 : (mrModel.mnXMode == XML_edge) && (mrModel.mfX >= 0.0) &&
385 0 : (mrModel.mnYMode == XML_edge) && (mrModel.mfY >= 0.0) )
386 : {
387 : RelativePosition aPos(
388 0 : getLimitedValue< double, double >( mrModel.mfX, 0.0, 1.0 ),
389 0 : getLimitedValue< double, double >( mrModel.mfY, 0.0, 1.0 ),
390 0 : Alignment_TOP_LEFT );
391 0 : rPropSet.setProperty( PROP_RelativePosition, aPos );
392 :
393 : RelativeSize aSize(
394 0 : lclCalcRelSize( aPos.Primary, mrModel.mfW, mrModel.mnWMode ),
395 0 : lclCalcRelSize( aPos.Secondary, mrModel.mfH, mrModel.mnHMode ) );
396 0 : if( (aSize.Primary > 0.0) && (aSize.Secondary > 0.0) )
397 : {
398 0 : rPropSet.setProperty( PROP_RelativeSize, aSize );
399 0 : return true;
400 : }
401 : }
402 0 : return false;
403 : }
404 :
405 0 : bool LayoutConverter::convertFromModel( const Reference< XShape >& rxShape, double fRotationAngle )
406 : {
407 0 : if( !mrModel.mbAutoLayout )
408 : {
409 0 : const awt::Size& rChartSize = getChartSize();
410 : awt::Point aShapePos(
411 0 : lclCalcPosition( rChartSize.Width, mrModel.mfX, mrModel.mnXMode ),
412 0 : lclCalcPosition( rChartSize.Height, mrModel.mfY, mrModel.mnYMode ) );
413 0 : if( (aShapePos.X >= 0) && (aShapePos.Y >= 0) )
414 : {
415 : // the call to XShape.getSize() may recalc the chart view
416 0 : awt::Size aShapeSize = rxShape->getSize();
417 : // rotated shapes need special handling...
418 0 : double fSin = fabs( sin( fRotationAngle * F_PI180 ) );
419 : // add part of height to X direction, if title is rotated down
420 0 : if( fRotationAngle > 180.0 )
421 0 : aShapePos.X += static_cast< sal_Int32 >( fSin * aShapeSize.Height + 0.5 );
422 : // add part of width to Y direction, if title is rotated up
423 0 : else if( fRotationAngle > 0.0 )
424 0 : aShapePos.Y += static_cast< sal_Int32 >( fSin * aShapeSize.Width + 0.5 );
425 : // set the resulting position at the shape
426 0 : rxShape->setPosition( aShapePos );
427 0 : return true;
428 : }
429 : }
430 0 : return false;
431 : }
432 :
433 :
434 :
435 : } // namespace chart
436 : } // namespace drawingml
437 0 : } // namespace oox
438 :
439 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|