LibreOffice Module chart2 (master) 1
ReferenceSizeProvider.cxx
Go to the documentation of this file.
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
22#include <ChartModelHelper.hxx>
23#include <ChartModel.hxx>
24#include <DataSeries.hxx>
26#include <DiagramHelper.hxx>
27#include <Diagram.hxx>
28#include <Axis.hxx>
29#include <AxisHelper.hxx>
30#include <Legend.hxx>
32
33#include <vector>
34
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::chart2;
37using namespace ::chart::DataSeriesProperties;
38
39using ::com::sun::star::uno::Reference;
40using ::com::sun::star::uno::Sequence;
41
42namespace chart
43{
44
46 awt::Size aPageSize,
47 const rtl::Reference<::chart::ChartModel> & xChartDoc ) :
48 m_aPageSize( aPageSize ),
49 m_xChartDoc( xChartDoc ),
50 m_bUseAutoScale( getAutoResizeState( xChartDoc ) == AUTO_RESIZE_YES )
51{}
52
54 const Reference< XTitled > & xTitled )
55{
56 if( xTitled.is())
57 {
58 Reference< XTitle > xTitle( xTitled->getTitleObject());
59 if( xTitle.is())
60 setValuesAtTitle( xTitle );
61 }
62}
63
65 const Reference< XTitle > & xTitle )
66{
67 try
68 {
69 Reference< beans::XPropertySet > xTitleProp( xTitle, uno::UNO_QUERY_THROW );
70 awt::Size aOldRefSize;
71 bool bHasOldRefSize(
72 xTitleProp->getPropertyValue( "ReferencePageSize") >>= aOldRefSize );
73
74 // set from auto-resize on to off -> adapt font sizes at XFormattedStrings
75 if( bHasOldRefSize && ! useAutoScale())
76 {
78 xTitle->getText());
79 for( uno::Reference< XFormattedString > const & formattedStr : aStrSeq )
80 {
82 Reference< beans::XPropertySet >( formattedStr, uno::UNO_QUERY ),
83 aOldRefSize, getPageSize());
84 }
85 }
86
87 setValuesAtPropertySet( xTitleProp, /* bAdaptFontSizes = */ false );
88 }
89 catch (const uno::Exception&)
90 {
92 }
93}
94
96{
97 rtl::Reference< Diagram > xDiagram( m_xChartDoc->getFirstChartDiagram());
98 if (!xDiagram)
99 return;
100
101 // DataSeries/Points
102 std::vector< rtl::Reference< DataSeries > > aSeries =
103 xDiagram->getDataSeries();
104
105 for (auto const& elem : aSeries)
106 {
107 // data points
108 Sequence< sal_Int32 > aPointIndexes;
109 try
110 {
111 // "AttributedDataPoints"
112 if( elem->getFastPropertyValue( PROP_DATASERIES_ATTRIBUTED_DATA_POINTS) >>= aPointIndexes )
113 {
114 for( sal_Int32 idx : std::as_const(aPointIndexes) )
116 elem->getDataPointByIndex( idx ) );
117 }
118 }
119 catch (const uno::Exception&)
120 {
121 DBG_UNHANDLED_EXCEPTION("chart2");
122 }
123
124 //it is important to correct the datapoint properties first as they do reference the series properties
126 }
127}
128
131 bool bAdaptFontSizes /* = true */ )
132{
133 if( ! xProp.is())
134 return;
135
136 static constexpr OUStringLiteral aRefSizeName = u"ReferencePageSize";
137
138 try
139 {
140 awt::Size aRefSize( getPageSize() );
141 awt::Size aOldRefSize;
142 bool bHasOldRefSize( xProp->getPropertyValue( aRefSizeName ) >>= aOldRefSize );
143
144 if( useAutoScale())
145 {
146 if( ! bHasOldRefSize )
147 xProp->setPropertyValue( aRefSizeName, uno::Any( aRefSize ));
148 }
149 else
150 {
151 if( bHasOldRefSize )
152 {
153 xProp->setPropertyValue( aRefSizeName, uno::Any());
154
155 // adapt font sizes
156 if( bAdaptFontSizes )
157 RelativeSizeHelper::adaptFontSizes( xProp, aOldRefSize, aRefSize );
158 }
159 }
160 }
161 catch (const uno::Exception&)
162 {
163 DBG_UNHANDLED_EXCEPTION("chart2");
164 }
165}
166
170{
172
173 if( xProp.is())
174 {
175 try
176 {
177 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue())
178 eSingleState = AUTO_RESIZE_YES;
179 else
180 eSingleState = AUTO_RESIZE_NO;
181 }
182 catch (const uno::Exception&)
183 {
184 // unknown property -> state stays unknown
185 }
186 }
187
188 // current state unknown => nothing changes. Otherwise if current state
189 // differs from state so far, we have an ambiguity
190 if( rInOutState == AUTO_RESIZE_UNKNOWN )
191 {
192 rInOutState = eSingleState;
193 }
194 else if( eSingleState != AUTO_RESIZE_UNKNOWN &&
195 eSingleState != rInOutState )
196 {
197 rInOutState = AUTO_RESIZE_AMBIGUOUS;
198 }
199}
200
202 const Reference< XTitled > & xTitled,
204{
205 if( xTitled.is())
206 {
207 Reference< beans::XPropertySet > xProp( xTitled->getTitleObject(), uno::UNO_QUERY );
208 if( xProp.is())
209 getAutoResizeFromPropSet( xProp, rInOutState );
210 }
211}
212
222 const rtl::Reference<::chart::ChartModel> & xChartDoc )
223{
225
226 // Main Title
227 if( xChartDoc.is())
228 impl_getAutoResizeFromTitled( xChartDoc, eResult );
229 if( eResult == AUTO_RESIZE_AMBIGUOUS )
230 return eResult;
231
232 // diagram is needed by the rest of the objects
233 rtl::Reference< Diagram > xDiagram = xChartDoc->getFirstChartDiagram();
234 if( ! xDiagram.is())
235 return eResult;
236
237 // Sub Title
238 if( xDiagram.is())
239 impl_getAutoResizeFromTitled( xDiagram, eResult );
240 if( eResult == AUTO_RESIZE_AMBIGUOUS )
241 return eResult;
242
243 // Legend
244 rtl::Reference< Legend > xLegend( xDiagram->getLegend2() );
245 if( xLegend.is())
246 getAutoResizeFromPropSet( xLegend, eResult );
247 if( eResult == AUTO_RESIZE_AMBIGUOUS )
248 return eResult;
249
250 // Axes (incl. Axis Titles)
251 const std::vector< rtl::Reference< Axis > > aAxes = AxisHelper::getAllAxesOfDiagram( xDiagram );
252 for( rtl::Reference< Axis > const & axis : aAxes )
253 {
254 getAutoResizeFromPropSet( axis, eResult );
255 impl_getAutoResizeFromTitled( axis, eResult );
256 if( eResult == AUTO_RESIZE_AMBIGUOUS )
257 return eResult;
258 }
259
260 // DataSeries/Points
261 std::vector< rtl::Reference< DataSeries > > aSeries =
262 xDiagram->getDataSeries();
263
264 for (auto const& elem : aSeries)
265 {
266 getAutoResizeFromPropSet( elem, eResult );
267 if( eResult == AUTO_RESIZE_AMBIGUOUS )
268 return eResult;
269
270 // data points
271 Sequence< sal_Int32 > aPointIndexes;
272 try
273 {
274 // "AttributedDataPoints"
275 if( elem->getFastPropertyValue( PROP_DATASERIES_ATTRIBUTED_DATA_POINTS) >>= aPointIndexes )
276 {
277 for( sal_Int32 idx : std::as_const(aPointIndexes) )
278 {
280 elem->getDataPointByIndex( idx ), eResult );
281 if( eResult == AUTO_RESIZE_AMBIGUOUS )
282 return eResult;
283 }
284 }
285 }
286 catch (const uno::Exception&)
287 {
288 DBG_UNHANDLED_EXCEPTION("chart2");
289 }
290 }
291
292 return eResult;
293}
294
296{
298}
299
304{
305 m_bUseAutoScale = (eNewState == AUTO_RESIZE_YES);
306
307 // Main Title
309
310 // diagram is needed by the rest of the objects
311 rtl::Reference< Diagram > xDiagram = m_xChartDoc->getFirstChartDiagram();
312 if( ! xDiagram.is())
313 return;
314
315 // Sub Title
316 impl_setValuesAtTitled( xDiagram );
317
318 // Legend
319 rtl::Reference< Legend > xLegend( xDiagram->getLegend2() );
320 if( xLegend.is())
321 setValuesAtPropertySet( xLegend );
322
323 // Axes (incl. Axis Titles)
324 const std::vector< rtl::Reference< Axis > > aAxes = AxisHelper::getAllAxesOfDiagram( xDiagram );
325 for( rtl::Reference< Axis > const & axis : aAxes )
326 {
329 }
330
331 // DataSeries/Points
333
334 // recalculate new state (in case it stays unknown or is ambiguous
336}
337
338} // namespace chart
339
340/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rtl::Reference<::chart::ChartModel > m_xChartDoc
static std::vector< rtl::Reference< ::chart::Axis > > getAllAxesOfDiagram(const rtl::Reference< ::chart::Diagram > &xDiagram, bool bOnlyVisible=false)
Definition: AxisHelper.cxx:809
SAL_DLLPRIVATE void setValuesAtPropertySet(const css::uno::Reference< css::beans::XPropertySet > &xProp, bool bAdaptFontSizes=true)
Sets the ReferencePageSize according to the internal settings of this class at the XPropertySet,...
SAL_DLLPRIVATE void setValuesAtTitle(const css::uno::Reference< css::chart2::XTitle > &xTitle)
Sets the ReferencePageSize according to the internal settings of this class at the XTitle,...
static SAL_DLLPRIVATE void getAutoResizeFromPropSet(const css::uno::Reference< css::beans::XPropertySet > &xProp, AutoResizeState &rInOutState)
Retrieves the auto-resize state from the given propertyset.
const css::awt::Size & getPageSize() const
rtl::Reference<::chart::ChartModel > m_xChartDoc
SAL_DLLPRIVATE bool useAutoScale() const
void toggleAutoResizeState()
sets or resets the auto-resize at all objects that support this feature for text to the opposite of t...
ReferenceSizeProvider(css::awt::Size aPageSize, const rtl::Reference<::chart::ChartModel > &xChartDoc)
SAL_DLLPRIVATE void setAutoResizeState(AutoResizeState eNewState)
sets the auto-resize at all objects that support this feature for text.
static AutoResizeState getAutoResizeState(const rtl::Reference<::chart::ChartModel > &xChartDoc)
Retrieves the state auto-resize from all objects that support this feature.
void setValuesAtAllDataSeries()
Sets the internal value at all data series in the currently set model.
static SAL_DLLPRIVATE void impl_getAutoResizeFromTitled(const css::uno::Reference< css::chart2::XTitled > &xTitled, AutoResizeState &rInOutState)
SAL_DLLPRIVATE void impl_setValuesAtTitled(const css::uno::Reference< css::chart2::XTitled > &xTitled)
static void adaptFontSizes(const css::uno::Reference< css::beans::XPropertySet > &xTargetProperties, const css::awt::Size &rOldReferenceSize, const css::awt::Size &rNewReferenceSize)
#define DBG_UNHANDLED_EXCEPTION(...)
float u
const sal_uInt16 idx[]