Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/chart/SchXMLLegendContext.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 "SchXMLLegendContext.hxx"
21
#include "SchXMLEnumConverter.hxx"
22
23
#include <xmloff/xmlimp.hxx>
24
#include <xmloff/xmlnamespace.hxx>
25
#include <xmloff/xmluconv.hxx>
26
27
#include <sal/log.hxx>
28
29
#include <com/sun/star/chart/ChartLegendExpansion.hpp>
30
#include <com/sun/star/chart/XChartDocument.hpp>
31
#include <com/sun/star/drawing/FillStyle.hpp>
32
33
using namespace ::xmloff::token;
34
using namespace com::sun::star;
35
36
SchXMLLegendContext::SchXMLLegendContext( SchXMLImportHelper& rImpHelper, SvXMLImport& rImport ) :
37
0
    SvXMLImportContext( rImport ),
38
0
    mrImportHelper( rImpHelper )
39
0
{
40
0
}
41
42
void SchXMLLegendContext::startFastElement( sal_Int32 /*nElement*/,
43
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
44
0
{
45
0
    uno::Reference< chart::XChartDocument > xDoc = mrImportHelper.GetChartDocument();
46
0
    if( !xDoc.is() )
47
0
        return;
48
49
    // turn on legend
50
0
    uno::Reference< beans::XPropertySet > xDocProp( xDoc, uno::UNO_QUERY );
51
0
    if( xDocProp.is() )
52
0
    {
53
0
        try
54
0
        {
55
0
            xDocProp->setPropertyValue(u"HasLegend"_ustr, uno::Any( true ) );
56
0
        }
57
0
        catch(const beans::UnknownPropertyException&)
58
0
        {
59
0
            SAL_INFO("xmloff.chart", "Property HasLegend not found" );
60
0
        }
61
0
    }
62
63
0
    uno::Reference< drawing::XShape > xLegendShape = xDoc->getLegend();
64
0
    uno::Reference< beans::XPropertySet > xLegendProps( xLegendShape, uno::UNO_QUERY );
65
0
    if( !xLegendShape.is() || !xLegendProps.is() )
66
0
    {
67
0
        SAL_INFO("xmloff.chart", "legend could not be created" );
68
0
        return;
69
0
    }
70
71
    // parse attributes
72
0
    awt::Point aLegendPos;
73
0
    bool bOverlay = false;
74
0
    bool bHasXPosition=false;
75
0
    bool bHasYPosition=false;
76
0
    awt::Size aLegendSize;
77
0
    bool bHasWidth=false;
78
0
    bool bHasHeight=false;
79
0
    chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
80
0
    bool bHasExpansion=false;
81
82
0
    OUString sAutoStyleName;
83
0
    uno::Any aAny;
84
85
0
    for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
86
0
    {
87
0
        switch(aIter.getToken())
88
0
        {
89
0
            case XML_ELEMENT(CHART, XML_LEGEND_POSITION):
90
0
                try
91
0
                {
92
0
                    if( SchXMLEnumConverter::getLegendPositionConverter().importXML( aIter.toString(), aAny, GetImport().GetMM100UnitConverter() ) )
93
0
                        xLegendProps->setPropertyValue(u"Alignment"_ustr, aAny );
94
0
                }
95
0
                catch(const beans::UnknownPropertyException&)
96
0
                {
97
0
                    SAL_INFO("xmloff.chart", "Property Alignment (legend) not found" );
98
0
                }
99
0
                break;
100
0
            case  XML_ELEMENT(LO_EXT, XML_OVERLAY):
101
0
                try
102
0
                {
103
0
                    bOverlay = aIter.toBoolean();
104
0
                    xLegendProps->setPropertyValue(u"Overlay"_ustr, uno::Any(bOverlay));
105
0
                }
106
0
                catch(const beans::UnknownPropertyException&)
107
0
                {
108
0
                    SAL_INFO("xmloff.chart", "Property Overlay (legend) not found" );
109
0
                }
110
0
                break;
111
0
            case XML_ELEMENT(SVG, XML_X):
112
0
            case XML_ELEMENT(SVG_COMPAT, XML_X):
113
0
                GetImport().GetMM100UnitConverter().convertMeasureToCore(
114
0
                        aLegendPos.X, aIter.toView() );
115
0
                bHasXPosition = true;
116
0
                break;
117
0
            case XML_ELEMENT(SVG, XML_Y):
118
0
            case XML_ELEMENT(SVG_COMPAT, XML_Y):
119
0
                GetImport().GetMM100UnitConverter().convertMeasureToCore(
120
0
                        aLegendPos.Y, aIter.toView() );
121
0
                bHasYPosition = true;
122
0
                break;
123
0
            case XML_ELEMENT(CHART, XML_STYLE_NAME):
124
0
                sAutoStyleName = aIter.toString();
125
0
                break;
126
0
            case  XML_ELEMENT(STYLE, XML_LEGEND_EXPANSION):
127
0
                SchXMLEnumConverter::getLegendPositionConverter().importXML( aIter.toString(), aAny, GetImport().GetMM100UnitConverter() );
128
0
                bHasExpansion = (aAny>>=nLegendExpansion);
129
0
                break;
130
0
            case XML_ELEMENT(STYLE, XML_LEGEND_EXPANSION_ASPECT_RATIO):
131
0
                break;
132
0
            case XML_ELEMENT(SVG, XML_WIDTH):
133
0
            case XML_ELEMENT(SVG_COMPAT, XML_WIDTH):
134
0
            case XML_ELEMENT(CHART_EXT, XML_WIDTH):
135
0
                GetImport().GetMM100UnitConverter().convertMeasureToCore(
136
0
                        aLegendSize.Width, aIter.toView() );
137
0
                bHasWidth = true;
138
0
                break;
139
0
            case XML_ELEMENT(SVG, XML_HEIGHT):
140
0
            case XML_ELEMENT(SVG_COMPAT, XML_HEIGHT):
141
0
            case XML_ELEMENT(CHART_EXT, XML_HEIGHT):
142
0
                GetImport().GetMM100UnitConverter().convertMeasureToCore(
143
0
                        aLegendSize.Height, aIter.toView() );
144
0
                bHasHeight = true;
145
0
                break;
146
0
            default:
147
0
                XMLOFF_WARN_UNKNOWN("xmloff", aIter);
148
0
                break;
149
0
        }
150
0
    }
151
152
0
    if( bHasExpansion && nLegendExpansion!= chart::ChartLegendExpansion_CUSTOM )
153
0
        xLegendProps->setPropertyValue(u"Expansion"_ustr, uno::Any(nLegendExpansion) );
154
0
    else if( bHasHeight && bHasWidth )
155
0
        xLegendShape->setSize( aLegendSize );
156
157
0
    if( bHasXPosition && bHasYPosition )
158
0
        xLegendShape->setPosition( aLegendPos );
159
160
    // the fill style has the default "none" in XML, but "solid" in the model.
161
0
    xLegendProps->setPropertyValue(u"FillStyle"_ustr, uno::Any( drawing::FillStyle_NONE ));
162
163
    // set auto-styles for Legend
164
0
    mrImportHelper.FillAutoStyle(sAutoStyleName, xLegendProps);
165
0
}
166
167
SchXMLLegendContext::~SchXMLLegendContext()
168
0
{
169
0
}
170
171
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */