LibreOffice Module xmloff (master)  1
SchXMLLegendContext.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 
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 
38  SvXMLImportContext( rImport ),
39  mrImportHelper( rImpHelper )
40 {
41 }
42 
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  {
91  try
92  {
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):
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):
121  aLegendPos.Y, aIter.toView() );
122  bHasYPosition = true;
123  break;
125  sAutoStyleName = aIter.toString();
126  break;
129  bHasExpansion = (aAny>>=nLegendExpansion);
130  break;
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):
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):
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 
169 {
170 }
171 
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool convertMeasureToCore(sal_Int32 &rValue, std::u16string_view rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
convert string to measure with meCoreMeasureUnit, using optional min and max values ...
Definition: xmluconv.cxx:186
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
SchXMLLegendContext(SchXMLImportHelper &rImpHelper, SvXMLImport &rImport)
void FillAutoStyle(const OUString &rAutoStyleName, const css::uno::Reference< css::beans::XPropertySet > &rProp)
Fill in the autostyle.
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
static XMLEnumPropertyHdl & getLegendPositionConverter()
SchXMLImportHelper & mrImportHelper
With this class you can import a element containing its data as element o...
XMLOFF_DLLPUBLIC bool importXML(css::uno::Reference< css::xml::sax::XFastAttributeList > const &xAttrList, css::uno::Any &rValue, OUString &rStrName, SvXMLImport &rImport)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
const css::uno::Reference< css::chart::XChartDocument > & GetChartDocument() const
Handling of tokens in XML:
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
#define SAL_INFO(area, stream)
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:400
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
virtual ~SchXMLLegendContext() override