use insert function instead of for loop
[LibreOffice.git] / oox / source / drawingml / diagram / diagramlayoutatoms.hxx
blob5458999029cbce0c987400194ff371134df147ae
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 #ifndef INCLUDED_OOX_SOURCE_DRAWINGML_DIAGRAM_DIAGRAMLAYOUTATOMS_HXX
21 #define INCLUDED_OOX_SOURCE_DRAWINGML_DIAGRAM_DIAGRAMLAYOUTATOMS_HXX
23 #include <map>
24 #include <memory>
26 #include <com/sun/star/xml/sax/XFastAttributeList.hpp>
27 #include <utility>
29 #include "diagram.hxx"
31 namespace oox::drawingml {
33 class DiagramLayout;
34 typedef std::shared_ptr< DiagramLayout > DiagramLayoutPtr;
36 // AG_IteratorAttributes
37 struct IteratorAttr
39 IteratorAttr();
41 // not sure this belong here, but wth
42 void loadFromXAttr( const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes );
44 std::vector<sal_Int32> maAxis;
45 sal_Int32 mnCnt;
46 bool mbHideLastTrans;
47 sal_Int32 mnPtType;
48 sal_Int32 mnSt;
49 sal_Int32 mnStep;
52 struct ConditionAttr
54 ConditionAttr();
56 // not sure this belong here, but wth
57 void loadFromXAttr( const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes );
59 OUString msVal;
60 sal_Int32 mnFunc;
61 sal_Int32 mnArg;
62 sal_Int32 mnOp;
63 sal_Int32 mnVal;
66 /// Constraints allow you to specify an ideal (or starting point) size for each shape.
67 struct Constraint
69 OUString msForName;
70 OUString msRefForName;
71 double mfFactor;
72 double mfValue;
73 sal_Int32 mnFor;
74 sal_Int32 mnPointType;
75 sal_Int32 mnType;
76 sal_Int32 mnRefFor;
77 sal_Int32 mnRefType;
78 sal_Int32 mnRefPointType;
79 sal_Int32 mnOperator;
82 /// Rules allow you to specify what to do when constraints can't be fully satisfied.
83 struct Rule
85 OUString msForName;
88 typedef std::map<sal_Int32, sal_Int32> LayoutProperty;
89 typedef std::map<OUString, LayoutProperty> LayoutPropertyMap;
91 struct LayoutAtomVisitor;
92 class LayoutAtom;
93 class LayoutNode;
95 typedef std::shared_ptr< LayoutAtom > LayoutAtomPtr;
97 /** abstract Atom for the layout */
98 class LayoutAtom
100 public:
101 LayoutAtom(LayoutNode& rLayoutNode) : mrLayoutNode(rLayoutNode) {}
102 virtual ~LayoutAtom() { }
104 LayoutNode& getLayoutNode()
105 { return mrLayoutNode; }
107 /** visitor acceptance
109 virtual void accept( LayoutAtomVisitor& ) = 0;
111 void setName( const OUString& sName )
112 { msName = sName; }
113 const OUString& getName() const
114 { return msName; }
116 private:
117 void addChild( const LayoutAtomPtr & pNode )
118 { mpChildNodes.push_back( pNode ); }
119 void setParent(const LayoutAtomPtr& pParent) { mpParent = pParent; }
121 public:
122 const std::vector<LayoutAtomPtr>& getChildren() const
123 { return mpChildNodes; }
125 LayoutAtomPtr getParent() const { return mpParent.lock(); }
127 static void connect(const LayoutAtomPtr& pParent, const LayoutAtomPtr& pChild)
129 pParent->addChild(pChild);
130 pChild->setParent(pParent);
133 // dump for debug
134 void dump(int level = 0);
136 protected:
137 LayoutNode& mrLayoutNode;
138 std::vector< LayoutAtomPtr > mpChildNodes;
139 std::weak_ptr<LayoutAtom> mpParent;
140 OUString msName;
143 class ConstraintAtom
144 : public LayoutAtom
146 public:
147 ConstraintAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode) {}
148 virtual void accept( LayoutAtomVisitor& ) override;
149 Constraint& getConstraint()
150 { return maConstraint; }
151 void parseConstraint(std::vector<Constraint>& rConstraints, bool bRequireForName) const;
152 private:
153 Constraint maConstraint;
156 /// Represents one <dgm:rule> element.
157 class RuleAtom
158 : public LayoutAtom
160 public:
161 RuleAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode) {}
162 virtual void accept( LayoutAtomVisitor& ) override;
163 Rule& getRule()
164 { return maRule; }
165 void parseRule(std::vector<Rule>& rRules) const;
166 private:
167 Rule maRule;
170 class AlgAtom
171 : public LayoutAtom
173 public:
174 AlgAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode), mnType(0), maMap() {}
176 typedef std::map<sal_Int32,sal_Int32> ParamMap;
178 virtual void accept( LayoutAtomVisitor& ) override;
180 void setType( sal_Int32 nToken )
181 { mnType = nToken; }
182 const ParamMap& getMap() const { return maMap; }
183 void addParam( sal_Int32 nType, sal_Int32 nVal )
184 { maMap[nType]=nVal; }
185 sal_Int32 getVerticalShapesCount(const ShapePtr& rShape);
186 void layoutShape( const ShapePtr& rShape,
187 const std::vector<Constraint>& rConstraints,
188 const std::vector<Rule>& rRules );
190 void setAspectRatio(double fAspectRatio) { mfAspectRatio = fAspectRatio; }
192 double getAspectRatio() const { return mfAspectRatio; }
194 private:
195 sal_Int32 mnType;
196 ParamMap maMap;
197 /// Aspect ratio is not integer, so not part of maMap.
198 double mfAspectRatio = 0;
200 /// Determines the connector shape type for conn algorithm
201 sal_Int32 getConnectorType();
204 typedef std::shared_ptr< AlgAtom > AlgAtomPtr;
206 /// Finds optimal grid to layout children that have fixed aspect ratio.
207 class SnakeAlg
209 public:
210 static void layoutShapeChildren(const AlgAtom& rAlg, const ShapePtr& rShape,
211 const std::vector<Constraint>& rConstraints);
215 * Lays out child layout nodes along a vertical path and works with the trapezoid shape to create a
216 * pyramid.
218 class PyraAlg
220 public:
221 static void layoutShapeChildren(const ShapePtr& rShape);
225 * Specifies the size and position for all child layout nodes.
227 class CompositeAlg
229 public:
230 static void layoutShapeChildren(AlgAtom& rAlg, const ShapePtr& rShape,
231 const std::vector<Constraint>& rConstraints);
233 private:
235 * Apply rConstraint to the rProperties shared layout state.
237 * Note that the order in which constraints are applied matters, given that constraints can refer to
238 * each other, and in case A depends on B and A is applied before B, the effect of A won't be
239 * updated when B is applied.
241 static void applyConstraintToLayout(const Constraint& rConstraint,
242 LayoutPropertyMap& rProperties);
245 * Decides if a certain reference type (e.g. "right") can be inferred from the available properties
246 * in rMap (e.g. left and width). Returns true if rValue is written to.
248 static bool inferFromLayoutProperty(const LayoutProperty& rMap, sal_Int32 nRefType,
249 sal_Int32& rValue);
252 class ForEachAtom
253 : public LayoutAtom
255 public:
256 explicit ForEachAtom(LayoutNode& rLayoutNode, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes);
258 IteratorAttr & iterator()
259 { return maIter; }
260 void setRef(const OUString& rsRef)
261 { msRef = rsRef; }
262 const OUString& getRef() const
263 { return msRef; }
264 virtual void accept( LayoutAtomVisitor& ) override;
265 LayoutAtomPtr getRefAtom();
267 private:
268 IteratorAttr maIter;
269 OUString msRef;
272 typedef std::shared_ptr< ForEachAtom > ForEachAtomPtr;
274 class ConditionAtom
275 : public LayoutAtom
277 public:
278 explicit ConditionAtom(LayoutNode& rLayoutNode, bool isElse, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes);
279 virtual void accept( LayoutAtomVisitor& ) override;
280 bool getDecision(const svx::diagram::Point* pPresPoint) const;
282 private:
283 static bool compareResult(sal_Int32 nOperator, sal_Int32 nFirst, sal_Int32 nSecond);
284 sal_Int32 getNodeCount(const svx::diagram::Point* pPresPoint) const;
286 bool mIsElse;
287 IteratorAttr maIter;
288 ConditionAttr maCond;
291 typedef std::shared_ptr< ConditionAtom > ConditionAtomPtr;
293 /** "choose" statements. Atoms will be tested in order. */
294 class ChooseAtom
295 : public LayoutAtom
297 public:
298 ChooseAtom(LayoutNode& rLayoutNode)
299 : LayoutAtom(rLayoutNode)
301 virtual void accept( LayoutAtomVisitor& ) override;
304 class LayoutNode
305 : public LayoutAtom
307 public:
308 typedef std::map<sal_Int32, OUString> VarMap;
310 LayoutNode(Diagram& rDgm)
311 : LayoutAtom(*this)
312 , mrDgm(rDgm)
313 , mnChildOrder(0)
316 Diagram& getDiagram() { return mrDgm; }
317 virtual void accept( LayoutAtomVisitor& ) override;
318 VarMap & variables()
319 { return mVariables; }
320 void setMoveWith( const OUString & sName )
321 { msMoveWith = sName; }
322 void setStyleLabel( const OUString & sLabel )
323 { msStyleLabel = sLabel; }
324 void setChildOrder( sal_Int32 nOrder )
325 { mnChildOrder = nOrder; }
326 sal_Int32 getChildOrder() const { return mnChildOrder; }
327 void setExistingShape( const ShapePtr& pShape )
328 { mpExistingShape = pShape; }
329 const ShapePtr& getExistingShape() const
330 { return mpExistingShape; }
331 const std::vector<ShapePtr> & getNodeShapes() const
332 { return mpNodeShapes; }
333 void addNodeShape(const ShapePtr& pShape)
334 { mpNodeShapes.push_back(pShape); }
336 bool setupShape( const ShapePtr& rShape,
337 const svx::diagram::Point* pPresNode,
338 sal_Int32 nCurrIdx ) const;
340 const LayoutNode* getParentLayoutNode() const;
342 private:
343 Diagram& mrDgm;
344 VarMap mVariables;
345 OUString msMoveWith;
346 OUString msStyleLabel;
347 ShapePtr mpExistingShape;
348 std::vector<ShapePtr> mpNodeShapes;
349 sal_Int32 mnChildOrder;
352 typedef std::shared_ptr< LayoutNode > LayoutNodePtr;
354 class ShapeAtom
355 : public LayoutAtom
357 public:
358 ShapeAtom(LayoutNode& rLayoutNode, ShapePtr pShape) : LayoutAtom(rLayoutNode), mpShapeTemplate(std::move(pShape)) {}
359 virtual void accept( LayoutAtomVisitor& ) override;
360 const ShapePtr& getShapeTemplate() const
361 { return mpShapeTemplate; }
363 private:
364 ShapePtr mpShapeTemplate;
367 typedef std::shared_ptr< ShapeAtom > ShapeAtomPtr;
371 #endif
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */