1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "ReferenceSizeProvider.hxx"
32 #include "RelativeSizeHelper.hxx"
33 #include "ChartModelHelper.hxx"
34 #include "DiagramHelper.hxx"
35 #include "macros.hxx"
36 #include "AxisHelper.hxx"
37 #include "DataSeriesHelper.hxx"
38 
39 #include <com/sun/star/chart2/XTitled.hpp>
40 #include <com/sun/star/chart2/XTitle.hpp>
41 #include <com/sun/star/chart2/XDataSeries.hpp>
42 
43 #include <vector>
44 
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::chart2;
47 
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
50 using ::rtl::OUString;
51 
52 // ================================================================================
53 
54 namespace chart
55 {
56 
57 ReferenceSizeProvider::ReferenceSizeProvider(
58     awt::Size aPageSize,
59     const Reference< XChartDocument > & xChartDoc ) :
60         m_aPageSize( aPageSize ),
61         m_xChartDoc( xChartDoc ),
62         m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
63 {}
64 
65 awt::Size ReferenceSizeProvider::getPageSize() const
66 {
67     return m_aPageSize;
68 }
69 
70 bool ReferenceSizeProvider::useAutoScale() const
71 {
72     return m_bUseAutoScale;
73 }
74 
75 void ReferenceSizeProvider::impl_setValuesAtTitled(
76     const Reference< XTitled > & xTitled )
77 {
78     if( xTitled.is())
79     {
80         Reference< XTitle > xTitle( xTitled->getTitleObject());
81         if( xTitle.is())
82             setValuesAtTitle( xTitle );
83     }
84 }
85 
86 void ReferenceSizeProvider::setValuesAtTitle(
87     const Reference< XTitle > & xTitle )
88 {
89     try
90     {
91         Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
92         awt::Size aOldRefSize;
93         bool bHasOldRefSize(
94             xTitleProp->getPropertyValue( C2U("ReferencePageSize")) >>= aOldRefSize );
95 
96         // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
97         if( bHasOldRefSize && ! useAutoScale())
98         {
99             uno::Sequence< uno::Reference< XFormattedString > > aStrSeq(
100                 xTitle->getText());
101             for( sal_Int32 i=0; i<aStrSeq.getLength(); ++i )
102             {
103                 RelativeSizeHelper::adaptFontSizes(
104                     Reference< beans::XPropertySet >( aStrSeq[i], uno::UNO_QUERY ),
105                     aOldRefSize, getPageSize());
106             }
107         }
108 
109         setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
110     }
111     catch( const uno::Exception & ex )
112     {
113         ASSERT_EXCEPTION( ex );
114     }
115 }
116 
117 void ReferenceSizeProvider::setValuesAtAllDataSeries()
118 {
119     Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ));
120 
121     // DataSeries/Points
122     ::std::vector< Reference< XDataSeries > > aSeries(
123         DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
124 
125     for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
126          aIt != aSeries.end(); ++aIt )
127     {
128         Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
129         if( xSeriesProp.is())
130         {
131             // data points
132             Sequence< sal_Int32 > aPointIndexes;
133             try
134             {
135                 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
136                 {
137                     for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
138                         setValuesAtPropertySet(
139                             (*aIt)->getDataPointByIndex( aPointIndexes[i] ) );
140                 }
141             }
142             catch( const uno::Exception & ex )
143             {
144                 ASSERT_EXCEPTION( ex );
145             }
146 
147             //it is important to correct the datapoint properties first as they do reference the series properties
148             setValuesAtPropertySet( xSeriesProp );
149         }
150     }
151 }
152 
153 void ReferenceSizeProvider::setValuesAtPropertySet(
154     const Reference< beans::XPropertySet > & xProp,
155     bool bAdaptFontSizes /* = true */ )
156 {
157     if( ! xProp.is())
158         return;
159 
160     static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
161 
162     try
163     {
164         awt::Size aRefSize( getPageSize() );
165         awt::Size aOldRefSize;
166         bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
167 
168         if( useAutoScale())
169         {
170             if( ! bHasOldRefSize )
171                 xProp->setPropertyValue( aRefSizeName, uno::makeAny( aRefSize ));
172         }
173         else
174         {
175             if( bHasOldRefSize )
176             {
177                 xProp->setPropertyValue( aRefSizeName, uno::Any());
178 
179                 // adapt font sizes
180                 if( bAdaptFontSizes )
181                     RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
182             }
183         }
184     }
185     catch( const uno::Exception & ex )
186     {
187         ASSERT_EXCEPTION( ex );
188     }
189 }
190 
191 void ReferenceSizeProvider::getAutoResizeFromPropSet(
192     const Reference< beans::XPropertySet > & xProp,
193     ReferenceSizeProvider::AutoResizeState & rInOutState )
194 {
195     static const OUString aRefSizeName( RTL_CONSTASCII_USTRINGPARAM("ReferencePageSize"));
196     AutoResizeState eSingleState = AUTO_RESIZE_UNKNOWN;
197 
198     if( xProp.is())
199     {
200         try
201         {
202             if( xProp->getPropertyValue( aRefSizeName ).hasValue())
203                 eSingleState = AUTO_RESIZE_YES;
204             else
205                 eSingleState = AUTO_RESIZE_NO;
206         }
207         catch( uno::Exception )
208         {
209             // unknown property -> state stays unknown
210         }
211     }
212 
213     // curent state unknown => nothing changes.  Otherwise if current state
214     // differs from state so far, we have an ambiguity
215     if( rInOutState == AUTO_RESIZE_UNKNOWN )
216     {
217         rInOutState = eSingleState;
218     }
219     else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
220         eSingleState != rInOutState )
221     {
222         rInOutState = AUTO_RESIZE_AMBIGUOUS;
223     }
224 }
225 
226 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
227     const Reference< XTitled > & xTitled,
228     ReferenceSizeProvider::AutoResizeState & rInOutState )
229 {
230     if( xTitled.is())
231     {
232         Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
233         if( xProp.is())
234             getAutoResizeFromPropSet( xProp, rInOutState );
235     }
236 }
237 
238 /** Retrieves the state auto-resize from all objects that support this
239     feature.  If all objects return the same state, AUTO_RESIZE_YES or
240     AUTO_RESIZE_NO is returned.
241 
242     If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
243     returned.  If there are multiple objects, some with state YES and some
244     with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
245 */
246 ReferenceSizeProvider::AutoResizeState ReferenceSizeProvider::getAutoResizeState(
247     const Reference< XChartDocument > & xChartDoc )
248 {
249     AutoResizeState eResult = AUTO_RESIZE_UNKNOWN;
250 
251     // Main Title
252     Reference< XTitled > xDocTitled( xChartDoc, uno::UNO_QUERY );
253     if( xDocTitled.is())
254         impl_getAutoResizeFromTitled( xDocTitled, eResult );
255     if( eResult == AUTO_RESIZE_AMBIGUOUS )
256         return eResult;
257 
258     // diagram is needed by the rest of the objects
259     Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartDoc ), uno::UNO_QUERY );
260     if( ! xDiagram.is())
261         return eResult;
262 
263     // Sub Title
264     Reference< XTitled > xDiaTitled( xDiagram, uno::UNO_QUERY );
265     if( xDiaTitled.is())
266         impl_getAutoResizeFromTitled( xDiaTitled, eResult );
267     if( eResult == AUTO_RESIZE_AMBIGUOUS )
268         return eResult;
269 
270     // Legend
271     Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
272     if( xLegendProp.is())
273         getAutoResizeFromPropSet( xLegendProp, eResult );
274     if( eResult == AUTO_RESIZE_AMBIGUOUS )
275         return eResult;
276 
277     // Axes (incl. Axis Titles)
278     Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
279     for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
280     {
281         Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
282         if( xProp.is())
283             getAutoResizeFromPropSet( xProp, eResult );
284         Reference< XTitled > xTitled( aAxes[i], uno::UNO_QUERY );
285         if( xTitled.is())
286         {
287             impl_getAutoResizeFromTitled( xTitled, eResult );
288             if( eResult == AUTO_RESIZE_AMBIGUOUS )
289                 return eResult;
290         }
291     }
292 
293     // DataSeries/Points
294     ::std::vector< Reference< XDataSeries > > aSeries(
295         DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
296 
297     for( ::std::vector< Reference< XDataSeries > >::const_iterator aIt( aSeries.begin());
298          aIt != aSeries.end(); ++aIt )
299     {
300         Reference< beans::XPropertySet > xSeriesProp( *aIt, uno::UNO_QUERY );
301         if( xSeriesProp.is())
302         {
303             getAutoResizeFromPropSet( xSeriesProp, eResult );
304             if( eResult == AUTO_RESIZE_AMBIGUOUS )
305                 return eResult;
306 
307             // data points
308             Sequence< sal_Int32 > aPointIndexes;
309             try
310             {
311                 if( xSeriesProp->getPropertyValue( C2U("AttributedDataPoints")) >>= aPointIndexes )
312                 {
313                     for( sal_Int32 i=0; i< aPointIndexes.getLength(); ++i )
314                     {
315                         getAutoResizeFromPropSet(
316                             (*aIt)->getDataPointByIndex( aPointIndexes[i] ), eResult );
317                         if( eResult == AUTO_RESIZE_AMBIGUOUS )
318                             return eResult;
319                     }
320                 }
321             }
322             catch( const uno::Exception & ex )
323             {
324                 ASSERT_EXCEPTION( ex );
325             }
326         }
327     }
328 
329     return eResult;
330 }
331 
332 void ReferenceSizeProvider::toggleAutoResizeState()
333 {
334     setAutoResizeState( m_bUseAutoScale ? AUTO_RESIZE_NO : AUTO_RESIZE_YES );
335 }
336 
337 
338 /** sets the auto-resize at all objects that support this feature for text.
339     eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
340 */
341 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState )
342 {
343     m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
344 
345     // Main Title
346     impl_setValuesAtTitled( Reference< XTitled >( m_xChartDoc, uno::UNO_QUERY ));
347 
348     // diagram is needed by the rest of the objects
349     Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartDoc ), uno::UNO_QUERY );
350     if( ! xDiagram.is())
351         return;
352 
353     // Sub Title
354     impl_setValuesAtTitled( Reference< XTitled >( xDiagram, uno::UNO_QUERY ));
355 
356     // Legend
357     Reference< beans::XPropertySet > xLegendProp( xDiagram->getLegend(), uno::UNO_QUERY );
358     if( xLegendProp.is())
359         setValuesAtPropertySet( xLegendProp );
360 
361     // Axes (incl. Axis Titles)
362     Sequence< Reference< XAxis > > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram ) );
363     for( sal_Int32 i=0; i<aAxes.getLength(); ++i )
364     {
365         Reference< beans::XPropertySet > xProp( aAxes[i], uno::UNO_QUERY );
366         if( xProp.is())
367             setValuesAtPropertySet( xProp );
368         impl_setValuesAtTitled( Reference< XTitled >( aAxes[i], uno::UNO_QUERY ));
369     }
370 
371     // DataSeries/Points
372     setValuesAtAllDataSeries();
373 
374     // recalculate new state (in case it stays unknown or is ambiguous
375     m_bUseAutoScale = (getAutoResizeState( m_xChartDoc ) == AUTO_RESIZE_YES);
376 }
377 
378 } //  namespace chart
379