Line data Source code
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 <sax/tools/converter.hxx>
23 :
24 : #include <xmloff/nmspmap.hxx>
25 : #include <xmloff/xmlnmspe.hxx>
26 : #include <xmloff/xmlement.hxx>
27 : #include <xmloff/xmluconv.hxx>
28 : #include <xmloff/prstylei.hxx>
29 : #include <xmloff/xmlstyle.hxx>
30 :
31 : #include <comphelper/processfactory.hxx>
32 :
33 : #include <com/sun/star/chart2/RegressionEquation.hpp>
34 : #include <com/sun/star/chart2/RelativePosition.hpp>
35 :
36 : using namespace com::sun::star;
37 : using namespace xmloff::token;
38 :
39 : using com::sun::star::uno::Reference;
40 :
41 12 : SchXMLRegressionCurveObjectContext::SchXMLRegressionCurveObjectContext(
42 : SchXMLImportHelper& rImpHelper,
43 : SvXMLImport& rImport,
44 : sal_uInt16 nPrefix,
45 : const OUString& rLocalName,
46 : const OUString &rSeriesStyleName,
47 : std::list< RegressionStyle >& rRegressionStyleList,
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 : maSeriesStyleName( rSeriesStyleName),
56 12 : mrRegressionStyleList( rRegressionStyleList )
57 : {
58 12 : }
59 :
60 24 : SchXMLRegressionCurveObjectContext::~SchXMLRegressionCurveObjectContext()
61 : {
62 24 : }
63 :
64 12 : void SchXMLRegressionCurveObjectContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttributeList )
65 : {
66 12 : sal_Int16 nAttributeCount = xAttributeList.is()? xAttributeList->getLength(): 0;
67 12 : OUString sAutoStyleName;
68 :
69 24 : for( sal_Int16 i = 0; i < nAttributeCount; i++ )
70 : {
71 12 : OUString sAttributeName = xAttributeList->getNameByIndex( i );
72 24 : OUString aLocalName;
73 :
74 12 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttributeName, &aLocalName );
75 :
76 12 : if( nPrefix == XML_NAMESPACE_CHART )
77 : {
78 12 : if( IsXMLToken( aLocalName, XML_STYLE_NAME ) )
79 : {
80 12 : sAutoStyleName = xAttributeList->getValueByIndex( i );
81 : }
82 : }
83 12 : }
84 :
85 24 : RegressionStyle aStyle( mxSeries, sAutoStyleName );
86 24 : mrRegressionStyleList.push_back( aStyle );
87 12 : }
88 :
89 8 : SvXMLImportContext* SchXMLRegressionCurveObjectContext::CreateChildContext(
90 : sal_uInt16 nPrefix,
91 : const OUString& rLocalName,
92 : const uno::Reference< xml::sax::XAttributeList >& xAttrList )
93 : {
94 8 : SvXMLImportContext* pContext = 0;
95 :
96 8 : if( nPrefix == XML_NAMESPACE_CHART && IsXMLToken( rLocalName, XML_EQUATION ) )
97 : {
98 : pContext = new SchXMLEquationContext(
99 8 : mrImportHelper, GetImport(), nPrefix, rLocalName, mxSeries, maChartSize, mrRegressionStyleList.back());
100 : }
101 : else
102 : {
103 0 : pContext = SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
104 : }
105 :
106 8 : return pContext;
107 : }
108 :
109 8 : SchXMLEquationContext::SchXMLEquationContext(
110 : SchXMLImportHelper& rImpHelper,
111 : SvXMLImport& rImport,
112 : sal_uInt16 nPrefix,
113 : const OUString& rLocalName,
114 : const css::uno::Reference<
115 : css::chart2::XDataSeries >& xSeries,
116 : const awt::Size& rChartSize,
117 : RegressionStyle& rRegressionStyle ) :
118 :
119 : SvXMLImportContext( rImport, nPrefix, rLocalName ),
120 : mrImportHelper( rImpHelper ),
121 : mrRegressionStyle( rRegressionStyle ),
122 : mxSeries( xSeries ),
123 8 : maChartSize( rChartSize )
124 8 : {}
125 :
126 16 : SchXMLEquationContext::~SchXMLEquationContext()
127 16 : {}
128 :
129 8 : void SchXMLEquationContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
130 : {
131 : // parse attributes
132 8 : sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
133 8 : SchXMLImport& rImport = ( SchXMLImport& )GetImport();
134 8 : const SvXMLTokenMap& rAttrTokenMap = mrImportHelper.GetRegEquationAttrTokenMap();
135 8 : OUString sAutoStyleName;
136 :
137 8 : bool bShowEquation = true;
138 8 : bool bShowRSquare = false;
139 8 : awt::Point aPosition;
140 8 : bool bHasXPos = false;
141 8 : bool bHasYPos = false;
142 :
143 44 : for( sal_Int16 i = 0; i < nAttrCount; i++ )
144 : {
145 36 : OUString sAttrName = xAttrList->getNameByIndex( i );
146 72 : OUString aLocalName;
147 72 : OUString aValue = xAttrList->getValueByIndex( i );
148 36 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
149 :
150 36 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
151 : {
152 : case XML_TOK_REGEQ_POS_X:
153 8 : rImport.GetMM100UnitConverter().convertMeasureToCore(
154 8 : aPosition.X, aValue );
155 8 : bHasXPos = true;
156 8 : break;
157 : case XML_TOK_REGEQ_POS_Y:
158 8 : rImport.GetMM100UnitConverter().convertMeasureToCore(
159 8 : aPosition.Y, aValue );
160 8 : bHasYPos = true;
161 8 : break;
162 : case XML_TOK_REGEQ_DISPLAY_EQUATION:
163 8 : ::sax::Converter::convertBool(bShowEquation, aValue);
164 8 : break;
165 : case XML_TOK_REGEQ_DISPLAY_R_SQUARE:
166 8 : ::sax::Converter::convertBool(bShowRSquare, aValue);
167 8 : break;
168 : case XML_TOK_REGEQ_STYLE_NAME:
169 4 : sAutoStyleName = aValue;
170 4 : break;
171 : }
172 36 : }
173 :
174 8 : if( !sAutoStyleName.isEmpty() || bShowEquation || bShowRSquare )
175 : {
176 8 : uno::Reference< beans::XPropertySet > xEqationProperties = chart2::RegressionEquation::create( comphelper::getProcessComponentContext() );
177 :
178 8 : if( !sAutoStyleName.isEmpty() )
179 : {
180 4 : const SvXMLStylesContext* pStylesCtxt = mrImportHelper.GetAutoStylesContext();
181 4 : if( pStylesCtxt )
182 : {
183 : const SvXMLStyleContext* pStyle = pStylesCtxt->FindStyleChildContext(
184 4 : mrImportHelper.GetChartFamilyID(), sAutoStyleName );
185 :
186 : XMLPropStyleContext* pPropStyleContext =
187 4 : const_cast< XMLPropStyleContext* >( dynamic_cast< const XMLPropStyleContext* >( pStyle ));
188 :
189 4 : if( pPropStyleContext )
190 4 : pPropStyleContext->FillPropertySet( xEqationProperties );
191 : }
192 : }
193 8 : xEqationProperties->setPropertyValue( OUString( "ShowEquation"), uno::makeAny( bShowEquation ));
194 8 : xEqationProperties->setPropertyValue( OUString( "ShowCorrelationCoefficient"), uno::makeAny( bShowRSquare ));
195 :
196 8 : if( bHasXPos && bHasYPos )
197 : {
198 8 : chart2::RelativePosition aRelPos;
199 8 : aRelPos.Primary = static_cast< double >( aPosition.X ) / static_cast< double >( maChartSize.Width );
200 8 : aRelPos.Secondary = static_cast< double >( aPosition.Y ) / static_cast< double >( maChartSize.Height );
201 8 : xEqationProperties->setPropertyValue( OUString( "RelativePosition" ), uno::makeAny( aRelPos ));
202 : }
203 8 : mrRegressionStyle.m_xEquationProperties.set( xEqationProperties );
204 8 : }
205 8 : }
206 :
207 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|