bump product version to 5.0.4.1
[LibreOffice.git] / chart2 / source / tools / ReferenceSizeProvider.cxx
blob03b71adc35fd6dd530784d15fb7d67db2687d4a9
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 .
20 #include "ReferenceSizeProvider.hxx"
21 #include "RelativeSizeHelper.hxx"
22 #include "ChartModelHelper.hxx"
23 #include "DiagramHelper.hxx"
24 #include "macros.hxx"
25 #include "AxisHelper.hxx"
26 #include "DataSeriesHelper.hxx"
28 #include <com/sun/star/chart2/XTitled.hpp>
29 #include <com/sun/star/chart2/XTitle.hpp>
30 #include <com/sun/star/chart2/XDataSeries.hpp>
32 #include <vector>
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::chart2;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Sequence;
40 namespace chart
43 ReferenceSizeProvider::ReferenceSizeProvider(
44 awt::Size aPageSize,
45 const Reference< XChartDocument > & xChartDoc ) :
46 m_aPageSize( aPageSize ),
47 m_xChartDoc( xChartDoc ),
48 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
51 void ReferenceSizeProvider::impl_setValuesAtTitled(
52 const Reference< XTitled > & xTitled )
54 if( xTitled.is())
56 Reference< XTitle > xTitle( xTitled->getTitleObject());
57 if( xTitle.is())
58 setValuesAtTitle( xTitle );
62 void ReferenceSizeProvider::setValuesAtTitle(
63 const Reference< XTitle > & xTitle )
65 try
67 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
68 awt::Size aOldRefSize;
69 bool bHasOldRefSize(
70 xTitleProp->getPropertyValue( "ReferencePageSize") >>= aOldRefSize );
72 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
73 if( bHasOldRefSize && ! useAutoScale())
75 uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
76 xTitle->getText());
77 for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
79 RelativeSizeHelper::adaptFontSizes(
80 Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
81 aOldRefSize, getPageSize());
85 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
87 catch (const uno::Exception& ex)
89 ASSERT_EXCEPTION( ex );
93 void ReferenceSizeProvider::setValuesAtAllDataSeries()
95 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
97 // DataSeries/Points
98 ::std::vector< Reference< XDataSeries > > aSeries(
99 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
101 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
102 aIt != aSeries.end(); ++aIt )
104 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
105 if( xSeriesProp.is())
107 // data points
108 Sequence< sal_Int32 > aPointIndexes;
111 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
113 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
114 setValuesAtPropertySet(
115 (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
118 catch (const uno::Exception& ex)
120 ASSERT_EXCEPTION( ex );
123 //it is important to correct the datapoint properties first as they do reference the series properties
124 setValuesAtPropertySet( xSeriesProp );
129 void ReferenceSizeProvider::setValuesAtPropertySet(
130 const Reference< beans::XPropertySet > & xProp,
131 bool bAdaptFontSizes /* = true */ )
133 if( ! xProp.is())
134 return;
136 static const char aRefSizeName[] = "ReferencePageSize";
140 awt::Size aRefSize( getPageSize() );
141 awt::Size aOldRefSize;
142 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
144 if( useAutoScale())
146 if( ! bHasOldRefSize )
147 xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
149 else
151 if( bHasOldRefSize )
153 xProp->setPropertyValue( aRefSizeName, uno::Any());
155 // adapt font sizes
156 if( bAdaptFontSizes )
157 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
161 catch (const uno::Exception& ex)
163 ASSERT_EXCEPTION( ex );
167 void ReferenceSizeProvider::getAutoResizeFromPropSet(
168 const Reference< beans::XPropertySet > & xProp,
169 ReferenceSizeProvider::AutoResizeState & rInOutState )
171 static const char aRefSizeName[] = "ReferencePageSize";
172 AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
174 if( xProp.is())
178 if( xProp->getPropertyValue( aRefSizeName ).hasValue())
179 eSingleState = AUTO_RESIZE_YES;
180 else
181 eSingleState = AUTO_RESIZE_NO;
183 catch (const uno::Exception&)
185 // unknown property -> state stays unknown
189 // current state unknown => nothing changes. Otherwise if current state
190 // differs from state so far, we have an ambiguity
191 if( rInOutState == AUTO_RESIZE_UNKNOWN )
193 rInOutState = eSingleState;
195 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
196 eSingleState != rInOutState )
198 rInOutState = AUTO_RESIZE_AMBIGUOUS;
202 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
203 const Reference< XTitled > & xTitled,
204 ReferenceSizeProvider::AutoResizeState & rInOutState )
206 if( xTitled.is())
208 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
209 if( xProp.is())
210 getAutoResizeFromPropSet( xProp, rInOutState );
214 /** Retrieves the state auto-resize from all objects that support this
215 feature. If all objects return the same state, AUTO_RESIZE_YES or
216 AUTO_RESIZE_NO is returned.
218 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
219 returned. If there are multiple objects, some with state YES and some
220 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
222 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
223 const Reference< XChartDocument > & xChartDoc )
225 AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
227 // Main Title
228 Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
229 if( xDocTitled.is())
230 impl_getAutoResizeFromTitled( xDocTitled, eResult );
231 if( eResult == AUTO_RESIZE_AMBIGUOUS )
232 return eResult;
234 // diagram is needed by the rest of the objects
235 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
236 if( ! xDiagram.is())
237 return eResult;
239 // Sub Title
240 Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
241 if( xDiaTitled.is())
242 impl_getAutoResizeFromTitled( xDiaTitled, eResult );
243 if( eResult == AUTO_RESIZE_AMBIGUOUS )
244 return eResult;
246 // Legend
247 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
248 if( xLegendProp.is())
249 getAutoResizeFromPropSet( xLegendProp, eResult );
250 if( eResult == AUTO_RESIZE_AMBIGUOUS )
251 return eResult;
253 // Axes (incl. Axis Titles)
254 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
255 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
257 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
258 if( xProp.is())
259 getAutoResizeFromPropSet( xProp, eResult );
260 Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
261 if( xTitled.is())
263 impl_getAutoResizeFromTitled( xTitled, eResult );
264 if( eResult == AUTO_RESIZE_AMBIGUOUS )
265 return eResult;
269 // DataSeries/Points
270 ::std::vector< Reference< XDataSeries > > aSeries(
271 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
273 for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
274 aIt != aSeries.end(); ++aIt )
276 Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
277 if( xSeriesProp.is())
279 getAutoResizeFromPropSet( xSeriesProp, eResult );
280 if( eResult == AUTO_RESIZE_AMBIGUOUS )
281 return eResult;
283 // data points
284 Sequence< sal_Int32 > aPointIndexes;
287 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
289 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
291 getAutoResizeFromPropSet(
292 (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
293 if( eResult == AUTO_RESIZE_AMBIGUOUS )
294 return eResult;
298 catch (const uno::Exception& ex)
300 ASSERT_EXCEPTION( ex );
305 return eResult;
308 void ReferenceSizeProvider::toggleAutoResizeState()
310 setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
313 /** sets the auto-resize at all objects that support this feature for text.
314 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
316 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
318 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
320 // Main Title
321 impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
323 // diagram is needed by the rest of the objects
324 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
325 if( ! xDiagram.is())
326 return;
328 // Sub Title
329 impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
331 // Legend
332 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
333 if( xLegendProp.is())
334 setValuesAtPropertySet( xLegendProp );
336 // Axes (incl. Axis Titles)
337 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
338 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
340 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
341 if( xProp.is())
342 setValuesAtPropertySet( xProp );
343 impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
346 // DataSeries/Points
347 setValuesAtAllDataSeries();
349 // recalculate new state (in case it stays unknown or is ambiguous
350 m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
353 } // namespace chart
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */