Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / dialogs / res_Trendline.cxx
blob0d6805af6155e068c316bbc4b07a17f4d543257a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <editeng/sizeitem.hxx>
25 #include <svl/intitem.hxx>
26 #include <svl/stritem.hxx>
27 #include <sfx2/tabdlg.hxx>
29 #include <vector>
30 #include <algorithm>
32 namespace chart
35 void lcl_setValue( FormattedField& rFmtField, double fValue )
37 rFmtField.SetValue( fValue );
38 rFmtField.SetDefaultValue( fValue );
41 TrendlineResources::TrendlineResources( vcl::Window * pParent, const SfxItemSet& rInAttrs ) :
42 m_eTrendLineType( SvxChartRegress::Linear ),
43 m_bTrendLineUnique( true ),
44 m_pNumFormatter( nullptr ),
45 m_nNbPoints( 0 )
47 SfxTabPage* pTabPage = reinterpret_cast<SfxTabPage*>(pParent);
48 pTabPage->get(m_pRB_Linear,"linear");
49 pTabPage->get(m_pRB_Logarithmic,"logarithmic");
50 pTabPage->get(m_pRB_Exponential,"exponential");
51 pTabPage->get(m_pRB_Power,"power");
52 pTabPage->get(m_pRB_Polynomial,"polynomial");
53 pTabPage->get(m_pRB_MovingAverage,"movingAverage");
54 pTabPage->get(m_pNF_Degree,"degree");
55 pTabPage->get(m_pNF_Period,"period");
56 pTabPage->get(m_pEE_Name,"entry_name");
57 pTabPage->get(m_pFmtFld_ExtrapolateForward,"extrapolateForward");
58 pTabPage->get(m_pFmtFld_ExtrapolateBackward,"extrapolateBackward");
59 pTabPage->get(m_pCB_SetIntercept,"setIntercept");
60 pTabPage->get(m_pFmtFld_InterceptValue,"interceptValue");
61 pTabPage->get(m_pCB_ShowEquation,"showEquation");
62 pTabPage->get(m_pEE_XName,"entry_Xname");
63 pTabPage->get(m_pEE_YName,"entry_Yname");
64 pTabPage->get(m_pCB_ShowCorrelationCoeff,"showCorrelationCoefficient");
65 pTabPage->get(m_pFI_Linear,"imageLinear");
66 pTabPage->get(m_pFI_Logarithmic,"imageLogarithmic");
67 pTabPage->get(m_pFI_Exponential,"imageExponential");
68 pTabPage->get(m_pFI_Power,"imagePower");
69 pTabPage->get(m_pFI_Polynomial,"imagePolynomial");
70 pTabPage->get(m_pFI_MovingAverage,"imageMovingAverage");
71 FillValueSets();
73 Link<Button*,void> aLink = LINK(this, TrendlineResources, SelectTrendLine );
74 m_pRB_Linear->SetClickHdl( aLink );
75 m_pRB_Logarithmic->SetClickHdl( aLink );
76 m_pRB_Exponential->SetClickHdl( aLink );
77 m_pRB_Power->SetClickHdl( aLink );
78 m_pRB_Polynomial->SetClickHdl( aLink );
79 m_pRB_MovingAverage->SetClickHdl( aLink );
81 Link<Edit&,void> aLink2 = LINK(this, TrendlineResources, ChangeValue );
82 m_pNF_Degree->SetModifyHdl( aLink2 );
83 m_pNF_Period->SetModifyHdl( aLink2 );
84 m_pFmtFld_InterceptValue->SetModifyHdl( aLink2 );
86 m_pCB_ShowEquation->SetToggleHdl( LINK(this, TrendlineResources, ShowEquation ) );
88 Reset( rInAttrs );
89 UpdateControlStates();
92 TrendlineResources::~TrendlineResources()
95 IMPL_LINK( TrendlineResources, SelectTrendLine, Button *, pRadioButton, void )
97 if( pRadioButton == m_pRB_Linear )
98 m_eTrendLineType = SvxChartRegress::Linear;
99 else if( pRadioButton == m_pRB_Logarithmic )
100 m_eTrendLineType = SvxChartRegress::Log;
101 else if( pRadioButton == m_pRB_Exponential )
102 m_eTrendLineType = SvxChartRegress::Exp;
103 else if( pRadioButton == m_pRB_Power )
104 m_eTrendLineType = SvxChartRegress::Power;
105 else if( pRadioButton == m_pRB_Polynomial )
106 m_eTrendLineType = SvxChartRegress::Polynomial;
107 else if( pRadioButton == m_pRB_MovingAverage )
108 m_eTrendLineType = SvxChartRegress::MovingAverage;
109 m_bTrendLineUnique = true;
111 UpdateControlStates();
114 void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
116 const SfxPoolItem *pPoolItem = nullptr;
118 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_CURVE_NAME, true, &pPoolItem ) == SfxItemState::SET )
120 OUString aName = static_cast< const SfxStringItem* >(pPoolItem)->GetValue();
121 m_pEE_Name->SetText(aName);
123 else
125 m_pEE_Name->SetText("");
127 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_XNAME, true, &pPoolItem ) == SfxItemState::SET )
129 OUString aName = static_cast< const SfxStringItem* >(pPoolItem)->GetValue();
130 m_pEE_XName->SetText(aName);
132 else
134 m_pEE_XName->SetText("x");
136 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_YNAME, true, &pPoolItem ) == SfxItemState::SET )
138 OUString aName = static_cast< const SfxStringItem* >(pPoolItem)->GetValue();
139 m_pEE_YName->SetText(aName);
141 else
143 m_pEE_YName->SetText("f(x)");
146 SfxItemState aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, true, &pPoolItem );
147 m_bTrendLineUnique = ( aState != SfxItemState::DONTCARE );
148 if( aState == SfxItemState::SET )
150 const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
151 if( pItem )
153 m_eTrendLineType = pItem->GetValue();
157 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_DEGREE, true, &pPoolItem ) == SfxItemState::SET )
159 sal_Int32 nDegree = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
160 m_pNF_Degree->SetValue( nDegree );
162 else
164 m_pNF_Degree->SetValue( 2 );
167 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_PERIOD, true, &pPoolItem ) == SfxItemState::SET )
169 sal_Int32 nPeriod = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
170 m_pNF_Period->SetValue( nPeriod );
172 else
174 m_pNF_Period->SetValue( 2 );
177 double nValue = 0.0;
178 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD, true, &pPoolItem ) == SfxItemState::SET )
180 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
182 lcl_setValue( *m_pFmtFld_ExtrapolateForward, nValue );
184 nValue = 0.0;
185 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD, true, &pPoolItem ) == SfxItemState::SET )
187 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
189 lcl_setValue( *m_pFmtFld_ExtrapolateBackward, nValue );
191 nValue = 0.0;
192 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_INTERCEPT_VALUE, true, &pPoolItem ) == SfxItemState::SET )
194 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
196 lcl_setValue( *m_pFmtFld_InterceptValue, nValue );
198 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SET_INTERCEPT, true, &pPoolItem );
199 if( aState == SfxItemState::DONTCARE )
201 m_pCB_SetIntercept->EnableTriState();
202 m_pCB_SetIntercept->SetState( TRISTATE_INDET );
204 else
206 m_pCB_SetIntercept->EnableTriState( false );
207 if( aState == SfxItemState::SET )
208 m_pCB_SetIntercept->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
211 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, true, &pPoolItem );
212 if( aState == SfxItemState::DONTCARE )
214 m_pCB_ShowEquation->EnableTriState();
215 m_pCB_ShowEquation->SetState( TRISTATE_INDET );
217 else
219 m_pCB_ShowEquation->EnableTriState( false );
220 if( aState == SfxItemState::SET )
221 m_pCB_ShowEquation->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
224 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, true, &pPoolItem );
225 if( aState == SfxItemState::DONTCARE )
227 m_pCB_ShowCorrelationCoeff->EnableTriState();
228 m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_INDET );
230 else
232 m_pCB_ShowCorrelationCoeff->EnableTriState( false );
233 if( aState == SfxItemState::SET )
234 m_pCB_ShowCorrelationCoeff->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
237 if( m_bTrendLineUnique )
239 switch( m_eTrendLineType )
241 case SvxChartRegress::Linear :
242 m_pRB_Linear->Check();
243 break;
244 case SvxChartRegress::Log :
245 m_pRB_Logarithmic->Check();
246 break;
247 case SvxChartRegress::Exp :
248 m_pRB_Exponential->Check();
249 break;
250 case SvxChartRegress::Power :
251 m_pRB_Power->Check();
252 break;
253 case SvxChartRegress::Polynomial :
254 m_pRB_Polynomial->Check();
255 break;
256 case SvxChartRegress::MovingAverage :
257 m_pRB_MovingAverage->Check();
258 break;
259 default:
260 break;
265 bool TrendlineResources::FillItemSet(SfxItemSet* rOutAttrs) const
267 if( m_bTrendLineUnique )
268 rOutAttrs->Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
270 if( m_pCB_ShowEquation->GetState() != TRISTATE_INDET )
271 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_pCB_ShowEquation->IsChecked() ));
273 if( m_pCB_ShowCorrelationCoeff->GetState() != TRISTATE_INDET )
274 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_pCB_ShowCorrelationCoeff->IsChecked() ));
276 OUString aName = m_pEE_Name->GetText();
277 rOutAttrs->Put(SfxStringItem(SCHATTR_REGRESSION_CURVE_NAME, aName));
278 aName = m_pEE_XName->GetText();
279 if ( aName.isEmpty() )
280 aName = "x";
281 rOutAttrs->Put(SfxStringItem(SCHATTR_REGRESSION_XNAME, aName));
282 aName = m_pEE_YName->GetText();
283 if ( aName.isEmpty() )
284 aName = "f(x)";
285 rOutAttrs->Put(SfxStringItem(SCHATTR_REGRESSION_YNAME, aName));
287 sal_Int32 aDegree = m_pNF_Degree->GetValue();
288 rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_DEGREE, aDegree ) );
290 sal_Int32 aPeriod = m_pNF_Period->GetValue();
291 rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_PERIOD, aPeriod ) );
293 sal_uInt32 nIndex = 0;
294 double aValue = 0.0;
295 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateForward->GetText(),nIndex,aValue);
296 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD ) );
298 aValue = 0.0;
299 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateBackward->GetText(),nIndex,aValue);
300 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD ) );
302 if( m_pCB_SetIntercept->GetState() != TRISTATE_INDET )
303 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SET_INTERCEPT, m_pCB_SetIntercept->IsChecked() ));
305 aValue = 0.0;
306 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_InterceptValue->GetText(),nIndex,aValue);
307 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_INTERCEPT_VALUE ) );
309 return true;
312 void TrendlineResources::FillValueSets()
314 m_pFI_Linear->SetImage(Image(BitmapEx(BMP_REGRESSION_LINEAR)));
315 m_pFI_Logarithmic->SetImage(Image(BitmapEx(BMP_REGRESSION_LOG)));
316 m_pFI_Exponential->SetImage(Image(BitmapEx(BMP_REGRESSION_EXP)));
317 m_pFI_Power->SetImage(Image(BitmapEx(BMP_REGRESSION_POWER)));
318 m_pFI_Polynomial->SetImage(Image(BitmapEx(BMP_REGRESSION_POLYNOMIAL)));
319 m_pFI_MovingAverage->SetImage(Image(BitmapEx(BMP_REGRESSION_MOVING_AVERAGE)));
322 void TrendlineResources::UpdateControlStates()
324 if( m_nNbPoints > 0 )
326 sal_Int32 nMaxValue = m_nNbPoints - 1 + ( m_pCB_SetIntercept->IsChecked()?1:0 );
327 // if( nMaxValue > 10) nMaxValue = 10;
328 m_pNF_Degree->SetMax( nMaxValue );
329 m_pNF_Period->SetMax( m_nNbPoints - 1 );
331 bool bMovingAverage = ( m_eTrendLineType == SvxChartRegress::MovingAverage );
332 bool bInterceptAvailable = ( m_eTrendLineType == SvxChartRegress::Linear )
333 || ( m_eTrendLineType == SvxChartRegress::Polynomial )
334 || ( m_eTrendLineType == SvxChartRegress::Exp );
335 m_pFmtFld_ExtrapolateForward->Enable( !bMovingAverage );
336 m_pFmtFld_ExtrapolateBackward->Enable( !bMovingAverage );
337 m_pCB_SetIntercept->Enable( bInterceptAvailable );
338 m_pFmtFld_InterceptValue->Enable( bInterceptAvailable );
339 if( bMovingAverage )
341 m_pCB_ShowEquation->SetState( TRISTATE_FALSE );
342 m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_FALSE );
344 m_pCB_ShowEquation->Enable( !bMovingAverage );
345 m_pCB_ShowCorrelationCoeff->Enable( !bMovingAverage );
346 m_pEE_XName->Enable( !bMovingAverage && m_pCB_ShowEquation->IsChecked() );
347 m_pEE_YName->Enable( !bMovingAverage && m_pCB_ShowEquation->IsChecked() );
350 IMPL_LINK( TrendlineResources, ChangeValue, Edit&, rNumericField, void)
352 if( &rNumericField == m_pNF_Degree )
354 if( !m_pRB_Polynomial->IsChecked() )
356 m_pRB_Polynomial->Check();
357 SelectTrendLine(m_pRB_Polynomial);
360 else if( &rNumericField == m_pNF_Period )
362 if( !m_pRB_MovingAverage->IsChecked() )
364 m_pRB_MovingAverage->Check();
365 SelectTrendLine(m_pRB_MovingAverage);
368 else if( &rNumericField == m_pFmtFld_InterceptValue )
370 if( !m_pCB_SetIntercept->IsChecked() )
371 m_pCB_SetIntercept->Check();
373 UpdateControlStates();
376 void TrendlineResources::SetNumFormatter( SvNumberFormatter* pFormatter )
378 m_pNumFormatter = pFormatter;
379 m_pFmtFld_ExtrapolateForward->SetFormatter( m_pNumFormatter );
380 m_pFmtFld_ExtrapolateBackward->SetFormatter( m_pNumFormatter );
381 m_pFmtFld_InterceptValue->SetFormatter( m_pNumFormatter );
384 void TrendlineResources::SetNbPoints( sal_Int32 nNbPoints )
386 m_nNbPoints = nNbPoints;
387 UpdateControlStates();
390 IMPL_LINK( TrendlineResources, ShowEquation, CheckBox&, rCheckBox, void)
392 if( &rCheckBox == m_pCB_ShowEquation )
394 m_pEE_XName->Enable( m_pCB_ShowEquation->IsChecked() );
395 m_pEE_YName->Enable( m_pCB_ShowEquation->IsChecked() );
397 UpdateControlStates();
400 } // namespace chart
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */