Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/drawingml/diagram/diagramlayoutatoms.hxx
Line
Count
Source
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
#ifndef INCLUDED_OOX_SOURCE_DRAWINGML_DIAGRAM_DIAGRAMLAYOUTATOMS_HXX
21
#define INCLUDED_OOX_SOURCE_DRAWINGML_DIAGRAM_DIAGRAMLAYOUTATOMS_HXX
22
23
#include <map>
24
#include <memory>
25
26
#include <com/sun/star/xml/sax/XFastAttributeList.hpp>
27
#include <utility>
28
29
#include "diagram.hxx"
30
31
namespace oox::drawingml {
32
33
typedef std::shared_ptr< DiagramLayout > DiagramLayoutPtr;
34
35
// AG_IteratorAttributes
36
struct IteratorAttr
37
{
38
    IteratorAttr();
39
40
    // not sure this belong here, but wth
41
    void loadFromXAttr( const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes );
42
43
    std::vector<sal_Int32> maAxis;
44
    sal_Int32 mnCnt;
45
    bool  mbHideLastTrans;
46
    sal_Int32 mnPtType;
47
    sal_Int32 mnSt;
48
    sal_Int32 mnStep;
49
};
50
51
struct ConditionAttr
52
{
53
    ConditionAttr();
54
55
    // not sure this belong here, but wth
56
    void loadFromXAttr( const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes );
57
58
    OUString msVal;
59
    sal_Int32 mnFunc;
60
    sal_Int32 mnArg;
61
    sal_Int32 mnOp;
62
    sal_Int32 mnVal;
63
};
64
65
/// Constraints allow you to specify an ideal (or starting point) size for each shape.
66
struct Constraint
67
{
68
    OUString msForName;
69
    OUString msRefForName;
70
    double mfFactor;
71
    double mfValue;
72
    sal_Int32 mnFor;
73
    sal_Int32 mnPointType;
74
    sal_Int32 mnType;
75
    sal_Int32 mnRefFor;
76
    sal_Int32 mnRefType;
77
    sal_Int32 mnRefPointType;
78
    sal_Int32 mnOperator;
79
};
80
81
/// Rules allow you to specify what to do when constraints can't be fully satisfied.
82
struct Rule
83
{
84
    OUString msForName;
85
};
86
87
typedef std::map<sal_Int32, sal_Int32> LayoutProperty;
88
typedef std::map<OUString, LayoutProperty> LayoutPropertyMap;
89
90
struct LayoutAtomVisitor;
91
class LayoutAtom;
92
class LayoutNode;
93
94
typedef std::shared_ptr< LayoutAtom > LayoutAtomPtr;
95
96
/** abstract Atom for the layout */
97
class LayoutAtom
98
{
99
public:
100
21.2k
    LayoutAtom(LayoutNode& rLayoutNode) : mrLayoutNode(rLayoutNode) {}
101
21.2k
    virtual ~LayoutAtom() { }
102
103
    LayoutNode& getLayoutNode()
104
22.5k
        { return mrLayoutNode; }
105
106
    /** visitor acceptance
107
     */
108
    virtual void accept( LayoutAtomVisitor& ) = 0;
109
110
    void setName( const OUString& sName )
111
4.83k
        { msName = sName; }
112
    const OUString& getName() const
113
2.85k
        { return msName; }
114
115
private:
116
    void addChild( const LayoutAtomPtr & pNode )
117
21.0k
        { mpChildNodes.push_back( pNode ); }
118
21.0k
    void setParent(const LayoutAtomPtr& pParent) { mpParent = pParent; }
119
120
public:
121
    const std::vector<LayoutAtomPtr>& getChildren() const
122
29.0k
        { return mpChildNodes; }
123
124
2.25k
    LayoutAtomPtr getParent() const { return mpParent.lock(); }
125
126
    static void connect(const LayoutAtomPtr& pParent, const LayoutAtomPtr& pChild)
127
21.0k
    {
128
21.0k
        pParent->addChild(pChild);
129
21.0k
        pChild->setParent(pParent);
130
21.0k
    }
131
132
    // dump for debug
133
    void dump(int level = 0);
134
135
protected:
136
    LayoutNode&            mrLayoutNode;
137
    std::vector< LayoutAtomPtr > mpChildNodes;
138
    std::weak_ptr<LayoutAtom> mpParent;
139
    OUString                     msName;
140
};
141
142
class ConstraintAtom
143
    : public LayoutAtom
144
{
145
public:
146
10.6k
    ConstraintAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode) {}
147
    virtual void accept( LayoutAtomVisitor& ) override;
148
    Constraint& getConstraint()
149
10.7k
        { return maConstraint; }
150
    void parseConstraint(std::vector<Constraint>& rConstraints, bool bRequireForName) const;
151
private:
152
    Constraint maConstraint;
153
};
154
155
/// Represents one <dgm:rule> element.
156
class RuleAtom
157
    : public LayoutAtom
158
{
159
public:
160
1.00k
    RuleAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode) {}
161
    virtual void accept( LayoutAtomVisitor& ) override;
162
    Rule& getRule()
163
1.00k
        { return maRule; }
164
    void parseRule(std::vector<Rule>& rRules) const;
165
private:
166
    Rule maRule;
167
};
168
169
class AlgAtom
170
    : public LayoutAtom
171
{
172
public:
173
1.70k
    AlgAtom(LayoutNode& rLayoutNode) : LayoutAtom(rLayoutNode), mnType(0), maMap() {}
174
175
    typedef std::map<sal_Int32,sal_Int32> ParamMap;
176
177
    virtual void accept( LayoutAtomVisitor& ) override;
178
179
    void setType( sal_Int32 nToken )
180
1.70k
        { mnType = nToken; }
181
0
    const ParamMap& getMap() const { return maMap; }
182
    void addParam( sal_Int32 nType, sal_Int32 nVal )
183
1.37k
        { maMap[nType]=nVal; }
184
    sal_Int32 getVerticalShapesCount(const ShapePtr& rShape);
185
    void layoutShape( const ShapePtr& rShape,
186
                      const std::vector<Constraint>& rConstraints,
187
                      const std::vector<Rule>& rRules );
188
189
26
    void setAspectRatio(double fAspectRatio) { mfAspectRatio = fAspectRatio; }
190
191
453
    double getAspectRatio() const { return mfAspectRatio; }
192
193
private:
194
    sal_Int32 mnType;
195
    ParamMap  maMap;
196
    /// Aspect ratio is not integer, so not part of maMap.
197
    double mfAspectRatio = 0;
198
199
    /// Determines the connector shape type for conn algorithm
200
    sal_Int32 getConnectorType();
201
};
202
203
typedef std::shared_ptr< AlgAtom > AlgAtomPtr;
204
205
/// Finds optimal grid to layout children that have fixed aspect ratio.
206
class SnakeAlg
207
{
208
public:
209
    static void layoutShapeChildren(const AlgAtom& rAlg, const ShapePtr& rShape,
210
                                    const std::vector<Constraint>& rConstraints);
211
};
212
213
/**
214
 * Lays out child layout nodes along a vertical path and works with the trapezoid shape to create a
215
 * pyramid.
216
 */
217
class PyraAlg
218
{
219
public:
220
    static void layoutShapeChildren(const ShapePtr& rShape);
221
};
222
223
/**
224
 * Specifies the size and position for all child layout nodes.
225
 */
226
class CompositeAlg
227
{
228
public:
229
    static void layoutShapeChildren(AlgAtom& rAlg, const ShapePtr& rShape,
230
                                    const std::vector<Constraint>& rConstraints);
231
232
private:
233
    /**
234
     * Apply rConstraint to the rProperties shared layout state.
235
     *
236
     * Note that the order in which constraints are applied matters, given that constraints can refer to
237
     * each other, and in case A depends on B and A is applied before B, the effect of A won't be
238
     * updated when B is applied.
239
     */
240
    static void applyConstraintToLayout(const Constraint& rConstraint,
241
                                        LayoutPropertyMap& rProperties);
242
243
    /**
244
     * Decides if a certain reference type (e.g. "right") can be inferred from the available properties
245
     * in rMap (e.g. left and width). Returns true if rValue is written to.
246
     */
247
    static bool inferFromLayoutProperty(const LayoutProperty& rMap, sal_Int32 nRefType,
248
                                        sal_Int32& rValue);
249
};
250
251
class ForEachAtom
252
    : public LayoutAtom
253
{
254
public:
255
    explicit ForEachAtom(LayoutNode& rLayoutNode, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes);
256
257
    IteratorAttr & iterator()
258
9.43k
        { return maIter; }
259
    void setRef(const OUString& rsRef)
260
727
        { msRef = rsRef; }
261
    const OUString& getRef() const
262
1.33k
        { return msRef; }
263
    virtual void accept( LayoutAtomVisitor& ) override;
264
    LayoutAtomPtr getRefAtom();
265
266
private:
267
    IteratorAttr maIter;
268
    OUString msRef;
269
};
270
271
typedef std::shared_ptr< ForEachAtom > ForEachAtomPtr;
272
273
class ConditionAtom
274
    : public LayoutAtom
275
{
276
public:
277
    explicit ConditionAtom(LayoutNode& rLayoutNode, bool isElse, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttributes);
278
    virtual void accept( LayoutAtomVisitor& ) override;
279
    bool getDecision(const svx::diagram::Point* pPresPoint) const;
280
281
private:
282
    static bool compareResult(sal_Int32 nOperator, sal_Int32 nFirst, sal_Int32 nSecond);
283
    sal_Int32 getNodeCount(const svx::diagram::Point* pPresPoint) const;
284
285
    bool          mIsElse;
286
    IteratorAttr  maIter;
287
    ConditionAttr maCond;
288
};
289
290
typedef std::shared_ptr< ConditionAtom > ConditionAtomPtr;
291
292
/** "choose" statements. Atoms will be tested in order. */
293
class ChooseAtom
294
    : public LayoutAtom
295
{
296
public:
297
    ChooseAtom(LayoutNode& rLayoutNode)
298
1.26k
        : LayoutAtom(rLayoutNode)
299
1.26k
    {}
300
    virtual void accept( LayoutAtomVisitor& ) override;
301
};
302
303
class LayoutNode
304
    : public LayoutAtom
305
{
306
public:
307
    typedef std::map<sal_Int32, OUString> VarMap;
308
309
    LayoutNode(Diagram& rDgm)
310
1.40k
        : LayoutAtom(*this)
311
1.40k
        , mrDgm(rDgm)
312
1.40k
        , mnChildOrder(0)
313
1.40k
    {
314
1.40k
    }
315
3.60k
    Diagram& getDiagram() { return mrDgm; }
316
    virtual void accept( LayoutAtomVisitor& ) override;
317
    VarMap & variables()
318
759
        { return mVariables; }
319
    void setMoveWith( const OUString & sName )
320
1.40k
        { msMoveWith = sName; }
321
    void setStyleLabel( const OUString & sLabel )
322
1.40k
        { msStyleLabel = sLabel; }
323
    void setChildOrder( sal_Int32 nOrder )
324
1.40k
        { mnChildOrder = nOrder; }
325
1.18k
    sal_Int32 getChildOrder() const { return mnChildOrder; }
326
    void setExistingShape( const ShapePtr& pShape )
327
43
        { mpExistingShape = pShape; }
328
    const ShapePtr& getExistingShape() const
329
479
        { return mpExistingShape; }
330
    const std::vector<ShapePtr> & getNodeShapes() const
331
0
        { return mpNodeShapes; }
332
    void addNodeShape(const ShapePtr& pShape)
333
436
        { mpNodeShapes.push_back(pShape); }
334
335
    bool setupShape( const ShapePtr& rShape,
336
                     const svx::diagram::Point* pPresNode,
337
                     sal_Int32 nCurrIdx ) const;
338
339
    const LayoutNode* getParentLayoutNode() const;
340
341
private:
342
    Diagram& mrDgm;
343
    VarMap                       mVariables;
344
    OUString                     msMoveWith;
345
    OUString                     msStyleLabel;
346
    ShapePtr                     mpExistingShape;
347
    std::vector<ShapePtr>        mpNodeShapes;
348
    sal_Int32                    mnChildOrder;
349
};
350
351
typedef std::shared_ptr< LayoutNode > LayoutNodePtr;
352
353
class ShapeAtom
354
    : public LayoutAtom
355
{
356
public:
357
1.78k
    ShapeAtom(LayoutNode& rLayoutNode, ShapePtr pShape) : LayoutAtom(rLayoutNode), mpShapeTemplate(std::move(pShape)) {}
358
    virtual void accept( LayoutAtomVisitor& ) override;
359
    const ShapePtr& getShapeTemplate() const
360
393
        { return mpShapeTemplate; }
361
362
private:
363
    ShapePtr mpShapeTemplate;
364
};
365
366
typedef std::shared_ptr< ShapeAtom > ShapeAtomPtr;
367
368
}
369
370
#endif
371
372
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */