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