merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / tools / ReferenceSizeProvider.cxx
blob6a9b60489e69d7420966147ac3b881195e5286d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ReferenceSizeProvider.cxx,v $
10 * $Revision: 1.5.44.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "ReferenceSizeProvider.hxx"
35 #include "RelativeSizeHelper.hxx"
36 #include "ChartModelHelper.hxx"
37 #include "DiagramHelper.hxx"
38 #include "macros.hxx"
39 #include "AxisHelper.hxx"
40 #include "DataSeriesHelper.hxx"
42 #include <com/sun/star/chart2/XTitled.hpp>
43 #include <com/sun/star/chart2/XTitle.hpp>
44 #include <com/sun/star/chart2/XDataSeries.hpp>
46 #include <vector>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::chart2;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 using ::rtl::OUString;
55 // ================================================================================
57 namespace chart
60 ReferenceSizeProvider::ReferenceSizeProvider(
61 awt::Size aPageSize,
62 const Reference< XChartDocument > & xChartDoc ) :
63 m_aPageSize( aPageSize ),
64 m_xChartDoc( xChartDoc ),
65 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
68 awt::Size ReferenceSizeProvider::getPageSize() const
70 return m_aPageSize;
73 bool ReferenceSizeProvider::useAutoScale() const
75 return m_bUseAutoScale;
78 void ReferenceSizeProvider::impl_setValuesAtTitled(
79 const Reference< XTitled > & xTitled )
81 if( xTitled.is())
83 Reference< XTitle > xTitle( xTitled->getTitleObject());
84 if( xTitle.is())
85 setValuesAtTitle( xTitle );
89 void ReferenceSizeProvider::setValuesAtTitle(
90 const Reference< XTitle > & xTitle )
92 try
94 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
95 awt::Size aOldRefSize;
96 bool bHasOldRefSize(
97 xTitleProp->getPropertyValue( C2U("ReferencePageSize")) >>= aOldRefSize );
99 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
100 if( bHasOldRefSize && ! useAutoScale())
102 uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
103 xTitle->getText());
104 for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
106 RelativeSizeHelper::adaptFontSizes(
107 Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
108 aOldRefSize, getPageSize());
112 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
114 catch( const uno::Exception & ex )
116 ASSERT_EXCEPTION( ex );
120 void ReferenceSizeProvider::setValuesAtAllDataSeries()
122 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
124 // DataSeries/Points
125 ::std::vector< Reference< XDataSeries > > aSeries(
126 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
128 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
129 aIt != aSeries.end(); ++aIt )
131 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
132 if( xSeriesProp.is())
134 // data points
135 Sequence< sal_Int32 > aPointIndexes;
138 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
140 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
141 setValuesAtPropertySet(
142 (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
145 catch( const uno::Exception & ex )
147 ASSERT_EXCEPTION( ex );
150 //it is important to correct the datapoint properties first as they do reference the series properties
151 setValuesAtPropertySet( xSeriesProp );
156 void ReferenceSizeProvider::setValuesAtPropertySet(
157 const Reference< beans::XPropertySet > & xProp,
158 bool bAdaptFontSizes /* = true */ )
160 if( ! xProp.is())
161 return;
163 static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
167 awt::Size aRefSize( getPageSize() );
168 awt::Size aOldRefSize;
169 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
171 if( useAutoScale())
173 if( ! bHasOldRefSize )
174 xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
176 else
178 if( bHasOldRefSize )
180 xProp->setPropertyValue( aRefSizeName, uno::Any());
182 // adapt font sizes
183 if( bAdaptFontSizes )
184 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
188 catch( const uno::Exception & ex )
190 ASSERT_EXCEPTION( ex );
194 void ReferenceSizeProvider::getAutoResizeFromPropSet(
195 const Reference< beans::XPropertySet > & xProp,
196 ReferenceSizeProvider::AutoResizeState & rInOutState )
198 static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
199 AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
201 if( xProp.is())
205 if( xProp->getPropertyValue( aRefSizeName ).hasValue())
206 eSingleState = AUTO_RESIZE_YES;
207 else
208 eSingleState = AUTO_RESIZE_NO;
210 catch( uno::Exception )
212 // unknown property -> state stays unknown
216 // curent state unknown => nothing changes. Otherwise if current state
217 // differs from state so far, we have an ambiguity
218 if( rInOutState == AUTO_RESIZE_UNKNOWN )
220 rInOutState = eSingleState;
222 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
223 eSingleState != rInOutState )
225 rInOutState = AUTO_RESIZE_AMBIGUOUS;
229 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
230 const Reference< XTitled > & xTitled,
231 ReferenceSizeProvider::AutoResizeState & rInOutState )
233 if( xTitled.is())
235 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
236 if( xProp.is())
237 getAutoResizeFromPropSet( xProp, rInOutState );
241 /** Retrieves the state auto-resize from all objects that support this
242 feature. If all objects return the same state, AUTO_RESIZE_YES or
243 AUTO_RESIZE_NO is returned.
245 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
246 returned. If there are multiple objects, some with state YES and some
247 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
249 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
250 const Reference< XChartDocument > & xChartDoc )
252 AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
254 // Main Title
255 Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
256 if( xDocTitled.is())
257 impl_getAutoResizeFromTitled( xDocTitled, eResult );
258 if( eResult == AUTO_RESIZE_AMBIGUOUS )
259 return eResult;
261 // diagram is needed by the rest of the objects
262 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
263 if( ! xDiagram.is())
264 return eResult;
266 // Sub Title
267 Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
268 if( xDiaTitled.is())
269 impl_getAutoResizeFromTitled( xDiaTitled, eResult );
270 if( eResult == AUTO_RESIZE_AMBIGUOUS )
271 return eResult;
273 // Legend
274 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
275 if( xLegendProp.is())
276 getAutoResizeFromPropSet( xLegendProp, eResult );
277 if( eResult == AUTO_RESIZE_AMBIGUOUS )
278 return eResult;
280 // Axes (incl. Axis Titles)
281 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
282 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
284 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
285 if( xProp.is())
286 getAutoResizeFromPropSet( xProp, eResult );
287 Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
288 if( xTitled.is())
290 impl_getAutoResizeFromTitled( xTitled, eResult );
291 if( eResult == AUTO_RESIZE_AMBIGUOUS )
292 return eResult;
296 // DataSeries/Points
297 ::std::vector< Reference< XDataSeries > > aSeries(
298 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
300 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
301 aIt != aSeries.end(); ++aIt )
303 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
304 if( xSeriesProp.is())
306 getAutoResizeFromPropSet( xSeriesProp, eResult );
307 if( eResult == AUTO_RESIZE_AMBIGUOUS )
308 return eResult;
310 // data points
311 Sequence< sal_Int32 > aPointIndexes;
314 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
316 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
318 getAutoResizeFromPropSet(
319 (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
320 if( eResult == AUTO_RESIZE_AMBIGUOUS )
321 return eResult;
325 catch( const uno::Exception & ex )
327 ASSERT_EXCEPTION( ex );
332 return eResult;
335 void ReferenceSizeProvider::toggleAutoResizeState()
337 setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
341 /** sets the auto-resize at all objects that support this feature for text.
342 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
344 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
346 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
348 // Main Title
349 impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
351 // diagram is needed by the rest of the objects
352 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
353 if( ! xDiagram.is())
354 return;
356 // Sub Title
357 impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
359 // Legend
360 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
361 if( xLegendProp.is())
362 setValuesAtPropertySet( xLegendProp );
364 // Axes (incl. Axis Titles)
365 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
366 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
368 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
369 if( xProp.is())
370 setValuesAtPropertySet( xProp );
371 impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
374 // DataSeries/Points
375 setValuesAtAllDataSeries();
377 // recalculate new state (in case it stays unknown or is ambiguous
378 m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
381 } // namespace chart