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 "res_Trendline.hxx"
21 #include <bitmaps.hlst>
22 #include <chartview/ChartSfxItemIds.hxx>
24 #include <svl/intitem.hxx>
25 #include <svl/stritem.hxx>
26 #include <svl/zforlist.hxx>
27 #include <vcl/weld.hxx>
32 static void lcl_setValue(weld::FormattedSpinButton
& rFmtField
, double fValue
)
34 rFmtField
.set_value(fValue
);
35 //TODO rFmtField.SetDefaultValue( fValue );
38 TrendlineResources::TrendlineResources(weld::Builder
& rBuilder
, const SfxItemSet
& rInAttrs
)
39 : m_eTrendLineType(SvxChartRegress::Linear
)
40 , m_bTrendLineUnique(true)
41 , m_pNumFormatter(nullptr)
43 , m_xRB_Linear(rBuilder
.weld_radio_button("linear"))
44 , m_xRB_Logarithmic(rBuilder
.weld_radio_button("logarithmic"))
45 , m_xRB_Exponential(rBuilder
.weld_radio_button("exponential"))
46 , m_xRB_Power(rBuilder
.weld_radio_button("power"))
47 , m_xRB_Polynomial(rBuilder
.weld_radio_button("polynomial"))
48 , m_xRB_MovingAverage(rBuilder
.weld_radio_button("movingAverage"))
49 , m_xFI_Linear(rBuilder
.weld_image("imageLinear"))
50 , m_xFI_Logarithmic(rBuilder
.weld_image("imageLogarithmic"))
51 , m_xFI_Exponential(rBuilder
.weld_image("imageExponential"))
52 , m_xFI_Power(rBuilder
.weld_image("imagePower"))
53 , m_xFI_Polynomial(rBuilder
.weld_image("imagePolynomial"))
54 , m_xFI_MovingAverage(rBuilder
.weld_image("imageMovingAverage"))
55 , m_xNF_Degree(rBuilder
.weld_spin_button("degree"))
56 , m_xNF_Period(rBuilder
.weld_spin_button("period"))
57 , m_xEE_Name(rBuilder
.weld_entry("entry_name"))
58 , m_xFmtFld_ExtrapolateForward(rBuilder
.weld_formatted_spin_button("extrapolateForward"))
59 , m_xFmtFld_ExtrapolateBackward(rBuilder
.weld_formatted_spin_button("extrapolateBackward"))
60 , m_xCB_SetIntercept(rBuilder
.weld_check_button("setIntercept"))
61 , m_xFmtFld_InterceptValue(rBuilder
.weld_formatted_spin_button("interceptValue"))
62 , m_xCB_ShowEquation(rBuilder
.weld_check_button("showEquation"))
63 , m_xEE_XName(rBuilder
.weld_entry("entry_Xname"))
64 , m_xEE_YName(rBuilder
.weld_entry("entry_Yname"))
65 , m_xCB_ShowCorrelationCoeff(rBuilder
.weld_check_button("showCorrelationCoefficient"))
69 Link
<weld::ToggleButton
&,void> aLink
= LINK(this, TrendlineResources
, SelectTrendLine
);
70 m_xRB_Linear
->connect_toggled( aLink
);
71 m_xRB_Logarithmic
->connect_toggled( aLink
);
72 m_xRB_Exponential
->connect_toggled( aLink
);
73 m_xRB_Power
->connect_toggled( aLink
);
74 m_xRB_Polynomial
->connect_toggled( aLink
);
75 m_xRB_MovingAverage
->connect_toggled( aLink
);
77 Link
<weld::SpinButton
&,void> aLink2
= LINK(this, TrendlineResources
, ChangeSpinValue
);
78 m_xNF_Degree
->connect_value_changed(aLink2
);
79 m_xNF_Period
->connect_value_changed(aLink2
);
80 m_xFmtFld_InterceptValue
->connect_value_changed(LINK(this, TrendlineResources
, ChangeFormattedValue
));
82 m_xCB_ShowEquation
->connect_toggled(LINK(this, TrendlineResources
, ShowEquation
));
85 UpdateControlStates();
88 TrendlineResources::~TrendlineResources()
91 IMPL_LINK_NOARG(TrendlineResources
, SelectTrendLine
, weld::ToggleButton
&, void)
93 if (m_xRB_Linear
->get_active())
94 m_eTrendLineType
= SvxChartRegress::Linear
;
95 else if (m_xRB_Logarithmic
->get_active())
96 m_eTrendLineType
= SvxChartRegress::Log
;
97 else if (m_xRB_Exponential
->get_active())
98 m_eTrendLineType
= SvxChartRegress::Exp
;
99 else if (m_xRB_Power
->get_active())
100 m_eTrendLineType
= SvxChartRegress::Power
;
101 else if (m_xRB_Polynomial
->get_active())
102 m_eTrendLineType
= SvxChartRegress::Polynomial
;
103 else if (m_xRB_MovingAverage
->get_active())
104 m_eTrendLineType
= SvxChartRegress::MovingAverage
;
105 m_bTrendLineUnique
= true;
107 UpdateControlStates();
110 void TrendlineResources::Reset( const SfxItemSet
& rInAttrs
)
112 const SfxPoolItem
*pPoolItem
= nullptr;
114 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_CURVE_NAME
, true, &pPoolItem
) == SfxItemState::SET
)
116 OUString aName
= static_cast< const SfxStringItem
* >(pPoolItem
)->GetValue();
117 m_xEE_Name
->set_text(aName
);
121 m_xEE_Name
->set_text("");
123 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_XNAME
, true, &pPoolItem
) == SfxItemState::SET
)
125 OUString aName
= static_cast< const SfxStringItem
* >(pPoolItem
)->GetValue();
126 m_xEE_XName
->set_text(aName
);
130 m_xEE_XName
->set_text("x");
132 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_YNAME
, true, &pPoolItem
) == SfxItemState::SET
)
134 OUString aName
= static_cast< const SfxStringItem
* >(pPoolItem
)->GetValue();
135 m_xEE_YName
->set_text(aName
);
139 m_xEE_YName
->set_text("f(x)");
142 SfxItemState aState
= rInAttrs
.GetItemState( SCHATTR_REGRESSION_TYPE
, true, &pPoolItem
);
143 m_bTrendLineUnique
= ( aState
!= SfxItemState::DONTCARE
);
144 if( aState
== SfxItemState::SET
)
146 const SvxChartRegressItem
* pItem
= dynamic_cast< const SvxChartRegressItem
* >( pPoolItem
);
149 m_eTrendLineType
= pItem
->GetValue();
153 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_DEGREE
, true, &pPoolItem
) == SfxItemState::SET
)
155 sal_Int32 nDegree
= static_cast< const SfxInt32Item
* >( pPoolItem
)->GetValue();
156 m_xNF_Degree
->set_value( nDegree
);
160 m_xNF_Degree
->set_value( 2 );
163 m_xNF_Degree
->save_value();
165 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_PERIOD
, true, &pPoolItem
) == SfxItemState::SET
)
167 sal_Int32 nPeriod
= static_cast< const SfxInt32Item
* >( pPoolItem
)->GetValue();
168 m_xNF_Period
->set_value( nPeriod
);
172 m_xNF_Period
->set_value( 2 );
175 m_xNF_Period
->save_value();
178 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
, true, &pPoolItem
) == SfxItemState::SET
)
180 nValue
= static_cast<const SvxDoubleItem
*>(pPoolItem
)->GetValue() ;
182 lcl_setValue(*m_xFmtFld_ExtrapolateForward
, nValue
);
185 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
, true, &pPoolItem
) == SfxItemState::SET
)
187 nValue
= static_cast<const SvxDoubleItem
*>(pPoolItem
)->GetValue() ;
189 lcl_setValue(*m_xFmtFld_ExtrapolateBackward
, nValue
);
192 if( rInAttrs
.GetItemState( SCHATTR_REGRESSION_INTERCEPT_VALUE
, true, &pPoolItem
) == SfxItemState::SET
)
194 nValue
= static_cast<const SvxDoubleItem
*>(pPoolItem
)->GetValue() ;
196 lcl_setValue(*m_xFmtFld_InterceptValue
, nValue
);
198 aState
= rInAttrs
.GetItemState( SCHATTR_REGRESSION_SET_INTERCEPT
, true, &pPoolItem
);
199 if( aState
== SfxItemState::DONTCARE
)
201 m_xCB_SetIntercept
->set_state(TRISTATE_INDET
);
205 if( aState
== SfxItemState::SET
)
206 m_xCB_SetIntercept
->set_active( static_cast< const SfxBoolItem
* >( pPoolItem
)->GetValue());
209 aState
= rInAttrs
.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION
, true, &pPoolItem
);
210 if( aState
== SfxItemState::DONTCARE
)
212 m_xCB_ShowEquation
->set_state(TRISTATE_INDET
);
216 if( aState
== SfxItemState::SET
)
217 m_xCB_ShowEquation
->set_active( static_cast< const SfxBoolItem
* >( pPoolItem
)->GetValue());
220 aState
= rInAttrs
.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF
, true, &pPoolItem
);
221 if( aState
== SfxItemState::DONTCARE
)
223 m_xCB_ShowCorrelationCoeff
->set_state(TRISTATE_INDET
);
227 if( aState
== SfxItemState::SET
)
228 m_xCB_ShowCorrelationCoeff
->set_active( static_cast< const SfxBoolItem
* >( pPoolItem
)->GetValue());
231 if( m_bTrendLineUnique
)
233 switch( m_eTrendLineType
)
235 case SvxChartRegress::Linear
:
236 m_xRB_Linear
->set_active(true);
238 case SvxChartRegress::Log
:
239 m_xRB_Logarithmic
->set_active(true);
241 case SvxChartRegress::Exp
:
242 m_xRB_Exponential
->set_active(true);
244 case SvxChartRegress::Power
:
245 m_xRB_Power
->set_active(true);
247 case SvxChartRegress::Polynomial
:
248 m_xRB_Polynomial
->set_active(true);
250 case SvxChartRegress::MovingAverage
:
251 m_xRB_MovingAverage
->set_active(true);
259 void TrendlineResources::FillItemSet(SfxItemSet
* rOutAttrs
) const
261 if( m_bTrendLineUnique
)
262 rOutAttrs
->Put( SvxChartRegressItem( m_eTrendLineType
, SCHATTR_REGRESSION_TYPE
));
264 if( m_xCB_ShowEquation
->get_state() != TRISTATE_INDET
)
265 rOutAttrs
->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION
, m_xCB_ShowEquation
->get_active() ));
267 if( m_xCB_ShowCorrelationCoeff
->get_state() != TRISTATE_INDET
)
268 rOutAttrs
->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF
, m_xCB_ShowCorrelationCoeff
->get_active() ));
270 OUString aName
= m_xEE_Name
->get_text();
271 rOutAttrs
->Put(SfxStringItem(SCHATTR_REGRESSION_CURVE_NAME
, aName
));
272 aName
= m_xEE_XName
->get_text();
273 if ( aName
.isEmpty() )
275 rOutAttrs
->Put(SfxStringItem(SCHATTR_REGRESSION_XNAME
, aName
));
276 aName
= m_xEE_YName
->get_text();
277 if ( aName
.isEmpty() )
279 rOutAttrs
->Put(SfxStringItem(SCHATTR_REGRESSION_YNAME
, aName
));
281 sal_Int32 aDegree
= m_xNF_Degree
->get_value();
282 rOutAttrs
->Put(SfxInt32Item( SCHATTR_REGRESSION_DEGREE
, aDegree
) );
284 sal_Int32 aPeriod
= m_xNF_Period
->get_value();
285 rOutAttrs
->Put(SfxInt32Item( SCHATTR_REGRESSION_PERIOD
, aPeriod
) );
287 sal_uInt32 nIndex
= 0;
289 (void)m_pNumFormatter
->IsNumberFormat(m_xFmtFld_ExtrapolateForward
->get_text(),nIndex
,aValue
);
290 rOutAttrs
->Put(SvxDoubleItem( aValue
, SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD
) );
293 (void)m_pNumFormatter
->IsNumberFormat(m_xFmtFld_ExtrapolateBackward
->get_text(),nIndex
,aValue
);
294 rOutAttrs
->Put(SvxDoubleItem( aValue
, SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD
) );
296 if( m_xCB_SetIntercept
->get_state() != TRISTATE_INDET
)
297 rOutAttrs
->Put( SfxBoolItem( SCHATTR_REGRESSION_SET_INTERCEPT
, m_xCB_SetIntercept
->get_active() ));
300 (void)m_pNumFormatter
->IsNumberFormat(m_xFmtFld_InterceptValue
->get_text(),nIndex
,aValue
);
301 rOutAttrs
->Put(SvxDoubleItem( aValue
, SCHATTR_REGRESSION_INTERCEPT_VALUE
) );
304 void TrendlineResources::FillValueSets()
306 m_xFI_Linear
->set_from_icon_name(BMP_REGRESSION_LINEAR
);
307 m_xFI_Logarithmic
->set_from_icon_name(BMP_REGRESSION_LOG
);
308 m_xFI_Exponential
->set_from_icon_name(BMP_REGRESSION_EXP
);
309 m_xFI_Power
->set_from_icon_name(BMP_REGRESSION_POWER
);
310 m_xFI_Polynomial
->set_from_icon_name(BMP_REGRESSION_POLYNOMIAL
);
311 m_xFI_MovingAverage
->set_from_icon_name(BMP_REGRESSION_MOVING_AVERAGE
);
314 void TrendlineResources::UpdateControlStates()
316 if( m_nNbPoints
> 0 )
318 sal_Int32 nMaxValue
= m_nNbPoints
- 1 + (m_xCB_SetIntercept
->get_active() ? 1 : 0);
319 m_xNF_Degree
->set_max(nMaxValue
);
320 m_xNF_Period
->set_max(m_nNbPoints
- 1);
322 bool bMovingAverage
= ( m_eTrendLineType
== SvxChartRegress::MovingAverage
);
323 bool bInterceptAvailable
= ( m_eTrendLineType
== SvxChartRegress::Linear
)
324 || ( m_eTrendLineType
== SvxChartRegress::Polynomial
)
325 || ( m_eTrendLineType
== SvxChartRegress::Exp
);
326 m_xFmtFld_ExtrapolateForward
->set_sensitive( !bMovingAverage
);
327 m_xFmtFld_ExtrapolateBackward
->set_sensitive( !bMovingAverage
);
328 m_xCB_SetIntercept
->set_sensitive( bInterceptAvailable
);
329 m_xFmtFld_InterceptValue
->set_sensitive( bInterceptAvailable
);
332 m_xCB_ShowEquation
->set_state(TRISTATE_FALSE
);
333 m_xCB_ShowCorrelationCoeff
->set_state(TRISTATE_FALSE
);
335 m_xCB_ShowEquation
->set_sensitive( !bMovingAverage
);
336 m_xCB_ShowCorrelationCoeff
->set_sensitive( !bMovingAverage
);
337 m_xEE_XName
->set_sensitive( !bMovingAverage
&& m_xCB_ShowEquation
->get_active() );
338 m_xEE_YName
->set_sensitive( !bMovingAverage
&& m_xCB_ShowEquation
->get_active() );
341 IMPL_LINK(TrendlineResources
, ChangeSpinValue
, weld::SpinButton
&, rNumericField
, void)
343 if (&rNumericField
== m_xNF_Degree
.get())
345 if (!m_xRB_Polynomial
->get_active() && m_xNF_Degree
->get_value_changed_from_saved())
347 m_xRB_Polynomial
->set_active(true);
348 SelectTrendLine(*m_xRB_Polynomial
);
351 else if (&rNumericField
== m_xNF_Period
.get())
353 if (!m_xRB_MovingAverage
->get_active() && m_xNF_Period
->get_value_changed_from_saved())
355 m_xRB_MovingAverage
->set_active(true);
356 SelectTrendLine(*m_xRB_MovingAverage
);
359 UpdateControlStates();
362 IMPL_LINK_NOARG(TrendlineResources
, ChangeFormattedValue
, weld::FormattedSpinButton
&, void)
364 if (!m_xCB_SetIntercept
->get_active())
365 m_xCB_SetIntercept
->set_active(true);
366 UpdateControlStates();
369 void TrendlineResources::SetNumFormatter( SvNumberFormatter
* pFormatter
)
371 m_pNumFormatter
= pFormatter
;
372 m_xFmtFld_ExtrapolateForward
->set_formatter( m_pNumFormatter
);
373 m_xFmtFld_ExtrapolateBackward
->set_formatter( m_pNumFormatter
);
374 m_xFmtFld_InterceptValue
->set_formatter( m_pNumFormatter
);
377 void TrendlineResources::SetNbPoints( sal_Int32 nNbPoints
)
379 m_nNbPoints
= nNbPoints
;
380 UpdateControlStates();
383 IMPL_LINK_NOARG(TrendlineResources
, ShowEquation
, weld::ToggleButton
&, void)
385 m_xEE_XName
->set_sensitive(m_xCB_ShowEquation
->get_active());
386 m_xEE_YName
->set_sensitive(m_xCB_ShowEquation
->get_active());
387 UpdateControlStates();
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */