Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / WrappedSplineProperties.cxx
blobae114a30ea64984d43754a3a4cf5fcbb75786598
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "WrappedSplineProperties.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <FastPropertyIdRanges.hxx>
23 #include <DiagramHelper.hxx>
24 #include <WrappedProperty.hxx>
25 #include <unonames.hxx>
27 #include <sal/log.hxx>
29 #include <com/sun/star/chart2/CurveStyle.hpp>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
33 using namespace ::com::sun::star;
34 using ::com::sun::star::uno::Any;
35 using ::com::sun::star::uno::Sequence;
36 using ::com::sun::star::beans::Property;
38 namespace chart
40 namespace wrapper
43 //PROPERTYTYPE is the type of the outer property
45 template< typename PROPERTYTYPE >
46 class WrappedSplineProperty : public WrappedProperty
48 public:
49 explicit WrappedSplineProperty( const OUString& rOuterName, const OUString& rInnerName
50 , const css::uno::Any& rDefaulValue
51 , const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact )
52 : WrappedProperty(rOuterName,OUString())
53 , m_spChart2ModelContact(spChart2ModelContact)
54 , m_aOuterValue(rDefaulValue)
55 , m_aDefaultValue(rDefaulValue)
56 , m_aOwnInnerName(rInnerName)
60 bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
62 bool bHasDetectableInnerValue = false;
63 rHasAmbiguousValue = false;
64 Sequence< css::uno::Reference< css::chart2::XChartType > > aChartTypes(
65 ::chart::DiagramHelper::getChartTypesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
66 for( sal_Int32 nN = aChartTypes.getLength(); nN--; )
68 try
70 uno::Reference<beans::XPropertySet> xChartTypePropertySet(aChartTypes[nN], uno::UNO_QUERY);
71 if (!xChartTypePropertySet.is())
72 continue;
74 Any aSingleValue = convertInnerToOuterValue( xChartTypePropertySet->getPropertyValue(m_aOwnInnerName) );
75 PROPERTYTYPE aCurValue = PROPERTYTYPE();
76 aSingleValue >>= aCurValue;
77 if( !bHasDetectableInnerValue )
78 rValue = aCurValue;
79 else
81 if( rValue != aCurValue )
83 rHasAmbiguousValue = true;
84 break;
86 else
87 rValue = aCurValue;
89 bHasDetectableInnerValue = true;
91 catch( uno::Exception & ex )
93 //spline properties are not supported by all charttypes
94 //in that cases this exception is ok
95 ex.Context.is();//to have debug information without compilation warnings
98 return bHasDetectableInnerValue;
100 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override
102 PROPERTYTYPE aNewValue;
103 if( ! (rOuterValue >>= aNewValue) )
104 throw css::lang::IllegalArgumentException( "spline property requires different type", nullptr, 0 );
106 m_aOuterValue = rOuterValue;
108 bool bHasAmbiguousValue = false;
109 PROPERTYTYPE aOldValue = PROPERTYTYPE();
110 if( detectInnerValue( aOldValue, bHasAmbiguousValue ) )
112 if( bHasAmbiguousValue || aNewValue != aOldValue )
114 Sequence< css::uno::Reference< css::chart2::XChartType > > aChartTypes(
115 ::chart::DiagramHelper::getChartTypesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
116 for( sal_Int32 nN = aChartTypes.getLength(); nN--; )
120 css::uno::Reference< css::beans::XPropertySet > xChartTypePropertySet( aChartTypes[nN], css::uno::UNO_QUERY );
121 if( xChartTypePropertySet.is() )
123 xChartTypePropertySet->setPropertyValue(m_aOwnInnerName,convertOuterToInnerValue(uno::Any(aNewValue)));
126 catch( uno::Exception & ex )
128 //spline properties are not supported by all charttypes
129 //in that cases this exception is ok
130 ex.Context.is();//to have debug information without compilation warnings
137 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override
139 bool bHasAmbiguousValue = false;
140 PROPERTYTYPE aValue;
141 if( detectInnerValue( aValue, bHasAmbiguousValue ) )
143 m_aOuterValue <<= aValue;
145 return m_aOuterValue;
148 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const override
150 return m_aDefaultValue;
153 protected:
154 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
155 mutable css::uno::Any m_aOuterValue;
156 css::uno::Any m_aDefaultValue;
157 // this inner name is not set as inner name at the base class
158 const OUString m_aOwnInnerName;
161 class WrappedSplineTypeProperty : public WrappedSplineProperty< sal_Int32 >
163 public:
164 explicit WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
166 virtual css::uno::Any convertInnerToOuterValue( const css::uno::Any& rInnerValue ) const override;
167 virtual css::uno::Any convertOuterToInnerValue( const css::uno::Any& rOuterValue ) const override;
170 namespace
172 enum
174 //spline properties
175 PROP_CHART_SPLINE_TYPE = FAST_PROPERTY_ID_START_CHART_SPLINE_PROP
176 , PROP_CHART_SPLINE_ORDER
177 , PROP_CHART_SPLINE_RESOLUTION
180 }//anonymous namespace
182 void WrappedSplineProperties::addProperties( std::vector< Property > & rOutProperties )
184 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_TYPE,
185 PROP_CHART_SPLINE_TYPE,
186 cppu::UnoType<sal_Int32>::get(),
187 beans::PropertyAttribute::BOUND
188 | beans::PropertyAttribute::MAYBEDEFAULT
189 | beans::PropertyAttribute::MAYBEVOID );
190 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_ORDER,
191 PROP_CHART_SPLINE_ORDER,
192 cppu::UnoType<sal_Int32>::get(),
193 beans::PropertyAttribute::BOUND
194 | beans::PropertyAttribute::MAYBEDEFAULT
195 | beans::PropertyAttribute::MAYBEVOID );
196 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_RESOLUTION,
197 PROP_CHART_SPLINE_RESOLUTION,
198 cppu::UnoType<sal_Int32>::get(),
199 beans::PropertyAttribute::BOUND
200 | beans::PropertyAttribute::MAYBEDEFAULT
201 | beans::PropertyAttribute::MAYBEVOID );
204 void WrappedSplineProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
205 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
207 rList.emplace_back( new WrappedSplineTypeProperty( spChart2ModelContact ) );
208 rList.emplace_back(
209 new WrappedSplineProperty<sal_Int32>(
210 CHART_UNONAME_SPLINE_ORDER, CHART_UNONAME_SPLINE_ORDER,
211 uno::Any(sal_Int32(3)), spChart2ModelContact));
212 rList.emplace_back(
213 new WrappedSplineProperty<sal_Int32>(
214 CHART_UNONAME_SPLINE_RESOLUTION, CHART_UNONAME_CURVE_RESOLUTION,
215 uno::Any(sal_Int32(20)), spChart2ModelContact));
218 WrappedSplineTypeProperty::WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
219 : WrappedSplineProperty<sal_Int32>(CHART_UNONAME_SPLINE_TYPE, CHART_UNONAME_CURVE_STYLE, uno::Any(sal_Int32(0)), spChart2ModelContact )
223 Any WrappedSplineTypeProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
225 chart2::CurveStyle aInnerValue = chart2::CurveStyle_LINES;
226 rInnerValue >>= aInnerValue;
228 sal_Int32 nOuterValue;
229 switch (aInnerValue)
231 case chart2::CurveStyle_CUBIC_SPLINES:
232 nOuterValue = 1;
233 break;
234 case chart2::CurveStyle_B_SPLINES:
235 nOuterValue = 2;
236 break;
237 case chart2::CurveStyle_STEP_START:
238 nOuterValue = 3;
239 break;
240 case chart2::CurveStyle_STEP_END:
241 nOuterValue = 4;
242 break;
243 case chart2::CurveStyle_STEP_CENTER_X:
244 nOuterValue = 5;
245 break;
246 case chart2::CurveStyle_STEP_CENTER_Y:
247 nOuterValue = 6;
248 break;
249 default:
250 nOuterValue = 0;
253 return uno::Any(nOuterValue);
255 Any WrappedSplineTypeProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
257 sal_Int32 nOuterValue=0;
258 rOuterValue >>= nOuterValue;
260 chart2::CurveStyle aInnerValue;
262 switch (nOuterValue)
264 case 1:
265 aInnerValue = chart2::CurveStyle_CUBIC_SPLINES;
266 break;
267 case 2:
268 aInnerValue = chart2::CurveStyle_B_SPLINES;
269 break;
270 case 3:
271 aInnerValue = chart2::CurveStyle_STEP_START;
272 break;
273 case 4:
274 aInnerValue = chart2::CurveStyle_STEP_END;
275 break;
276 case 5:
277 aInnerValue = chart2::CurveStyle_STEP_CENTER_X;
278 break;
279 case 6:
280 aInnerValue = chart2::CurveStyle_STEP_CENTER_Y;
281 break;
282 default:
283 SAL_WARN_IF(nOuterValue != 0, "chart2", "Unknown line style");
284 aInnerValue = chart2::CurveStyle_LINES;
287 return uno::Any(aInnerValue);
290 } //namespace wrapper
291 } //namespace chart
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */