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