1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "dlg_InsertTrendline.hrc"
32 #include "res_Trendline.hxx"
33 #include "res_Trendline_IDs.hrc"
34 #include "ResId.hxx"
35 #include "Strings.hrc"
36 #include "Bitmaps.hrc"
37 #include "Bitmaps_HC.hrc"
38 #include "chartview/ChartSfxItemIds.hxx"
39 
40 #include <vector>
41 #include <algorithm>
42 
43 // macro for selecting a normal or high contrast bitmap the stack variable
44 // bIsHighContrast must exist and reflect the correct state
45 #define SELECT_IMAGE(name) Image( SchResId( bIsHighContrast ? name ## _HC : name ))
46 
47 namespace
48 {
49 template< class T >
50     long lcl_getRightEdge( T & rControl )
51 {
52     return rControl.CalcMinimumSize().Width() + rControl.GetPosPixel().X() - rControl.GetParent()->GetPosPixel().X();
53 }
54 
55 template< class T >
56     void lcl_AdjustControlSize( T & rControl )
57 {
58     Size aSize( rControl.GetSizePixel());
59     aSize.setWidth( rControl.CalcMinimumSize().Width());
60     rControl.SetSizePixel( aSize );
61 }
62 
63 void lcl_AdjustControlSize( Control & rControl, long nRightEdge )
64 {
65     Size aSize( rControl.GetSizePixel());
66     Point aPosition( rControl.GetPosPixel());
67     aSize.setWidth( nRightEdge - aPosition.getX());
68     rControl.SetSizePixel( aSize );
69 }
70 
71 } // anonymous namespace
72 
73 namespace chart
74 {
75 
76 enum StatTrendLine
77 {
78     TRENDLINE_NONE,
79     TRENDLINE_LINE,
80     TRENDLINE_LOG,
81     TRENDLINE_EXP,
82     TRENDLINE_POW
83 };
84 
85 TrendlineResources::TrendlineResources( Window * pParent, const SfxItemSet& rInAttrs, bool bNoneAvailable ) :
86         m_aFLType( pParent, SchResId( FL_TYPE )),
87 
88         m_aRBNone( pParent, SchResId( RB_NONE )),
89         m_aRBLinear( pParent, SchResId( RB_LINEAR )),
90         m_aRBLogarithmic( pParent, SchResId( RB_LOGARITHMIC )),
91         m_aRBExponential( pParent, SchResId( RB_EXPONENTIAL )),
92         m_aRBPower( pParent, SchResId( RB_POWER )),
93 
94         m_aFINone( pParent, SchResId( FI_NONE )),
95         m_aFILinear( pParent, SchResId( FI_LINEAR )),
96         m_aFILogarithmic( pParent, SchResId( FI_LOGARITHMIC )),
97         m_aFIExponential( pParent, SchResId( FI_EXPONENTIAL )),
98         m_aFIPower( pParent, SchResId( FI_POWER )),
99 
100         m_aFLEquation( pParent, SchResId( FL_EQUATION )),
101         m_aCBShowEquation( pParent, SchResId( CB_SHOW_EQUATION )),
102         m_aCBShowCorrelationCoeff( pParent, SchResId( CB_SHOW_CORRELATION_COEFF )),
103         m_eTrendLineType( CHREGRESS_NONE ),
104         m_bNoneAvailable( bNoneAvailable ),
105         m_bTrendLineUnique( true )
106 {
107     FillValueSets();
108 
109     if( m_bNoneAvailable )
110         m_aRBNone.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
111     else
112         m_aRBNone.Hide();
113 
114     m_aRBLinear.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
115     m_aRBLogarithmic.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
116     m_aRBExponential.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
117     m_aRBPower.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
118 
119     Reset( rInAttrs );
120     UpdateControlStates();
121 }
122 
123 TrendlineResources::~TrendlineResources()
124 {}
125 
126 long TrendlineResources::adjustControlSizes()
127 {
128     // calculate right edge
129     ::std::vector< long > aControlRightEdges;
130     aControlRightEdges.push_back( lcl_getRightEdge( m_aRBNone ));
131     aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLinear ));
132     aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLogarithmic ));
133     aControlRightEdges.push_back( lcl_getRightEdge( m_aRBExponential ));
134     aControlRightEdges.push_back( lcl_getRightEdge( m_aRBPower ));
135     aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowEquation ));
136     aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowCorrelationCoeff ));
137 
138     lcl_AdjustControlSize( m_aRBNone );
139     lcl_AdjustControlSize( m_aRBLinear );
140     lcl_AdjustControlSize( m_aRBLogarithmic );
141     lcl_AdjustControlSize( m_aRBExponential );
142     lcl_AdjustControlSize( m_aRBPower );
143     lcl_AdjustControlSize( m_aCBShowEquation );
144     lcl_AdjustControlSize( m_aCBShowCorrelationCoeff );
145 
146     // Note: FixedLine has no CalcMinimumSize, workaround: use a FixedText
147     FixedText aDummyTextCtrl( m_aFLType.GetParent());
148     aDummyTextCtrl.SetText( m_aFLType.GetText());
149     aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
150     aDummyTextCtrl.SetText( m_aFLEquation.GetText());
151     aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
152 
153     long nRightEdgeOfControls = *(::std::max_element( aControlRightEdges.begin(), aControlRightEdges.end()));
154     // leave some more space after the longest text
155     nRightEdgeOfControls += m_aFLType.LogicToPixel( Size( 6, 0 ), MapMode( MAP_APPFONT )).getWidth();
156 
157     lcl_AdjustControlSize( m_aFLType, nRightEdgeOfControls );
158     lcl_AdjustControlSize( m_aFLEquation, nRightEdgeOfControls );
159 
160     return nRightEdgeOfControls;
161 }
162 
163 IMPL_LINK( TrendlineResources, SelectTrendLine, RadioButton *, pRadioButton )
164 {
165     if( pRadioButton == &m_aRBLinear )
166         m_eTrendLineType = CHREGRESS_LINEAR;
167     else if( pRadioButton == &m_aRBLogarithmic )
168         m_eTrendLineType = CHREGRESS_LOG;
169     else if( pRadioButton == &m_aRBExponential )
170         m_eTrendLineType = CHREGRESS_EXP;
171     else if( pRadioButton == &m_aRBPower )
172         m_eTrendLineType = CHREGRESS_POWER;
173     else if( pRadioButton == &m_aRBNone )
174     {
175         OSL_ASSERT( m_bNoneAvailable );
176         m_eTrendLineType = CHREGRESS_NONE;
177     }
178     m_bTrendLineUnique = true;
179 
180     UpdateControlStates();
181 
182     return 0;
183 }
184 
185 void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
186 {
187 	const SfxPoolItem *pPoolItem = NULL;
188     SfxItemState aState = SFX_ITEM_UNKNOWN;
189 
190 	aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, sal_True, &pPoolItem );
191     m_bTrendLineUnique = ( aState != SFX_ITEM_DONTCARE );
192     if( aState == SFX_ITEM_SET )
193     {
194         const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
195         if( pItem )
196             m_eTrendLineType = pItem->GetValue();
197     }
198 
199     aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, sal_True, &pPoolItem );
200     if( aState == SFX_ITEM_DONTCARE )
201     {
202         m_aCBShowEquation.EnableTriState( sal_True );
203         m_aCBShowEquation.SetState( STATE_DONTKNOW );
204     }
205     else
206     {
207         m_aCBShowEquation.EnableTriState( sal_False );
208         if( aState == SFX_ITEM_SET )
209             m_aCBShowEquation.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
210     }
211 
212     aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, sal_True, &pPoolItem );
213     if( aState == SFX_ITEM_DONTCARE )
214     {
215         m_aCBShowCorrelationCoeff.EnableTriState( sal_True );
216         m_aCBShowCorrelationCoeff.SetState( STATE_DONTKNOW );
217     }
218     else
219     {
220         m_aCBShowCorrelationCoeff.EnableTriState( sal_False );
221         if( aState == SFX_ITEM_SET )
222             m_aCBShowCorrelationCoeff.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
223     }
224 
225     if( m_bTrendLineUnique )
226     {
227         switch( m_eTrendLineType )
228         {
229             case CHREGRESS_LINEAR :
230                 m_aRBLinear.Check();
231                 break;
232             case CHREGRESS_LOG :
233                 m_aRBLogarithmic.Check();
234                 break;
235             case CHREGRESS_EXP :
236                 m_aRBExponential.Check();
237                 break;
238             case CHREGRESS_POWER :
239                 m_aRBPower.Check();
240                 break;
241             case CHREGRESS_NONE:
242                 OSL_ASSERT( m_bNoneAvailable );
243                 m_aRBNone.Check();
244                 break;
245         }
246     }
247 }
248 
249 sal_Bool TrendlineResources::FillItemSet(SfxItemSet& rOutAttrs) const
250 {
251     if( m_bTrendLineUnique )
252         rOutAttrs.Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
253     if( m_aCBShowEquation.GetState() != STATE_DONTKNOW )
254         rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_aCBShowEquation.IsChecked() ));
255     if( m_aCBShowCorrelationCoeff.GetState() != STATE_DONTKNOW )
256         rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_aCBShowCorrelationCoeff.IsChecked() ));
257 	return sal_True;
258 }
259 
260 void TrendlineResources::FillValueSets()
261 {
262     bool bIsHighContrast = ( true && m_aFLType.GetSettings().GetStyleSettings().GetHighContrastMode() );
263 
264     if( m_bNoneAvailable )
265         m_aFINone.SetImage( SELECT_IMAGE( BMP_REGRESSION_NONE ));
266     m_aFILinear.SetImage( SELECT_IMAGE( BMP_REGRESSION_LINEAR ));
267     m_aFILogarithmic.SetImage( SELECT_IMAGE( BMP_REGRESSION_LOG ));
268     m_aFIExponential.SetImage( SELECT_IMAGE( BMP_REGRESSION_EXP ));
269     m_aFIPower.SetImage( SELECT_IMAGE( BMP_REGRESSION_POWER ));
270 }
271 
272 void TrendlineResources::UpdateControlStates()
273 {
274     if( m_bNoneAvailable )
275     {
276         bool bEnableEquationControls = !m_bTrendLineUnique || (m_eTrendLineType != CHREGRESS_NONE);
277         m_aCBShowEquation.Enable( bEnableEquationControls );
278         m_aCBShowCorrelationCoeff.Enable( bEnableEquationControls );
279     }
280 }
281 
282 } //  namespace chart
283