Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / ReferenceSizeProvider.cxx
blob7cd07d9117039a405f41d4ed96a9f87864e6d116
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 <AxisHelper.hxx>
26 #include <com/sun/star/chart2/XTitled.hpp>
27 #include <com/sun/star/chart2/XTitle.hpp>
28 #include <com/sun/star/chart2/XDataSeries.hpp>
29 #include <tools/diagnose_ex.h>
31 #include <vector>
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::chart2;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Sequence;
39 namespace chart
42 ReferenceSizeProvider::ReferenceSizeProvider(
43 awt::Size aPageSize,
44 const Reference< XChartDocument > & xChartDoc ) :
45 m_aPageSize( aPageSize ),
46 m_xChartDoc( xChartDoc ),
47 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
50 void ReferenceSizeProvider::impl_setValuesAtTitled(
51 const Reference< XTitled > & xTitled )
53 if( xTitled.is())
55 Reference< XTitle > xTitle( xTitled->getTitleObject());
56 if( xTitle.is())
57 setValuesAtTitle( xTitle );
61 void ReferenceSizeProvider::setValuesAtTitle(
62 const Reference< XTitle > & xTitle )
64 try
66 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
67 awt::Size aOldRefSize;
68 bool bHasOldRefSize(
69 xTitleProp->getPropertyValue( "ReferencePageSize") >>= aOldRefSize );
71 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
72 if( bHasOldRefSize && ! useAutoScale())
74 uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
75 xTitle->getText());
76 for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
78 RelativeSizeHelper::adaptFontSizes(
79 Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
80 aOldRefSize, getPageSize());
84 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
86 catch (const uno::Exception&)
88 DBG_UNHANDLED_EXCEPTION("chart2");
92 void ReferenceSizeProvider::setValuesAtAllDataSeries()
94 Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
96 // DataSeries/Points
97 std::vector< Reference< XDataSeries > > aSeries(
98 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
100 for (auto const& elem : aSeries)
102 Reference< beans::XPropertySet > xSeriesProp(elem, uno::UNO_QUERY );
103 if( xSeriesProp.is())
105 // data points
106 Sequence< sal_Int32 > aPointIndexes;
109 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
111 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
112 setValuesAtPropertySet(
113 elem->getDataPointByIndex( aPointIndexes[i] ) );
116 catch (const uno::Exception&)
118 DBG_UNHANDLED_EXCEPTION("chart2");
121 //it is important to correct the datapoint properties first as they do reference the series properties
122 setValuesAtPropertySet( xSeriesProp );
127 void ReferenceSizeProvider::setValuesAtPropertySet(
128 const Reference< beans::XPropertySet > & xProp,
129 bool bAdaptFontSizes /* = true */ )
131 if( ! xProp.is())
132 return;
134 static const char aRefSizeName[] = "ReferencePageSize";
138 awt::Size aRefSize( getPageSize() );
139 awt::Size aOldRefSize;
140 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
142 if( useAutoScale())
144 if( ! bHasOldRefSize )
145 xProp->setPropertyValue( aRefSizeName, uno::Any( aRefSize ));
147 else
149 if( bHasOldRefSize )
151 xProp->setPropertyValue( aRefSizeName, uno::Any());
153 // adapt font sizes
154 if( bAdaptFontSizes )
155 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
159 catch (const uno::Exception&)
161 DBG_UNHANDLED_EXCEPTION("chart2");
165 void ReferenceSizeProvider::getAutoResizeFromPropSet(
166 const Reference< beans::XPropertySet > & xProp,
167 ReferenceSizeProvider::AutoResizeState & rInOutState )
169 AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
171 if( xProp.is())
175 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue())
176 eSingleState = AUTO_RESIZE_YES;
177 else
178 eSingleState = AUTO_RESIZE_NO;
180 catch (const uno::Exception&)
182 // unknown property -> state stays unknown
186 // current state unknown => nothing changes. Otherwise if current state
187 // differs from state so far, we have an ambiguity
188 if( rInOutState == AUTO_RESIZE_UNKNOWN )
190 rInOutState = eSingleState;
192 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
193 eSingleState != rInOutState )
195 rInOutState = AUTO_RESIZE_AMBIGUOUS;
199 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
200 const Reference< XTitled > & xTitled,
201 ReferenceSizeProvider::AutoResizeState & rInOutState )
203 if( xTitled.is())
205 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
206 if( xProp.is())
207 getAutoResizeFromPropSet( xProp, rInOutState );
211 /** Retrieves the state auto-resize from all objects that support this
212 feature. If all objects return the same state, AUTO_RESIZE_YES or
213 AUTO_RESIZE_NO is returned.
215 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
216 returned. If there are multiple objects, some with state YES and some
217 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
219 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
220 const Reference< XChartDocument > & xChartDoc )
222 AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
224 // Main Title
225 Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
226 if( xDocTitled.is())
227 impl_getAutoResizeFromTitled( xDocTitled, eResult );
228 if( eResult == AUTO_RESIZE_AMBIGUOUS )
229 return eResult;
231 // diagram is needed by the rest of the objects
232 Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
233 if( ! xDiagram.is())
234 return eResult;
236 // Sub Title
237 Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
238 if( xDiaTitled.is())
239 impl_getAutoResizeFromTitled( xDiaTitled, eResult );
240 if( eResult == AUTO_RESIZE_AMBIGUOUS )
241 return eResult;
243 // Legend
244 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
245 if( xLegendProp.is())
246 getAutoResizeFromPropSet( xLegendProp, eResult );
247 if( eResult == AUTO_RESIZE_AMBIGUOUS )
248 return eResult;
250 // Axes (incl. Axis Titles)
251 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
252 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
254 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
255 if( xProp.is())
256 getAutoResizeFromPropSet( xProp, eResult );
257 Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
258 if( xTitled.is())
260 impl_getAutoResizeFromTitled( xTitled, eResult );
261 if( eResult == AUTO_RESIZE_AMBIGUOUS )
262 return eResult;
266 // DataSeries/Points
267 std::vector< Reference< XDataSeries > > aSeries(
268 DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
270 for (auto const& elem : aSeries)
272 Reference< beans::XPropertySet > xSeriesProp(elem, uno::UNO_QUERY);
273 if( xSeriesProp.is())
275 getAutoResizeFromPropSet( xSeriesProp, eResult );
276 if( eResult == AUTO_RESIZE_AMBIGUOUS )
277 return eResult;
279 // data points
280 Sequence< sal_Int32 > aPointIndexes;
283 if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
285 for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
287 getAutoResizeFromPropSet(
288 elem->getDataPointByIndex( aPointIndexes[i] ), eResult );
289 if( eResult == AUTO_RESIZE_AMBIGUOUS )
290 return eResult;
294 catch (const uno::Exception&)
296 DBG_UNHANDLED_EXCEPTION("chart2");
301 return eResult;
304 void ReferenceSizeProvider::toggleAutoResizeState()
306 setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
309 /** sets the auto-resize at all objects that support this feature for text.
310 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
312 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
314 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
316 // Main Title
317 impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
319 // diagram is needed by the rest of the objects
320 Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( m_xChartDoc );
321 if( ! xDiagram.is())
322 return;
324 // Sub Title
325 impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
327 // Legend
328 Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
329 if( xLegendProp.is())
330 setValuesAtPropertySet( xLegendProp );
332 // Axes (incl. Axis Titles)
333 Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
334 for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
336 Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
337 if( xProp.is())
338 setValuesAtPropertySet( xProp );
339 impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
342 // DataSeries/Points
343 setValuesAtAllDataSeries();
345 // recalculate new state (in case it stays unknown or is ambiguous
346 m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
349 } // namespace chart
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */