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