1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "SchXMLRegressionCurveObjectContext.hxx"
22 #include <SchXMLImport.hxx>
24 #include <sax/tools/converter.hxx>
25 #include <sal/log.hxx>
28 #include <xmloff/namespacemap.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmluconv.hxx>
31 #include <xmloff/prstylei.hxx>
32 #include <xmloff/xmlstyle.hxx>
34 #include <comphelper/processfactory.hxx>
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>
41 using namespace com::sun::star
;
42 using namespace xmloff::token
;
44 SchXMLRegressionCurveObjectContext::SchXMLRegressionCurveObjectContext(
45 SchXMLImportHelper
& rImpHelper
,
47 std::vector
< RegressionStyle
>& rRegressionStyleVector
,
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
)
59 SchXMLRegressionCurveObjectContext::~SchXMLRegressionCurveObjectContext()
63 void SchXMLRegressionCurveObjectContext::startFastElement (sal_Int32
/*nElement*/,
64 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
66 OUString sAutoStyleName
;
67 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
69 if (aIter
.getToken() == XML_ELEMENT(CHART
, XML_STYLE_NAME
) )
70 sAutoStyleName
= aIter
.toString();
72 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
75 RegressionStyle
aStyle( mxSeries
, sAutoStyleName
);
76 mrRegressionStyleVector
.push_back( aStyle
);
79 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SchXMLRegressionCurveObjectContext::createFastChildContext(
81 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
83 if( nElement
== XML_ELEMENT(CHART
, XML_EQUATION
) )
85 return new SchXMLEquationContext(
86 mrImportHelper
, GetImport(), maChartSize
, mrRegressionStyleVector
.back());
89 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
93 SchXMLEquationContext::SchXMLEquationContext(
94 SchXMLImportHelper
& rImpHelper
,
96 const awt::Size
& rChartSize
,
97 RegressionStyle
& rRegressionStyle
) :
98 SvXMLImportContext( rImport
),
99 mrImportHelper( rImpHelper
),
100 mrRegressionStyle( rRegressionStyle
),
101 maChartSize( rChartSize
)
104 SchXMLEquationContext::~SchXMLEquationContext()
107 void SchXMLEquationContext::startFastElement (sal_Int32
/*nElement*/,
108 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
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;
119 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
121 switch(aIter
.getToken())
123 case XML_ELEMENT(SVG
, XML_X
):
124 case XML_ELEMENT(SVG_COMPAT
, XML_X
):
125 rImport
.GetMM100UnitConverter().convertMeasureToCore(
126 aPosition
.X
, aIter
.toView() );
129 case XML_ELEMENT(SVG
, XML_Y
):
130 case XML_ELEMENT(SVG_COMPAT
, XML_Y
):
131 rImport
.GetMM100UnitConverter().convertMeasureToCore(
132 aPosition
.Y
, aIter
.toView() );
135 case XML_ELEMENT(CHART
, XML_DISPLAY_EQUATION
):
136 (void)::sax::Converter::convertBool(bShowEquation
, aIter
.toView());
138 case XML_ELEMENT(CHART
, XML_DISPLAY_R_SQUARE
):
139 (void)::sax::Converter::convertBool(bShowRSquare
, aIter
.toView());
141 case XML_ELEMENT(CHART
, XML_STYLE_NAME
):
142 sAutoStyleName
= aIter
.toString();
145 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
149 if( sAutoStyleName
.isEmpty() && !bShowEquation
&& !bShowRSquare
)
152 uno::Reference
< beans::XPropertySet
> xEquationProperties
= chart2::RegressionEquation::create( comphelper::getProcessComponentContext() );
154 if( !sAutoStyleName
.isEmpty() )
156 const SvXMLStylesContext
* pStylesCtxt
= mrImportHelper
.GetAutoStylesContext();
159 const SvXMLStyleContext
* pStyle
= pStylesCtxt
->FindStyleChildContext(
160 SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName
);
162 XMLPropStyleContext
* pPropStyleContext
=
163 const_cast< XMLPropStyleContext
* >( dynamic_cast< const XMLPropStyleContext
* >( pStyle
));
165 if( pPropStyleContext
)
166 pPropStyleContext
->FillPropertySet( xEquationProperties
);
169 xEquationProperties
->setPropertyValue( "ShowEquation", uno::Any( bShowEquation
));
170 xEquationProperties
->setPropertyValue( "ShowCorrelationCoefficient", uno::Any( bShowRSquare
));
172 if( bHasXPos
&& bHasYPos
)
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
));
179 mrRegressionStyle
.m_xEquationProperties
.set( xEquationProperties
);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */