LibreOffice Module xmloff (master)  1
SchXMLRegressionCurveObjectContext.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 
21 
22 #include <SchXMLImport.hxx>
23 
24 #include <sax/tools/converter.hxx>
25 
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlnmspe.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmloff/prstylei.hxx>
30 #include <xmloff/xmlstyle.hxx>
31 
33 
34 #include <com/sun/star/awt/Point.hpp>
35 #include <com/sun/star/chart2/RegressionEquation.hpp>
36 #include <com/sun/star/chart2/RelativePosition.hpp>
37 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 
39 using namespace com::sun::star;
40 using namespace xmloff::token;
41 
43  SchXMLImportHelper& rImpHelper,
44  SvXMLImport& rImport,
45  sal_uInt16 nPrefix,
46  const OUString& rLocalName,
47  std::vector< RegressionStyle >& rRegressionStyleVector,
48  const css::uno::Reference<
49  css::chart2::XDataSeries >& xSeries,
50  const awt::Size & rChartSize) :
51  SvXMLImportContext( rImport, nPrefix, rLocalName ),
52  mrImportHelper( rImpHelper ),
53  mxSeries( xSeries ),
54  maChartSize( rChartSize ),
55  mrRegressionStyleVector( rRegressionStyleVector )
56 {
57 }
58 
60 {
61 }
62 
63 void SchXMLRegressionCurveObjectContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttributeList )
64 {
65  sal_Int16 nAttributeCount = xAttributeList.is()? xAttributeList->getLength(): 0;
66  OUString sAutoStyleName;
67 
68  for( sal_Int16 i = 0; i < nAttributeCount; i++ )
69  {
70  OUString sAttributeName = xAttributeList->getNameByIndex( i );
71  OUString aLocalName;
72 
73  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttributeName, &aLocalName );
74 
75  if( nPrefix == XML_NAMESPACE_CHART )
76  {
77  if( IsXMLToken( aLocalName, XML_STYLE_NAME ) )
78  {
79  sAutoStyleName = xAttributeList->getValueByIndex( i );
80  }
81  }
82  }
83 
84  RegressionStyle aStyle( mxSeries, sAutoStyleName );
85  mrRegressionStyleVector.push_back( aStyle );
86 }
87 
89  sal_uInt16 nPrefix,
90  const OUString& rLocalName,
91  const uno::Reference< xml::sax::XAttributeList >& /*xAttrList*/ )
92 {
93  SvXMLImportContextRef xContext;
94 
95  if( nPrefix == XML_NAMESPACE_CHART && IsXMLToken( rLocalName, XML_EQUATION ) )
96  {
97  xContext = new SchXMLEquationContext(
98  mrImportHelper, GetImport(), nPrefix, rLocalName, maChartSize, mrRegressionStyleVector.back());
99  }
100 
101  return xContext;
102 }
103 
105  SchXMLImportHelper& rImpHelper,
106  SvXMLImport& rImport,
107  sal_uInt16 nPrefix,
108  const OUString& rLocalName,
109  const awt::Size& rChartSize,
110  RegressionStyle& rRegressionStyle ) :
111 
112  SvXMLImportContext( rImport, nPrefix, rLocalName ),
113  mrImportHelper( rImpHelper ),
114  mrRegressionStyle( rRegressionStyle ),
115  maChartSize( rChartSize )
116 {}
117 
119 {}
120 
121 void SchXMLEquationContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
122 {
123  // parse attributes
124  sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
125  SchXMLImport& rImport = static_cast< SchXMLImport& >(GetImport());
127  OUString sAutoStyleName;
128 
129  bool bShowEquation = true;
130  bool bShowRSquare = false;
131  awt::Point aPosition;
132  bool bHasXPos = false;
133  bool bHasYPos = false;
134 
135  for( sal_Int16 i = 0; i < nAttrCount; i++ )
136  {
137  OUString sAttrName = xAttrList->getNameByIndex( i );
138  OUString aLocalName;
139  OUString aValue = xAttrList->getValueByIndex( i );
140  sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
141 
142  switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
143  {
144  case XML_TOK_REGEQ_POS_X:
146  aPosition.X, aValue );
147  bHasXPos = true;
148  break;
149  case XML_TOK_REGEQ_POS_Y:
151  aPosition.Y, aValue );
152  bHasYPos = true;
153  break;
155  (void)::sax::Converter::convertBool(bShowEquation, aValue);
156  break;
158  (void)::sax::Converter::convertBool(bShowRSquare, aValue);
159  break;
161  sAutoStyleName = aValue;
162  break;
163  }
164  }
165 
166  if( !sAutoStyleName.isEmpty() || bShowEquation || bShowRSquare )
167  {
168  uno::Reference< beans::XPropertySet > xEquationProperties = chart2::RegressionEquation::create( comphelper::getProcessComponentContext() );
169 
170  if( !sAutoStyleName.isEmpty() )
171  {
173  if( pStylesCtxt )
174  {
175  const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
176  SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
177 
178  XMLPropStyleContext* pPropStyleContext =
179  const_cast< XMLPropStyleContext* >( dynamic_cast< const XMLPropStyleContext* >( pStyle ));
180 
181  if( pPropStyleContext )
182  pPropStyleContext->FillPropertySet( xEquationProperties );
183  }
184  }
185  xEquationProperties->setPropertyValue( "ShowEquation", uno::makeAny( bShowEquation ));
186  xEquationProperties->setPropertyValue( "ShowCorrelationCoefficient", uno::makeAny( bShowRSquare ));
187 
188  if( bHasXPos && bHasYPos )
189  {
190  chart2::RelativePosition aRelPos;
191  aRelPos.Primary = static_cast< double >( aPosition.X ) / static_cast< double >( maChartSize.Width );
192  aRelPos.Secondary = static_cast< double >( aPosition.Y ) / static_cast< double >( maChartSize.Height );
193  xEquationProperties->setPropertyValue( "RelativePosition", uno::makeAny( aRelPos ));
194  }
195  mrRegressionStyle.m_xEquationProperties.set( xEquationProperties );
196  }
197 }
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
css::uno::Reference< css::chart2::XDataSeries > mxSeries
static bool convertBool(bool &rBool, const OUString &rString)
SvXMLStylesContext * GetAutoStylesContext() const
constexpr sal_uInt16 XML_NAMESPACE_CHART
Definition: xmlnmspe.hxx:41
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
css::uno::Reference< css::beans::XPropertySet > m_xEquationProperties
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
Definition: nmspmap.cxx:437
bool convertMeasureToCore(sal_Int32 &rValue, const OUString &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:171
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
int i
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:231
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:93
std::vector< RegressionStyle > & mrRegressionStyleVector
static XmlStyleFamily GetChartFamilyID()
With this class you can import a element containing its data as element o...
SchXMLEquationContext(SchXMLImportHelper &rImportHelper, SvXMLImport &rImport, sal_uInt16 nPrefix, const OUString &rLocalName, const css::awt::Size &rChartSize, RegressionStyle &rRegressionStyle)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
const SvXMLTokenMap & GetRegEquationAttrTokenMap()
Handling of tokens in XML:
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:399
Reference< XComponentContext > getProcessComponentContext()
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
Definition: xmlstyle.cxx:870
SchXMLRegressionCurveObjectContext(SchXMLImportHelper &rImportHelper, SvXMLImport &rImport, sal_uInt16 nPrefix, const OUString &rLocalName, std::vector< RegressionStyle > &rRegressionStyleVector, const css::uno::Reference< css::chart2::XDataSeries > &xSeries, const css::awt::Size &rChartSize)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo