1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ReferenceSizeProvider.hxx>
21 #include <RelativeSizeHelper.hxx>
22 #include <ChartModelHelper.hxx>
23 #include <DiagramHelper.hxx>
24 #include <AxisHelper.hxx>
26 #include <com/sun/star/chart2/XTitled.hpp>
27 #include <com/sun/star/chart2/XTitle.hpp>
28 #include <com/sun/star/chart2/XDataSeries.hpp>
29 #include <tools/diagnose_ex.h>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::chart2
;
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::uno::Sequence
;
42 ReferenceSizeProvider::ReferenceSizeProvider(
44 const Reference
< XChartDocument
> & xChartDoc
) :
45 m_aPageSize( aPageSize
),
46 m_xChartDoc( xChartDoc
),
47 m_bUseAutoScale( getAutoResizeState( xChartDoc
) == AUTO_RESIZE_YES
)
50 void ReferenceSizeProvider::impl_setValuesAtTitled(
51 const Reference
< XTitled
> & xTitled
)
55 Reference
< XTitle
> xTitle( xTitled
->getTitleObject());
57 setValuesAtTitle( xTitle
);
61 void ReferenceSizeProvider::setValuesAtTitle(
62 const Reference
< XTitle
> & xTitle
)
66 Reference
< beans::XPropertySet
> xTitleProp( xTitle
, uno::UNO_QUERY_THROW
);
67 awt::Size aOldRefSize
;
69 xTitleProp
->getPropertyValue( "ReferencePageSize") >>= aOldRefSize
);
71 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
72 if( bHasOldRefSize
&& ! useAutoScale())
74 uno::Sequence
< uno::Reference
< XFormattedString
> > aStrSeq(
76 for( sal_Int32 i
=0; i
<aStrSeq
.getLength(); ++i
)
78 RelativeSizeHelper::adaptFontSizes(
79 Reference
< beans::XPropertySet
>( aStrSeq
[i
], uno::UNO_QUERY
),
80 aOldRefSize
, getPageSize());
84 setValuesAtPropertySet( xTitleProp
, /* bAdaptFontSizes = */ false );
86 catch (const uno::Exception
&)
88 DBG_UNHANDLED_EXCEPTION("chart2");
92 void ReferenceSizeProvider::setValuesAtAllDataSeries()
94 Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( m_xChartDoc
));
97 std::vector
< Reference
< XDataSeries
> > aSeries(
98 DiagramHelper::getDataSeriesFromDiagram( xDiagram
));
100 for (auto const& elem
: aSeries
)
102 Reference
< beans::XPropertySet
> xSeriesProp(elem
, uno::UNO_QUERY
);
103 if( xSeriesProp
.is())
106 Sequence
< sal_Int32
> aPointIndexes
;
109 if( xSeriesProp
->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes
)
111 for( sal_Int32 i
=0; i
< aPointIndexes
.getLength(); ++i
)
112 setValuesAtPropertySet(
113 elem
->getDataPointByIndex( aPointIndexes
[i
] ) );
116 catch (const uno::Exception
&)
118 DBG_UNHANDLED_EXCEPTION("chart2");
121 //it is important to correct the datapoint properties first as they do reference the series properties
122 setValuesAtPropertySet( xSeriesProp
);
127 void ReferenceSizeProvider::setValuesAtPropertySet(
128 const Reference
< beans::XPropertySet
> & xProp
,
129 bool bAdaptFontSizes
/* = true */ )
134 static const char aRefSizeName
[] = "ReferencePageSize";
138 awt::Size
aRefSize( getPageSize() );
139 awt::Size aOldRefSize
;
140 bool bHasOldRefSize( xProp
->getPropertyValue( aRefSizeName
) >>= aOldRefSize
);
144 if( ! bHasOldRefSize
)
145 xProp
->setPropertyValue( aRefSizeName
, uno::Any( aRefSize
));
151 xProp
->setPropertyValue( aRefSizeName
, uno::Any());
154 if( bAdaptFontSizes
)
155 RelativeSizeHelper::adaptFontSizes( xProp
, aOldRefSize
, aRefSize
);
159 catch (const uno::Exception
&)
161 DBG_UNHANDLED_EXCEPTION("chart2");
165 void ReferenceSizeProvider::getAutoResizeFromPropSet(
166 const Reference
< beans::XPropertySet
> & xProp
,
167 ReferenceSizeProvider::AutoResizeState
& rInOutState
)
169 AutoResizeState eSingleState
= AUTO_RESIZE_UNKNOWN
;
175 if( xProp
->getPropertyValue( "ReferencePageSize" ).hasValue())
176 eSingleState
= AUTO_RESIZE_YES
;
178 eSingleState
= AUTO_RESIZE_NO
;
180 catch (const uno::Exception
&)
182 // unknown property -> state stays unknown
186 // current state unknown => nothing changes. Otherwise if current state
187 // differs from state so far, we have an ambiguity
188 if( rInOutState
== AUTO_RESIZE_UNKNOWN
)
190 rInOutState
= eSingleState
;
192 else if( eSingleState
!= AUTO_RESIZE_UNKNOWN
&&
193 eSingleState
!= rInOutState
)
195 rInOutState
= AUTO_RESIZE_AMBIGUOUS
;
199 void ReferenceSizeProvider::impl_getAutoResizeFromTitled(
200 const Reference
< XTitled
> & xTitled
,
201 ReferenceSizeProvider::AutoResizeState
& rInOutState
)
205 Reference
< beans::XPropertySet
> xProp( xTitled
->getTitleObject(), uno::UNO_QUERY
);
207 getAutoResizeFromPropSet( xProp
, rInOutState
);
211 /** Retrieves the state auto-resize from all objects that support this
212 feature. If all objects return the same state, AUTO_RESIZE_YES or
213 AUTO_RESIZE_NO is returned.
215 If no object supporting the feature is found, AUTO_RESIZE_UNKNOWN is
216 returned. If there are multiple objects, some with state YES and some
217 with state NO, AUTO_RESIZE_AMBIGUOUS is returned.
219 ReferenceSizeProvider::AutoResizeState
ReferenceSizeProvider::getAutoResizeState(
220 const Reference
< XChartDocument
> & xChartDoc
)
222 AutoResizeState eResult
= AUTO_RESIZE_UNKNOWN
;
225 Reference
< XTitled
> xDocTitled( xChartDoc
, uno::UNO_QUERY
);
227 impl_getAutoResizeFromTitled( xDocTitled
, eResult
);
228 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
231 // diagram is needed by the rest of the objects
232 Reference
< XDiagram
> xDiagram
= ChartModelHelper::findDiagram( xChartDoc
);
237 Reference
< XTitled
> xDiaTitled( xDiagram
, uno::UNO_QUERY
);
239 impl_getAutoResizeFromTitled( xDiaTitled
, eResult
);
240 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
244 Reference
< beans::XPropertySet
> xLegendProp( xDiagram
->getLegend(), uno::UNO_QUERY
);
245 if( xLegendProp
.is())
246 getAutoResizeFromPropSet( xLegendProp
, eResult
);
247 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
250 // Axes (incl. Axis Titles)
251 Sequence
< Reference
< XAxis
> > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram
) );
252 for( sal_Int32 i
=0; i
<aAxes
.getLength(); ++i
)
254 Reference
< beans::XPropertySet
> xProp( aAxes
[i
], uno::UNO_QUERY
);
256 getAutoResizeFromPropSet( xProp
, eResult
);
257 Reference
< XTitled
> xTitled( aAxes
[i
], uno::UNO_QUERY
);
260 impl_getAutoResizeFromTitled( xTitled
, eResult
);
261 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
267 std::vector
< Reference
< XDataSeries
> > aSeries(
268 DiagramHelper::getDataSeriesFromDiagram( xDiagram
));
270 for (auto const& elem
: aSeries
)
272 Reference
< beans::XPropertySet
> xSeriesProp(elem
, uno::UNO_QUERY
);
273 if( xSeriesProp
.is())
275 getAutoResizeFromPropSet( xSeriesProp
, eResult
);
276 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
280 Sequence
< sal_Int32
> aPointIndexes
;
283 if( xSeriesProp
->getPropertyValue( "AttributedDataPoints") >>= aPointIndexes
)
285 for( sal_Int32 i
=0; i
< aPointIndexes
.getLength(); ++i
)
287 getAutoResizeFromPropSet(
288 elem
->getDataPointByIndex( aPointIndexes
[i
] ), eResult
);
289 if( eResult
== AUTO_RESIZE_AMBIGUOUS
)
294 catch (const uno::Exception
&)
296 DBG_UNHANDLED_EXCEPTION("chart2");
304 void ReferenceSizeProvider::toggleAutoResizeState()
306 setAutoResizeState( m_bUseAutoScale
? AUTO_RESIZE_NO
: AUTO_RESIZE_YES
);
309 /** sets the auto-resize at all objects that support this feature for text.
310 eNewState must be either AUTO_RESIZE_YES or AUTO_RESIZE_NO
312 void ReferenceSizeProvider::setAutoResizeState( ReferenceSizeProvider::AutoResizeState eNewState
)
314 m_bUseAutoScale
= (eNewState
== AUTO_RESIZE_YES
);
317 impl_setValuesAtTitled( Reference
< XTitled
>( m_xChartDoc
, uno::UNO_QUERY
));
319 // diagram is needed by the rest of the objects
320 Reference
< XDiagram
> xDiagram
= ChartModelHelper::findDiagram( m_xChartDoc
);
325 impl_setValuesAtTitled( Reference
< XTitled
>( xDiagram
, uno::UNO_QUERY
));
328 Reference
< beans::XPropertySet
> xLegendProp( xDiagram
->getLegend(), uno::UNO_QUERY
);
329 if( xLegendProp
.is())
330 setValuesAtPropertySet( xLegendProp
);
332 // Axes (incl. Axis Titles)
333 Sequence
< Reference
< XAxis
> > aAxes( AxisHelper::getAllAxesOfDiagram( xDiagram
) );
334 for( sal_Int32 i
=0; i
<aAxes
.getLength(); ++i
)
336 Reference
< beans::XPropertySet
> xProp( aAxes
[i
], uno::UNO_QUERY
);
338 setValuesAtPropertySet( xProp
);
339 impl_setValuesAtTitled( Reference
< XTitled
>( aAxes
[i
], uno::UNO_QUERY
));
343 setValuesAtAllDataSeries();
345 // recalculate new state (in case it stays unknown or is ambiguous
346 m_bUseAutoScale
= (getAutoResizeState( m_xChartDoc
) == AUTO_RESIZE_YES
);
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */