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