update credits
[LibreOffice.git] / chart2 / source / tools / ReferenceSizeProvider.cxx
blob9e41285f2afc1f0852b83802f5bf8225683b16e2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "ReferenceSizeProvider.hxx"
22 #include "RelativeSizeHelper.hxx"
23 #include "ChartModelHelper.hxx"
24 #include "DiagramHelper.hxx"
25 #include "macros.hxx"
26 #include "AxisHelper.hxx"
27 #include "DataSeriesHelper.hxx"
29 #include <com/sun/star/chart2/XTitled.hpp>
30 #include <com/sun/star/chart2/XTitle.hpp>
31 #include <com/sun/star/chart2/XDataSeries.hpp>
33 #include <vector>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::chart2;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
41 // ================================================================================
43 namespace chart
46 ReferenceSizeProvider::ReferenceSizeProvider(
47 awt::Size aPageSize,
48 const Reference< XChartDocument > & xChartDoc ) :
49 m_aPageSize( aPageSize ),
50 m_xChartDoc( xChartDoc ),
51 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
54 awt::Size ReferenceSizeProvider::getPageSize() const
56 return m_aPageSize;
59 bool ReferenceSizeProvider::useAutoScale() const
61 return m_bUseAutoScale;
64 void ReferenceSizeProvider::impl_setValuesAtTitled(
65 const Reference< XTitled > & xTitled )
67 if( xTitled.is())
69 Reference< XTitle > xTitle( xTitled->getTitleObject());
70 if( xTitle.is())
71 setValuesAtTitle( xTitle );
75 void ReferenceSizeProvider::setValuesAtTitle(
76 const Reference< XTitle > & xTitle )
78 try
80 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
81 awt::Size aOldRefSize;
82 bool bHasOldRefSize(
83 xTitleProp->getPropertyValue( "ReferencePageSize") >>= aOldRefSize );
85 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
86 if( bHasOldRefSize && ! useAutoScale())
88 uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
89 xTitle->getText());
90 for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
92 RelativeSizeHelper::adaptFontSizes(
93 Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
94 aOldRefSize, getPageSize());
98 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
100 catch (const uno::Exception& ex)
102 ASSERT_EXCEPTION( ex );
106 void ReferenceSizeProvider::setValuesAtAllDataSeries()
108 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
110 // DataSeries/Points
111 ::std::vector< Reference< XDataSeries > > aSeries(
112 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
114 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
115 aIt != aSeries.end(); ++aIt )
117 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
118 if( xSeriesProp.is())
120 // data points
121 Sequence< sal_Int32 > aPointIndexes;
124 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
126 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
127 setValuesAtPropertySet(
128 (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
131 catch (const uno::Exception& ex)
133 ASSERT_EXCEPTION( ex );
136 //it is important to correct the datapoint properties first as they do reference the series properties
137 setValuesAtPropertySet( xSeriesProp );
142 void ReferenceSizeProvider::setValuesAtPropertySet(
143 const Reference< beans::XPropertySet > & xProp,
144 bool bAdaptFontSizes /* = true */ )
146 if( ! xProp.is())
147 return;
149 static const OUString aRefSizeName( "ReferencePageSize" );
153 awt::Size aRefSize( getPageSize() );
154 awt::Size aOldRefSize;
155 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
157 if( useAutoScale())
159 if( ! bHasOldRefSize )
160 xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
162 else
164 if( bHasOldRefSize )
166 xProp->setPropertyValue( aRefSizeName, uno::Any());
168 // adapt font sizes
169 if( bAdaptFontSizes )
170 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
174 catch (const uno::Exception& ex)
176 ASSERT_EXCEPTION( ex );
180 void ReferenceSizeProvider::getAutoResizeFromPropSet(
181 const Reference< beans::XPropertySet > & xProp,
182 ReferenceSizeProvider::AutoResizeState & rInOutState )
184 static const OUString aRefSizeName( "ReferencePageSize" );
185 AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
187 if( xProp.is())
191 if( xProp->getPropertyValue( aRefSizeName ).hasValue())
192 eSingleState = AUTO_RESIZE_YES;
193 else
194 eSingleState = AUTO_RESIZE_NO;
196 catch (const uno::Exception&)
198 // unknown property -> state stays unknown
202 // curent state unknown => nothing changes. Otherwise if current state
203 // differs from state so far, we have an ambiguity
204 if( rInOutState == AUTO_RESIZE_UNKNOWN )
206 rInOutState = eSingleState;
208 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
209 eSingleState != rInOutState )
211 rInOutState = AUTO_RESIZE_AMBIGUOUS;
215 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
216 const Reference< XTitled > & xTitled,
217 ReferenceSizeProvider::AutoResizeState & rInOutState )
219 if( xTitled.is())
221 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
222 if( xProp.is())
223 getAutoResizeFromPropSet( xProp, rInOutState );
227 /** Retrieves the state auto-resize from all objects that support this
228 feature. If all objects return the same state, AUTO_RESIZE_YES or
229 AUTO_RESIZE_NO is returned.
231 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
232 returned. If there are multiple objects, some with state YES and some
233 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
235 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
236 const Reference< XChartDocument > & xChartDoc )
238 AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
240 // Main Title
241 Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
242 if( xDocTitled.is())
243 impl_getAutoResizeFromTitled( xDocTitled, eResult );
244 if( eResult == AUTO_RESIZE_AMBIGUOUS )
245 return eResult;
247 // diagram is needed by the rest of the objects
248 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
249 if( ! xDiagram.is())
250 return eResult;
252 // Sub Title
253 Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
254 if( xDiaTitled.is())
255 impl_getAutoResizeFromTitled( xDiaTitled, eResult );
256 if( eResult == AUTO_RESIZE_AMBIGUOUS )
257 return eResult;
259 // Legend
260 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
261 if( xLegendProp.is())
262 getAutoResizeFromPropSet( xLegendProp, eResult );
263 if( eResult == AUTO_RESIZE_AMBIGUOUS )
264 return eResult;
266 // Axes (incl. Axis Titles)
267 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
268 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
270 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
271 if( xProp.is())
272 getAutoResizeFromPropSet( xProp, eResult );
273 Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
274 if( xTitled.is())
276 impl_getAutoResizeFromTitled( xTitled, eResult );
277 if( eResult == AUTO_RESIZE_AMBIGUOUS )
278 return eResult;
282 // DataSeries/Points
283 ::std::vector< Reference< XDataSeries > > aSeries(
284 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
286 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
287 aIt != aSeries.end(); ++aIt )
289 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
290 if( xSeriesProp.is())
292 getAutoResizeFromPropSet( xSeriesProp, eResult );
293 if( eResult == AUTO_RESIZE_AMBIGUOUS )
294 return eResult;
296 // data points
297 Sequence< sal_Int32 > aPointIndexes;
300 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
302 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
304 getAutoResizeFromPropSet(
305 (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
306 if( eResult == AUTO_RESIZE_AMBIGUOUS )
307 return eResult;
311 catch (const uno::Exception& ex)
313 ASSERT_EXCEPTION( ex );
318 return eResult;
321 void ReferenceSizeProvider::toggleAutoResizeState()
323 setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
327 /** sets the auto-resize at all objects that support this feature for text.
328 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
330 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
332 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
334 // Main Title
335 impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
337 // diagram is needed by the rest of the objects
338 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
339 if( ! xDiagram.is())
340 return;
342 // Sub Title
343 impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
345 // Legend
346 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
347 if( xLegendProp.is())
348 setValuesAtPropertySet( xLegendProp );
350 // Axes (incl. Axis Titles)
351 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
352 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
354 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
355 if( xProp.is())
356 setValuesAtPropertySet( xProp );
357 impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
360 // DataSeries/Points
361 setValuesAtAllDataSeries();
363 // recalculate new state (in case it stays unknown or is ambiguous
364 m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
367 } // namespace chart
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */