1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "RegressionCurveHelper.hxx"
27cdf0e10cSrcweir #include "RegressionCurveItemConverter.hxx"
28cdf0e10cSrcweir #include "SchWhichPairs.hxx"
29cdf0e10cSrcweir #include "macros.hxx"
30cdf0e10cSrcweir #include "ItemPropertyMap.hxx"
31cdf0e10cSrcweir #include "GraphicPropertyItemConverter.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/chart2/XRegressionCurve.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // for SfxBoolItem
36cdf0e10cSrcweir #include <svl/eitem.hxx>
37cdf0e10cSrcweir #include <svx/chrtitem.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <functional>
40cdf0e10cSrcweir #include <algorithm>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 
lcl_convertRegressionType(SvxChartRegress eRegress)47cdf0e10cSrcweir ::chart::RegressionCurveHelper::tRegressionType lcl_convertRegressionType( SvxChartRegress eRegress )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     ::chart::RegressionCurveHelper::tRegressionType eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_NONE;
50cdf0e10cSrcweir     switch( eRegress )
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         case CHREGRESS_LINEAR:
53cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LINEAR;
54cdf0e10cSrcweir             break;
55cdf0e10cSrcweir         case CHREGRESS_LOG:
56cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_LOG;
57cdf0e10cSrcweir             break;
58cdf0e10cSrcweir         case CHREGRESS_EXP:
59cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_EXP;
60cdf0e10cSrcweir             break;
61cdf0e10cSrcweir         case CHREGRESS_POWER:
62cdf0e10cSrcweir             eType = ::chart::RegressionCurveHelper::REGRESSION_TYPE_POWER;
63cdf0e10cSrcweir             break;
64cdf0e10cSrcweir         case CHREGRESS_NONE:
65cdf0e10cSrcweir             break;
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir     return eType;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir } // anonymous namespace
71cdf0e10cSrcweir 
72cdf0e10cSrcweir namespace chart
73cdf0e10cSrcweir {
74cdf0e10cSrcweir namespace wrapper
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 
RegressionCurveItemConverter(const uno::Reference<beans::XPropertySet> & rPropertySet,const uno::Reference<chart2::XRegressionCurveContainer> & xRegCurveCnt,SfxItemPool & rItemPool,SdrModel & rDrawModel,const uno::Reference<lang::XMultiServiceFactory> & xNamedPropertyContainerFactory)77cdf0e10cSrcweir RegressionCurveItemConverter::RegressionCurveItemConverter(
78cdf0e10cSrcweir     const uno::Reference< beans::XPropertySet > & rPropertySet,
79cdf0e10cSrcweir     const uno::Reference< chart2::XRegressionCurveContainer > & xRegCurveCnt,
80cdf0e10cSrcweir     SfxItemPool& rItemPool,
81cdf0e10cSrcweir     SdrModel& rDrawModel,
82cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
83cdf0e10cSrcweir         ItemConverter( rPropertySet, rItemPool ),
84cdf0e10cSrcweir         m_spGraphicConverter( new GraphicPropertyItemConverter(
85cdf0e10cSrcweir                                   rPropertySet, rItemPool, rDrawModel,
86cdf0e10cSrcweir                                   xNamedPropertyContainerFactory,
87cdf0e10cSrcweir                                   GraphicPropertyItemConverter::LINE_PROPERTIES )),
88cdf0e10cSrcweir         m_xCurveContainer( xRegCurveCnt )
89cdf0e10cSrcweir {}
90cdf0e10cSrcweir 
~RegressionCurveItemConverter()91cdf0e10cSrcweir RegressionCurveItemConverter::~RegressionCurveItemConverter()
92cdf0e10cSrcweir {}
93cdf0e10cSrcweir 
FillItemSet(SfxItemSet & rOutItemSet) const94cdf0e10cSrcweir void RegressionCurveItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     m_spGraphicConverter->FillItemSet( rOutItemSet );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     // own items
99cdf0e10cSrcweir     ItemConverter::FillItemSet( rOutItemSet );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
ApplyItemSet(const SfxItemSet & rItemSet)102cdf0e10cSrcweir bool RegressionCurveItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     // own items
107cdf0e10cSrcweir     return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
GetWhichPairs() const110cdf0e10cSrcweir const sal_uInt16 * RegressionCurveItemConverter::GetWhichPairs() const
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     // must span all used items!
113cdf0e10cSrcweir     return nRegressionCurveWhichPairs;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
GetItemProperty(tWhichIdType,tPropertyNameWithMemberId &) const116cdf0e10cSrcweir bool RegressionCurveItemConverter::GetItemProperty(
117cdf0e10cSrcweir     tWhichIdType /* nWhichId */, tPropertyNameWithMemberId & /* rOutProperty */ ) const
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     // No own (non-special) properties
120cdf0e10cSrcweir     return false;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 
ApplySpecialItem(sal_uInt16 nWhichId,const SfxItemSet & rItemSet)124cdf0e10cSrcweir bool RegressionCurveItemConverter::ApplySpecialItem(
125cdf0e10cSrcweir     sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
126cdf0e10cSrcweir     throw( uno::Exception )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
129cdf0e10cSrcweir     bool bChanged = false;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     switch( nWhichId )
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         case SCHATTR_REGRESSION_TYPE:
134cdf0e10cSrcweir         {
135cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
136cdf0e10cSrcweir             if( xCurve.is())
137cdf0e10cSrcweir             {
138cdf0e10cSrcweir                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
139cdf0e10cSrcweir                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
140cdf0e10cSrcweir                 SvxChartRegress eNewRegress = static_cast< const SvxChartRegressItem & >(
141cdf0e10cSrcweir                     rItemSet.Get( nWhichId )).GetValue();
142cdf0e10cSrcweir                 if( eRegress != eNewRegress )
143cdf0e10cSrcweir                 {
144cdf0e10cSrcweir                     // note that changing the regression type changes the object
145cdf0e10cSrcweir                     // for which this converter was created. Not optimal, but
146cdf0e10cSrcweir                     // currently the only way to handle the type in the
147cdf0e10cSrcweir                     // regression curve properties dialog
148cdf0e10cSrcweir                     RegressionCurveHelper::replaceOrAddCurveAndReduceToOne(
149cdf0e10cSrcweir                         lcl_convertRegressionType( eNewRegress ), m_xCurveContainer,
150cdf0e10cSrcweir                         uno::Reference< uno::XComponentContext >());
151cdf0e10cSrcweir                     uno::Reference< beans::XPropertySet > xNewPropSet(
152cdf0e10cSrcweir                         RegressionCurveHelper::getFirstCurveNotMeanValueLine( m_xCurveContainer ),
153cdf0e10cSrcweir                         uno::UNO_QUERY );
154cdf0e10cSrcweir                     OSL_ASSERT( xNewPropSet.is());
155cdf0e10cSrcweir                     if( xNewPropSet.is())
156cdf0e10cSrcweir                     {
157cdf0e10cSrcweir                         resetPropertySet( xNewPropSet );
158cdf0e10cSrcweir                         bChanged = true;
159cdf0e10cSrcweir                     }
160cdf0e10cSrcweir                 }
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         break;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_EQUATION:
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
168cdf0e10cSrcweir             if( xCurve.is())
169cdf0e10cSrcweir             {
170cdf0e10cSrcweir                 bool bNewShow = static_cast< sal_Bool >(
171cdf0e10cSrcweir                     static_cast< const SfxBoolItem & >(
172cdf0e10cSrcweir                         rItemSet.Get( nWhichId )).GetValue());
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
175cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
176cdf0e10cSrcweir                 bool bOldShow = false;
177cdf0e10cSrcweir                 if( xEqProp.is() &&
178cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bOldShow) &&
179cdf0e10cSrcweir                     bOldShow != bNewShow )
180cdf0e10cSrcweir                 {
181cdf0e10cSrcweir                     xEqProp->setPropertyValue( C2U( "ShowEquation" ), uno::makeAny( bNewShow ));
182cdf0e10cSrcweir                     bChanged = true;
183cdf0e10cSrcweir                 }
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         break;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_COEFF:
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
191cdf0e10cSrcweir             if( xCurve.is())
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 bool bNewShow = static_cast< sal_Bool >(
194cdf0e10cSrcweir                     static_cast< const SfxBoolItem & >(
195cdf0e10cSrcweir                         rItemSet.Get( nWhichId )).GetValue());
196cdf0e10cSrcweir 
197cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
198cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
199cdf0e10cSrcweir                 bool bOldShow = false;
200cdf0e10cSrcweir                 if( xEqProp.is() &&
201cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bOldShow) &&
202cdf0e10cSrcweir                     bOldShow != bNewShow )
203cdf0e10cSrcweir                 {
204cdf0e10cSrcweir                     xEqProp->setPropertyValue( C2U( "ShowCorrelationCoefficient" ), uno::makeAny( bNewShow ));
205cdf0e10cSrcweir                     bChanged = true;
206cdf0e10cSrcweir                 }
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         break;
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     return bChanged;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
FillSpecialItem(sal_uInt16 nWhichId,SfxItemSet & rOutItemSet) const215cdf0e10cSrcweir void RegressionCurveItemConverter::FillSpecialItem(
216cdf0e10cSrcweir     sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
217cdf0e10cSrcweir     throw( uno::Exception )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir     uno::Reference< chart2::XRegressionCurve > xCurve( GetPropertySet(), uno::UNO_QUERY );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     switch( nWhichId )
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         case SCHATTR_REGRESSION_TYPE:
224cdf0e10cSrcweir         {
225cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
226cdf0e10cSrcweir             if( xCurve.is())
227cdf0e10cSrcweir             {
228cdf0e10cSrcweir                 SvxChartRegress eRegress = static_cast< SvxChartRegress >(
229cdf0e10cSrcweir                     static_cast< sal_Int32 >( RegressionCurveHelper::getRegressionType( xCurve )));
230cdf0e10cSrcweir                 rOutItemSet.Put( SvxChartRegressItem( eRegress, SCHATTR_REGRESSION_TYPE ));
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir         break;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_EQUATION:
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
238cdf0e10cSrcweir             if( xCurve.is())
239cdf0e10cSrcweir             {
240cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
241cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
242cdf0e10cSrcweir                 bool bShow = false;
243cdf0e10cSrcweir                 if( xEqProp.is() &&
244cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowEquation" )) >>= bShow))
245cdf0e10cSrcweir                 {
246cdf0e10cSrcweir                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
247cdf0e10cSrcweir                 }
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir         break;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         case SCHATTR_REGRESSION_SHOW_COEFF:
253cdf0e10cSrcweir         {
254cdf0e10cSrcweir             OSL_ASSERT( xCurve.is());
255cdf0e10cSrcweir             if( xCurve.is())
256cdf0e10cSrcweir             {
257cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties());
258cdf0e10cSrcweir                 OSL_ASSERT( xEqProp.is());
259cdf0e10cSrcweir                 bool bShow = false;
260cdf0e10cSrcweir                 if( xEqProp.is() &&
261cdf0e10cSrcweir                     (xEqProp->getPropertyValue( C2U( "ShowCorrelationCoefficient" )) >>= bShow))
262cdf0e10cSrcweir                 {
263cdf0e10cSrcweir                     rOutItemSet.Put( SfxBoolItem( nWhichId, bShow ));
264cdf0e10cSrcweir                 }
265cdf0e10cSrcweir             }
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir         break;
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir } //  namespace wrapper
272cdf0e10cSrcweir } //  namespace chart
273