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