fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / controller / dialogs / res_Trendline.cxx
blobacc8d0872502e8ba9b4a4ade564f9ef26420db48
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 "ResId.hxx"
22 #include "Strings.hrc"
23 #include "Bitmaps.hrc"
24 #include "chartview/ChartSfxItemIds.hxx"
26 #include <svl/intitem.hxx>
27 #include <svl/stritem.hxx>
28 #include <sfx2/tabdlg.hxx>
30 #include <vector>
31 #include <algorithm>
33 namespace chart
36 void lcl_setValue( FormattedField& rFmtField, double fValue )
38 rFmtField.SetValue( fValue );
39 rFmtField.SetDefaultValue( fValue );
42 TrendlineResources::TrendlineResources( vcl::Window * pParent, const SfxItemSet& rInAttrs ) :
43 m_eTrendLineType( CHREGRESS_LINEAR ),
44 m_bTrendLineUnique( true ),
45 m_pNumFormatter( NULL ),
46 m_nNbPoints( 0 )
48 SfxTabPage* pTabPage = reinterpret_cast<SfxTabPage*>(pParent);
49 pTabPage->get(m_pRB_Linear,"linear");
50 pTabPage->get(m_pRB_Logarithmic,"logarithmic");
51 pTabPage->get(m_pRB_Exponential,"exponential");
52 pTabPage->get(m_pRB_Power,"power");
53 pTabPage->get(m_pRB_Polynomial,"polynomial");
54 pTabPage->get(m_pRB_MovingAverage,"movingAverage");
55 pTabPage->get(m_pNF_Degree,"degree");
56 pTabPage->get(m_pNF_Period,"period");
57 pTabPage->get(m_pEE_Name,"entry_name");
58 pTabPage->get(m_pFmtFld_ExtrapolateForward,"extrapolateForward");
59 pTabPage->get(m_pFmtFld_ExtrapolateBackward,"extrapolateBackward");
60 pTabPage->get(m_pCB_SetIntercept,"setIntercept");
61 pTabPage->get(m_pFmtFld_InterceptValue,"interceptValue");
62 pTabPage->get(m_pCB_ShowEquation,"showEquation");
63 pTabPage->get(m_pCB_ShowCorrelationCoeff,"showCorrelationCoefficient");
64 pTabPage->get(m_pFI_Linear,"imageLinear");
65 pTabPage->get(m_pFI_Logarithmic,"imageLogarithmic");
66 pTabPage->get(m_pFI_Exponential,"imageExponential");
67 pTabPage->get(m_pFI_Power,"imagePower");
68 pTabPage->get(m_pFI_Polynomial,"imagePolynomial");
69 pTabPage->get(m_pFI_MovingAverage,"imageMovingAverage");
70 FillValueSets();
72 Link<> aLink = LINK(this, TrendlineResources, SelectTrendLine );
73 m_pRB_Linear->SetClickHdl( aLink );
74 m_pRB_Logarithmic->SetClickHdl( aLink );
75 m_pRB_Exponential->SetClickHdl( aLink );
76 m_pRB_Power->SetClickHdl( aLink );
77 m_pRB_Polynomial->SetClickHdl( aLink );
78 m_pRB_MovingAverage->SetClickHdl( aLink );
80 aLink = LINK(this, TrendlineResources, ChangeValue );
81 m_pNF_Degree->SetModifyHdl( aLink );
82 m_pNF_Period->SetModifyHdl( aLink );
83 m_pFmtFld_InterceptValue->SetModifyHdl( aLink );
85 Reset( rInAttrs );
86 UpdateControlStates();
89 TrendlineResources::~TrendlineResources()
92 IMPL_LINK( TrendlineResources, SelectTrendLine, RadioButton *, pRadioButton )
94 if( pRadioButton == m_pRB_Linear )
95 m_eTrendLineType = CHREGRESS_LINEAR;
96 else if( pRadioButton == m_pRB_Logarithmic )
97 m_eTrendLineType = CHREGRESS_LOG;
98 else if( pRadioButton == m_pRB_Exponential )
99 m_eTrendLineType = CHREGRESS_EXP;
100 else if( pRadioButton == m_pRB_Power )
101 m_eTrendLineType = CHREGRESS_POWER;
102 else if( pRadioButton == m_pRB_Polynomial )
103 m_eTrendLineType = CHREGRESS_POLYNOMIAL;
104 else if( pRadioButton == m_pRB_MovingAverage )
105 m_eTrendLineType = CHREGRESS_MOVING_AVERAGE;
106 m_bTrendLineUnique = true;
108 UpdateControlStates();
110 return 0;
113 void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
115 const SfxPoolItem *pPoolItem = NULL;
117 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_CURVE_NAME, true, &pPoolItem ) == SfxItemState::SET )
119 OUString aName = static_cast< const SfxStringItem* >(pPoolItem)->GetValue();
120 m_pEE_Name->SetText(aName);
122 else
124 m_pEE_Name->SetText("");
127 SfxItemState aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, true, &pPoolItem );
128 m_bTrendLineUnique = ( aState != SfxItemState::DONTCARE );
129 if( aState == SfxItemState::SET )
131 const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
132 if( pItem )
134 m_eTrendLineType = pItem->GetValue();
138 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_DEGREE, true, &pPoolItem ) == SfxItemState::SET )
140 sal_Int32 nDegree = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
141 m_pNF_Degree->SetValue( nDegree );
143 else
145 m_pNF_Degree->SetValue( 2 );
148 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_PERIOD, true, &pPoolItem ) == SfxItemState::SET )
150 sal_Int32 nPeriod = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
151 m_pNF_Period->SetValue( nPeriod );
153 else
155 m_pNF_Period->SetValue( 2 );
158 double nValue = 0.0;
159 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD, true, &pPoolItem ) == SfxItemState::SET )
161 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
163 lcl_setValue( *m_pFmtFld_ExtrapolateForward, nValue );
165 nValue = 0.0;
166 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD, true, &pPoolItem ) == SfxItemState::SET )
168 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
170 lcl_setValue( *m_pFmtFld_ExtrapolateBackward, nValue );
172 nValue = 0.0;
173 if( rInAttrs.GetItemState( SCHATTR_REGRESSION_INTERCEPT_VALUE, true, &pPoolItem ) == SfxItemState::SET )
175 nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
177 lcl_setValue( *m_pFmtFld_InterceptValue, nValue );
179 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SET_INTERCEPT, true, &pPoolItem );
180 if( aState == SfxItemState::DONTCARE )
182 m_pCB_SetIntercept->EnableTriState( true );
183 m_pCB_SetIntercept->SetState( TRISTATE_INDET );
185 else
187 m_pCB_SetIntercept->EnableTriState( false );
188 if( aState == SfxItemState::SET )
189 m_pCB_SetIntercept->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
192 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, true, &pPoolItem );
193 if( aState == SfxItemState::DONTCARE )
195 m_pCB_ShowEquation->EnableTriState( true );
196 m_pCB_ShowEquation->SetState( TRISTATE_INDET );
198 else
200 m_pCB_ShowEquation->EnableTriState( false );
201 if( aState == SfxItemState::SET )
202 m_pCB_ShowEquation->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
205 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, true, &pPoolItem );
206 if( aState == SfxItemState::DONTCARE )
208 m_pCB_ShowCorrelationCoeff->EnableTriState( true );
209 m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_INDET );
211 else
213 m_pCB_ShowCorrelationCoeff->EnableTriState( false );
214 if( aState == SfxItemState::SET )
215 m_pCB_ShowCorrelationCoeff->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
218 if( m_bTrendLineUnique )
220 switch( m_eTrendLineType )
222 case CHREGRESS_LINEAR :
223 m_pRB_Linear->Check();
224 break;
225 case CHREGRESS_LOG :
226 m_pRB_Logarithmic->Check();
227 break;
228 case CHREGRESS_EXP :
229 m_pRB_Exponential->Check();
230 break;
231 case CHREGRESS_POWER :
232 m_pRB_Power->Check();
233 break;
234 case CHREGRESS_POLYNOMIAL :
235 m_pRB_Polynomial->Check();
236 break;
237 case CHREGRESS_MOVING_AVERAGE :
238 m_pRB_MovingAverage->Check();
239 break;
240 default:
241 break;
246 bool TrendlineResources::FillItemSet(SfxItemSet* rOutAttrs) const
248 if( m_bTrendLineUnique )
249 rOutAttrs->Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
251 if( m_pCB_ShowEquation->GetState() != TRISTATE_INDET )
252 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_pCB_ShowEquation->IsChecked() ));
254 if( m_pCB_ShowCorrelationCoeff->GetState() != TRISTATE_INDET )
255 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_pCB_ShowCorrelationCoeff->IsChecked() ));
257 OUString aName = m_pEE_Name->GetText();
258 rOutAttrs->Put(SfxStringItem(SCHATTR_REGRESSION_CURVE_NAME, aName));
260 sal_Int32 aDegree = m_pNF_Degree->GetValue();
261 rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_DEGREE, aDegree ) );
263 sal_Int32 aPeriod = m_pNF_Period->GetValue();
264 rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_PERIOD, aPeriod ) );
266 sal_uInt32 nIndex = 0;
267 double aValue = 0.0;
268 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateForward->GetText(),nIndex,aValue);
269 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD ) );
271 aValue = 0.0;
272 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateBackward->GetText(),nIndex,aValue);
273 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD ) );
275 if( m_pCB_SetIntercept->GetState() != TRISTATE_INDET )
276 rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SET_INTERCEPT, m_pCB_SetIntercept->IsChecked() ));
278 aValue = 0.0;
279 (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_InterceptValue->GetText(),nIndex,aValue);
280 rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_INTERCEPT_VALUE ) );
282 return true;
285 void TrendlineResources::FillValueSets()
287 m_pFI_Linear->SetImage( Image( SchResId( BMP_REGRESSION_LINEAR ) ) );
288 m_pFI_Logarithmic->SetImage( Image( SchResId( BMP_REGRESSION_LOG ) ) );
289 m_pFI_Exponential->SetImage( Image( SchResId( BMP_REGRESSION_EXP ) ) );
290 m_pFI_Power->SetImage( Image( SchResId( BMP_REGRESSION_POWER ) ) );
291 m_pFI_Polynomial->SetImage( Image( SchResId( BMP_REGRESSION_POLYNOMIAL ) ) );
292 m_pFI_MovingAverage->SetImage(Image( SchResId( BMP_REGRESSION_MOVING_AVERAGE ) ) );
295 void TrendlineResources::UpdateControlStates()
297 if( m_nNbPoints > 0 )
299 sal_Int32 nMaxValue = m_nNbPoints - 1 + ( m_pCB_SetIntercept->IsChecked()?1:0 );
300 // if( nMaxValue > 10) nMaxValue = 10;
301 m_pNF_Degree->SetMax( nMaxValue );
302 m_pNF_Period->SetMax( m_nNbPoints - 1 );
304 bool bMovingAverage = ( m_eTrendLineType == CHREGRESS_MOVING_AVERAGE );
305 bool bInterceptAvailable = ( m_eTrendLineType == CHREGRESS_LINEAR )
306 || ( m_eTrendLineType == CHREGRESS_POLYNOMIAL )
307 || ( m_eTrendLineType == CHREGRESS_EXP );
308 m_pFmtFld_ExtrapolateForward->Enable( !bMovingAverage );
309 m_pFmtFld_ExtrapolateBackward->Enable( !bMovingAverage );
310 m_pCB_SetIntercept->Enable( bInterceptAvailable );
311 m_pFmtFld_InterceptValue->Enable( bInterceptAvailable );
312 if( bMovingAverage )
314 m_pCB_ShowEquation->SetState( TRISTATE_FALSE );
315 m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_FALSE );
317 m_pCB_ShowEquation->Enable( !bMovingAverage );
318 m_pCB_ShowCorrelationCoeff->Enable( !bMovingAverage );
321 IMPL_LINK( TrendlineResources, ChangeValue, void *, pNumericField)
323 if( pNumericField == m_pNF_Degree )
325 if( !m_pRB_Polynomial->IsChecked() )
327 m_pRB_Polynomial->Check();
328 SelectTrendLine(m_pRB_Polynomial);
331 else if( pNumericField == m_pNF_Period )
333 if( !m_pRB_MovingAverage->IsChecked() )
335 m_pRB_MovingAverage->Check();
336 SelectTrendLine(m_pRB_MovingAverage);
339 else if( pNumericField == m_pFmtFld_InterceptValue )
341 if( !m_pCB_SetIntercept->IsChecked() )
342 m_pCB_SetIntercept->Check();
344 UpdateControlStates();
346 return 0;
349 void TrendlineResources::SetNumFormatter( SvNumberFormatter* pFormatter )
351 m_pNumFormatter = pFormatter;
352 m_pFmtFld_ExtrapolateForward->SetFormatter( m_pNumFormatter );
353 m_pFmtFld_ExtrapolateBackward->SetFormatter( m_pNumFormatter );
354 m_pFmtFld_InterceptValue->SetFormatter( m_pNumFormatter );
357 void TrendlineResources::SetNbPoints( sal_Int32 nNbPoints )
359 m_nNbPoints = nNbPoints;
360 UpdateControlStates();
363 } // namespace chart
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */