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 : static const char 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 5813 : void ReferenceSizeProvider::getAutoResizeFromPropSet(
168 : const Reference< beans::XPropertySet > & xProp,
169 : ReferenceSizeProvider::AutoResizeState & rInOutState )
170 : {
171 : static const char aRefSizeName[] = "ReferencePageSize";
172 5813 : AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
173 :
174 5813 : if( xProp.is())
175 : {
176 : try
177 : {
178 5813 : if( xProp->getPropertyValue( aRefSizeName ).hasValue())
179 0 : eSingleState = AUTO_RESIZE_YES;
180 : else
181 5813 : eSingleState = AUTO_RESIZE_NO;
182 : }
183 0 : catch (const uno::Exception&)
184 : {
185 : // unknown property -> state stays unknown
186 : }
187 : }
188 :
189 : // current state unknown => nothing changes. Otherwise if current state
190 : // differs from state so far, we have an ambiguity
191 5813 : if( rInOutState == AUTO_RESIZE_UNKNOWN )
192 : {
193 804 : rInOutState = eSingleState;
194 : }
195 10018 : else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
196 5009 : eSingleState != rInOutState )
197 : {
198 0 : rInOutState = AUTO_RESIZE_AMBIGUOUS;
199 : }
200 5813 : }
201 :
202 3234 : void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
203 : const Reference< XTitled > & xTitled,
204 : ReferenceSizeProvider::AutoResizeState & rInOutState )
205 : {
206 3234 : if( xTitled.is())
207 : {
208 3234 : Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
209 3234 : if( xProp.is())
210 775 : getAutoResizeFromPropSet( xProp, rInOutState );
211 : }
212 3234 : }
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 820 : ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
223 : const Reference< XChartDocument > & xChartDoc )
224 : {
225 820 : AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
226 :
227 : // Main Title
228 820 : Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
229 820 : if( xDocTitled.is())
230 820 : impl_getAutoResizeFromTitled( xDocTitled, eResult );
231 820 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
232 0 : return eResult;
233 :
234 : // diagram is needed by the rest of the objects
235 1640 : Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
236 820 : if( ! xDiagram.is())
237 16 : return eResult;
238 :
239 : // Sub Title
240 1608 : Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
241 804 : if( xDiaTitled.is())
242 804 : impl_getAutoResizeFromTitled( xDiaTitled, eResult );
243 804 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
244 0 : return eResult;
245 :
246 : // Legend
247 1608 : Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
248 804 : if( xLegendProp.is())
249 804 : getAutoResizeFromPropSet( xLegendProp, eResult );
250 804 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
251 0 : return eResult;
252 :
253 : // Axes (incl. Axis Titles)
254 1608 : Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
255 2414 : for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
256 : {
257 1610 : Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
258 1610 : if( xProp.is())
259 1610 : getAutoResizeFromPropSet( xProp, eResult );
260 3220 : Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
261 1610 : if( xTitled.is())
262 : {
263 1610 : impl_getAutoResizeFromTitled( xTitled, eResult );
264 1610 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
265 0 : return eResult;
266 : }
267 1610 : }
268 :
269 : // DataSeries/Points
270 : ::std::vector< Reference< XDataSeries > > aSeries(
271 1608 : DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
272 :
273 9630 : for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
274 6420 : aIt != aSeries.end(); ++aIt )
275 : {
276 2406 : Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
277 2406 : if( xSeriesProp.is())
278 : {
279 2406 : getAutoResizeFromPropSet( xSeriesProp, eResult );
280 2406 : if( eResult == AUTO_RESIZE_AMBIGUOUS )
281 0 : return eResult;
282 :
283 : // data points
284 2406 : Sequence< sal_Int32 > aPointIndexes;
285 : try
286 : {
287 2406 : if( xSeriesProp->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes )
288 : {
289 2624 : for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
290 : {
291 : getAutoResizeFromPropSet(
292 218 : (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
293 218 : 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 2406 : }
302 : }
303 2406 : }
304 :
305 1624 : 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 : } // namespace chart
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|