Line data Source code
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 :
20 : #include "res_Trendline.hxx"
21 : #include "ResId.hxx"
22 : #include "Strings.hrc"
23 : #include "Bitmaps.hrc"
24 : #include "chartview/ChartSfxItemIds.hxx"
25 :
26 : #include <svl/intitem.hxx>
27 : #include <svl/stritem.hxx>
28 : #include <sfx2/tabdlg.hxx>
29 :
30 : #include <vector>
31 : #include <algorithm>
32 :
33 : namespace chart
34 : {
35 :
36 0 : void lcl_setValue( FormattedField& rFmtField, double fValue )
37 : {
38 0 : rFmtField.SetValue( fValue );
39 0 : rFmtField.SetDefaultValue( fValue );
40 0 : }
41 :
42 0 : TrendlineResources::TrendlineResources( vcl::Window * pParent, const SfxItemSet& rInAttrs ) :
43 : m_eTrendLineType( CHREGRESS_LINEAR ),
44 : m_bTrendLineUnique( true ),
45 : m_pNumFormatter( NULL ),
46 0 : m_nNbPoints( 0 )
47 : {
48 0 : SfxTabPage* pTabPage = reinterpret_cast<SfxTabPage*>(pParent);
49 0 : pTabPage->get(m_pRB_Linear,"linear");
50 0 : pTabPage->get(m_pRB_Logarithmic,"logarithmic");
51 0 : pTabPage->get(m_pRB_Exponential,"exponential");
52 0 : pTabPage->get(m_pRB_Power,"power");
53 0 : pTabPage->get(m_pRB_Polynomial,"polynomial");
54 0 : pTabPage->get(m_pRB_MovingAverage,"movingAverage");
55 0 : pTabPage->get(m_pNF_Degree,"degree");
56 0 : pTabPage->get(m_pNF_Period,"period");
57 0 : pTabPage->get(m_pEE_Name,"entry_name");
58 0 : pTabPage->get(m_pFmtFld_ExtrapolateForward,"extrapolateForward");
59 0 : pTabPage->get(m_pFmtFld_ExtrapolateBackward,"extrapolateBackward");
60 0 : pTabPage->get(m_pCB_SetIntercept,"setIntercept");
61 0 : pTabPage->get(m_pFmtFld_InterceptValue,"interceptValue");
62 0 : pTabPage->get(m_pCB_ShowEquation,"showEquation");
63 0 : pTabPage->get(m_pCB_ShowCorrelationCoeff,"showCorrelationCoefficient");
64 0 : pTabPage->get(m_pFI_Linear,"imageLinear");
65 0 : pTabPage->get(m_pFI_Logarithmic,"imageLogarithmic");
66 0 : pTabPage->get(m_pFI_Exponential,"imageExponential");
67 0 : pTabPage->get(m_pFI_Power,"imagePower");
68 0 : pTabPage->get(m_pFI_Polynomial,"imagePolynomial");
69 0 : pTabPage->get(m_pFI_MovingAverage,"imageMovingAverage");
70 0 : FillValueSets();
71 :
72 0 : Link aLink = LINK(this, TrendlineResources, SelectTrendLine );
73 0 : m_pRB_Linear->SetClickHdl( aLink );
74 0 : m_pRB_Logarithmic->SetClickHdl( aLink );
75 0 : m_pRB_Exponential->SetClickHdl( aLink );
76 0 : m_pRB_Power->SetClickHdl( aLink );
77 0 : m_pRB_Polynomial->SetClickHdl( aLink );
78 0 : m_pRB_MovingAverage->SetClickHdl( aLink );
79 :
80 0 : aLink = LINK(this, TrendlineResources, ChangeValue );
81 0 : m_pNF_Degree->SetModifyHdl( aLink );
82 0 : m_pNF_Period->SetModifyHdl( aLink );
83 0 : m_pFmtFld_InterceptValue->SetModifyHdl( aLink );
84 :
85 0 : Reset( rInAttrs );
86 0 : UpdateControlStates();
87 0 : }
88 :
89 0 : TrendlineResources::~TrendlineResources()
90 0 : {}
91 :
92 0 : IMPL_LINK( TrendlineResources, SelectTrendLine, RadioButton *, pRadioButton )
93 : {
94 0 : if( pRadioButton == m_pRB_Linear )
95 0 : m_eTrendLineType = CHREGRESS_LINEAR;
96 0 : else if( pRadioButton == m_pRB_Logarithmic )
97 0 : m_eTrendLineType = CHREGRESS_LOG;
98 0 : else if( pRadioButton == m_pRB_Exponential )
99 0 : m_eTrendLineType = CHREGRESS_EXP;
100 0 : else if( pRadioButton == m_pRB_Power )
101 0 : m_eTrendLineType = CHREGRESS_POWER;
102 0 : else if( pRadioButton == m_pRB_Polynomial )
103 0 : m_eTrendLineType = CHREGRESS_POLYNOMIAL;
104 0 : else if( pRadioButton == m_pRB_MovingAverage )
105 0 : m_eTrendLineType = CHREGRESS_MOVING_AVERAGE;
106 0 : m_bTrendLineUnique = true;
107 :
108 0 : UpdateControlStates();
109 :
110 0 : return 0;
111 : }
112 :
113 0 : void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
114 : {
115 0 : const SfxPoolItem *pPoolItem = NULL;
116 :
117 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_CURVE_NAME, true, &pPoolItem ) == SfxItemState::SET )
118 : {
119 0 : OUString aName = static_cast< const SfxStringItem* >(pPoolItem)->GetValue();
120 0 : m_pEE_Name->SetText(aName);
121 : }
122 : else
123 : {
124 0 : m_pEE_Name->SetText("");
125 : }
126 :
127 0 : SfxItemState aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, true, &pPoolItem );
128 0 : m_bTrendLineUnique = ( aState != SfxItemState::DONTCARE );
129 0 : if( aState == SfxItemState::SET )
130 : {
131 0 : const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
132 0 : if( pItem )
133 : {
134 0 : m_eTrendLineType = pItem->GetValue();
135 : }
136 : }
137 :
138 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_DEGREE, true, &pPoolItem ) == SfxItemState::SET )
139 : {
140 0 : sal_Int32 nDegree = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
141 0 : m_pNF_Degree->SetValue( nDegree );
142 : }
143 : else
144 : {
145 0 : m_pNF_Degree->SetValue( 2 );
146 : }
147 :
148 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_PERIOD, true, &pPoolItem ) == SfxItemState::SET )
149 : {
150 0 : sal_Int32 nPeriod = static_cast< const SfxInt32Item * >( pPoolItem )->GetValue();
151 0 : m_pNF_Period->SetValue( nPeriod );
152 : }
153 : else
154 : {
155 0 : m_pNF_Period->SetValue( 2 );
156 : }
157 :
158 0 : double nValue = 0.0;
159 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD, true, &pPoolItem ) == SfxItemState::SET )
160 : {
161 0 : nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
162 : }
163 0 : lcl_setValue( *m_pFmtFld_ExtrapolateForward, nValue );
164 :
165 0 : nValue = 0.0;
166 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD, true, &pPoolItem ) == SfxItemState::SET )
167 : {
168 0 : nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
169 : }
170 0 : lcl_setValue( *m_pFmtFld_ExtrapolateBackward, nValue );
171 :
172 0 : nValue = 0.0;
173 0 : if( rInAttrs.GetItemState( SCHATTR_REGRESSION_INTERCEPT_VALUE, true, &pPoolItem ) == SfxItemState::SET )
174 : {
175 0 : nValue = static_cast<const SvxDoubleItem*>(pPoolItem)->GetValue() ;
176 : }
177 0 : lcl_setValue( *m_pFmtFld_InterceptValue, nValue );
178 :
179 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SET_INTERCEPT, true, &pPoolItem );
180 0 : if( aState == SfxItemState::DONTCARE )
181 : {
182 0 : m_pCB_SetIntercept->EnableTriState( true );
183 0 : m_pCB_SetIntercept->SetState( TRISTATE_INDET );
184 : }
185 : else
186 : {
187 0 : m_pCB_SetIntercept->EnableTriState( false );
188 0 : if( aState == SfxItemState::SET )
189 0 : m_pCB_SetIntercept->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
190 : }
191 :
192 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, true, &pPoolItem );
193 0 : if( aState == SfxItemState::DONTCARE )
194 : {
195 0 : m_pCB_ShowEquation->EnableTriState( true );
196 0 : m_pCB_ShowEquation->SetState( TRISTATE_INDET );
197 : }
198 : else
199 : {
200 0 : m_pCB_ShowEquation->EnableTriState( false );
201 0 : if( aState == SfxItemState::SET )
202 0 : m_pCB_ShowEquation->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
203 : }
204 :
205 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, true, &pPoolItem );
206 0 : if( aState == SfxItemState::DONTCARE )
207 : {
208 0 : m_pCB_ShowCorrelationCoeff->EnableTriState( true );
209 0 : m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_INDET );
210 : }
211 : else
212 : {
213 0 : m_pCB_ShowCorrelationCoeff->EnableTriState( false );
214 0 : if( aState == SfxItemState::SET )
215 0 : m_pCB_ShowCorrelationCoeff->Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
216 : }
217 :
218 0 : if( m_bTrendLineUnique )
219 : {
220 0 : switch( m_eTrendLineType )
221 : {
222 : case CHREGRESS_LINEAR :
223 0 : m_pRB_Linear->Check();
224 0 : break;
225 : case CHREGRESS_LOG :
226 0 : m_pRB_Logarithmic->Check();
227 0 : break;
228 : case CHREGRESS_EXP :
229 0 : m_pRB_Exponential->Check();
230 0 : break;
231 : case CHREGRESS_POWER :
232 0 : m_pRB_Power->Check();
233 0 : break;
234 : case CHREGRESS_POLYNOMIAL :
235 0 : m_pRB_Polynomial->Check();
236 0 : break;
237 : case CHREGRESS_MOVING_AVERAGE :
238 0 : m_pRB_MovingAverage->Check();
239 0 : break;
240 : default:
241 0 : break;
242 : }
243 : }
244 0 : }
245 :
246 0 : bool TrendlineResources::FillItemSet(SfxItemSet* rOutAttrs) const
247 : {
248 0 : if( m_bTrendLineUnique )
249 0 : rOutAttrs->Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
250 :
251 0 : if( m_pCB_ShowEquation->GetState() != TRISTATE_INDET )
252 0 : rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_pCB_ShowEquation->IsChecked() ));
253 :
254 0 : if( m_pCB_ShowCorrelationCoeff->GetState() != TRISTATE_INDET )
255 0 : rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_pCB_ShowCorrelationCoeff->IsChecked() ));
256 :
257 0 : OUString aName = m_pEE_Name->GetText();
258 0 : rOutAttrs->Put(SfxStringItem(SCHATTR_REGRESSION_CURVE_NAME, aName));
259 :
260 0 : sal_Int32 aDegree = m_pNF_Degree->GetValue();
261 0 : rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_DEGREE, aDegree ) );
262 :
263 0 : sal_Int32 aPeriod = m_pNF_Period->GetValue();
264 0 : rOutAttrs->Put(SfxInt32Item( SCHATTR_REGRESSION_PERIOD, aPeriod ) );
265 :
266 0 : sal_uInt32 nIndex = 0;
267 0 : double aValue = 0.0;
268 0 : (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateForward->GetText(),nIndex,aValue);
269 0 : rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_FORWARD ) );
270 :
271 0 : aValue = 0.0;
272 0 : (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_ExtrapolateBackward->GetText(),nIndex,aValue);
273 0 : rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_EXTRAPOLATE_BACKWARD ) );
274 :
275 0 : if( m_pCB_SetIntercept->GetState() != TRISTATE_INDET )
276 0 : rOutAttrs->Put( SfxBoolItem( SCHATTR_REGRESSION_SET_INTERCEPT, m_pCB_SetIntercept->IsChecked() ));
277 :
278 0 : aValue = 0.0;
279 0 : (void)m_pNumFormatter->IsNumberFormat(m_pFmtFld_InterceptValue->GetText(),nIndex,aValue);
280 0 : rOutAttrs->Put(SvxDoubleItem( aValue, SCHATTR_REGRESSION_INTERCEPT_VALUE ) );
281 :
282 0 : return true;
283 : }
284 :
285 0 : void TrendlineResources::FillValueSets()
286 : {
287 0 : m_pFI_Linear->SetImage( Image( SchResId( BMP_REGRESSION_LINEAR ) ) );
288 0 : m_pFI_Logarithmic->SetImage( Image( SchResId( BMP_REGRESSION_LOG ) ) );
289 0 : m_pFI_Exponential->SetImage( Image( SchResId( BMP_REGRESSION_EXP ) ) );
290 0 : m_pFI_Power->SetImage( Image( SchResId( BMP_REGRESSION_POWER ) ) );
291 0 : m_pFI_Polynomial->SetImage( Image( SchResId( BMP_REGRESSION_POLYNOMIAL ) ) );
292 0 : m_pFI_MovingAverage->SetImage(Image( SchResId( BMP_REGRESSION_MOVING_AVERAGE ) ) );
293 0 : }
294 :
295 0 : void TrendlineResources::UpdateControlStates()
296 : {
297 0 : if( m_nNbPoints > 0 )
298 : {
299 0 : sal_Int32 nMaxValue = m_nNbPoints - 1 + ( m_pCB_SetIntercept->IsChecked()?1:0 );
300 : // if( nMaxValue > 10) nMaxValue = 10;
301 0 : m_pNF_Degree->SetMax( nMaxValue );
302 0 : m_pNF_Period->SetMax( m_nNbPoints - 1 );
303 : }
304 0 : bool bMovingAverage = ( m_eTrendLineType == CHREGRESS_MOVING_AVERAGE );
305 0 : bool bInterceptAvailable = ( m_eTrendLineType == CHREGRESS_LINEAR ) || ( m_eTrendLineType == CHREGRESS_POLYNOMIAL );
306 0 : m_pFmtFld_ExtrapolateForward->Enable( !bMovingAverage );
307 0 : m_pFmtFld_ExtrapolateBackward->Enable( !bMovingAverage );
308 0 : m_pCB_SetIntercept->Enable( bInterceptAvailable );
309 0 : m_pFmtFld_InterceptValue->Enable( bInterceptAvailable );
310 0 : if( bMovingAverage )
311 : {
312 0 : m_pCB_ShowEquation->SetState( TRISTATE_FALSE );
313 0 : m_pCB_ShowCorrelationCoeff->SetState( TRISTATE_FALSE );
314 : }
315 0 : m_pCB_ShowEquation->Enable( !bMovingAverage );
316 0 : m_pCB_ShowCorrelationCoeff->Enable( !bMovingAverage );
317 0 : }
318 :
319 0 : IMPL_LINK( TrendlineResources, ChangeValue, void *, pNumericField)
320 : {
321 0 : if( pNumericField == m_pNF_Degree )
322 : {
323 0 : if( !m_pRB_Polynomial->IsChecked() )
324 : {
325 0 : m_pRB_Polynomial->Check();
326 0 : SelectTrendLine(m_pRB_Polynomial);
327 : }
328 : }
329 0 : else if( pNumericField == m_pNF_Period )
330 : {
331 0 : if( !m_pRB_MovingAverage->IsChecked() )
332 : {
333 0 : m_pRB_MovingAverage->Check();
334 0 : SelectTrendLine(m_pRB_MovingAverage);
335 : }
336 : }
337 0 : else if( pNumericField == m_pFmtFld_InterceptValue )
338 : {
339 0 : if( !m_pCB_SetIntercept->IsChecked() )
340 0 : m_pCB_SetIntercept->Check();
341 : }
342 0 : UpdateControlStates();
343 :
344 0 : return 0;
345 : }
346 :
347 0 : void TrendlineResources::SetNumFormatter( SvNumberFormatter* pFormatter )
348 : {
349 0 : m_pNumFormatter = pFormatter;
350 0 : m_pFmtFld_ExtrapolateForward->SetFormatter( m_pNumFormatter );
351 0 : m_pFmtFld_ExtrapolateBackward->SetFormatter( m_pNumFormatter );
352 0 : m_pFmtFld_InterceptValue->SetFormatter( m_pNumFormatter );
353 0 : }
354 :
355 0 : void TrendlineResources::SetNbPoints( sal_Int32 nNbPoints )
356 : {
357 0 : m_nNbPoints = nNbPoints;
358 0 : UpdateControlStates();
359 0 : }
360 :
361 102 : } // namespace chart
362 :
363 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|