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 #include "RegressionCurveHelper.hxx"
31 #include "RegressionCurveItemConverter.hxx"
32 #include "SchWhichPairs.hxx"
33 #include "macros.hxx"
34 #include "ItemPropertyMap.hxx"
35 #include "GraphicPropertyItemConverter.hxx"
36 
37 #include <com/sun/star/chart2/XRegressionCurve.hpp>
38 
39 // for SfxBoolItem
40 #include <svl/eitem.hxx>
41 #include <svx/chrtitem.hxx>
42 
43 #include <functional>
44 #include <algorithm>
45 
46 using namespace ::com::sun::star;
47 
48 namespace
49 {
50 
51 ::chart::RegressionCurveHelper::tRegressionType lcl_convertRegressionType( SvxChartRegress eRegress )
52 {
53     ::chart::RegressionCurveHelper::tRegressionType eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_NONE;
54     switch( eRegress )
55     {
56         case CHREGRESS_LINEAR:
57             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LINEAR;
58             break;
59         case CHREGRESS_LOG:
60             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LOG;
61             break;
62         case CHREGRESS_EXP:
63             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_EXP;
64             break;
65         case CHREGRESS_POWER:
66             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_POWER;
67             break;
68         case CHREGRESS_NONE:
69             break;
70     }
71     return eType;
72 }
73 
74 } // anonymous namespace
75 
76 namespace chart
77 {
78 namespace wrapper
79 {
80 
81 RegressionCurveItemConverter::RegressionCurveItemConverter(
82     const uno::Reference< beans::XPropertySet > & rPropertySet,
83     const uno::Reference< chart2::XRegressionCurveContainer > & xRegCurveCnt,
84     SfxItemPool& rItemPool,
85     SdrModel& rDrawModel,
86     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
87         ItemConverter( rPropertySet, rItemPool ),
88         m_spGraphicConverter( new GraphicPropertyItemConverter(
89                                   rPropertySet, rItemPool, rDrawModel,
90                                   xNamedPropertyContainerFactory,
91                                   GraphicPropertyItemConverter::LINE_PROPERTIES )),
92         m_xCurveContainer( xRegCurveCnt )
93 {}
94 
95 RegressionCurveItemConverter::~RegressionCurveItemConverter()
96 {}
97 
98 void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
99 {
100     m_spGraphicConverter->FillItemSet( rOutItemSet );
101 
102     // own items
103     ItemConverter::FillItemSet( rOutItemSet );
104 }
105 
106 bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
107 {
108     bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
109 
110     // own items
111     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
112 }
113 
114 const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
115 {
116     // must span all used items!
117     return nRegressionCurveWhichPairs;
118 }
119 
120 bool RegressionCurveItemConverter::GetItemProperty(
121     tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
122 {
123     // No own (non-special) properties
124     return false;
125 }
126 
127 
128 bool RegressionCurveItemConverter::ApplySpecialItem(
129     sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
130     throw( uno::Exception )
131 {
132     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
133     bool bChanged = false;
134 
135     switch( nWhichId )
136     {
137         case SCHATTR_REGRESSION_TYPE:
138         {
139             OSL_ASSERT( xCurve.is());
140             if( xCurve.is())
141             {
142                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
143                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
144                 SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
145                     rItemSet.Get( nWhichId )).GetValue();
146                 if( eRegress != eNewRegress )
147                 {
148                     // note that changing the regression type changes the object
149                     // for which this converter was created. Not optimal, but
150                     // currently the only way to handle the type in the
151                     // regression curve properties dialog
152                     RegressionCurveHelper::replaceOrAddCurveAndReduceToOne(
153                         lcl_convertRegressionType( eNewRegress ), m_xCurveContainer,
154                         uno::Reference< uno::XComponentContext >());
155                     uno::Reference< beans::XPropertySet > xNewPropSet(
156                         RegressionCurveHelper::getFirstCurveNotMeanValueLine( m_xCurveContainer ),
157                         uno::UNO_QUERY );
158                     OSL_ASSERT( xNewPropSet.is());
159                     if( xNewPropSet.is())
160                     {
161                         resetPropertySet( xNewPropSet );
162                         bChanged = true;
163                     }
164                 }
165             }
166         }
167         break;
168 
169         case SCHATTR_REGRESSION_SHOW_EQUATION:
170         {
171             OSL_ASSERT( xCurve.is());
172             if( xCurve.is())
173             {
174                 bool bNewShow = static_cast< sal_Bool >(
175                     static_cast< const SfxBoolItem & >(
176                         rItemSet.Get( nWhichId )).GetValue());
177 
178                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
179                 OSL_ASSERT( xEqProp.is());
180                 bool bOldShow = false;
181                 if( xEqProp.is() &&
182                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bOldShow) &&
183                     bOldShow != bNewShow )
184                 {
185                     xEqProp->setPropertyValue( C2U( "ShowEquation" ), uno::makeAny( bNewShow ));
186                     bChanged = true;
187                 }
188             }
189         }
190         break;
191 
192         case SCHATTR_REGRESSION_SHOW_COEFF:
193         {
194             OSL_ASSERT( xCurve.is());
195             if( xCurve.is())
196             {
197                 bool bNewShow = static_cast< sal_Bool >(
198                     static_cast< const SfxBoolItem & >(
199                         rItemSet.Get( nWhichId )).GetValue());
200 
201                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
202                 OSL_ASSERT( xEqProp.is());
203                 bool bOldShow = false;
204                 if( xEqProp.is() &&
205                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bOldShow) &&
206                     bOldShow != bNewShow )
207                 {
208                     xEqProp->setPropertyValue( C2U( "ShowCorrelationCoefficient" ), uno::makeAny( bNewShow ));
209                     bChanged = true;
210                 }
211             }
212         }
213         break;
214     }
215 
216     return bChanged;
217 }
218 
219 void RegressionCurveItemConverter::FillSpecialItem(
220     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
221     throw( uno::Exception )
222 {
223     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
224 
225     switch( nWhichId )
226     {
227         case SCHATTR_REGRESSION_TYPE:
228         {
229             OSL_ASSERT( xCurve.is());
230             if( xCurve.is())
231             {
232                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
233                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
234                 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
235             }
236         }
237         break;
238 
239         case SCHATTR_REGRESSION_SHOW_EQUATION:
240         {
241             OSL_ASSERT( xCurve.is());
242             if( xCurve.is())
243             {
244                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
245                 OSL_ASSERT( xEqProp.is());
246                 bool bShow = false;
247                 if( xEqProp.is() &&
248                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bShow))
249                 {
250                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
251                 }
252             }
253         }
254         break;
255 
256         case SCHATTR_REGRESSION_SHOW_COEFF:
257         {
258             OSL_ASSERT( xCurve.is());
259             if( xCurve.is())
260             {
261                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
262                 OSL_ASSERT( xEqProp.is());
263                 bool bShow = false;
264                 if( xEqProp.is() &&
265                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bShow))
266                 {
267                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
268                 }
269             }
270         }
271         break;
272     }
273 }
274 
275 } //  namespace wrapper
276 } //  namespace chart
277