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