merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / ReferenceSizeProvider.cxx
blob5a37f4980323ef89616372c070f2915cc0218479
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "ReferenceSizeProvider.hxx"
32 #include "RelativeSizeHelper.hxx"
33 #include "ChartModelHelper.hxx"
34 #include "DiagramHelper.hxx"
35 #include "macros.hxx"
36 #include "AxisHelper.hxx"
37 #include "DataSeriesHelper.hxx"
39 #include <com/sun/star/chart2/XTitled.hpp>
40 #include <com/sun/star/chart2/XTitle.hpp>
41 #include <com/sun/star/chart2/XDataSeries.hpp>
43 #include <vector>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::chart2;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
50 using ::rtl::OUString;
52 // ================================================================================
54 namespace chart
57 ReferenceSizeProvider::ReferenceSizeProvider(
58 awt::Size aPageSize,
59 const Reference< XChartDocument > & xChartDoc ) :
60 m_aPageSize( aPageSize ),
61 m_xChartDoc( xChartDoc ),
62 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
65 awt::Size ReferenceSizeProvider::getPageSize() const
67 return m_aPageSize;
70 bool ReferenceSizeProvider::useAutoScale() const
72 return m_bUseAutoScale;
75 void ReferenceSizeProvider::impl_setValuesAtTitled(
76 const Reference< XTitled > & xTitled )
78 if( xTitled.is())
80 Reference< XTitle > xTitle( xTitled->getTitleObject());
81 if( xTitle.is())
82 setValuesAtTitle( xTitle );
86 void ReferenceSizeProvider::setValuesAtTitle(
87 const Reference< XTitle > & xTitle )
89 try
91 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
92 awt::Size aOldRefSize;
93 bool bHasOldRefSize(
94 xTitleProp->getPropertyValue( C2U("ReferencePageSize")) >>= aOldRefSize );
96 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
97 if( bHasOldRefSize && ! useAutoScale())
99 uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
100 xTitle->getText());
101 for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
103 RelativeSizeHelper::adaptFontSizes(
104 Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
105 aOldRefSize, getPageSize());
109 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
111 catch( const uno::Exception & ex )
113 ASSERT_EXCEPTION( ex );
117 void ReferenceSizeProvider::setValuesAtAllDataSeries()
119 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
121 // DataSeries/Points
122 ::std::vector< Reference< XDataSeries > > aSeries(
123 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
125 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
126 aIt != aSeries.end(); ++aIt )
128 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
129 if( xSeriesProp.is())
131 // data points
132 Sequence< sal_Int32 > aPointIndexes;
135 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
137 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
138 setValuesAtPropertySet(
139 (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
142 catch( const uno::Exception & ex )
144 ASSERT_EXCEPTION( ex );
147 //it is important to correct the datapoint properties first as they do reference the series properties
148 setValuesAtPropertySet( xSeriesProp );
153 void ReferenceSizeProvider::setValuesAtPropertySet(
154 const Reference< beans::XPropertySet > & xProp,
155 bool bAdaptFontSizes /* = true */ )
157 if( ! xProp.is())
158 return;
160 static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
164 awt::Size aRefSize( getPageSize() );
165 awt::Size aOldRefSize;
166 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
168 if( useAutoScale())
170 if( ! bHasOldRefSize )
171 xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
173 else
175 if( bHasOldRefSize )
177 xProp->setPropertyValue( aRefSizeName, uno::Any());
179 // adapt font sizes
180 if( bAdaptFontSizes )
181 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
185 catch( const uno::Exception & ex )
187 ASSERT_EXCEPTION( ex );
191 void ReferenceSizeProvider::getAutoResizeFromPropSet(
192 const Reference< beans::XPropertySet > & xProp,
193 ReferenceSizeProvider::AutoResizeState & rInOutState )
195 static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
196 AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
198 if( xProp.is())
202 if( xProp->getPropertyValue( aRefSizeName ).hasValue())
203 eSingleState = AUTO_RESIZE_YES;
204 else
205 eSingleState = AUTO_RESIZE_NO;
207 catch( uno::Exception )
209 // unknown property -> state stays unknown
213 // curent state unknown => nothing changes. Otherwise if current state
214 // differs from state so far, we have an ambiguity
215 if( rInOutState == AUTO_RESIZE_UNKNOWN )
217 rInOutState = eSingleState;
219 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
220 eSingleState != rInOutState )
222 rInOutState = AUTO_RESIZE_AMBIGUOUS;
226 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
227 const Reference< XTitled > & xTitled,
228 ReferenceSizeProvider::AutoResizeState & rInOutState )
230 if( xTitled.is())
232 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
233 if( xProp.is())
234 getAutoResizeFromPropSet( xProp, rInOutState );
238 /** Retrieves the state auto-resize from all objects that support this
239 feature. If all objects return the same state, AUTO_RESIZE_YES or
240 AUTO_RESIZE_NO is returned.
242 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
243 returned. If there are multiple objects, some with state YES and some
244 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
246 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
247 const Reference< XChartDocument > & xChartDoc )
249 AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
251 // Main Title
252 Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
253 if( xDocTitled.is())
254 impl_getAutoResizeFromTitled( xDocTitled, eResult );
255 if( eResult == AUTO_RESIZE_AMBIGUOUS )
256 return eResult;
258 // diagram is needed by the rest of the objects
259 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
260 if( ! xDiagram.is())
261 return eResult;
263 // Sub Title
264 Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
265 if( xDiaTitled.is())
266 impl_getAutoResizeFromTitled( xDiaTitled, eResult );
267 if( eResult == AUTO_RESIZE_AMBIGUOUS )
268 return eResult;
270 // Legend
271 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
272 if( xLegendProp.is())
273 getAutoResizeFromPropSet( xLegendProp, eResult );
274 if( eResult == AUTO_RESIZE_AMBIGUOUS )
275 return eResult;
277 // Axes (incl. Axis Titles)
278 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
279 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
281 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
282 if( xProp.is())
283 getAutoResizeFromPropSet( xProp, eResult );
284 Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
285 if( xTitled.is())
287 impl_getAutoResizeFromTitled( xTitled, eResult );
288 if( eResult == AUTO_RESIZE_AMBIGUOUS )
289 return eResult;
293 // DataSeries/Points
294 ::std::vector< Reference< XDataSeries > > aSeries(
295 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
297 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
298 aIt != aSeries.end(); ++aIt )
300 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
301 if( xSeriesProp.is())
303 getAutoResizeFromPropSet( xSeriesProp, eResult );
304 if( eResult == AUTO_RESIZE_AMBIGUOUS )
305 return eResult;
307 // data points
308 Sequence< sal_Int32 > aPointIndexes;
311 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
313 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
315 getAutoResizeFromPropSet(
316 (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
317 if( eResult == AUTO_RESIZE_AMBIGUOUS )
318 return eResult;
322 catch( const uno::Exception & ex )
324 ASSERT_EXCEPTION( ex );
329 return eResult;
332 void ReferenceSizeProvider::toggleAutoResizeState()
334 setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
338 /** sets the auto-resize at all objects that support this feature for text.
339 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
341 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
343 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
345 // Main Title
346 impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
348 // diagram is needed by the rest of the objects
349 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
350 if( ! xDiagram.is())
351 return;
353 // Sub Title
354 impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
356 // Legend
357 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
358 if( xLegendProp.is())
359 setValuesAtPropertySet( xLegendProp );
361 // Axes (incl. Axis Titles)
362 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
363 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
365 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
366 if( xProp.is())
367 setValuesAtPropertySet( xProp );
368 impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
371 // DataSeries/Points
372 setValuesAtAllDataSeries();
374 // recalculate new state (in case it stays unknown or is ambiguous
375 m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
378 } // namespace chart