use insert function instead of for loop
[LibreOffice.git] / oox / source / drawingml / diagram / datamodelcontext.cxx
blob009606fa322f112bb2261d8d21253d1ee36bc9fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "datamodelcontext.hxx"
21 #include <oox/helper/attributelist.hxx>
22 #include <drawingml/misccontexts.hxx>
23 #include <drawingml/shapepropertiescontext.hxx>
24 #include <drawingml/textbodycontext.hxx>
25 #include <oox/token/namespaces.hxx>
26 #include <oox/token/tokens.hxx>
28 using namespace ::oox::core;
29 using namespace ::com::sun::star::uno;
31 namespace oox::drawingml {
33 namespace {
35 // CT_CxnList
36 class CxnListContext
37 : public ContextHandler2
39 public:
40 CxnListContext( ContextHandler2Helper const & rParent,
41 svx::diagram::Connections & aConnections )
42 : ContextHandler2( rParent )
43 , mrConnection( aConnections )
47 virtual ContextHandlerRef
48 onCreateContext( sal_Int32 aElementToken,
49 const AttributeList& rAttribs ) override
51 switch( aElementToken )
53 case DGM_TOKEN( cxn ):
55 mrConnection.emplace_back( );
56 svx::diagram::Connection& rConnection=mrConnection.back();
58 rConnection.mnXMLType = static_cast<svx::diagram::TypeConstant>(rAttribs.getToken( XML_type, XML_parOf ));
59 rConnection.msModelId = rAttribs.getStringDefaulted( XML_modelId );
60 rConnection.msSourceId = rAttribs.getStringDefaulted( XML_srcId );
61 rConnection.msDestId = rAttribs.getStringDefaulted( XML_destId );
62 rConnection.msPresId = rAttribs.getStringDefaulted( XML_presId );
63 rConnection.msSibTransId = rAttribs.getStringDefaulted( XML_sibTransId );
64 rConnection.msParTransId = rAttribs.getStringDefaulted( XML_parTransId );
65 rConnection.mnSourceOrder = rAttribs.getInteger( XML_srcOrd, 0 );
66 rConnection.mnDestOrder = rAttribs.getInteger( XML_destOrd, 0 );
68 // skip CT_extLst
69 return nullptr;
71 default:
72 break;
75 return this;
77 private:
78 svx::diagram::Connections& mrConnection;
81 // CT_presLayoutVars
82 class PresLayoutVarsContext
83 : public ContextHandler2
85 public:
86 PresLayoutVarsContext( ContextHandler2Helper const & rParent,
87 svx::diagram::Point & rPoint ) :
88 ContextHandler2( rParent ),
89 mrPoint( rPoint )
92 virtual ContextHandlerRef
93 onCreateContext( sal_Int32 aElementToken,
94 const AttributeList& rAttribs ) override
96 switch( aElementToken )
98 // TODO
99 case DGM_TOKEN( animLvl ):
100 case DGM_TOKEN( animOne ):
101 break;
102 case DGM_TOKEN( bulletEnabled ):
103 mrPoint.mbBulletEnabled = rAttribs.getBool( XML_val, false );
104 break;
105 case DGM_TOKEN( chMax ):
106 mrPoint.mnMaxChildren = rAttribs.getInteger( XML_val, -1 );
107 break;
108 case DGM_TOKEN( chPref ):
109 mrPoint.mnPreferredChildren = rAttribs.getInteger( XML_val, -1 );
110 break;
111 case DGM_TOKEN( dir ):
112 mrPoint.mnDirection = rAttribs.getToken( XML_val, XML_norm );
113 break;
114 case DGM_TOKEN( hierBranch ):
116 // need to convert from oox::OptValue to std::optional since 1st is not available in svx
117 const std::optional< sal_Int32 > aOptVal(rAttribs.getToken( XML_val ));
118 if(aOptVal.has_value())
119 mrPoint.moHierarchyBranch = aOptVal.value();
120 break;
122 case DGM_TOKEN( orgChart ):
123 mrPoint.mbOrgChartEnabled = rAttribs.getBool( XML_val, false );
124 break;
125 case DGM_TOKEN( resizeHandles ):
126 mrPoint.mnResizeHandles = rAttribs.getToken( XML_val, XML_rel );
127 break;
128 default:
129 break;
132 return this;
135 private:
136 svx::diagram::Point& mrPoint;
139 // CT_prSet
140 class PropertiesContext
141 : public ContextHandler2
143 public:
144 PropertiesContext( ContextHandler2Helper const & rParent,
145 svx::diagram::Point & rPoint,
146 const AttributeList& rAttribs ) :
147 ContextHandler2( rParent ),
148 mrPoint( rPoint )
150 mrPoint.msColorTransformCategoryId = rAttribs.getStringDefaulted( XML_csCatId);
151 mrPoint.msColorTransformTypeId = rAttribs.getStringDefaulted( XML_csTypeId);
152 mrPoint.msLayoutCategoryId = rAttribs.getStringDefaulted( XML_loCatId);
153 mrPoint.msLayoutTypeId = rAttribs.getStringDefaulted( XML_loTypeId);
154 mrPoint.msPlaceholderText = rAttribs.getStringDefaulted( XML_phldrT);
155 mrPoint.msPresentationAssociationId = rAttribs.getStringDefaulted( XML_presAssocID);
156 mrPoint.msPresentationLayoutName = rAttribs.getStringDefaulted( XML_presName);
157 mrPoint.msPresentationLayoutStyleLabel = rAttribs.getStringDefaulted( XML_presStyleLbl);
158 mrPoint.msQuickStyleCategoryId = rAttribs.getStringDefaulted( XML_qsCatId);
159 mrPoint.msQuickStyleTypeId = rAttribs.getStringDefaulted( XML_qsTypeId);
161 mrPoint.mnCustomAngle = rAttribs.getInteger( XML_custAng, -1 );
162 mrPoint.mnPercentageNeighbourWidth = rAttribs.getInteger( XML_custLinFactNeighborX, -1 );
163 mrPoint.mnPercentageNeighbourHeight = rAttribs.getInteger( XML_custLinFactNeighborY, -1 );
164 mrPoint.mnPercentageOwnWidth = rAttribs.getInteger( XML_custLinFactX, -1 );
165 mrPoint.mnPercentageOwnHeight = rAttribs.getInteger( XML_custLinFactY, -1 );
166 mrPoint.mnIncludeAngleScale = rAttribs.getInteger( XML_custRadScaleInc, -1 );
167 mrPoint.mnRadiusScale = rAttribs.getInteger( XML_custRadScaleRad, -1 );
168 mrPoint.mnWidthScale = rAttribs.getInteger( XML_custScaleX, -1 );
169 mrPoint.mnHeightScale = rAttribs.getInteger( XML_custScaleY, -1 );
170 mrPoint.mnWidthOverride = rAttribs.getInteger( XML_custSzX, -1 );
171 mrPoint.mnHeightOverride = rAttribs.getInteger( XML_custSzY, -1 );
172 mrPoint.mnLayoutStyleCount = rAttribs.getInteger( XML_presStyleCnt, -1 );
173 mrPoint.mnLayoutStyleIndex = rAttribs.getInteger( XML_presStyleIdx, -1 );
175 mrPoint.mbCoherent3DOffset = rAttribs.getBool( XML_coherent3DOff, false );
176 mrPoint.mbCustomHorizontalFlip = rAttribs.getBool( XML_custFlipHor, false );
177 mrPoint.mbCustomVerticalFlip = rAttribs.getBool( XML_custFlipVert, false );
178 mrPoint.mbCustomText = rAttribs.getBool( XML_custT, false );
179 mrPoint.mbIsPlaceholder = rAttribs.getBool( XML_phldr, false );
182 virtual ContextHandlerRef
183 onCreateContext( sal_Int32 aElementToken,
184 const AttributeList& ) override
186 switch( aElementToken )
188 case DGM_TOKEN( presLayoutVars ):
189 return new PresLayoutVarsContext( *this, mrPoint );
190 case DGM_TOKEN( style ):
191 // skip CT_shapeStyle
192 return nullptr;
193 default:
194 break;
196 return this;
199 private:
200 svx::diagram::Point& mrPoint;
203 // CL_Pt
204 class PtContext
205 : public ContextHandler2
207 public:
208 PtContext( ContextHandler2Helper const& rParent,
209 const AttributeList& rAttribs,
210 svx::diagram::Point& rPoint,
211 DiagramData& rDiagramData):
212 ContextHandler2( rParent ),
213 mrPoint( rPoint ),
214 mrDiagramData( rDiagramData )
216 mrPoint.msModelId = rAttribs.getStringDefaulted( XML_modelId );
218 // the default type is XML_node
219 const sal_Int32 nType = rAttribs.getToken( XML_type, XML_node );
220 mrPoint.mnXMLType = static_cast<svx::diagram::TypeConstant>(nType);
222 // ignore the cxnId unless it is this type. See 5.15.3.1.3 in Primer
223 if( ( nType == XML_parTrans ) || ( nType == XML_sibTrans ) )
224 mrPoint.msCnxId = rAttribs.getStringDefaulted( XML_cxnId );
227 virtual ContextHandlerRef
228 onCreateContext( sal_Int32 aElementToken,
229 const AttributeList& rAttribs ) override
231 switch( aElementToken )
233 case DGM_TOKEN( extLst ):
234 return nullptr;
235 case DGM_TOKEN( prSet ):
236 return new PropertiesContext( *this, mrPoint, rAttribs );
237 case DGM_TOKEN( spPr ):
239 Shape* pShape(mrDiagramData.getOrCreateAssociatedShape(mrPoint, true));
240 return new ShapePropertiesContext( *this, *pShape );
242 case DGM_TOKEN( t ):
244 Shape* pShape(mrDiagramData.getOrCreateAssociatedShape(mrPoint, true));
245 TextBodyPtr xTextBody = std::make_shared<TextBody>();
246 pShape->setTextBody( xTextBody );
247 return new TextBodyContext( *this, *xTextBody );
249 default:
250 break;
252 return this;
255 private:
256 svx::diagram::Point& mrPoint;
257 DiagramData& mrDiagramData;
260 // CT_PtList
261 class PtListContext
262 : public ContextHandler2
264 public:
265 PtListContext( ContextHandler2Helper const & rParent, svx::diagram::Points& rPoints, DiagramData& rDiagramData) :
266 ContextHandler2( rParent ),
267 mrPoints( rPoints ),
268 mrDiagramData( rDiagramData )
270 virtual ContextHandlerRef
271 onCreateContext( sal_Int32 aElementToken,
272 const AttributeList& rAttribs ) override
274 switch( aElementToken )
276 case DGM_TOKEN( pt ):
278 // CT_Pt
279 mrPoints.emplace_back( );
280 return new PtContext( *this, rAttribs, mrPoints.back(), mrDiagramData );
282 default:
283 break;
285 return this;
288 private:
289 svx::diagram::Points& mrPoints;
290 DiagramData& mrDiagramData;
293 // CT_BackgroundFormatting
294 class BackgroundFormattingContext
295 : public ContextHandler2
297 public:
298 BackgroundFormattingContext( ContextHandler2Helper const & rParent, OoxDiagramDataPtr const& pModel )
299 : ContextHandler2( rParent )
300 , mpDataModel( pModel )
302 assert( pModel && "the data model MUST NOT be NULL" );
305 virtual ContextHandlerRef
306 onCreateContext( sal_Int32 aElementToken,
307 const AttributeList& rAttribs ) override
309 switch( aElementToken )
311 case A_TOKEN( blipFill ):
312 case A_TOKEN( gradFill ):
313 case A_TOKEN( grpFill ):
314 case A_TOKEN( noFill ):
315 case A_TOKEN( pattFill ):
316 case A_TOKEN( solidFill ):
317 // EG_FillProperties
318 return FillPropertiesContext::createFillContext(*this, aElementToken, rAttribs, *mpDataModel->getBackgroundShapeFillProperties(), nullptr);
319 case A_TOKEN( effectDag ):
320 case A_TOKEN( effectLst ):
321 // TODO
322 // EG_EffectProperties
323 break;
324 default:
325 break;
327 return this;
329 private:
330 OoxDiagramDataPtr mpDataModel;
335 DataModelContext::DataModelContext( ContextHandler2Helper const& rParent,
336 const OoxDiagramDataPtr& pDataModel )
337 : ContextHandler2( rParent )
338 , mpDataModel( pDataModel )
340 assert( pDataModel && "Data Model must not be NULL" );
343 DataModelContext::~DataModelContext()
345 // some debug
346 mpDataModel->dump();
349 ContextHandlerRef
350 DataModelContext::onCreateContext( ::sal_Int32 aElement,
351 const AttributeList& rAttribs )
353 switch( aElement )
355 case DGM_TOKEN( cxnLst ):
356 // CT_CxnList
357 return new CxnListContext( *this, mpDataModel->getConnections() );
358 case DGM_TOKEN( ptLst ):
359 // CT_PtList
360 return new PtListContext( *this, mpDataModel->getPoints(), *mpDataModel );
361 case DGM_TOKEN( bg ):
362 // CT_BackgroundFormatting
363 return new BackgroundFormattingContext( *this, mpDataModel );
364 case DGM_TOKEN( whole ):
365 // CT_WholeE2oFormatting
366 // TODO
367 return nullptr;
368 case DGM_TOKEN( extLst ):
369 case A_TOKEN( ext ):
370 break;
371 case DSP_TOKEN( dataModelExt ):
372 mpDataModel->getExtDrawings().push_back( rAttribs.getStringDefaulted( XML_relId ) );
373 break;
374 default:
375 break;
378 return this;
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */