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
25#include <sal/log.hxx>
26
27#include <utility>
30#include <xmloff/xmluconv.hxx>
31#include <xmloff/prstylei.hxx>
32#include <xmloff/xmlstyle.hxx>
33
35
36#include <com/sun/star/awt/Point.hpp>
37#include <com/sun/star/chart2/RegressionEquation.hpp>
38#include <com/sun/star/chart2/RelativePosition.hpp>
39#include <com/sun/star/xml/sax/XAttributeList.hpp>
40
41using namespace com::sun::star;
42using namespace xmloff::token;
43
45 SchXMLImportHelper& rImpHelper,
46 SvXMLImport& rImport,
47 std::vector< RegressionStyle >& rRegressionStyleVector,
48 css::uno::Reference<
49 css::chart2::XDataSeries > xSeries,
50 const awt::Size & rChartSize) :
51 SvXMLImportContext( rImport ),
52 mrImportHelper( rImpHelper ),
53 mxSeries(std::move( xSeries )),
54 maChartSize( rChartSize ),
55 mrRegressionStyleVector( rRegressionStyleVector )
56{
57}
58
60{
61}
62
64 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
65{
66 OUString sAutoStyleName;
67 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
68 {
69 if (aIter.getToken() == XML_ELEMENT(CHART, XML_STYLE_NAME) )
70 sAutoStyleName = aIter.toString();
71 else
72 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
73 }
74
75 RegressionStyle aStyle( mxSeries, sAutoStyleName );
76 mrRegressionStyleVector.push_back( aStyle );
77}
78
79css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLRegressionCurveObjectContext::createFastChildContext(
80 sal_Int32 nElement,
81 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
82{
83 if( nElement == XML_ELEMENT(CHART, XML_EQUATION) )
84 {
85 return new SchXMLEquationContext(
87 }
88 else
89 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
90 return nullptr;
91}
92
94 SchXMLImportHelper& rImpHelper,
95 SvXMLImport& rImport,
96 const awt::Size& rChartSize,
97 RegressionStyle& rRegressionStyle ) :
98 SvXMLImportContext( rImport ),
99 mrImportHelper( rImpHelper ),
100 mrRegressionStyle( rRegressionStyle ),
101 maChartSize( rChartSize )
102{}
103
105{}
106
107void SchXMLEquationContext::startFastElement (sal_Int32 /*nElement*/,
108 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
109{
110 // parse attributes
111 SchXMLImport& rImport = static_cast< SchXMLImport& >(GetImport());
112 OUString sAutoStyleName;
113 bool bShowEquation = true;
114 bool bShowRSquare = false;
115 awt::Point aPosition;
116 bool bHasXPos = false;
117 bool bHasYPos = false;
118
119 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
120 {
121 switch(aIter.getToken())
122 {
123 case XML_ELEMENT(SVG, XML_X):
124 case XML_ELEMENT(SVG_COMPAT, XML_X):
125 rImport.GetMM100UnitConverter().convertMeasureToCore(
126 aPosition.X, aIter.toView() );
127 bHasXPos = true;
128 break;
129 case XML_ELEMENT(SVG, XML_Y):
130 case XML_ELEMENT(SVG_COMPAT, XML_Y):
131 rImport.GetMM100UnitConverter().convertMeasureToCore(
132 aPosition.Y, aIter.toView() );
133 bHasYPos = true;
134 break;
136 (void)::sax::Converter::convertBool(bShowEquation, aIter.toView());
137 break;
139 (void)::sax::Converter::convertBool(bShowRSquare, aIter.toView());
140 break;
142 sAutoStyleName = aIter.toString();
143 break;
144 default:
145 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
146 }
147 }
148
149 if( sAutoStyleName.isEmpty() && !bShowEquation && !bShowRSquare )
150 return;
151
152 uno::Reference< beans::XPropertySet > xEquationProperties = chart2::RegressionEquation::create( comphelper::getProcessComponentContext() );
153
154 if( !sAutoStyleName.isEmpty() )
155 {
157 if( pStylesCtxt )
158 {
159 const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
160 SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName );
161
162 XMLPropStyleContext* pPropStyleContext =
163 const_cast< XMLPropStyleContext* >( dynamic_cast< const XMLPropStyleContext* >( pStyle ));
164
165 if( pPropStyleContext )
166 pPropStyleContext->FillPropertySet( xEquationProperties );
167 }
168 }
169 xEquationProperties->setPropertyValue( "ShowEquation", uno::Any( bShowEquation ));
170 xEquationProperties->setPropertyValue( "ShowCorrelationCoefficient", uno::Any( bShowRSquare ));
171
172 if( bHasXPos && bHasYPos )
173 {
174 chart2::RelativePosition aRelPos;
175 aRelPos.Primary = static_cast< double >( aPosition.X ) / static_cast< double >( maChartSize.Width );
176 aRelPos.Secondary = static_cast< double >( aPosition.Y ) / static_cast< double >( maChartSize.Height );
177 xEquationProperties->setPropertyValue( "RelativePosition", uno::Any( aRelPos ));
178 }
179 mrRegressionStyle.m_xEquationProperties.set( xEquationProperties );
180}
181
182/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SchXMLEquationContext(SchXMLImportHelper &rImportHelper, SvXMLImport &rImport, const css::awt::Size &rChartSize, RegressionStyle &rRegressionStyle)
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
With this class you can import a <chart:chart> element containing its data as <table:table> element o...
SvXMLStylesContext * GetAutoStylesContext() const
static XmlStyleFamily GetChartFamilyID()
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
SchXMLRegressionCurveObjectContext(SchXMLImportHelper &rImportHelper, SvXMLImport &rImport, std::vector< RegressionStyle > &rRegressionStyleVector, css::uno::Reference< css::chart2::XDataSeries > xSeries, const css::awt::Size &rChartSize)
css::uno::Reference< css::chart2::XDataSeries > mxSeries
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
std::vector< RegressionStyle > & mrRegressionStyleVector
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
Definition: xmlstyle.cxx:783
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:222
static bool convertBool(bool &rBool, std::u16string_view rString)
Reference< XComponentContext > getProcessComponentContext()
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
css::uno::Reference< css::beans::XPropertySet > m_xEquationProperties
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97