Coverage Report

Created: 2025-12-31 10:39

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