Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
27 :
28 : #include <com/sun/star/chart2/XTitled.hpp>
29 : #include <com/sun/star/chart2/XTitle.hpp>
30 : #include <com/sun/star/chart2/XDataSeries.hpp>
31 :
32 : #include <vector>
33 :
34 : using namespace ::com::sun::star;
35 : using namespace ::com::sun::star::chart2;
36 :
37 : using ::com::sun::star::uno::Reference;
38 : using ::com::sun::star::uno::Sequence;
39 :
40 : namespace chart
41 : {
42 :
43 0 : ReferenceSizeProvider::ReferenceSizeProvider(
44 : awt::Size aPageSize,
45 : const Reference< XChartDocument > & xChartDoc ) :
46 : m_aPageSize( aPageSize ),
47 : m_xChartDoc( xChartDoc ),
48 0 : m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
49 0 : {}
50 :
51 0 : void ReferenceSizeProvider::impl_setValuesAtTitled(
52 : const Reference< XTitled > & xTitled )
53 : {
54 0 : if( xTitled.is())
55 : {
56 0 : Reference< XTitle > xTitle( xTitled->getTitleObject());
57 0 : if( xTitle.is())
58 0 : setValuesAtTitle( xTitle );
59 : }
60 0 : }
61 :
62 0 : void ReferenceSizeProvider::setValuesAtTitle(
63 : const Reference< XTitle > & xTitle )
64 : {
65 : try
66 : {
67 0 : Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
68 0 : awt::Size aOldRefSize;
69 : bool bHasOldRefSize(
70 0 : xTitleProp->getPropertyValue( "ReferencePageSize") >>= aOldRefSize );
71 :
72 : // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
73 0 : if( bHasOldRefSize && ! useAutoScale())
74 : {
75 : uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
76 0 : xTitle->getText());
77 0 : for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
78 : {
79 : RelativeSizeHelper::adaptFontSizes(
80 0 : Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
81 0 : aOldRefSize, getPageSize());
82 0 : }
83 : }
84 :
85 0 : setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
86 : }
87 0 : catch (const uno::Exception& ex)
88 : {
89 : ASSERT_EXCEPTION( ex );
90 : }
91 0 : }
92 :
93 0 : void ReferenceSizeProvider::setValuesAtAllDataSeries()
94 : {
95 0 : Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
96 :
97 : // DataSeries/Points
98 : ::std::vector< Reference< XDataSeries > > aSeries(
99 0 : DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
100 :
101 0 : for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
102 0 : aIt != aSeries.end(); ++aIt )
103 : {
104 0 : Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
105 0 : if( xSeriesProp.is())
106 : {
107 : // data points
108 0 : Sequence< sal_Int32 > aPointIndexes;
109 : try
110 : {
111 0 : if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
112 : {
113 0 : for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
114 : setValuesAtPropertySet(
115 0 : (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
116 : }
117 : }
118 0 : catch (const uno::Exception& ex)
119 : {
120 : ASSERT_EXCEPTION( ex );
121 : }
122 :
123 : //it is important to correct the datapoint properties first as they do reference the series properties
124 0 : setValuesAtPropertySet( xSeriesProp );
125 : }
126 0 : }
127 0 : }
128 :
129 0 : void ReferenceSizeProvider::setValuesAtPropertySet(
130 : const Reference< beans::XPropertySet > & xProp,
131 : bool bAdaptFontSizes /* = true */ )
132 : {
133 0 : if( ! xProp.is())
134 0 : return;
135 :
136 0 : static const OUString aRefSizeName( "ReferencePageSize" );
137 :
138 : try
139 : {
140 0 : awt::Size aRefSize( getPageSize() );
141 0 : awt::Size aOldRefSize;
142 0 : bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
143 :
144 0 : if( useAutoScale())
145 : {
146 0 : if( ! bHasOldRefSize )
147 0 : xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
148 : }
149 : else
150 : {
151 0 : if( bHasOldRefSize )
152 : {
153 0 : xProp->setPropertyValue( aRefSizeName, uno::Any());
154 :
155 : // adapt font sizes
156 0 : if( bAdaptFontSizes )
157 0 : RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
158 : }
159 : }
160 : }
161 0 : catch (const uno::Exception& ex)
162 : {
163 : ASSERT_EXCEPTION( ex );
164 : }
165 : }
166 :
167 11627 : void ReferenceSizeProvider::getAutoResizeFromPropSet(
168 : const Reference< beans::XPropertySet > & xProp,
169 : ReferenceSizeProvider::AutoResizeState & rInOutState )
170 : {
171 11627 : static const OUString aRefSizeName( "ReferencePageSize" );
172 11627 : AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
173 :
174 11627 : if( xProp.is())
175 : {
176 : try
177 : {
178 11627 : if( xProp->getPropertyValue( aRefSizeName ).hasValue())
179 0 : eSingleState = AUTO_RESIZE_YES;
180 : else
181 11627 : eSingleState = AUTO_RESIZE_NO;
182 : }
183 0 : catch (const uno::Exception&)
184 : {
185 : // unknown property -> state stays unknown
186 : }
187 : }
188 :
189 : // curent state unknown => nothing changes. Otherwise if current state
190 : // differs from state so far, we have an ambiguity
191 11627 : if( rInOutState == AUTO_RESIZE_UNKNOWN )
192 : {
193 1605 : rInOutState = eSingleState;
194 : }
195 20044 : else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
196 10022 : eSingleState != rInOutState )
197 : {
198 0 : rInOutState = AUTO_RESIZE_AMBIGUOUS;
199 : }
200 11627 : }
201 :
202 6455 : void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
203 : const Reference< XTitled > & xTitled,
204 : ReferenceSizeProvider::AutoResizeState & rInOutState )
205 : {
206 6455 : if( xTitled.is())
207 : {
208 6455 : Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
209 6455 : if( xProp.is())
210 1568 : getAutoResizeFromPropSet( xProp, rInOutState );
211 : }
212 6455 : }
213 :
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.
217 :
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.
221 : */
222 1637 : ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
223 : const Reference< XChartDocument > & xChartDoc )
224 : {
225 1637 : AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
226 :
227 : // Main Title
228 1637 : Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
229 1637 : if( xDocTitled.is())
230 1637 : impl_getAutoResizeFromTitled( xDocTitled, eResult );
231 1637 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
232 0 : return eResult;
233 :
234 : // diagram is needed by the rest of the objects
235 3274 : Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
236 1637 : if( ! xDiagram.is())
237 32 : return eResult;
238 :
239 : // Sub Title
240 3210 : Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
241 1605 : if( xDiaTitled.is())
242 1605 : impl_getAutoResizeFromTitled( xDiaTitled, eResult );
243 1605 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
244 0 : return eResult;
245 :
246 : // Legend
247 3210 : Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
248 1605 : if( xLegendProp.is())
249 1605 : getAutoResizeFromPropSet( xLegendProp, eResult );
250 1605 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
251 0 : return eResult;
252 :
253 : // Axes (incl. Axis Titles)
254 3210 : Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
255 4818 : for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
256 : {
257 3213 : Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
258 3213 : if( xProp.is())
259 3213 : getAutoResizeFromPropSet( xProp, eResult );
260 6426 : Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
261 3213 : if( xTitled.is())
262 : {
263 3213 : impl_getAutoResizeFromTitled( xTitled, eResult );
264 3213 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
265 0 : return eResult;
266 : }
267 3213 : }
268 :
269 : // DataSeries/Points
270 : ::std::vector< Reference< XDataSeries > > aSeries(
271 3210 : DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
272 :
273 19224 : for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
274 12816 : aIt != aSeries.end(); ++aIt )
275 : {
276 4803 : Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
277 4803 : if( xSeriesProp.is())
278 : {
279 4803 : getAutoResizeFromPropSet( xSeriesProp, eResult );
280 4803 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
281 0 : return eResult;
282 :
283 : // data points
284 4803 : Sequence< sal_Int32 > aPointIndexes;
285 : try
286 : {
287 4803 : if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
288 : {
289 5241 : for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
290 : {
291 : getAutoResizeFromPropSet(
292 438 : (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
293 438 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
294 0 : return eResult;
295 : }
296 : }
297 : }
298 0 : catch (const uno::Exception& ex)
299 : {
300 : ASSERT_EXCEPTION( ex );
301 4803 : }
302 : }
303 4803 : }
304 :
305 3242 : return eResult;
306 : }
307 :
308 0 : void ReferenceSizeProvider::toggleAutoResizeState()
309 : {
310 0 : setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
311 0 : }
312 :
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
315 : */
316 0 : void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
317 : {
318 0 : m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
319 :
320 : // Main Title
321 0 : impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
322 :
323 : // diagram is needed by the rest of the objects
324 0 : Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
325 0 : if( ! xDiagram.is())
326 0 : return;
327 :
328 : // Sub Title
329 0 : impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
330 :
331 : // Legend
332 0 : Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
333 0 : if( xLegendProp.is())
334 0 : setValuesAtPropertySet( xLegendProp );
335 :
336 : // Axes (incl. Axis Titles)
337 0 : Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
338 0 : for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
339 : {
340 0 : Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
341 0 : if( xProp.is())
342 0 : setValuesAtPropertySet( xProp );
343 0 : impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
344 0 : }
345 :
346 : // DataSeries/Points
347 0 : setValuesAtAllDataSeries();
348 :
349 : // recalculate new state (in case it stays unknown or is ambiguous
350 0 : m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
351 : }
352 :
353 108 : } // namespace chart
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|