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