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/LegendWrapper.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 "LegendWrapper.hxx"
21
#include "Chart2ModelContact.hxx"
22
#include <comphelper/sequence.hxx>
23
#include <cppuhelper/supportsservice.hxx>
24
#include <com/sun/star/beans/PropertyAttribute.hpp>
25
#include <com/sun/star/chart/ChartLegendPosition.hpp>
26
#include <com/sun/star/chart2/LegendPosition.hpp>
27
#include <com/sun/star/chart/ChartLegendExpansion.hpp>
28
#include <com/sun/star/chart2/RelativePosition.hpp>
29
30
#include <CharacterProperties.hxx>
31
#include <LinePropertiesHelper.hxx>
32
#include <FillProperties.hxx>
33
#include <UserDefinedProperties.hxx>
34
#include "WrappedCharacterHeightProperty.hxx"
35
#include <PositionAndSizeHelper.hxx>
36
#include <WrappedDirectStateProperty.hxx>
37
#include "WrappedAutomaticPositionProperties.hxx"
38
#include "WrappedScaleTextProperties.hxx"
39
40
#include <algorithm>
41
#include <utility>
42
43
using namespace ::com::sun::star;
44
using ::com::sun::star::beans::Property;
45
using ::com::sun::star::uno::Any;
46
using ::com::sun::star::uno::Reference;
47
using ::com::sun::star::uno::Sequence;
48
49
namespace chart
50
{
51
namespace {
52
53
class WrappedLegendAlignmentProperty : public WrappedProperty
54
{
55
public:
56
    WrappedLegendAlignmentProperty();
57
58
    virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
59
    virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
60
61
protected:
62
    virtual Any convertInnerToOuterValue( const Any& rInnerValue ) const override;
63
    virtual Any convertOuterToInnerValue( const Any& rOuterValue ) const override;
64
};
65
66
}
67
68
WrappedLegendAlignmentProperty::WrappedLegendAlignmentProperty()
69
0
    : ::chart::WrappedProperty( u"Alignment"_ustr, u"AnchorPosition"_ustr )
70
0
{
71
0
}
72
73
Any WrappedLegendAlignmentProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
74
0
{
75
0
    Any aRet;
76
0
    if( xInnerPropertySet.is() )
77
0
    {
78
0
        bool bShowLegend = true;
79
0
        xInnerPropertySet->getPropertyValue( u"Show"_ustr ) >>= bShowLegend;
80
0
        if(!bShowLegend)
81
0
        {
82
0
            aRet <<= css::chart::ChartLegendPosition_NONE;
83
0
        }
84
0
        else
85
0
        {
86
0
            aRet = xInnerPropertySet->getPropertyValue( m_aInnerName );
87
0
            aRet = convertInnerToOuterValue( aRet );
88
0
        }
89
0
    }
90
0
    return aRet;
91
0
}
92
93
void WrappedLegendAlignmentProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
94
0
{
95
0
    if(!xInnerPropertySet.is())
96
0
        return;
97
98
0
    bool bNewShowLegend = true;
99
0
    bool bOldShowLegend = true;
100
0
    {
101
0
        css::chart::ChartLegendPosition eOuterPos(css::chart::ChartLegendPosition_NONE);
102
0
        if( (rOuterValue >>= eOuterPos)  && eOuterPos == css::chart::ChartLegendPosition_NONE )
103
0
            bNewShowLegend = false;
104
0
        xInnerPropertySet->getPropertyValue( u"Show"_ustr ) >>= bOldShowLegend;
105
0
    }
106
0
    if(bNewShowLegend!=bOldShowLegend)
107
0
    {
108
0
        xInnerPropertySet->setPropertyValue( u"Show"_ustr, uno::Any(bNewShowLegend) );
109
0
    }
110
0
    if(!bNewShowLegend)
111
0
        return;
112
113
    //set corresponding LegendPosition
114
0
    Any aInnerValue = convertOuterToInnerValue( rOuterValue );
115
0
    xInnerPropertySet->setPropertyValue( m_aInnerName, aInnerValue );
116
117
    //correct LegendExpansion
118
0
    chart2::LegendPosition eNewInnerPos(chart2::LegendPosition_LINE_END);
119
0
    if( aInnerValue >>= eNewInnerPos )
120
0
    {
121
0
        css::chart::ChartLegendExpansion eNewExpansion =
122
0
            ( eNewInnerPos == chart2::LegendPosition_LINE_END ||
123
0
              eNewInnerPos == chart2::LegendPosition_LINE_START )
124
0
            ? css::chart::ChartLegendExpansion_HIGH
125
0
            : css::chart::ChartLegendExpansion_WIDE;
126
127
0
        css::chart::ChartLegendExpansion eOldExpansion( css::chart::ChartLegendExpansion_HIGH );
128
0
        bool bExpansionWasSet(
129
0
            xInnerPropertySet->getPropertyValue( u"Expansion"_ustr ) >>= eOldExpansion );
130
131
0
        if( !bExpansionWasSet || (eOldExpansion != eNewExpansion))
132
0
            xInnerPropertySet->setPropertyValue( u"Expansion"_ustr, uno::Any( eNewExpansion ));
133
0
    }
134
135
    //correct RelativePosition
136
0
    Any aRelativePosition( xInnerPropertySet->getPropertyValue(u"RelativePosition"_ustr) );
137
0
    if(aRelativePosition.hasValue())
138
0
    {
139
0
        xInnerPropertySet->setPropertyValue( u"RelativePosition"_ustr, Any() );
140
0
    }
141
0
}
142
143
Any WrappedLegendAlignmentProperty::convertInnerToOuterValue( const Any& rInnerValue ) const
144
0
{
145
0
    css::chart::ChartLegendPosition ePos = css::chart::ChartLegendPosition_NONE;
146
147
0
    chart2::LegendPosition eNewPos;
148
0
    if( rInnerValue >>= eNewPos )
149
0
    {
150
0
        switch( eNewPos )
151
0
        {
152
0
            case chart2::LegendPosition_LINE_START:
153
0
                ePos = css::chart::ChartLegendPosition_LEFT;
154
0
                break;
155
0
            case chart2::LegendPosition_LINE_END:
156
0
                ePos = css::chart::ChartLegendPosition_RIGHT;
157
0
                break;
158
0
            case chart2::LegendPosition_PAGE_START:
159
0
                ePos = css::chart::ChartLegendPosition_TOP;
160
0
                break;
161
0
            case chart2::LegendPosition_PAGE_END:
162
0
                ePos = css::chart::ChartLegendPosition_BOTTOM;
163
0
                break;
164
165
0
            default:
166
0
                ePos = css::chart::ChartLegendPosition_NONE;
167
0
                break;
168
0
        }
169
0
    }
170
0
    return uno::Any( ePos );
171
0
}
172
Any WrappedLegendAlignmentProperty::convertOuterToInnerValue( const Any& rOuterValue ) const
173
0
{
174
0
    chart2::LegendPosition eNewPos = chart2::LegendPosition_LINE_END;
175
176
0
    css::chart::ChartLegendPosition ePos;
177
0
    if( rOuterValue >>= ePos )
178
0
    {
179
0
        switch( ePos )
180
0
        {
181
0
            case css::chart::ChartLegendPosition_LEFT:
182
0
                eNewPos = chart2::LegendPosition_LINE_START;
183
0
                break;
184
0
            case css::chart::ChartLegendPosition_RIGHT:
185
0
                eNewPos = chart2::LegendPosition_LINE_END;
186
0
                break;
187
0
            case css::chart::ChartLegendPosition_TOP:
188
0
                eNewPos = chart2::LegendPosition_PAGE_START;
189
0
                break;
190
0
            case css::chart::ChartLegendPosition_BOTTOM:
191
0
                eNewPos = chart2::LegendPosition_PAGE_END;
192
0
                break;
193
0
            default: // NONE
194
0
                break;
195
0
        }
196
0
    }
197
198
0
    return uno::Any( eNewPos );
199
0
}
200
}
201
202
namespace
203
{
204
205
enum
206
{
207
    PROP_LEGEND_ALIGNMENT,
208
    PROP_LEGEND_EXPANSION
209
};
210
211
void lcl_AddPropertiesToVector(
212
    std::vector< Property > & rOutProperties )
213
0
{
214
0
    rOutProperties.emplace_back( "Alignment",
215
0
                  PROP_LEGEND_ALIGNMENT,
216
0
                  cppu::UnoType<css::chart::ChartLegendPosition>::get(),
217
                  //#i111967# no PropertyChangeEvent is fired on change so far
218
0
                  beans::PropertyAttribute::MAYBEDEFAULT );
219
220
0
    rOutProperties.emplace_back( "Expansion",
221
0
                  PROP_LEGEND_EXPANSION,
222
0
                  cppu::UnoType<css::chart::ChartLegendExpansion>::get(),
223
                  //#i111967# no PropertyChangeEvent is fired on change so far
224
0
                  beans::PropertyAttribute::MAYBEDEFAULT );
225
0
}
226
227
const Sequence< Property >& StaticLegendWrapperPropertyArray()
228
0
{
229
0
    static Sequence< Property > aPropSeq = []()
230
0
        {
231
0
            std::vector< css::beans::Property > aProperties;
232
0
            lcl_AddPropertiesToVector( aProperties );
233
0
            ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
234
0
            ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
235
0
            ::chart::FillProperties::AddPropertiesToVector( aProperties );
236
0
            ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
237
0
            ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
238
0
            ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
239
240
0
            std::sort( aProperties.begin(), aProperties.end(),
241
0
                         ::chart::PropertyNameLess() );
242
243
0
            return comphelper::containerToSequence( aProperties );
244
0
        }();
245
0
    return aPropSeq;
246
0
};
247
248
} // anonymous namespace
249
250
namespace chart::wrapper
251
{
252
253
LegendWrapper::LegendWrapper(std::shared_ptr<Chart2ModelContact> spChart2ModelContact)
254
0
    : m_spChart2ModelContact(std::move(spChart2ModelContact))
255
0
{
256
0
}
257
258
LegendWrapper::~LegendWrapper()
259
0
{
260
0
}
261
262
// ____ XShape ____
263
awt::Point SAL_CALL LegendWrapper::getPosition()
264
0
{
265
0
    return m_spChart2ModelContact->GetLegendPosition();
266
0
}
267
268
void SAL_CALL LegendWrapper::setPosition( const awt::Point& aPosition )
269
0
{
270
0
    Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
271
0
    if( xProp.is() )
272
0
    {
273
0
        awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
274
275
0
        chart2::RelativePosition aRelativePosition;
276
0
        aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
277
0
        aRelativePosition.Primary = aPageSize.Width == 0 ? 0 : double(aPosition.X)/double(aPageSize.Width);
278
0
        aRelativePosition.Secondary = aPageSize.Height == 0 ? 0 : double(aPosition.Y)/double(aPageSize.Height);
279
0
        xProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
280
0
    }
281
0
}
282
283
awt::Size SAL_CALL LegendWrapper::getSize()
284
0
{
285
0
    return m_spChart2ModelContact->GetLegendSize();
286
0
}
287
288
void SAL_CALL LegendWrapper::setSize( const awt::Size& aSize )
289
0
{
290
0
    Reference< beans::XPropertySet > xProp( getInnerPropertySet() );
291
0
    if( xProp.is() )
292
0
    {
293
0
        awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
294
0
        awt::Rectangle aPageRectangle( 0,0,aPageSize.Width,aPageSize.Height);
295
296
0
        awt::Point aPos( getPosition() );
297
0
        awt::Rectangle aNewPositionAndSize(aPos.X,aPos.Y,aSize.Width,aSize.Height);
298
299
0
        PositionAndSizeHelper::moveObject( OBJECTTYPE_LEGEND
300
0
                , xProp, aNewPositionAndSize, awt::Rectangle(), aPageRectangle );
301
0
    }
302
0
}
303
304
// ____ XShapeDescriptor (base of XShape) ____
305
OUString SAL_CALL LegendWrapper::getShapeType()
306
0
{
307
0
    return u"com.sun.star.chart.ChartLegend"_ustr;
308
0
}
309
310
// ____ XComponent ____
311
void SAL_CALL LegendWrapper::dispose()
312
0
{
313
0
    std::unique_lock g(m_aMutex);
314
0
    Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
315
0
    m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
316
317
0
    clearWrappedPropertySet();
318
0
}
319
320
void SAL_CALL LegendWrapper::addEventListener(
321
    const Reference< lang::XEventListener >& xListener )
322
0
{
323
0
    std::unique_lock g(m_aMutex);
324
0
    m_aEventListenerContainer.addInterface( g, xListener );
325
0
}
326
327
void SAL_CALL LegendWrapper::removeEventListener(
328
    const Reference< lang::XEventListener >& aListener )
329
0
{
330
0
    std::unique_lock g(m_aMutex);
331
0
    m_aEventListenerContainer.removeInterface( g, aListener );
332
0
}
333
334
//ReferenceSizePropertyProvider
335
void LegendWrapper::updateReferenceSize()
336
0
{
337
0
    Reference< beans::XPropertySet > xProp = getInnerPropertySet();
338
0
    if( xProp.is() )
339
0
    {
340
0
        if( xProp->getPropertyValue( u"ReferencePageSize"_ustr ).hasValue() )
341
0
            xProp->setPropertyValue( u"ReferencePageSize"_ustr, uno::Any(
342
0
                m_spChart2ModelContact->GetPageSize() ));
343
0
    }
344
0
}
345
Any LegendWrapper::getReferenceSize()
346
0
{
347
0
    Any aRet;
348
0
    Reference< beans::XPropertySet > xProp = getInnerPropertySet();
349
0
    if( xProp.is() )
350
0
        aRet = xProp->getPropertyValue( u"ReferencePageSize"_ustr );
351
352
0
    return aRet;
353
0
}
354
awt::Size LegendWrapper::getCurrentSizeForReference()
355
0
{
356
0
    return m_spChart2ModelContact->GetPageSize();
357
0
}
358
359
// WrappedPropertySet
360
Reference< beans::XPropertySet > LegendWrapper::getInnerPropertySet()
361
0
{
362
0
    Reference< beans::XPropertySet > xRet;
363
0
    rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
364
0
    if( xDiagram.is() )
365
0
        xRet.set( xDiagram->getLegend(), uno::UNO_QUERY );
366
0
    OSL_ENSURE(xRet.is(),"LegendWrapper::getInnerPropertySet() is NULL");
367
0
    return xRet;
368
0
}
369
370
const Sequence< beans::Property >& LegendWrapper::getPropertySequence()
371
0
{
372
0
    return StaticLegendWrapperPropertyArray();
373
0
}
374
375
std::vector< std::unique_ptr<WrappedProperty> > LegendWrapper::createWrappedProperties()
376
0
{
377
0
    std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
378
379
0
    aWrappedProperties.emplace_back( new WrappedLegendAlignmentProperty() );
380
0
    aWrappedProperties.emplace_back( new WrappedProperty( u"Expansion"_ustr, u"Expansion"_ustr));
381
0
    WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
382
    //same problem as for wall: the defaults in the old chart are different for different charttypes, so we need to export explicitly
383
0
    aWrappedProperties.emplace_back( new WrappedDirectStateProperty(u"FillStyle"_ustr, u"FillStyle"_ustr));
384
0
    aWrappedProperties.emplace_back( new WrappedDirectStateProperty(u"FillColor"_ustr, u"FillColor"_ustr));
385
0
    WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
386
0
    WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
387
388
0
    return aWrappedProperties;
389
0
}
390
391
OUString SAL_CALL LegendWrapper::getImplementationName()
392
0
{
393
0
    return u"com.sun.star.comp.chart.Legend"_ustr;
394
0
}
395
396
sal_Bool SAL_CALL LegendWrapper::supportsService( const OUString& rServiceName )
397
0
{
398
0
    return cppu::supportsService(this, rServiceName);
399
0
}
400
401
css::uno::Sequence< OUString > SAL_CALL LegendWrapper::getSupportedServiceNames()
402
0
{
403
0
    return {
404
0
        u"com.sun.star.chart.ChartLegend"_ustr,
405
0
        u"com.sun.star.drawing.Shape"_ustr,
406
0
        u"com.sun.star.xml.UserDefinedAttributesSupplier"_ustr,
407
0
        u"com.sun.star.style.CharacterProperties"_ustr
408
0
    };
409
0
}
410
411
} //  namespace chart::wrapper
412
413
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */