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 :
21 : #include "dlg_InsertTrendline.hrc"
22 : #include "res_Trendline.hxx"
23 : #include "res_Trendline_IDs.hrc"
24 : #include "ResId.hxx"
25 : #include "Strings.hrc"
26 : #include "Bitmaps.hrc"
27 : #include "chartview/ChartSfxItemIds.hxx"
28 :
29 : #include <vector>
30 : #include <algorithm>
31 :
32 : namespace
33 : {
34 : template< class T >
35 0 : long lcl_getRightEdge( T & rControl )
36 : {
37 0 : return rControl.CalcMinimumSize().Width() + rControl.GetPosPixel().X() - rControl.GetParent()->GetPosPixel().X();
38 : }
39 :
40 : template< class T >
41 0 : void lcl_AdjustControlSize( T & rControl )
42 : {
43 0 : Size aSize( rControl.GetSizePixel());
44 0 : aSize.setWidth( rControl.CalcMinimumSize().Width());
45 0 : rControl.SetSizePixel( aSize );
46 0 : }
47 :
48 0 : void lcl_AdjustControlSize( Control & rControl, long nRightEdge )
49 : {
50 0 : Size aSize( rControl.GetSizePixel());
51 0 : Point aPosition( rControl.GetPosPixel());
52 0 : aSize.setWidth( nRightEdge - aPosition.getX());
53 0 : rControl.SetSizePixel( aSize );
54 0 : }
55 :
56 : } // anonymous namespace
57 :
58 : namespace chart
59 : {
60 :
61 : enum StatTrendLine
62 : {
63 : TRENDLINE_NONE,
64 : TRENDLINE_LINE,
65 : TRENDLINE_LOG,
66 : TRENDLINE_EXP,
67 : TRENDLINE_POW
68 : };
69 :
70 0 : TrendlineResources::TrendlineResources( Window * pParent, const SfxItemSet& rInAttrs, bool bNoneAvailable ) :
71 : m_aFLType( pParent, SchResId( FL_TYPE )),
72 :
73 : m_aRBNone( pParent, SchResId( RB_NONE )),
74 : m_aRBLinear( pParent, SchResId( RB_LINEAR )),
75 : m_aRBLogarithmic( pParent, SchResId( RB_LOGARITHMIC )),
76 : m_aRBExponential( pParent, SchResId( RB_EXPONENTIAL )),
77 : m_aRBPower( pParent, SchResId( RB_POWER )),
78 :
79 : m_aFINone( pParent, SchResId( FI_NONE )),
80 : m_aFILinear( pParent, SchResId( FI_LINEAR )),
81 : m_aFILogarithmic( pParent, SchResId( FI_LOGARITHMIC )),
82 : m_aFIExponential( pParent, SchResId( FI_EXPONENTIAL )),
83 : m_aFIPower( pParent, SchResId( FI_POWER )),
84 :
85 : m_aFLEquation( pParent, SchResId( FL_EQUATION )),
86 : m_aCBShowEquation( pParent, SchResId( CB_SHOW_EQUATION )),
87 : m_aCBShowCorrelationCoeff( pParent, SchResId( CB_SHOW_CORRELATION_COEFF )),
88 :
89 : m_eTrendLineType( CHREGRESS_NONE ),
90 : m_bNoneAvailable( bNoneAvailable ),
91 0 : m_bTrendLineUnique( true )
92 : {
93 0 : FillValueSets();
94 :
95 0 : if( m_bNoneAvailable )
96 0 : m_aRBNone.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
97 : else
98 0 : m_aRBNone.Hide();
99 :
100 0 : m_aRBLinear.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
101 0 : m_aRBLogarithmic.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
102 0 : m_aRBExponential.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
103 0 : m_aRBPower.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
104 :
105 0 : Reset( rInAttrs );
106 0 : UpdateControlStates();
107 0 : }
108 :
109 0 : TrendlineResources::~TrendlineResources()
110 0 : {}
111 :
112 0 : long TrendlineResources::adjustControlSizes()
113 : {
114 : // calculate right edge
115 0 : ::std::vector< long > aControlRightEdges;
116 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aRBNone ));
117 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLinear ));
118 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLogarithmic ));
119 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aRBExponential ));
120 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aRBPower ));
121 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowEquation ));
122 0 : aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowCorrelationCoeff ));
123 :
124 0 : lcl_AdjustControlSize( m_aRBNone );
125 0 : lcl_AdjustControlSize( m_aRBLinear );
126 0 : lcl_AdjustControlSize( m_aRBLogarithmic );
127 0 : lcl_AdjustControlSize( m_aRBExponential );
128 0 : lcl_AdjustControlSize( m_aRBPower );
129 0 : lcl_AdjustControlSize( m_aCBShowEquation );
130 0 : lcl_AdjustControlSize( m_aCBShowCorrelationCoeff );
131 :
132 : // Note: FixedLine has no CalcMinimumSize, workaround: use a FixedText
133 0 : FixedText aDummyTextCtrl( m_aFLType.GetParent());
134 0 : aDummyTextCtrl.SetText( m_aFLType.GetText());
135 0 : aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
136 0 : aDummyTextCtrl.SetText( m_aFLEquation.GetText());
137 0 : aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
138 :
139 0 : long nRightEdgeOfControls = *(::std::max_element( aControlRightEdges.begin(), aControlRightEdges.end()));
140 : // leave some more space after the longest text
141 0 : nRightEdgeOfControls += m_aFLType.LogicToPixel( Size( 6, 0 ), MapMode( MAP_APPFONT )).getWidth();
142 :
143 0 : lcl_AdjustControlSize( m_aFLType, nRightEdgeOfControls );
144 0 : lcl_AdjustControlSize( m_aFLEquation, nRightEdgeOfControls );
145 :
146 0 : return nRightEdgeOfControls;
147 : }
148 :
149 0 : IMPL_LINK( TrendlineResources, SelectTrendLine, RadioButton *, pRadioButton )
150 : {
151 0 : if( pRadioButton == &m_aRBLinear )
152 0 : m_eTrendLineType = CHREGRESS_LINEAR;
153 0 : else if( pRadioButton == &m_aRBLogarithmic )
154 0 : m_eTrendLineType = CHREGRESS_LOG;
155 0 : else if( pRadioButton == &m_aRBExponential )
156 0 : m_eTrendLineType = CHREGRESS_EXP;
157 0 : else if( pRadioButton == &m_aRBPower )
158 0 : m_eTrendLineType = CHREGRESS_POWER;
159 0 : else if( pRadioButton == &m_aRBNone )
160 : {
161 : OSL_ASSERT( m_bNoneAvailable );
162 0 : m_eTrendLineType = CHREGRESS_NONE;
163 : }
164 0 : m_bTrendLineUnique = true;
165 :
166 0 : UpdateControlStates();
167 :
168 0 : return 0;
169 : }
170 :
171 0 : void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
172 : {
173 0 : const SfxPoolItem *pPoolItem = NULL;
174 0 : SfxItemState aState = SFX_ITEM_UNKNOWN;
175 :
176 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, sal_True, &pPoolItem );
177 0 : m_bTrendLineUnique = ( aState != SFX_ITEM_DONTCARE );
178 0 : if( aState == SFX_ITEM_SET )
179 : {
180 0 : const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
181 0 : if( pItem )
182 0 : m_eTrendLineType = pItem->GetValue();
183 : }
184 :
185 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, sal_True, &pPoolItem );
186 0 : if( aState == SFX_ITEM_DONTCARE )
187 : {
188 0 : m_aCBShowEquation.EnableTriState( sal_True );
189 0 : m_aCBShowEquation.SetState( STATE_DONTKNOW );
190 : }
191 : else
192 : {
193 0 : m_aCBShowEquation.EnableTriState( sal_False );
194 0 : if( aState == SFX_ITEM_SET )
195 0 : m_aCBShowEquation.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
196 : }
197 :
198 0 : aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, sal_True, &pPoolItem );
199 0 : if( aState == SFX_ITEM_DONTCARE )
200 : {
201 0 : m_aCBShowCorrelationCoeff.EnableTriState( sal_True );
202 0 : m_aCBShowCorrelationCoeff.SetState( STATE_DONTKNOW );
203 : }
204 : else
205 : {
206 0 : m_aCBShowCorrelationCoeff.EnableTriState( sal_False );
207 0 : if( aState == SFX_ITEM_SET )
208 0 : m_aCBShowCorrelationCoeff.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
209 : }
210 :
211 0 : if( m_bTrendLineUnique )
212 : {
213 0 : switch( m_eTrendLineType )
214 : {
215 : case CHREGRESS_LINEAR :
216 0 : m_aRBLinear.Check();
217 0 : break;
218 : case CHREGRESS_LOG :
219 0 : m_aRBLogarithmic.Check();
220 0 : break;
221 : case CHREGRESS_EXP :
222 0 : m_aRBExponential.Check();
223 0 : break;
224 : case CHREGRESS_POWER :
225 0 : m_aRBPower.Check();
226 0 : break;
227 : case CHREGRESS_NONE:
228 : OSL_ASSERT( m_bNoneAvailable );
229 0 : m_aRBNone.Check();
230 0 : break;
231 : }
232 : }
233 0 : }
234 :
235 0 : sal_Bool TrendlineResources::FillItemSet(SfxItemSet& rOutAttrs) const
236 : {
237 0 : if( m_bTrendLineUnique )
238 0 : rOutAttrs.Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
239 0 : if( m_aCBShowEquation.GetState() != STATE_DONTKNOW )
240 0 : rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_aCBShowEquation.IsChecked() ));
241 0 : if( m_aCBShowCorrelationCoeff.GetState() != STATE_DONTKNOW )
242 0 : rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_aCBShowCorrelationCoeff.IsChecked() ));
243 0 : return sal_True;
244 : }
245 :
246 0 : void TrendlineResources::FillValueSets()
247 : {
248 0 : if( m_bNoneAvailable )
249 0 : m_aFINone.SetImage( Image( SchResId( BMP_REGRESSION_NONE ) ) );
250 0 : m_aFILinear.SetImage( Image( SchResId( BMP_REGRESSION_LINEAR ) ) );
251 0 : m_aFILogarithmic.SetImage( Image( SchResId( BMP_REGRESSION_LOG ) ) );
252 0 : m_aFIExponential.SetImage( Image( SchResId( BMP_REGRESSION_EXP ) ) );
253 0 : m_aFIPower.SetImage( Image( SchResId( BMP_REGRESSION_POWER ) ) );
254 0 : }
255 :
256 0 : void TrendlineResources::UpdateControlStates()
257 : {
258 0 : if( m_bNoneAvailable )
259 : {
260 0 : bool bEnableEquationControls = !m_bTrendLineUnique || (m_eTrendLineType != CHREGRESS_NONE);
261 0 : m_aCBShowEquation.Enable( bEnableEquationControls );
262 0 : m_aCBShowCorrelationCoeff.Enable( bEnableEquationControls );
263 : }
264 0 : }
265 :
266 : } // namespace chart
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|