LibreOffice Module chart2 (master) 1
WrappedSplineProperties.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
23#include <ChartType.hxx>
24#include <DiagramHelper.hxx>
25#include <WrappedProperty.hxx>
26#include <unonames.hxx>
27
28#include <sal/log.hxx>
29
30#include <com/sun/star/chart2/CurveStyle.hpp>
31#include <com/sun/star/beans/PropertyAttribute.hpp>
32#include <com/sun/star/beans/XPropertySet.hpp>
33#include <utility>
34
35using namespace ::com::sun::star;
36using ::com::sun::star::uno::Any;
37using ::com::sun::star::beans::Property;
38
39namespace chart::wrapper
40{
41
42namespace
43{
44
45//PROPERTYTYPE is the type of the outer property
46
47template< typename PROPERTYTYPE >
48class WrappedSplineProperty : public WrappedProperty
49{
50public:
51 explicit WrappedSplineProperty( const OUString& rOuterName, OUString aInnerName
52 , const css::uno::Any& rDefaulValue
53 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact )
54 : WrappedProperty(rOuterName,OUString())
55 , m_spChart2ModelContact(std::move(spChart2ModelContact))
56 , m_aOuterValue(rDefaulValue)
57 , m_aDefaultValue(rDefaulValue)
58 , m_aOwnInnerName(std::move(aInnerName))
59 {
60 }
61
62 bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
63 {
64 rHasAmbiguousValue = false;
65 rtl::Reference<Diagram> xDiagram = m_spChart2ModelContact->getDiagram();
66 if (!xDiagram)
67 return false;
68 bool bHasDetectableInnerValue = false;
69 std::vector< rtl::Reference< ChartType > > aChartTypes = xDiagram->getChartTypes();
70 for( sal_Int32 nN = aChartTypes.size(); nN--; )
71 {
72 try
73 {
74 Any aSingleValue = convertInnerToOuterValue( aChartTypes[nN]->getPropertyValue(m_aOwnInnerName) );
75 PROPERTYTYPE aCurValue = PROPERTYTYPE();
76 aSingleValue >>= aCurValue;
77 if( !bHasDetectableInnerValue )
78 rValue = aCurValue;
79 else
80 {
81 if( rValue != aCurValue )
82 {
83 rHasAmbiguousValue = true;
84 break;
85 }
86 else
87 rValue = aCurValue;
88 }
89 bHasDetectableInnerValue = true;
90 }
91 catch( uno::Exception & ex )
92 {
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
96 }
97 }
98 return bHasDetectableInnerValue;
99 }
100 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override
101 {
102 PROPERTYTYPE aNewValue;
103 if( ! (rOuterValue >>= aNewValue) )
104 throw css::lang::IllegalArgumentException( "spline property requires different type", nullptr, 0 );
105
106 m_aOuterValue = rOuterValue;
107
108 bool bHasAmbiguousValue = false;
109 PROPERTYTYPE aOldValue = PROPERTYTYPE();
110 if( !detectInnerValue( aOldValue, bHasAmbiguousValue ) )
111 return;
112
113 if( !(bHasAmbiguousValue || aNewValue != aOldValue) )
114 return;
115
116 std::vector< rtl::Reference< ChartType > > aChartTypes =
117 m_spChart2ModelContact->getDiagram()->getChartTypes();
118 for( sal_Int32 nN = aChartTypes.size(); nN--; )
119 {
120 try
121 {
122 aChartTypes[nN]->setPropertyValue(m_aOwnInnerName,convertOuterToInnerValue(uno::Any(aNewValue)));
123 }
124 catch( uno::Exception & ex )
125 {
126 //spline properties are not supported by all charttypes
127 //in that cases this exception is ok
128 ex.Context.is();//to have debug information without compilation warnings
129 }
130 }
131 }
132
133 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const override
134 {
135 bool bHasAmbiguousValue = false;
136 PROPERTYTYPE aValue;
137 if( detectInnerValue( aValue, bHasAmbiguousValue ) )
138 {
139 m_aOuterValue <<= aValue;
140 }
141 return m_aOuterValue;
142 }
143
144 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const override
145 {
146 return m_aDefaultValue;
147 }
148
149protected:
150 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
151 mutable css::uno::Any m_aOuterValue;
152 css::uno::Any m_aDefaultValue;
153 // this inner name is not set as inner name at the base class
154 const OUString m_aOwnInnerName;
155};
156
157class WrappedSplineTypeProperty : public WrappedSplineProperty< sal_Int32 >
158{
159public:
160 explicit WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
161
162 virtual css::uno::Any convertInnerToOuterValue( const css::uno::Any& rInnerValue ) const override;
163 virtual css::uno::Any convertOuterToInnerValue( const css::uno::Any& rOuterValue ) const override;
164};
165
166enum
167{
168 //spline properties
169 PROP_CHART_SPLINE_TYPE = FAST_PROPERTY_ID_START_CHART_SPLINE_PROP
170 , PROP_CHART_SPLINE_ORDER
171 , PROP_CHART_SPLINE_RESOLUTION
172};
173
174}//anonymous namespace
175
176void WrappedSplineProperties::addProperties( std::vector< Property > & rOutProperties )
177{
178 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_TYPE,
179 PROP_CHART_SPLINE_TYPE,
181 beans::PropertyAttribute::BOUND
182 | beans::PropertyAttribute::MAYBEDEFAULT
183 | beans::PropertyAttribute::MAYBEVOID );
184 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_ORDER,
185 PROP_CHART_SPLINE_ORDER,
187 beans::PropertyAttribute::BOUND
188 | beans::PropertyAttribute::MAYBEDEFAULT
189 | beans::PropertyAttribute::MAYBEVOID );
190 rOutProperties.emplace_back( CHART_UNONAME_SPLINE_RESOLUTION,
191 PROP_CHART_SPLINE_RESOLUTION,
193 beans::PropertyAttribute::BOUND
194 | beans::PropertyAttribute::MAYBEDEFAULT
195 | beans::PropertyAttribute::MAYBEVOID );
196}
197
198void WrappedSplineProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
199 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
200{
201 rList.emplace_back( new WrappedSplineTypeProperty( spChart2ModelContact ) );
202 rList.emplace_back(
203 new WrappedSplineProperty<sal_Int32>(
205 uno::Any(sal_Int32(3)), spChart2ModelContact));
206 rList.emplace_back(
207 new WrappedSplineProperty<sal_Int32>(
209 uno::Any(sal_Int32(20)), spChart2ModelContact));
210}
211
212WrappedSplineTypeProperty::WrappedSplineTypeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
213 : WrappedSplineProperty<sal_Int32>(CHART_UNONAME_SPLINE_TYPE, CHART_UNONAME_CURVE_STYLE, uno::Any(sal_Int32(0)), spChart2ModelContact )
214{
215}
216
217Any WrappedSplineTypeProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
218{
219 chart2::CurveStyle aInnerValue = chart2::CurveStyle_LINES;
220 rInnerValue >>= aInnerValue;
221
222 sal_Int32 nOuterValue;
223 switch (aInnerValue)
224 {
225 case chart2::CurveStyle_CUBIC_SPLINES:
226 nOuterValue = 1;
227 break;
228 case chart2::CurveStyle_B_SPLINES:
229 nOuterValue = 2;
230 break;
231 case chart2::CurveStyle_STEP_START:
232 nOuterValue = 3;
233 break;
234 case chart2::CurveStyle_STEP_END:
235 nOuterValue = 4;
236 break;
237 case chart2::CurveStyle_STEP_CENTER_X:
238 nOuterValue = 5;
239 break;
240 case chart2::CurveStyle_STEP_CENTER_Y:
241 nOuterValue = 6;
242 break;
243 default:
244 nOuterValue = 0;
245 }
246
247 return uno::Any(nOuterValue);
248}
249Any WrappedSplineTypeProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
250{
251 sal_Int32 nOuterValue=0;
252 rOuterValue >>= nOuterValue;
253
254 chart2::CurveStyle aInnerValue;
255
256 switch (nOuterValue)
257 {
258 case 1:
259 aInnerValue = chart2::CurveStyle_CUBIC_SPLINES;
260 break;
261 case 2:
262 aInnerValue = chart2::CurveStyle_B_SPLINES;
263 break;
264 case 3:
265 aInnerValue = chart2::CurveStyle_STEP_START;
266 break;
267 case 4:
268 aInnerValue = chart2::CurveStyle_STEP_END;
269 break;
270 case 5:
271 aInnerValue = chart2::CurveStyle_STEP_CENTER_X;
272 break;
273 case 6:
274 aInnerValue = chart2::CurveStyle_STEP_CENTER_Y;
275 break;
276 default:
277 SAL_WARN_IF(nOuterValue != 0, "chart2", "Unknown line style");
278 aInnerValue = chart2::CurveStyle_LINES;
279 }
280
281 return uno::Any(aInnerValue);
282}
283
284} //namespace chart::wrapper
285
286/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString m_aOwnInnerName
std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact
css::uno::Any m_aOuterValue
css::uno::Any m_aDefaultValue
static void addProperties(std::vector< css::beans::Property > &rOutProperties)
static void addWrappedProperties(std::vector< std::unique_ptr< WrappedProperty > > &rList, const std::shared_ptr< Chart2ModelContact > &spChart2ModelContact)
#define SAL_WARN_IF(condition, area, stream)
void setPropertyValue(tPropertyValueMap &rOutMap, tPropertyValueMapKey key, const Value &value)
Set a property to a certain value in the given map.
@ FAST_PROPERTY_ID_START_CHART_SPLINE_PROP
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
constexpr OUStringLiteral CHART_UNONAME_SPLINE_RESOLUTION
Definition: unonames.hxx:17
constexpr OUStringLiteral CHART_UNONAME_SPLINE_ORDER
Definition: unonames.hxx:16
constexpr OUStringLiteral CHART_UNONAME_CURVE_STYLE
Definition: unonames.hxx:18
constexpr OUStringLiteral CHART_UNONAME_CURVE_RESOLUTION
Definition: unonames.hxx:19
constexpr OUStringLiteral CHART_UNONAME_SPLINE_TYPE
Definition: unonames.hxx:15