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 "WrappedSplineProperties.hxx"
28 #include "macros.hxx"
29 #include "FastPropertyIdRanges.hxx"
30 #include "DiagramHelper.hxx"
31 #include <com/sun/star/chart2/CurveStyle.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 
34 using namespace ::com::sun::star;
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Sequence;
38 using ::com::sun::star::beans::Property;
39 using ::rtl::OUString;
40 
41 //.............................................................................
42 namespace chart
43 {
44 namespace wrapper
45 {
46 
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 
51 //PROPERTYTYPE is the type of the outer property
52 
53 template< typename PROPERTYTYPE >
54 class WrappedSplineProperty : public WrappedProperty
55 {
56 public:
WrappedSplineProperty(const::rtl::OUString & rOuterName,const::rtl::OUString & rInnerName,const::com::sun::star::uno::Any & rDefaulValue,::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)57     explicit WrappedSplineProperty( const ::rtl::OUString& rOuterName, const ::rtl::OUString& rInnerName
58         , const ::com::sun::star::uno::Any& rDefaulValue
59         , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
60             : WrappedProperty(rOuterName,OUString())
61             , m_spChart2ModelContact(spChart2ModelContact)
62             , m_aOuterValue(rDefaulValue)
63             , m_aDefaultValue(rDefaulValue)
64             , m_aOwnInnerName(rInnerName)
65     {
66     }
~WrappedSplineProperty()67     virtual ~WrappedSplineProperty() {};
68 
detectInnerValue(PROPERTYTYPE & rValue,bool & rHasAmbiguousValue) const69     bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
70     {
71         bool bHasDetectableInnerValue = false;
72         rHasAmbiguousValue = false;
73         Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XChartType > > aChartTypes(
74             ::chart::DiagramHelper::getChartTypesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
75         for( sal_Int32 nN = aChartTypes.getLength(); nN--; )
76         {
77             try
78             {
79                 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xChartTypePropertySet( aChartTypes[nN], ::com::sun::star::uno::UNO_QUERY );
80 
81                 Any aSingleValue = this->convertInnerToOuterValue( xChartTypePropertySet->getPropertyValue(m_aOwnInnerName) );
82                 PROPERTYTYPE aCurValue = PROPERTYTYPE();
83                 aSingleValue >>= aCurValue;
84                 if( !bHasDetectableInnerValue )
85                     rValue = aCurValue;
86                 else
87                 {
88                     if( rValue != aCurValue )
89                     {
90                         rHasAmbiguousValue = true;
91                         break;
92                     }
93                     else
94                         rValue = aCurValue;
95                 }
96                 bHasDetectableInnerValue = true;
97             }
98             catch( uno::Exception & ex )
99             {
100                 //spline properties are not supported by all charttypes
101                 //in that cases this exception is ok
102                 ex.Context.is();//to have debug information without compilation warnings
103             }
104         }
105         return bHasDetectableInnerValue;
106     }
setPropertyValue(const::com::sun::star::uno::Any & rOuterValue,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> &) const107     void setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
108                     throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
109     {
110         PROPERTYTYPE aNewValue;
111         if( ! (rOuterValue >>= aNewValue) )
112             throw ::com::sun::star::lang::IllegalArgumentException( C2U("spline property requires different type"), 0, 0 );
113 
114         m_aOuterValue = rOuterValue;
115 
116         bool bHasAmbiguousValue = false;
117         PROPERTYTYPE aOldValue;
118         if( detectInnerValue( aOldValue, bHasAmbiguousValue ) )
119         {
120             if( bHasAmbiguousValue || aNewValue != aOldValue )
121             {
122                 Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XChartType > > aChartTypes(
123                     ::chart::DiagramHelper::getChartTypesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
124                 for( sal_Int32 nN = aChartTypes.getLength(); nN--; )
125                 {
126                     try
127                     {
128                         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xChartTypePropertySet( aChartTypes[nN], ::com::sun::star::uno::UNO_QUERY );
129                         if( xChartTypePropertySet.is() )
130                         {
131                             xChartTypePropertySet->setPropertyValue(m_aOwnInnerName,this->convertOuterToInnerValue(uno::makeAny(aNewValue)));
132                         }
133                     }
134                     catch( uno::Exception & ex )
135                     {
136                         //spline properties are not supported by all charttypes
137                         //in that cases this exception is ok
138                         ex.Context.is();//to have debug information without compilation warnings
139                     }
140                 }
141             }
142         }
143     }
144 
getPropertyValue(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> &) const145     ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
146                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
147     {
148         bool bHasAmbiguousValue = false;
149         PROPERTYTYPE aValue;
150         if( detectInnerValue( aValue, bHasAmbiguousValue ) )
151         {
152             m_aOuterValue <<= aValue;
153         }
154         return m_aOuterValue;
155     }
156 
getPropertyDefault(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertyState> &) const157     ::com::sun::star::uno::Any getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
158                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
159     {
160         return m_aDefaultValue;
161     }
162 
163 protected:
164     ::boost::shared_ptr< Chart2ModelContact >   m_spChart2ModelContact;
165     mutable ::com::sun::star::uno::Any     m_aOuterValue;
166     ::com::sun::star::uno::Any             m_aDefaultValue;
167     // this inner name is not set as inner name at the base class
168     const OUString m_aOwnInnerName;
169 };
170 
171 class WrappedSplineTypeProperty : public WrappedSplineProperty< sal_Int32 >
172 {
173 public:
174     explicit WrappedSplineTypeProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact );
175     virtual ~WrappedSplineTypeProperty();
176 
177     virtual ::com::sun::star::uno::Any convertInnerToOuterValue( const ::com::sun::star::uno::Any& rInnerValue ) const;
178     virtual ::com::sun::star::uno::Any convertOuterToInnerValue( const ::com::sun::star::uno::Any& rOuterValue ) const;
179 };
180 
181 namespace
182 {
183 enum
184 {
185     //spline properties
186       PROP_CHART_SPLINE_TYPE = FAST_PROPERTY_ID_START_CHART_SPLINE_PROP
187     , PROP_CHART_SPLINE_ORDER
188     , PROP_CHART_SPLINE_RESOLUTION
189 };
190 
191 }//anonymous namespace
192 
193 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
addProperties(::std::vector<Property> & rOutProperties)196 void WrappedSplineProperties::addProperties( ::std::vector< Property > & rOutProperties )
197 {
198     rOutProperties.push_back(
199         Property( C2U( "SplineType" ),
200                   PROP_CHART_SPLINE_TYPE,
201                   ::getCppuType( reinterpret_cast< sal_Int32 * >(0)),
202                   beans::PropertyAttribute::BOUND
203                   | beans::PropertyAttribute::MAYBEDEFAULT
204                   | beans::PropertyAttribute::MAYBEVOID ));
205     rOutProperties.push_back(
206         Property( C2U( "SplineOrder" ),
207                   PROP_CHART_SPLINE_ORDER,
208                   ::getCppuType( reinterpret_cast< sal_Int32 * >(0)),
209                   beans::PropertyAttribute::BOUND
210                   | beans::PropertyAttribute::MAYBEDEFAULT
211                   | beans::PropertyAttribute::MAYBEVOID ));
212     rOutProperties.push_back(
213         Property( C2U( "SplineResolution" ),
214                   PROP_CHART_SPLINE_RESOLUTION,
215                   ::getCppuType( reinterpret_cast< sal_Int32 * >(0)),
216                   beans::PropertyAttribute::BOUND
217                   | beans::PropertyAttribute::MAYBEDEFAULT
218                   | beans::PropertyAttribute::MAYBEVOID ));
219 }
220 
221 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
223 
addWrappedProperties(std::vector<WrappedProperty * > & rList,::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)224 void WrappedSplineProperties::addWrappedProperties( std::vector< WrappedProperty* >& rList
225                                     , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
226 {
227     rList.push_back( new WrappedSplineTypeProperty( spChart2ModelContact ) );
228     rList.push_back( new WrappedSplineProperty<sal_Int32>( C2U("SplineOrder"),      C2U("SplineOrder"), uno::makeAny(sal_Int32(3)), spChart2ModelContact ) );
229     rList.push_back( new WrappedSplineProperty<sal_Int32>( C2U("SplineResolution"), C2U("CurveResolution"), uno::makeAny(sal_Int32(20)), spChart2ModelContact ) );
230 }
231 
232 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
235 
236 
WrappedSplineTypeProperty(::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)237 WrappedSplineTypeProperty::WrappedSplineTypeProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
238     : WrappedSplineProperty<sal_Int32>( C2U("SplineType"), C2U("CurveStyle"), uno::makeAny(sal_Int32(0)), spChart2ModelContact )
239 {
240 }
~WrappedSplineTypeProperty()241 WrappedSplineTypeProperty::~WrappedSplineTypeProperty()
242 {
243 }
convertInnerToOuterValue(const Any & rInnerValue) const244 Any WrappedSplineTypeProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
245 {
246     chart2::CurveStyle aInnerValue = chart2::CurveStyle_LINES;
247     rInnerValue >>= aInnerValue;
248 
249     sal_Int32 nOuterValue;
250     if( chart2::CurveStyle_CUBIC_SPLINES == aInnerValue )
251         nOuterValue = 1;
252     else if( chart2::CurveStyle_B_SPLINES == aInnerValue )
253         nOuterValue = 2;
254     else
255         nOuterValue = 0;
256 
257     return uno::makeAny(nOuterValue);
258 }
convertOuterToInnerValue(const Any & rOuterValue) const259 Any WrappedSplineTypeProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
260 {
261     sal_Int32 nOuterValue=0;
262     rOuterValue >>= nOuterValue;
263 
264     chart2::CurveStyle aInnerValue;
265 
266     if(1==nOuterValue)
267         aInnerValue = chart2::CurveStyle_CUBIC_SPLINES;
268     else if(2==nOuterValue)
269         aInnerValue = chart2::CurveStyle_B_SPLINES;
270     else
271         aInnerValue = chart2::CurveStyle_LINES;
272 
273     return uno::makeAny(aInnerValue);
274 }
275 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
278 
279 } //namespace wrapper
280 } //namespace chart
281 //.............................................................................
282