1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_chart2.hxx"
30*cdf0e10cSrcweir #include "RegressionCurveHelper.hxx"
31*cdf0e10cSrcweir #include "RegressionCurveItemConverter.hxx"
32*cdf0e10cSrcweir #include "SchWhichPairs.hxx"
33*cdf0e10cSrcweir #include "macros.hxx"
34*cdf0e10cSrcweir #include "ItemPropertyMap.hxx"
35*cdf0e10cSrcweir #include "GraphicPropertyItemConverter.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/chart2/XRegressionCurve.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // for SfxBoolItem
40*cdf0e10cSrcweir #include <svl/eitem.hxx>
41*cdf0e10cSrcweir #include <svx/chrtitem.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <functional>
44*cdf0e10cSrcweir #include <algorithm>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::com::sun::star;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir ::chart::RegressionCurveHelper::tRegressionType lcl_convertRegressionType( SvxChartRegress eRegress )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     ::chart::RegressionCurveHelper::tRegressionType eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_NONE;
54*cdf0e10cSrcweir     switch( eRegress )
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         case CHREGRESS_LINEAR:
57*cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LINEAR;
58*cdf0e10cSrcweir             break;
59*cdf0e10cSrcweir         case CHREGRESS_LOG:
60*cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LOG;
61*cdf0e10cSrcweir             break;
62*cdf0e10cSrcweir         case CHREGRESS_EXP:
63*cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_EXP;
64*cdf0e10cSrcweir             break;
65*cdf0e10cSrcweir         case CHREGRESS_POWER:
66*cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_POWER;
67*cdf0e10cSrcweir             break;
68*cdf0e10cSrcweir         case CHREGRESS_NONE:
69*cdf0e10cSrcweir             break;
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir     return eType;
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir } // anonymous namespace
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir namespace chart
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir namespace wrapper
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir RegressionCurveItemConverter::RegressionCurveItemConverter(
82*cdf0e10cSrcweir     const uno::Reference< beans::XPropertySet > & rPropertySet,
83*cdf0e10cSrcweir     const uno::Reference< chart2::XRegressionCurveContainer > & xRegCurveCnt,
84*cdf0e10cSrcweir     SfxItemPool& rItemPool,
85*cdf0e10cSrcweir     SdrModel& rDrawModel,
86*cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
87*cdf0e10cSrcweir         ItemConverter( rPropertySet, rItemPool ),
88*cdf0e10cSrcweir         m_spGraphicConverter( new GraphicPropertyItemConverter(
89*cdf0e10cSrcweir                                   rPropertySet, rItemPool, rDrawModel,
90*cdf0e10cSrcweir                                   xNamedPropertyContainerFactory,
91*cdf0e10cSrcweir                                   GraphicPropertyItemConverter::LINE_PROPERTIES )),
92*cdf0e10cSrcweir         m_xCurveContainer( xRegCurveCnt )
93*cdf0e10cSrcweir {}
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir RegressionCurveItemConverter::~RegressionCurveItemConverter()
96*cdf0e10cSrcweir {}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     m_spGraphicConverter->FillItemSet( rOutItemSet );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     // own items
103*cdf0e10cSrcweir     ItemConverter::FillItemSet( rOutItemSet );
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     // own items
111*cdf0e10cSrcweir     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     // must span all used items!
117*cdf0e10cSrcweir     return nRegressionCurveWhichPairs;
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir bool RegressionCurveItemConverter::GetItemProperty(
121*cdf0e10cSrcweir     tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     // No own (non-special) properties
124*cdf0e10cSrcweir     return false;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir bool RegressionCurveItemConverter::ApplySpecialItem(
129*cdf0e10cSrcweir     sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
130*cdf0e10cSrcweir     throw( uno::Exception )
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
133*cdf0e10cSrcweir     bool bChanged = false;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     switch( nWhichId )
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         case SCHATTR_REGRESSION_TYPE:
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
140*cdf0e10cSrcweir             if( xCurve.is())
141*cdf0e10cSrcweir             {
142*cdf0e10cSrcweir                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
143*cdf0e10cSrcweir                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
144*cdf0e10cSrcweir                 SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
145*cdf0e10cSrcweir                     rItemSet.Get( nWhichId )).GetValue();
146*cdf0e10cSrcweir                 if( eRegress != eNewRegress )
147*cdf0e10cSrcweir                 {
148*cdf0e10cSrcweir                     // note that changing the regression type changes the object
149*cdf0e10cSrcweir                     // for which this converter was created. Not optimal, but
150*cdf0e10cSrcweir                     // currently the only way to handle the type in the
151*cdf0e10cSrcweir                     // regression curve properties dialog
152*cdf0e10cSrcweir                     RegressionCurveHelper::replaceOrAddCurveAndReduceToOne(
153*cdf0e10cSrcweir                         lcl_convertRegressionType( eNewRegress ), m_xCurveContainer,
154*cdf0e10cSrcweir                         uno::Reference< uno::XComponentContext >());
155*cdf0e10cSrcweir                     uno::Reference< beans::XPropertySet > xNewPropSet(
156*cdf0e10cSrcweir                         RegressionCurveHelper::getFirstCurveNotMeanValueLine( m_xCurveContainer ),
157*cdf0e10cSrcweir                         uno::UNO_QUERY );
158*cdf0e10cSrcweir                     OSL_ASSERT( xNewPropSet.is());
159*cdf0e10cSrcweir                     if( xNewPropSet.is())
160*cdf0e10cSrcweir                     {
161*cdf0e10cSrcweir                         resetPropertySet( xNewPropSet );
162*cdf0e10cSrcweir                         bChanged = true;
163*cdf0e10cSrcweir                     }
164*cdf0e10cSrcweir                 }
165*cdf0e10cSrcweir             }
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir         break;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_EQUATION:
170*cdf0e10cSrcweir         {
171*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
172*cdf0e10cSrcweir             if( xCurve.is())
173*cdf0e10cSrcweir             {
174*cdf0e10cSrcweir                 bool bNewShow = static_cast< sal_Bool >(
175*cdf0e10cSrcweir                     static_cast< const SfxBoolItem & >(
176*cdf0e10cSrcweir                         rItemSet.Get( nWhichId )).GetValue());
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
179*cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
180*cdf0e10cSrcweir                 bool bOldShow = false;
181*cdf0e10cSrcweir                 if( xEqProp.is() &&
182*cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bOldShow) &&
183*cdf0e10cSrcweir                     bOldShow != bNewShow )
184*cdf0e10cSrcweir                 {
185*cdf0e10cSrcweir                     xEqProp->setPropertyValue( C2U( "ShowEquation" ), uno::makeAny( bNewShow ));
186*cdf0e10cSrcweir                     bChanged = true;
187*cdf0e10cSrcweir                 }
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir         break;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_COEFF:
193*cdf0e10cSrcweir         {
194*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
195*cdf0e10cSrcweir             if( xCurve.is())
196*cdf0e10cSrcweir             {
197*cdf0e10cSrcweir                 bool bNewShow = static_cast< sal_Bool >(
198*cdf0e10cSrcweir                     static_cast< const SfxBoolItem & >(
199*cdf0e10cSrcweir                         rItemSet.Get( nWhichId )).GetValue());
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
202*cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
203*cdf0e10cSrcweir                 bool bOldShow = false;
204*cdf0e10cSrcweir                 if( xEqProp.is() &&
205*cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bOldShow) &&
206*cdf0e10cSrcweir                     bOldShow != bNewShow )
207*cdf0e10cSrcweir                 {
208*cdf0e10cSrcweir                     xEqProp->setPropertyValue( C2U( "ShowCorrelationCoefficient" ), uno::makeAny( bNewShow ));
209*cdf0e10cSrcweir                     bChanged = true;
210*cdf0e10cSrcweir                 }
211*cdf0e10cSrcweir             }
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir         break;
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     return bChanged;
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir void RegressionCurveItemConverter::FillSpecialItem(
220*cdf0e10cSrcweir     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
221*cdf0e10cSrcweir     throw( uno::Exception )
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     switch( nWhichId )
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         case SCHATTR_REGRESSION_TYPE:
228*cdf0e10cSrcweir         {
229*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
230*cdf0e10cSrcweir             if( xCurve.is())
231*cdf0e10cSrcweir             {
232*cdf0e10cSrcweir                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
233*cdf0e10cSrcweir                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
234*cdf0e10cSrcweir                 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir         break;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_EQUATION:
240*cdf0e10cSrcweir         {
241*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
242*cdf0e10cSrcweir             if( xCurve.is())
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
245*cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
246*cdf0e10cSrcweir                 bool bShow = false;
247*cdf0e10cSrcweir                 if( xEqProp.is() &&
248*cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bShow))
249*cdf0e10cSrcweir                 {
250*cdf0e10cSrcweir                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
251*cdf0e10cSrcweir                 }
252*cdf0e10cSrcweir             }
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir         break;
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_COEFF:
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
259*cdf0e10cSrcweir             if( xCurve.is())
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
262*cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
263*cdf0e10cSrcweir                 bool bShow = false;
264*cdf0e10cSrcweir                 if( xEqProp.is() &&
265*cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bShow))
266*cdf0e10cSrcweir                 {
267*cdf0e10cSrcweir                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
268*cdf0e10cSrcweir                 }
269*cdf0e10cSrcweir             }
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir         break;
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir } //  namespace wrapper
276*cdf0e10cSrcweir } //  namespace chart
277