/src/libreoffice/xmloff/source/chart/SchXMLRegressionCurveObjectContext.cxx
Line | Count | Source |
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 "SchXMLRegressionCurveObjectContext.hxx" |
21 | | |
22 | | #include <SchXMLImport.hxx> |
23 | | |
24 | | #include <sax/tools/converter.hxx> |
25 | | #include <sal/log.hxx> |
26 | | |
27 | | #include <utility> |
28 | | #include <xmloff/xmlnamespace.hxx> |
29 | | #include <xmloff/xmluconv.hxx> |
30 | | #include <xmloff/prstylei.hxx> |
31 | | #include <xmloff/xmlstyle.hxx> |
32 | | |
33 | | #include <comphelper/processfactory.hxx> |
34 | | |
35 | | #include <com/sun/star/awt/Point.hpp> |
36 | | #include <com/sun/star/chart2/RegressionEquation.hpp> |
37 | | #include <com/sun/star/chart2/RelativePosition.hpp> |
38 | | |
39 | | using namespace com::sun::star; |
40 | | using namespace xmloff::token; |
41 | | |
42 | | SchXMLRegressionCurveObjectContext::SchXMLRegressionCurveObjectContext( |
43 | | SchXMLImportHelper& rImpHelper, |
44 | | SvXMLImport& rImport, |
45 | | std::vector< RegressionStyle >& rRegressionStyleVector, |
46 | | css::uno::Reference< |
47 | | css::chart2::XDataSeries > xSeries, |
48 | | const awt::Size & rChartSize) : |
49 | 0 | SvXMLImportContext( rImport ), |
50 | 0 | mrImportHelper( rImpHelper ), |
51 | 0 | mxSeries(std::move( xSeries )), |
52 | 0 | maChartSize( rChartSize ), |
53 | 0 | mrRegressionStyleVector( rRegressionStyleVector ) |
54 | 0 | { |
55 | 0 | } |
56 | | |
57 | | SchXMLRegressionCurveObjectContext::~SchXMLRegressionCurveObjectContext() |
58 | 0 | { |
59 | 0 | } |
60 | | |
61 | | void SchXMLRegressionCurveObjectContext::startFastElement (sal_Int32 /*nElement*/, |
62 | | const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList) |
63 | 0 | { |
64 | 0 | OUString sAutoStyleName; |
65 | 0 | for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) ) |
66 | 0 | { |
67 | 0 | if (aIter.getToken() == XML_ELEMENT(CHART, XML_STYLE_NAME) ) |
68 | 0 | sAutoStyleName = aIter.toString(); |
69 | 0 | else |
70 | 0 | XMLOFF_WARN_UNKNOWN("xmloff", aIter); |
71 | 0 | } |
72 | | |
73 | 0 | RegressionStyle aStyle( mxSeries, sAutoStyleName ); |
74 | 0 | mrRegressionStyleVector.push_back( aStyle ); |
75 | 0 | } |
76 | | |
77 | | css::uno::Reference< css::xml::sax::XFastContextHandler > SchXMLRegressionCurveObjectContext::createFastChildContext( |
78 | | sal_Int32 nElement, |
79 | | const css::uno::Reference< css::xml::sax::XFastAttributeList >& ) |
80 | 0 | { |
81 | 0 | if( nElement == XML_ELEMENT(CHART, XML_EQUATION) ) |
82 | 0 | { |
83 | 0 | return new SchXMLEquationContext( |
84 | 0 | mrImportHelper, GetImport(), maChartSize, mrRegressionStyleVector.back()); |
85 | 0 | } |
86 | 0 | else |
87 | 0 | XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement); |
88 | 0 | return nullptr; |
89 | 0 | } |
90 | | |
91 | | SchXMLEquationContext::SchXMLEquationContext( |
92 | | SchXMLImportHelper& rImpHelper, |
93 | | SvXMLImport& rImport, |
94 | | const awt::Size& rChartSize, |
95 | | RegressionStyle& rRegressionStyle ) : |
96 | 0 | SvXMLImportContext( rImport ), |
97 | 0 | mrImportHelper( rImpHelper ), |
98 | 0 | mrRegressionStyle( rRegressionStyle ), |
99 | 0 | maChartSize( rChartSize ) |
100 | 0 | {} |
101 | | |
102 | | SchXMLEquationContext::~SchXMLEquationContext() |
103 | 0 | {} |
104 | | |
105 | | void SchXMLEquationContext::startFastElement (sal_Int32 /*nElement*/, |
106 | | const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList) |
107 | 0 | { |
108 | | // parse attributes |
109 | 0 | SchXMLImport& rImport = static_cast< SchXMLImport& >(GetImport()); |
110 | 0 | OUString sAutoStyleName; |
111 | 0 | bool bShowEquation = true; |
112 | 0 | bool bShowRSquare = false; |
113 | 0 | awt::Point aPosition; |
114 | 0 | bool bHasXPos = false; |
115 | 0 | bool bHasYPos = false; |
116 | |
|
117 | 0 | for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) ) |
118 | 0 | { |
119 | 0 | switch(aIter.getToken()) |
120 | 0 | { |
121 | 0 | case XML_ELEMENT(SVG, XML_X): |
122 | 0 | case XML_ELEMENT(SVG_COMPAT, XML_X): |
123 | 0 | rImport.GetMM100UnitConverter().convertMeasureToCore( |
124 | 0 | aPosition.X, aIter.toView() ); |
125 | 0 | bHasXPos = true; |
126 | 0 | break; |
127 | 0 | case XML_ELEMENT(SVG, XML_Y): |
128 | 0 | case XML_ELEMENT(SVG_COMPAT, XML_Y): |
129 | 0 | rImport.GetMM100UnitConverter().convertMeasureToCore( |
130 | 0 | aPosition.Y, aIter.toView() ); |
131 | 0 | bHasYPos = true; |
132 | 0 | break; |
133 | 0 | case XML_ELEMENT(CHART, XML_DISPLAY_EQUATION): |
134 | 0 | (void)::sax::Converter::convertBool(bShowEquation, aIter.toView()); |
135 | 0 | break; |
136 | 0 | case XML_ELEMENT(CHART, XML_DISPLAY_R_SQUARE): |
137 | 0 | (void)::sax::Converter::convertBool(bShowRSquare, aIter.toView()); |
138 | 0 | break; |
139 | 0 | case XML_ELEMENT(CHART, XML_STYLE_NAME): |
140 | 0 | sAutoStyleName = aIter.toString(); |
141 | 0 | break; |
142 | 0 | default: |
143 | 0 | XMLOFF_WARN_UNKNOWN("xmloff", aIter); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | 0 | if( sAutoStyleName.isEmpty() && !bShowEquation && !bShowRSquare ) |
148 | 0 | return; |
149 | | |
150 | 0 | uno::Reference< beans::XPropertySet > xEquationProperties = chart2::RegressionEquation::create( comphelper::getProcessComponentContext() ); |
151 | |
|
152 | 0 | if( !sAutoStyleName.isEmpty() ) |
153 | 0 | { |
154 | 0 | const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext(); |
155 | 0 | if( pStylesCtxt ) |
156 | 0 | { |
157 | 0 | const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext( |
158 | 0 | SchXMLImportHelper::GetChartFamilyID(), sAutoStyleName ); |
159 | |
|
160 | 0 | XMLPropStyleContext* pPropStyleContext = |
161 | 0 | const_cast< XMLPropStyleContext* >( dynamic_cast< const XMLPropStyleContext* >( pStyle )); |
162 | |
|
163 | 0 | if( pPropStyleContext ) |
164 | 0 | pPropStyleContext->FillPropertySet( xEquationProperties ); |
165 | 0 | } |
166 | 0 | } |
167 | 0 | xEquationProperties->setPropertyValue( u"ShowEquation"_ustr, uno::Any( bShowEquation )); |
168 | 0 | xEquationProperties->setPropertyValue( u"ShowCorrelationCoefficient"_ustr, uno::Any( bShowRSquare )); |
169 | |
|
170 | 0 | if( bHasXPos && bHasYPos ) |
171 | 0 | { |
172 | 0 | chart2::RelativePosition aRelPos; |
173 | 0 | aRelPos.Primary = static_cast< double >( aPosition.X ) / static_cast< double >( maChartSize.Width ); |
174 | 0 | aRelPos.Secondary = static_cast< double >( aPosition.Y ) / static_cast< double >( maChartSize.Height ); |
175 | 0 | xEquationProperties->setPropertyValue( u"RelativePosition"_ustr, uno::Any( aRelPos )); |
176 | 0 | } |
177 | 0 | mrRegressionStyle.m_xEquationProperties.set( xEquationProperties ); |
178 | 0 | } |
179 | | |
180 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |