use insert function instead of for loop
[LibreOffice.git] / oox / source / drawingml / diagram / layoutnodecontext.cxx
blobde7d25286f6453c582b0cd6784c3ee28b2d78912
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 "layoutnodecontext.hxx"
22 #include <oox/helper/attributelist.hxx>
23 #include <oox/drawingml/shapecontext.hxx>
24 #include <drawingml/customshapeproperties.hxx>
25 #include "constraintlistcontext.hxx"
26 #include "rulelistcontext.hxx"
27 #include <oox/token/namespaces.hxx>
28 #include <oox/token/tokens.hxx>
29 #include <sal/log.hxx>
30 #include <utility>
32 using namespace ::oox::core;
34 namespace oox::drawingml {
36 namespace {
38 class IfContext
39 : public LayoutNodeContext
41 public:
42 IfContext( ContextHandler2Helper const & rParent,
43 const AttributeList& rAttribs,
44 const ConditionAtomPtr& pAtom )
45 : LayoutNodeContext( rParent, rAttribs, pAtom )
49 class AlgorithmContext
50 : public ContextHandler2
52 public:
53 AlgorithmContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs, const AlgAtomPtr & pNode )
54 : ContextHandler2( rParent )
55 , mnRevision( 0 )
56 , mpNode( pNode )
58 mnRevision = rAttribs.getInteger( XML_rev, 0 );
59 pNode->setType(rAttribs.getToken(XML_type, 0));
62 virtual ContextHandlerRef
63 onCreateContext( ::sal_Int32 aElement,
64 const AttributeList& rAttribs ) override
66 switch( aElement )
68 case DGM_TOKEN( param ):
70 sal_Int32 nType = rAttribs.getToken(XML_type, 0);
71 switch (nType)
73 case XML_ar:
74 mpNode->setAspectRatio(rAttribs.getDouble(XML_val, 0));
75 break;
76 default:
77 const sal_Int32 nValTok = rAttribs.getToken(XML_val, 0);
78 mpNode->addParam(nType, nValTok > 0 ? nValTok
79 : rAttribs.getInteger(XML_val, 0));
80 break;
82 break;
84 default:
85 break;
88 return this;
91 private:
92 sal_Int32 mnRevision;
93 AlgAtomPtr mpNode;
96 class ChooseContext
97 : public ContextHandler2
99 public:
100 ChooseContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs, LayoutAtomPtr pNode )
101 : ContextHandler2( rParent )
102 , mpNode(std::move( pNode ))
104 msName = rAttribs.getStringDefaulted( XML_name );
107 virtual ContextHandlerRef
108 onCreateContext( ::sal_Int32 aElement,
109 const AttributeList& rAttribs ) override
111 switch( aElement )
113 case DGM_TOKEN( if ):
115 // CT_When
116 ConditionAtomPtr pNode = std::make_shared<ConditionAtom>(mpNode->getLayoutNode(), false, rAttribs.getFastAttributeList());
117 LayoutAtom::connect(mpNode, pNode);
118 return new IfContext( *this, rAttribs, pNode );
120 case DGM_TOKEN( else ):
122 // CT_Otherwise
123 ConditionAtomPtr pNode = std::make_shared<ConditionAtom>(mpNode->getLayoutNode(), true, rAttribs.getFastAttributeList());
124 LayoutAtom::connect(mpNode, pNode);
125 return new IfContext( *this, rAttribs, pNode );
127 default:
128 break;
131 return this;
133 private:
134 OUString msName;
135 LayoutAtomPtr mpNode;
138 class ForEachContext
139 : public LayoutNodeContext
141 public:
142 ForEachContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs, const ForEachAtomPtr& pAtom )
143 : LayoutNodeContext( rParent, rAttribs, pAtom )
145 pAtom->setRef(rAttribs.getStringDefaulted(XML_ref));
146 pAtom->iterator().loadFromXAttr( rAttribs.getFastAttributeList() );
148 LayoutAtomMap& rLayoutAtomMap = pAtom->getLayoutNode().getDiagram().getLayout()->getLayoutAtomMap();
149 rLayoutAtomMap[pAtom->getName()] = pAtom;
153 // CT_LayoutVariablePropertySet
154 class LayoutVariablePropertySetContext
155 : public ContextHandler2
157 public:
158 LayoutVariablePropertySetContext( ContextHandler2Helper const & rParent, LayoutNode::VarMap & aVar )
159 : ContextHandler2( rParent )
160 , mVariables( aVar )
164 virtual ContextHandlerRef onCreateContext( ::sal_Int32 aElement, const AttributeList& rAttribs ) override
166 mVariables[ getBaseToken(aElement) ] = rAttribs.getStringDefaulted( XML_val );
167 return this;
169 private:
170 LayoutNode::VarMap & mVariables;
175 // CT_LayoutNode
176 LayoutNodeContext::LayoutNodeContext( ContextHandler2Helper const & rParent,
177 const AttributeList& rAttribs,
178 const LayoutAtomPtr& pAtom )
179 : ContextHandler2( rParent )
180 , mpNode( pAtom )
182 assert( pAtom && "Node must NOT be NULL" );
183 mpNode->setName( rAttribs.getStringDefaulted( XML_name ) );
186 LayoutNodeContext::~LayoutNodeContext()
190 ContextHandlerRef
191 LayoutNodeContext::onCreateContext( ::sal_Int32 aElement,
192 const AttributeList& rAttribs )
194 switch( aElement )
196 case DGM_TOKEN( layoutNode ):
198 LayoutNodePtr pNode = std::make_shared<LayoutNode>(mpNode->getLayoutNode().getDiagram());
199 LayoutAtom::connect(mpNode, pNode);
201 if (rAttribs.hasAttribute(XML_chOrder))
203 pNode->setChildOrder(rAttribs.getToken(XML_chOrder, XML_b));
205 else
207 for (LayoutAtomPtr pAtom = mpNode; pAtom; pAtom = pAtom->getParent())
209 auto pLayoutNode = dynamic_cast<LayoutNode*>(pAtom.get());
210 if (pLayoutNode)
212 pNode->setChildOrder(pLayoutNode->getChildOrder());
213 break;
218 pNode->setMoveWith( rAttribs.getStringDefaulted( XML_moveWith ) );
219 pNode->setStyleLabel( rAttribs.getStringDefaulted( XML_styleLbl ) );
220 return new LayoutNodeContext( *this, rAttribs, pNode );
222 case DGM_TOKEN( shape ):
224 ShapePtr pShape;
226 if( rAttribs.hasAttribute( XML_type ) )
228 pShape = std::make_shared<Shape>("com.sun.star.drawing.CustomShape");
229 if (!rAttribs.getBool(XML_hideGeom, false))
231 const sal_Int32 nType(rAttribs.getToken( XML_type, XML_obj ));
232 pShape->setSubType( nType );
233 pShape->getCustomShapeProperties()->setShapePresetType( nType );
236 else
238 pShape = std::make_shared<Shape>("com.sun.star.drawing.GroupShape");
241 pShape->setDiagramRotation(rAttribs.getInteger(XML_rot, 0) * PER_DEGREE);
243 pShape->setZOrderOff(rAttribs.getInteger(XML_zOrderOff, 0));
245 ShapeAtomPtr pAtom = std::make_shared<ShapeAtom>(mpNode->getLayoutNode(), pShape);
246 LayoutAtom::connect(mpNode, pAtom);
247 return new ShapeContext( *this, ShapePtr(), std::move(pShape) );
249 case DGM_TOKEN( extLst ):
250 return nullptr;
251 case DGM_TOKEN( alg ):
253 // CT_Algorithm
254 AlgAtomPtr pAtom = std::make_shared<AlgAtom>(mpNode->getLayoutNode());
255 LayoutAtom::connect(mpNode, pAtom);
256 return new AlgorithmContext( *this, rAttribs, pAtom );
258 case DGM_TOKEN( choose ):
260 // CT_Choose
261 LayoutAtomPtr pAtom = std::make_shared<ChooseAtom>(mpNode->getLayoutNode());
262 LayoutAtom::connect(mpNode, pAtom);
263 return new ChooseContext( *this, rAttribs, std::move(pAtom) );
265 case DGM_TOKEN( forEach ):
267 // CT_ForEach
268 ForEachAtomPtr pAtom = std::make_shared<ForEachAtom>(mpNode->getLayoutNode(), rAttribs.getFastAttributeList());
269 LayoutAtom::connect(mpNode, pAtom);
270 return new ForEachContext( *this, rAttribs, pAtom );
272 case DGM_TOKEN( constrLst ):
273 // CT_Constraints
274 return new ConstraintListContext( *this, mpNode );
275 case DGM_TOKEN( presOf ):
277 // CT_PresentationOf
278 // TODO
279 IteratorAttr aIterator;
280 aIterator.loadFromXAttr(rAttribs.getFastAttributeList());
281 break;
283 case DGM_TOKEN( ruleLst ):
284 // CT_Rules
285 return new RuleListContext( *this, mpNode );
286 case DGM_TOKEN( varLst ):
288 LayoutNodePtr pNode(std::dynamic_pointer_cast<LayoutNode>(mpNode));
289 if( pNode )
291 return new LayoutVariablePropertySetContext( *this, pNode->variables() );
293 else
295 SAL_WARN("oox", "OOX: encountered a varLst in a non layoutNode context" );
297 break;
299 default:
300 break;
303 return this;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */