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 "VLegend.hxx"
21 : #include "macros.hxx"
22 : #include "PropertyMapper.hxx"
23 : #include "CommonConverters.hxx"
24 : #include "ObjectIdentifier.hxx"
25 : #include "RelativePositionHelper.hxx"
26 : #include "AbstractShapeFactory.hxx"
27 : #include "RelativeSizeHelper.hxx"
28 : #include "LegendEntryProvider.hxx"
29 : #include "chartview/DrawModelWrapper.hxx"
30 : #include <com/sun/star/text/XTextRange.hpp>
31 : #include <com/sun/star/text/WritingMode2.hpp>
32 : #include <com/sun/star/beans/XPropertySet.hpp>
33 : #include <com/sun/star/beans/XPropertyState.hpp>
34 : #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
35 : #include <com/sun/star/drawing/LineJoint.hpp>
36 : #include <com/sun/star/chart/ChartLegendExpansion.hpp>
37 : #include <com/sun/star/chart2/LegendPosition.hpp>
38 : #include <com/sun/star/chart2/RelativePosition.hpp>
39 : #include <rtl/ustrbuf.hxx>
40 : #include <svl/languageoptions.hxx>
41 :
42 : #include <vector>
43 : #include <algorithm>
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::chart2;
47 :
48 : using ::com::sun::star::uno::Reference;
49 : using ::com::sun::star::uno::Sequence;
50 :
51 : namespace chart
52 : {
53 :
54 : namespace
55 : {
56 :
57 : typedef ::std::pair< ::chart::tNameSequence, ::chart::tAnySequence > tPropertyValues;
58 :
59 : typedef ::std::vector< ViewLegendEntry > tViewLegendEntryContainer;
60 :
61 426 : double lcl_CalcViewFontSize(
62 : const Reference< beans::XPropertySet > & xProp,
63 : const awt::Size & rReferenceSize )
64 : {
65 426 : double fResult = 10.0;
66 :
67 426 : awt::Size aPropRefSize;
68 426 : float fFontHeight( 0.0 );
69 426 : if( xProp.is() && ( xProp->getPropertyValue( "CharHeight") >>= fFontHeight ))
70 : {
71 426 : fResult = fFontHeight;
72 : try
73 : {
74 426 : if( (xProp->getPropertyValue( "ReferencePageSize") >>= aPropRefSize) &&
75 0 : (aPropRefSize.Height > 0))
76 : {
77 0 : fResult = ::chart::RelativeSizeHelper::calculate( fFontHeight, aPropRefSize, rReferenceSize );
78 : }
79 : }
80 0 : catch( const uno::Exception & ex )
81 : {
82 : ASSERT_EXCEPTION( ex );
83 : }
84 : }
85 :
86 : // pt -> 1/100th mm
87 426 : return (fResult * (2540.0 / 72.0));
88 : }
89 :
90 426 : void lcl_getProperties(
91 : const Reference< beans::XPropertySet > & xLegendProp,
92 : tPropertyValues & rOutLineFillProperties,
93 : tPropertyValues & rOutTextProperties,
94 : const awt::Size & rReferenceSize )
95 : {
96 : // Get Line- and FillProperties from model legend
97 426 : if( xLegendProp.is())
98 : {
99 : // set rOutLineFillProperties
100 426 : ::chart::tPropertyNameValueMap aLineFillValueMap;
101 426 : ::chart::PropertyMapper::getValueMap( aLineFillValueMap, ::chart::PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xLegendProp );
102 :
103 426 : aLineFillValueMap[ "LineJoint" ] = uno::makeAny( drawing::LineJoint_ROUND );
104 :
105 : ::chart::PropertyMapper::getMultiPropertyListsFromValueMap(
106 426 : rOutLineFillProperties.first, rOutLineFillProperties.second, aLineFillValueMap );
107 :
108 : // set rOutTextProperties
109 852 : ::chart::tPropertyNameValueMap aTextValueMap;
110 426 : ::chart::PropertyMapper::getValueMap( aTextValueMap, ::chart::PropertyMapper::getPropertyNameMapForCharacterProperties(), xLegendProp );
111 :
112 426 : drawing::TextHorizontalAdjust eHorizAdjust( drawing::TextHorizontalAdjust_LEFT );
113 426 : aTextValueMap[ "TextAutoGrowHeight" ] = uno::makeAny( sal_True );
114 426 : aTextValueMap[ "TextAutoGrowWidth" ] = uno::makeAny( sal_True );
115 426 : aTextValueMap[ "TextHorizontalAdjust" ] = uno::makeAny( eHorizAdjust );
116 426 : aTextValueMap[ "TextMaximumFrameWidth" ] = uno::makeAny( rReferenceSize.Width ); //needs to be overwritten by actual available space in the legend
117 :
118 : // recalculate font size
119 426 : awt::Size aPropRefSize;
120 426 : float fFontHeight( 0.0 );
121 1704 : if( (xLegendProp->getPropertyValue( "ReferencePageSize") >>= aPropRefSize) &&
122 1278 : (aPropRefSize.Height > 0) &&
123 426 : (aTextValueMap[ "CharHeight" ] >>= fFontHeight) )
124 : {
125 0 : aTextValueMap[ "CharHeight" ] = uno::makeAny(
126 : static_cast< float >(
127 0 : ::chart::RelativeSizeHelper::calculate( fFontHeight, aPropRefSize, rReferenceSize )));
128 :
129 0 : if( aTextValueMap[ "CharHeightAsian" ] >>= fFontHeight )
130 : {
131 0 : aTextValueMap[ "CharHeightAsian" ] = uno::makeAny(
132 : static_cast< float >(
133 0 : ::chart::RelativeSizeHelper::calculate( fFontHeight, aPropRefSize, rReferenceSize )));
134 : }
135 0 : if( aTextValueMap[ "CharHeightComplex" ] >>= fFontHeight )
136 : {
137 0 : aTextValueMap[ "CharHeightComplex" ] = uno::makeAny(
138 : static_cast< float >(
139 0 : ::chart::RelativeSizeHelper::calculate( fFontHeight, aPropRefSize, rReferenceSize )));
140 : }
141 : }
142 :
143 : ::chart::PropertyMapper::getMultiPropertyListsFromValueMap(
144 852 : rOutTextProperties.first, rOutTextProperties.second, aTextValueMap );
145 : }
146 426 : }
147 :
148 426 : awt::Size lcl_createTextShapes(
149 : const tViewLegendEntryContainer & rEntries,
150 : const Reference< lang::XMultiServiceFactory > & xShapeFactory,
151 : const Reference< drawing::XShapes > & xTarget,
152 : ::std::vector< Reference< drawing::XShape > > & rOutTextShapes,
153 : const tPropertyValues & rTextProperties )
154 : {
155 426 : awt::Size aResult;
156 426 : AbstractShapeFactory* pShapeFactory = AbstractShapeFactory::getOrCreateShapeFactory(xShapeFactory);
157 :
158 4881 : for( tViewLegendEntryContainer::const_iterator aIt( rEntries.begin());
159 3254 : aIt != rEntries.end(); ++aIt )
160 : {
161 : try
162 : {
163 1201 : OUString aLabelString;
164 2402 : Sequence< Reference< XFormattedString2 > > aLabelSeq = (*aIt).aLabel;
165 2402 : for( sal_Int32 i = 0; i < aLabelSeq.getLength(); ++i )
166 : {
167 : // todo: support more than one text range
168 1201 : if( i == 1 )
169 0 : break;
170 :
171 1201 : aLabelString = aLabelString + aLabelSeq[i]->getString();
172 : // workaround for Issue #i67540#
173 1201 : if( aLabelString.isEmpty())
174 2 : aLabelString = " ";
175 : }
176 :
177 : Reference< drawing::XShape > xEntry =
178 : pShapeFactory->createText( xTarget, aLabelString,
179 2402 : rTextProperties.first, rTextProperties.second, uno::Any() );
180 :
181 : // adapt max-extent
182 1201 : awt::Size aCurrSize( xEntry->getSize());
183 1201 : aResult.Width = ::std::max( aResult.Width, aCurrSize.Width );
184 1201 : aResult.Height = ::std::max( aResult.Height, aCurrSize.Height );
185 :
186 2402 : rOutTextShapes.push_back( xEntry );
187 : }
188 0 : catch( const uno::Exception & ex )
189 : {
190 : ASSERT_EXCEPTION( ex );
191 : }
192 : }
193 :
194 426 : return aResult;
195 : }
196 :
197 426 : void lcl_collectColumnWidths( std::vector< sal_Int32 >& rColumnWidths, const sal_Int32 nNumberOfRows, const sal_Int32 nNumberOfColumns
198 : , const ::std::vector< Reference< drawing::XShape > > aTextShapes, sal_Int32 nSymbolPlusDistanceWidth )
199 : {
200 426 : rColumnWidths.clear();
201 426 : sal_Int32 nRow = 0;
202 426 : sal_Int32 nColumn = 0;
203 426 : sal_Int32 nNumberOfEntries = aTextShapes.size();
204 1586 : for( ; nRow < nNumberOfRows; ++nRow )
205 : {
206 2368 : for( nColumn = 0; nColumn < nNumberOfColumns; ++nColumn )
207 : {
208 1208 : sal_Int32 nEntry = (nColumn + nRow * nNumberOfColumns);
209 1208 : if( nEntry < nNumberOfEntries )
210 : {
211 1201 : awt::Size aTextSize( aTextShapes[ nEntry ]->getSize() );
212 1201 : sal_Int32 nWidth = nSymbolPlusDistanceWidth + aTextSize.Width;
213 1201 : if( nRow==0 )
214 467 : rColumnWidths.push_back( nWidth );
215 : else
216 734 : rColumnWidths[nColumn] = ::std::max( nWidth, rColumnWidths[nColumn] );
217 : }
218 : }
219 : }
220 426 : }
221 :
222 426 : void lcl_collectRowHeighs( std::vector< sal_Int32 >& rRowHeights, const sal_Int32 nNumberOfRows, const sal_Int32 nNumberOfColumns
223 : , const ::std::vector< Reference< drawing::XShape > > aTextShapes )
224 : {
225 : // calculate maximum height for each row
226 : // and collect column widths
227 426 : rRowHeights.clear();
228 426 : sal_Int32 nRow = 0;
229 426 : sal_Int32 nColumn = 0;
230 426 : sal_Int32 nNumberOfEntries = aTextShapes.size();
231 1586 : for( ; nRow < nNumberOfRows; ++nRow )
232 : {
233 1160 : sal_Int32 nCurrentRowHeight = 0;
234 2368 : for( nColumn = 0; nColumn < nNumberOfColumns; ++nColumn )
235 : {
236 1208 : sal_Int32 nEntry = (nColumn + nRow * nNumberOfColumns);
237 1208 : if( nEntry < nNumberOfEntries )
238 : {
239 1201 : awt::Size aTextSize( aTextShapes[ nEntry ]->getSize() );
240 1201 : nCurrentRowHeight = ::std::max( nCurrentRowHeight, aTextSize.Height );
241 : }
242 : }
243 1160 : rRowHeights.push_back( nCurrentRowHeight );
244 : }
245 426 : }
246 :
247 426 : sal_Int32 lcl_getTextLineHeight( const std::vector< sal_Int32 >& aRowHeights, const sal_Int32 nNumberOfRows, double fViewFontSize )
248 : {
249 426 : const sal_Int32 nFontHeight = static_cast< sal_Int32 >( fViewFontSize );
250 426 : sal_Int32 nTextLineHeight = nFontHeight;
251 426 : for( sal_Int32 nR=0; nR<nNumberOfRows; nR++ )
252 : {
253 426 : sal_Int32 nFullTextHeight = aRowHeights[ nR ];
254 426 : if( ( nFullTextHeight / nFontHeight ) <= 1 )
255 : {
256 426 : nTextLineHeight = nFullTextHeight;//found an entry with one line-> have real text height
257 426 : break;
258 : }
259 : }
260 426 : return nTextLineHeight;
261 : }
262 :
263 : //returns resulting legend size
264 426 : awt::Size lcl_placeLegendEntries(
265 : tViewLegendEntryContainer & rEntries,
266 : ::com::sun::star::chart::ChartLegendExpansion eExpansion,
267 : bool bSymbolsLeftSide,
268 : double fViewFontSize,
269 : const awt::Size& rMaxSymbolExtent,
270 : tPropertyValues & rTextProperties,
271 : const Reference< drawing::XShapes > & xTarget,
272 : const Reference< lang::XMultiServiceFactory > & xShapeFactory,
273 : const awt::Size & rAvailableSpace )
274 : {
275 426 : bool bIsCustomSize = (eExpansion == ::com::sun::star::chart::ChartLegendExpansion_CUSTOM);
276 426 : awt::Size aResultingLegendSize(0,0);
277 426 : if( bIsCustomSize )
278 0 : aResultingLegendSize = rAvailableSpace;
279 :
280 : // #i109336# Improve auto positioning in chart
281 426 : sal_Int32 nXPadding = static_cast< sal_Int32 >( std::max( 100.0, fViewFontSize * 0.33 ) );
282 : //sal_Int32 nXPadding = static_cast< sal_Int32 >( std::max( 200.0, fViewFontSize * 0.33 ) );
283 426 : sal_Int32 nXOffset = static_cast< sal_Int32 >( std::max( 100.0, fViewFontSize * 0.66 ) );
284 426 : sal_Int32 nYPadding = static_cast< sal_Int32 >( std::max( 100.0, fViewFontSize * 0.2 ) );
285 426 : sal_Int32 nYOffset = static_cast< sal_Int32 >( std::max( 100.0, fViewFontSize * 0.2 ) );
286 : //sal_Int32 nYOffset = static_cast< sal_Int32 >( std::max( 230.0, fViewFontSize * 0.45 ) );
287 :
288 426 : const sal_Int32 nSymbolToTextDistance = static_cast< sal_Int32 >( std::max( 100.0, fViewFontSize * 0.22 ) );//minimum 1mm
289 426 : const sal_Int32 nSymbolPlusDistanceWidth = rMaxSymbolExtent.Width + nSymbolToTextDistance;
290 426 : sal_Int32 nMaxTextWidth = rAvailableSpace.Width - (2 * nXPadding) - nSymbolPlusDistanceWidth;
291 426 : OUString aPropNameTextMaximumFrameWidth( "TextMaximumFrameWidth" );
292 426 : uno::Any* pFrameWidthAny = PropertyMapper::getValuePointer( rTextProperties.second, rTextProperties.first, aPropNameTextMaximumFrameWidth);
293 426 : if(pFrameWidthAny)
294 : {
295 426 : if( eExpansion == ::com::sun::star::chart::ChartLegendExpansion_HIGH )
296 : {
297 : // limit the width of texts to 30% of the total available width
298 : // #i109336# Improve auto positioning in chart
299 404 : nMaxTextWidth = rAvailableSpace.Width * 3 / 10;
300 : }
301 426 : *pFrameWidthAny = uno::makeAny(nMaxTextWidth);
302 : }
303 :
304 852 : ::std::vector< Reference< drawing::XShape > > aTextShapes;
305 426 : awt::Size aMaxEntryExtent = lcl_createTextShapes( rEntries, xShapeFactory, xTarget, aTextShapes, rTextProperties );
306 : OSL_ASSERT( aTextShapes.size() == rEntries.size());
307 :
308 426 : sal_Int32 nMaxEntryWidth = nXOffset + nSymbolPlusDistanceWidth + aMaxEntryExtent.Width;
309 426 : sal_Int32 nMaxEntryHeight = nYOffset + aMaxEntryExtent.Height;
310 426 : sal_Int32 nNumberOfEntries = rEntries.size();
311 :
312 426 : sal_Int32 nNumberOfColumns = 0, nNumberOfRows = 0;
313 852 : std::vector< sal_Int32 > aColumnWidths;
314 852 : std::vector< sal_Int32 > aRowHeights;
315 :
316 426 : sal_Int32 nTextLineHeight = static_cast< sal_Int32 >( fViewFontSize );
317 :
318 : // determine layout depending on LegendExpansion
319 426 : if( eExpansion == ::com::sun::star::chart::ChartLegendExpansion_CUSTOM )
320 : {
321 0 : sal_Int32 nCurrentRow=0;
322 0 : sal_Int32 nCurrentColumn=-1;
323 0 : sal_Int32 nMaxColumnCount=-1;
324 0 : for( sal_Int32 nN=0; nN<static_cast<sal_Int32>(aTextShapes.size()); nN++ )
325 : {
326 0 : Reference< drawing::XShape > xShape( aTextShapes[nN] );
327 0 : if( !xShape.is() )
328 0 : continue;
329 0 : awt::Size aSize( xShape->getSize() );
330 0 : sal_Int32 nNewWidth = aSize.Width + nSymbolPlusDistanceWidth;
331 0 : sal_Int32 nCurrentColumnCount = aColumnWidths.size();
332 :
333 : //are we allowed to add a new column?
334 0 : if( nMaxColumnCount==-1 || (nCurrentColumn+1) < nMaxColumnCount )
335 : {
336 : //try add a new column
337 0 : nCurrentColumn++;
338 0 : if( nCurrentColumn < nCurrentColumnCount )
339 : {
340 : //check whether the current column width is sufficient for the new entry
341 0 : if( aColumnWidths[nCurrentColumn]>=nNewWidth )
342 : {
343 : //all good proceed with next entry
344 0 : continue;
345 : }
346 : }
347 0 : if( nCurrentColumn < nCurrentColumnCount )
348 0 : aColumnWidths[nCurrentColumn] = std::max( nNewWidth, aColumnWidths[nCurrentColumn] );
349 : else
350 0 : aColumnWidths.push_back(nNewWidth);
351 :
352 : //do the columns still fit into the given size?
353 0 : nCurrentColumnCount = aColumnWidths.size();//update count
354 0 : sal_Int32 nSumWidth = 0;
355 0 : for( sal_Int32 nC=0; nC<nCurrentColumnCount; nC++ )
356 0 : nSumWidth += aColumnWidths[nC];
357 :
358 0 : if( nSumWidth <= rAvailableSpace.Width || nCurrentColumnCount==1 )
359 : {
360 : //all good proceed with next entry
361 0 : continue;
362 : }
363 : else
364 : {
365 : //not enough space for the current amount of columns
366 : //try again with less columns
367 0 : nMaxColumnCount = nCurrentColumnCount-1;
368 0 : nN=-1;
369 0 : nCurrentRow=0;
370 0 : nCurrentColumn=-1;
371 0 : aColumnWidths.clear();
372 0 : }
373 : }
374 : else
375 : {
376 : //add a new row and try the same entry again
377 0 : nCurrentRow++;
378 0 : nCurrentColumn=-1;
379 0 : nN--;
380 : }
381 0 : }
382 0 : nNumberOfColumns = aColumnWidths.size();
383 0 : nNumberOfRows = nCurrentRow+1;
384 :
385 : //check if there is not enough space so that some entries must be removed
386 0 : lcl_collectRowHeighs( aRowHeights, nNumberOfRows, nNumberOfColumns, aTextShapes );
387 0 : nTextLineHeight = lcl_getTextLineHeight( aRowHeights, nNumberOfRows, fViewFontSize );
388 0 : sal_Int32 nSumHeight = 0;
389 0 : for( sal_Int32 nR=0; nR<nNumberOfRows; nR++ )
390 0 : nSumHeight += aRowHeights[nR];
391 0 : sal_Int32 nRemainingSpace = rAvailableSpace.Height - nSumHeight;
392 :
393 0 : if( nRemainingSpace<0 )
394 : {
395 : //remove entries that are too big
396 0 : for( sal_Int32 nR=nNumberOfRows; nR--; )
397 : {
398 0 : for( sal_Int32 nC=nNumberOfColumns; nC--; )
399 : {
400 0 : sal_Int32 nEntry = (nC + nR * nNumberOfColumns);
401 0 : if( nEntry < static_cast<sal_Int32>(aTextShapes.size()) )
402 : {
403 0 : DrawModelWrapper::removeShape( aTextShapes[nEntry] );
404 0 : aTextShapes.pop_back();
405 : }
406 0 : if( nEntry < nNumberOfEntries )
407 : {
408 0 : DrawModelWrapper::removeShape( rEntries[ nEntry ].aSymbol );
409 0 : rEntries.pop_back();
410 0 : nNumberOfEntries--;
411 : }
412 : }
413 0 : nSumHeight -= aRowHeights[nR];
414 0 : aRowHeights.pop_back();
415 0 : nRemainingSpace = rAvailableSpace.Height - nSumHeight;
416 0 : if( nRemainingSpace>=0 )
417 0 : break;
418 : }
419 0 : nNumberOfRows = static_cast<sal_Int32>(aRowHeights.size());
420 : }
421 0 : if( nRemainingSpace > 0 )
422 : {
423 0 : sal_Int32 nNormalSpacingHeight = 2*nYPadding+(nNumberOfRows-1)*nYOffset;
424 0 : if( nRemainingSpace < nNormalSpacingHeight )
425 : {
426 : //reduce spacing between the entries
427 0 : nYPadding = nYOffset = nRemainingSpace/(nNumberOfRows+1);
428 : }
429 : else
430 : {
431 : //we have some space left that should be spread equally between all rows
432 0 : sal_Int32 nRemainingSingleSpace = (nRemainingSpace-nNormalSpacingHeight)/(nNumberOfRows+1);
433 0 : nYPadding += nRemainingSingleSpace;
434 0 : nYOffset += nRemainingSingleSpace;
435 : }
436 : }
437 :
438 : //check spacing between columns
439 0 : sal_Int32 nSumWidth = 0;
440 0 : for( sal_Int32 nC=0; nC<nNumberOfColumns; nC++ )
441 0 : nSumWidth += aColumnWidths[nC];
442 0 : nRemainingSpace = rAvailableSpace.Width - nSumWidth;
443 0 : if( nRemainingSpace>=0 )
444 : {
445 0 : sal_Int32 nNormalSpacingWidth = 2*nXPadding+(nNumberOfColumns-1)*nXOffset;
446 0 : if( nRemainingSpace < nNormalSpacingWidth )
447 : {
448 : //reduce spacing between the entries
449 0 : nXPadding = nXOffset = nRemainingSpace/(nNumberOfColumns+1);
450 : }
451 : else
452 : {
453 : //we have some space left that should be spread equally between all columns
454 0 : sal_Int32 nRemainingSingleSpace = (nRemainingSpace-nNormalSpacingWidth)/(nNumberOfColumns+1);
455 0 : nXPadding += nRemainingSingleSpace;
456 0 : nXOffset += nRemainingSingleSpace;
457 : }
458 : }
459 : }
460 426 : else if( eExpansion == ::com::sun::star::chart::ChartLegendExpansion_HIGH )
461 : {
462 : sal_Int32 nMaxNumberOfRows = nMaxEntryHeight
463 404 : ? (rAvailableSpace.Height - 2*nYPadding ) / nMaxEntryHeight
464 808 : : 0;
465 :
466 : nNumberOfColumns = nMaxNumberOfRows
467 : ? static_cast< sal_Int32 >(
468 : ceil( static_cast< double >( nNumberOfEntries ) /
469 404 : static_cast< double >( nMaxNumberOfRows ) ))
470 808 : : 0;
471 : nNumberOfRows = nNumberOfColumns
472 : ? static_cast< sal_Int32 >(
473 : ceil( static_cast< double >( nNumberOfEntries ) /
474 404 : static_cast< double >( nNumberOfColumns ) ))
475 808 : : 0;
476 : }
477 22 : else if( eExpansion == ::com::sun::star::chart::ChartLegendExpansion_WIDE )
478 : {
479 : sal_Int32 nMaxNumberOfColumns = nMaxEntryWidth
480 22 : ? (rAvailableSpace.Width - 2*nXPadding ) / nMaxEntryWidth
481 44 : : 0;
482 :
483 : nNumberOfRows = nMaxNumberOfColumns
484 : ? static_cast< sal_Int32 >(
485 : ceil( static_cast< double >( nNumberOfEntries ) /
486 22 : static_cast< double >( nMaxNumberOfColumns ) ))
487 44 : : 0;
488 : nNumberOfColumns = nNumberOfRows
489 : ? static_cast< sal_Int32 >(
490 : ceil( static_cast< double >( nNumberOfEntries ) /
491 22 : static_cast< double >( nNumberOfRows ) ))
492 44 : : 0;
493 : }
494 : else // ::com::sun::star::chart::ChartLegendExpansion_BALANCED
495 : {
496 : double fAspect = nMaxEntryHeight
497 0 : ? static_cast< double >( nMaxEntryWidth ) / static_cast< double >( nMaxEntryHeight )
498 0 : : 0.0;
499 :
500 : nNumberOfRows = static_cast< sal_Int32 >(
501 0 : ceil( sqrt( static_cast< double >( nNumberOfEntries ) * fAspect )));
502 : nNumberOfColumns = nNumberOfRows
503 : ? static_cast< sal_Int32 >(
504 : ceil( static_cast< double >( nNumberOfEntries ) /
505 0 : static_cast< double >( nNumberOfRows ) ))
506 0 : : 0;
507 : }
508 :
509 426 : if(nNumberOfRows<=0)
510 0 : return aResultingLegendSize;
511 :
512 426 : if( eExpansion != ::com::sun::star::chart::ChartLegendExpansion_CUSTOM )
513 : {
514 426 : lcl_collectColumnWidths( aColumnWidths, nNumberOfRows, nNumberOfColumns, aTextShapes, nSymbolPlusDistanceWidth );
515 426 : lcl_collectRowHeighs( aRowHeights, nNumberOfRows, nNumberOfColumns, aTextShapes );
516 426 : nTextLineHeight = lcl_getTextLineHeight( aRowHeights, nNumberOfRows, fViewFontSize );
517 : }
518 :
519 426 : sal_Int32 nCurrentXPos = nXPadding;
520 426 : sal_Int32 nCurrentYPos = nYPadding;
521 426 : if( !bSymbolsLeftSide )
522 0 : nCurrentXPos = -nXPadding;
523 :
524 : // place entries into column and rows
525 426 : sal_Int32 nMaxYPos = 0;
526 426 : sal_Int32 nRow = 0;
527 426 : sal_Int32 nColumn = 0;
528 893 : for( nColumn = 0; nColumn < nNumberOfColumns; ++nColumn )
529 : {
530 467 : nCurrentYPos = nYPadding;
531 1668 : for( nRow = 0; nRow < nNumberOfRows; ++nRow )
532 : {
533 1208 : sal_Int32 nEntry = (nColumn + nRow * nNumberOfColumns);
534 1208 : if( nEntry >= nNumberOfEntries )
535 7 : break;
536 :
537 : // text shape
538 1201 : Reference< drawing::XShape > xTextShape( aTextShapes[nEntry] );
539 1201 : if( xTextShape.is() )
540 : {
541 1201 : awt::Size aTextSize( xTextShape->getSize() );
542 1201 : sal_Int32 nTextXPos = nCurrentXPos + nSymbolPlusDistanceWidth;
543 1201 : if( !bSymbolsLeftSide )
544 0 : nTextXPos = nCurrentXPos - nSymbolPlusDistanceWidth - aTextSize.Width;
545 1201 : xTextShape->setPosition( awt::Point( nTextXPos, nCurrentYPos ));
546 : }
547 :
548 : // symbol
549 2402 : Reference< drawing::XShape > xSymbol( rEntries[ nEntry ].aSymbol );
550 1201 : if( xSymbol.is() )
551 : {
552 1201 : awt::Size aSymbolSize( rMaxSymbolExtent );
553 1201 : sal_Int32 nSymbolXPos = nCurrentXPos;
554 1201 : if( !bSymbolsLeftSide )
555 0 : nSymbolXPos = nCurrentXPos - rMaxSymbolExtent.Width;
556 1201 : sal_Int32 nSymbolYPos = nCurrentYPos + ( ( nTextLineHeight - aSymbolSize.Height ) / 2 );
557 1201 : xSymbol->setPosition( awt::Point( nSymbolXPos, nSymbolYPos ) );
558 : }
559 :
560 1201 : nCurrentYPos += aRowHeights[ nRow ];
561 1201 : if( nRow+1 < nNumberOfRows )
562 741 : nCurrentYPos += nYOffset;
563 1201 : nMaxYPos = ::std::max( nMaxYPos, nCurrentYPos );
564 1201 : }
565 467 : if( bSymbolsLeftSide )
566 : {
567 467 : nCurrentXPos += aColumnWidths[nColumn];
568 467 : if( nColumn+1 < nNumberOfColumns )
569 41 : nCurrentXPos += nXOffset;
570 : }
571 : else
572 : {
573 0 : nCurrentXPos -= aColumnWidths[nColumn];
574 0 : if( nColumn+1 < nNumberOfColumns )
575 0 : nCurrentXPos -= nXOffset;
576 : }
577 : }
578 :
579 426 : if( !bIsCustomSize )
580 : {
581 426 : if( bSymbolsLeftSide )
582 426 : aResultingLegendSize.Width = nCurrentXPos + nXPadding;
583 : else
584 : {
585 0 : sal_Int32 nLegendWidth = -(nCurrentXPos-nXPadding);
586 0 : aResultingLegendSize.Width = nLegendWidth;
587 : }
588 426 : aResultingLegendSize.Height = nMaxYPos + nYPadding;
589 : }
590 :
591 426 : if( !bSymbolsLeftSide )
592 : {
593 0 : sal_Int32 nLegendWidth = aResultingLegendSize.Width;
594 0 : awt::Point aPos(0,0);
595 0 : for( sal_Int32 nEntry=0; nEntry<nNumberOfEntries; nEntry++ )
596 : {
597 0 : Reference< drawing::XShape > xSymbol( rEntries[ nEntry ].aSymbol );
598 0 : aPos = xSymbol->getPosition();
599 0 : aPos.X += nLegendWidth;
600 0 : xSymbol->setPosition( aPos );
601 0 : Reference< drawing::XShape > xText( aTextShapes[ nEntry ] );
602 0 : aPos = xText->getPosition();
603 0 : aPos.X += nLegendWidth;
604 0 : xText->setPosition( aPos );
605 0 : }
606 : }
607 :
608 852 : return aResultingLegendSize;
609 : }
610 :
611 : // #i109336# Improve auto positioning in chart
612 913 : sal_Int32 lcl_getLegendLeftRightMargin()
613 : {
614 913 : return 210; // 1/100 mm
615 : }
616 :
617 : // #i109336# Improve auto positioning in chart
618 531 : sal_Int32 lcl_getLegendTopBottomMargin()
619 : {
620 531 : return 185; // 1/100 mm
621 : }
622 :
623 426 : chart2::RelativePosition lcl_getDefaultPosition( LegendPosition ePos, const awt::Rectangle& rOutAvailableSpace, const awt::Size & rPageSize )
624 : {
625 426 : chart2::RelativePosition aResult;
626 :
627 426 : switch( ePos )
628 : {
629 : case LegendPosition_LINE_START:
630 : {
631 : // #i109336# Improve auto positioning in chart
632 2 : const double fDefaultDistance = ( static_cast< double >( lcl_getLegendLeftRightMargin() ) /
633 2 : static_cast< double >( rPageSize.Width ) );
634 : aResult = chart2::RelativePosition(
635 2 : fDefaultDistance, 0.5, drawing::Alignment_LEFT );
636 : }
637 2 : break;
638 : case LegendPosition_LINE_END:
639 : {
640 : // #i109336# Improve auto positioning in chart
641 402 : const double fDefaultDistance = ( static_cast< double >( lcl_getLegendLeftRightMargin() ) /
642 402 : static_cast< double >( rPageSize.Width ) );
643 : aResult = chart2::RelativePosition(
644 402 : 1.0 - fDefaultDistance, 0.5, drawing::Alignment_RIGHT );
645 : }
646 402 : break;
647 : case LegendPosition_PAGE_START:
648 : {
649 : // #i109336# Improve auto positioning in chart
650 2 : const double fDefaultDistance = ( static_cast< double >( lcl_getLegendTopBottomMargin() ) /
651 2 : static_cast< double >( rPageSize.Height ) );
652 2 : double fDistance = (static_cast<double>(rOutAvailableSpace.Y)/static_cast<double>(rPageSize.Height)) + fDefaultDistance;
653 : aResult = chart2::RelativePosition(
654 2 : 0.5, fDistance, drawing::Alignment_TOP );
655 : }
656 2 : break;
657 : case LegendPosition_PAGE_END:
658 : {
659 : // #i109336# Improve auto positioning in chart
660 20 : const double fDefaultDistance = ( static_cast< double >( lcl_getLegendTopBottomMargin() ) /
661 20 : static_cast< double >( rPageSize.Height ) );
662 : aResult = chart2::RelativePosition(
663 20 : 0.5, 1.0 - fDefaultDistance, drawing::Alignment_BOTTOM );
664 : }
665 20 : break;
666 :
667 : case LegendPosition_CUSTOM:
668 : // to avoid warning
669 : case LegendPosition_MAKE_FIXED_SIZE:
670 : // nothing to be set
671 0 : break;
672 : }
673 :
674 426 : return aResult;
675 : }
676 :
677 : /** @return
678 : a point relative to the upper left corner that can be used for
679 : XShape::setPosition()
680 : */
681 509 : awt::Point lcl_calculatePositionAndRemainingSpace(
682 : awt::Rectangle & rRemainingSpace,
683 : const awt::Size & rPageSize,
684 : chart2::RelativePosition aRelPos,
685 : LegendPosition ePos,
686 : const awt::Size& aLegendSize )
687 : {
688 : // calculate position
689 : awt::Point aResult(
690 509 : static_cast< sal_Int32 >( aRelPos.Primary * rPageSize.Width ),
691 1018 : static_cast< sal_Int32 >( aRelPos.Secondary * rPageSize.Height ));
692 :
693 : aResult = RelativePositionHelper::getUpperLeftCornerOfAnchoredObject(
694 509 : aResult, aLegendSize, aRelPos.Anchor );
695 :
696 : // adapt rRemainingSpace if LegendPosition is not CUSTOM
697 : // #i109336# Improve auto positioning in chart
698 509 : sal_Int32 nXDistance = lcl_getLegendLeftRightMargin();
699 509 : sal_Int32 nYDistance = lcl_getLegendTopBottomMargin();
700 509 : switch( ePos )
701 : {
702 : case LegendPosition_LINE_START:
703 : {
704 2 : sal_Int32 nExtent = aLegendSize.Width;
705 2 : rRemainingSpace.Width -= ( nExtent + nXDistance );
706 2 : rRemainingSpace.X += ( nExtent + nXDistance );
707 : }
708 2 : break;
709 : case LegendPosition_LINE_END:
710 : {
711 485 : rRemainingSpace.Width -= ( aLegendSize.Width + nXDistance );
712 : }
713 485 : break;
714 : case LegendPosition_PAGE_START:
715 : {
716 2 : sal_Int32 nExtent = aLegendSize.Height;
717 2 : rRemainingSpace.Height -= ( nExtent + nYDistance );
718 2 : rRemainingSpace.Y += ( nExtent + nYDistance );
719 : }
720 2 : break;
721 : case LegendPosition_PAGE_END:
722 : {
723 20 : rRemainingSpace.Height -= ( aLegendSize.Height + nYDistance );
724 : }
725 20 : break;
726 :
727 : default:
728 : // nothing
729 0 : break;
730 : }
731 :
732 : // adjust the legend position. Esp. for old files that had slightly smaller legends
733 509 : const sal_Int32 nEdgeDistance( 30 );
734 509 : if( aResult.X + aLegendSize.Width > rPageSize.Width )
735 : {
736 0 : sal_Int32 nNewX( (rPageSize.Width - aLegendSize.Width) - nEdgeDistance );
737 0 : if( nNewX > rPageSize.Width / 4 )
738 0 : aResult.X = nNewX;
739 : }
740 509 : if( aResult.Y + aLegendSize.Height > rPageSize.Height )
741 : {
742 0 : sal_Int32 nNewY( (rPageSize.Height - aLegendSize.Height) - nEdgeDistance );
743 0 : if( nNewY > rPageSize.Height / 4 )
744 0 : aResult.Y = nNewY;
745 : }
746 :
747 509 : return aResult;
748 : }
749 :
750 426 : bool lcl_shouldSymbolsBePlacedOnTheLeftSide( const Reference< beans::XPropertySet >& xLegendProp, sal_Int16 nDefaultWritingMode )
751 : {
752 426 : bool bSymbolsLeftSide = true;
753 : try
754 : {
755 426 : if( SvtLanguageOptions().IsCTLFontEnabled() )
756 : {
757 426 : if(xLegendProp.is())
758 : {
759 426 : sal_Int16 nWritingMode=-1;
760 426 : if( (xLegendProp->getPropertyValue( "WritingMode" ) >>= nWritingMode) )
761 : {
762 426 : if( nWritingMode == text::WritingMode2::PAGE )
763 426 : nWritingMode = nDefaultWritingMode;
764 426 : if( nWritingMode == text::WritingMode2::RL_TB )
765 0 : bSymbolsLeftSide=false;
766 : }
767 : }
768 : }
769 : }
770 0 : catch( const uno::Exception & ex )
771 : {
772 : ASSERT_EXCEPTION( ex );
773 : }
774 426 : return bSymbolsLeftSide;
775 : }
776 :
777 : } // anonymous namespace
778 :
779 426 : VLegend::VLegend(
780 : const Reference< XLegend > & xLegend,
781 : const Reference< uno::XComponentContext > & xContext,
782 : const std::vector< LegendEntryProvider* >& rLegendEntryProviderList,
783 : const Reference< drawing::XShapes >& xTargetPage,
784 : const Reference< lang::XMultiServiceFactory >& xFactory,
785 : ChartModel& rModel )
786 : : m_xTarget(xTargetPage)
787 : , m_xShapeFactory(xFactory)
788 : , m_xLegend(xLegend)
789 : , mrModel(rModel)
790 : , m_xContext(xContext)
791 : , m_aLegendEntryProviderList(rLegendEntryProviderList)
792 426 : , m_nDefaultWritingMode(text::WritingMode2::LR_TB)
793 : {
794 426 : }
795 :
796 426 : void VLegend::setDefaultWritingMode( sal_Int16 nDefaultWritingMode )
797 : {
798 426 : m_nDefaultWritingMode = nDefaultWritingMode;
799 426 : }
800 :
801 680 : bool VLegend::isVisible( const Reference< XLegend > & xLegend )
802 : {
803 680 : if( ! xLegend.is())
804 240 : return false;
805 :
806 440 : bool bShow = false;
807 : try
808 : {
809 440 : Reference< beans::XPropertySet > xLegendProp( xLegend, uno::UNO_QUERY_THROW );
810 440 : xLegendProp->getPropertyValue( "Show") >>= bShow;
811 : }
812 0 : catch( const uno::Exception & ex )
813 : {
814 : ASSERT_EXCEPTION( ex );
815 : }
816 :
817 440 : return bShow;
818 : }
819 :
820 426 : void VLegend::createShapes(
821 : const awt::Size & rAvailableSpace,
822 : const awt::Size & rPageSize )
823 : {
824 1278 : if(! (m_xLegend.is() &&
825 426 : m_xShapeFactory.is() &&
826 852 : m_xTarget.is()))
827 426 : return;
828 :
829 : try
830 : {
831 : //create shape and add to page
832 426 : AbstractShapeFactory* pShapeFactory = AbstractShapeFactory::getOrCreateShapeFactory(m_xShapeFactory);
833 426 : OUString aLegendParticle( ObjectIdentifier::createParticleForLegend( m_xLegend, mrModel ) );
834 : m_xShape.set( pShapeFactory->createGroup2D( m_xTarget,
835 426 : ObjectIdentifier::createClassifiedIdentifierForParticle( aLegendParticle )),
836 426 : uno::UNO_QUERY);
837 :
838 : // create and insert sub-shapes
839 852 : Reference< drawing::XShapes > xLegendContainer( m_xShape, uno::UNO_QUERY );
840 426 : if( xLegendContainer.is())
841 : {
842 : // for quickly setting properties
843 426 : tPropertyValues aLineFillProperties;
844 852 : tPropertyValues aTextProperties;
845 :
846 852 : Reference< beans::XPropertySet > xLegendProp( m_xLegend, uno::UNO_QUERY );
847 426 : ::com::sun::star::chart::ChartLegendExpansion eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
848 426 : awt::Size aLegendSize( rAvailableSpace );
849 :
850 426 : if( xLegendProp.is())
851 : {
852 : // get Expansion property
853 426 : xLegendProp->getPropertyValue( "Expansion") >>= eExpansion;
854 426 : if( eExpansion == ::com::sun::star::chart::ChartLegendExpansion_CUSTOM )
855 : {
856 0 : RelativeSize aRelativeSize;
857 0 : if ((xLegendProp->getPropertyValue( "RelativeSize") >>= aRelativeSize))
858 : {
859 0 : aLegendSize.Width = static_cast<sal_Int32>(::rtl::math::approxCeil( aRelativeSize.Primary * rPageSize.Width ));
860 0 : aLegendSize.Height = static_cast<sal_Int32>(::rtl::math::approxCeil( aRelativeSize.Secondary * rPageSize.Height ));
861 : }
862 : else
863 0 : eExpansion = ::com::sun::star::chart::ChartLegendExpansion_HIGH;
864 : }
865 426 : lcl_getProperties( xLegendProp, aLineFillProperties, aTextProperties, rPageSize );
866 : }
867 :
868 : // create entries
869 426 : double fViewFontSize = lcl_CalcViewFontSize( xLegendProp, rPageSize );//todo
870 : // #i109336# Improve auto positioning in chart
871 426 : sal_Int32 nSymbolHeight = static_cast< sal_Int32 >( fViewFontSize * 0.6 );
872 426 : sal_Int32 nSymbolWidth = static_cast< sal_Int32 >( nSymbolHeight );
873 :
874 426 : ::std::vector< LegendEntryProvider* >::const_iterator aIter = m_aLegendEntryProviderList.begin();
875 426 : const ::std::vector< LegendEntryProvider* >::const_iterator aEnd = m_aLegendEntryProviderList.end();
876 858 : for( aIter = m_aLegendEntryProviderList.begin(); aIter != aEnd; ++aIter )
877 : {
878 432 : LegendEntryProvider* pLegendEntryProvider( *aIter );
879 432 : if( pLegendEntryProvider )
880 : {
881 432 : awt::Size aCurrentRatio = pLegendEntryProvider->getPreferredLegendKeyAspectRatio();
882 432 : sal_Int32 nCurrentWidth = aCurrentRatio.Width;
883 432 : if( aCurrentRatio.Height > 0 )
884 : {
885 394 : nCurrentWidth = nSymbolHeight* aCurrentRatio.Width/aCurrentRatio.Height;
886 : }
887 432 : nSymbolWidth = std::max( nSymbolWidth, nCurrentWidth );
888 : }
889 : }
890 426 : awt::Size aMaxSymbolExtent( nSymbolWidth, nSymbolHeight );
891 :
892 852 : tViewLegendEntryContainer aViewEntries;
893 858 : for( aIter = m_aLegendEntryProviderList.begin(); aIter != aEnd; ++aIter )
894 : {
895 432 : LegendEntryProvider* pLegendEntryProvider( *aIter );
896 432 : if( pLegendEntryProvider )
897 : {
898 432 : std::vector< ViewLegendEntry > aNewEntries = pLegendEntryProvider->createLegendEntries( aMaxSymbolExtent, eExpansion, xLegendProp, xLegendContainer, m_xShapeFactory, m_xContext );
899 432 : aViewEntries.insert( aViewEntries.end(), aNewEntries.begin(), aNewEntries.end() );
900 : }
901 : }
902 :
903 426 : bool bSymbolsLeftSide = lcl_shouldSymbolsBePlacedOnTheLeftSide( xLegendProp, m_nDefaultWritingMode );
904 :
905 426 : if( !aViewEntries.empty() ) {
906 : // place entries
907 : aLegendSize = lcl_placeLegendEntries( aViewEntries, eExpansion, bSymbolsLeftSide, fViewFontSize, aMaxSymbolExtent,
908 426 : aTextProperties, xLegendContainer, m_xShapeFactory, aLegendSize );
909 :
910 : }
911 :
912 : Reference< drawing::XShape > xBorder =
913 : pShapeFactory->createRectangle( xLegendContainer,
914 : aLegendSize,
915 : awt::Point(0,0),
916 : aLineFillProperties.first,
917 852 : aLineFillProperties.second, AbstractShapeFactory::Bottom );
918 :
919 : //because of this name this border will be used for marking the legend
920 852 : AbstractShapeFactory::setShapeName( xBorder, "MarkHandles" );
921 426 : }
922 : }
923 0 : catch( const uno::Exception & ex )
924 : {
925 : ASSERT_EXCEPTION( ex );
926 : }
927 : }
928 :
929 426 : void VLegend::changePosition(
930 : awt::Rectangle & rOutAvailableSpace,
931 : const awt::Size & rPageSize )
932 : {
933 426 : if(! m_xShape.is())
934 426 : return;
935 :
936 : try
937 : {
938 : // determine position and alignment depending on default position
939 426 : awt::Size aLegendSize = m_xShape->getSize();
940 426 : Reference< beans::XPropertySet > xLegendProp( m_xLegend, uno::UNO_QUERY_THROW );
941 426 : chart2::RelativePosition aRelativePosition;
942 :
943 : bool bAutoPosition =
944 426 : ! (xLegendProp->getPropertyValue( "RelativePosition") >>= aRelativePosition);
945 :
946 426 : LegendPosition ePos = LegendPosition_CUSTOM;
947 426 : xLegendProp->getPropertyValue( "AnchorPosition") >>= ePos;
948 :
949 : //calculate position
950 426 : if( bAutoPosition )
951 : {
952 : // auto position: relative to remaining space
953 343 : aRelativePosition = lcl_getDefaultPosition( ePos, rOutAvailableSpace, rPageSize );
954 : awt::Point aPos = lcl_calculatePositionAndRemainingSpace(
955 343 : rOutAvailableSpace, rPageSize, aRelativePosition, ePos, aLegendSize );
956 343 : m_xShape->setPosition( aPos );
957 : }
958 : else
959 : {
960 : // manual position: relative to whole page
961 83 : awt::Rectangle aAvailableSpace( 0, 0, rPageSize.Width, rPageSize.Height );
962 : awt::Point aPos = lcl_calculatePositionAndRemainingSpace(
963 83 : aAvailableSpace, rPageSize, aRelativePosition, ePos, aLegendSize );
964 83 : m_xShape->setPosition( aPos );
965 :
966 83 : if( ePos != LegendPosition_CUSTOM )
967 : {
968 : // calculate remaining space as if having autoposition:
969 83 : aRelativePosition = lcl_getDefaultPosition( ePos, rOutAvailableSpace, rPageSize );
970 : lcl_calculatePositionAndRemainingSpace(
971 83 : rOutAvailableSpace, rPageSize, aRelativePosition, ePos, aLegendSize );
972 : }
973 426 : }
974 : }
975 0 : catch( const uno::Exception & ex )
976 : {
977 : ASSERT_EXCEPTION( ex );
978 : }
979 : }
980 :
981 108 : } //namespace chart
982 :
983 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|