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