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