KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
layerwiselayouter.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14*/
15
16#include <config-kdsme.h>
17
18#include "layerwiselayouter.h"
19
20#if HAVE_GRAPHVIZ
23#endif
24#include "layoutproperties.h"
25#include "layoututils.h"
26#include "elementwalker.h"
27#include "element.h"
28#include "state.h"
29#include "transition.h"
30
31#include "debug.h"
32#include <QElapsedTimer>
33#include <QFontMetrics>
34#include <QRectF>
35
36using namespace KDSME;
37
38namespace {
39
40QRectF boundingRectForCollapsedRegion(State *state, const LayoutProperties *prop)
41{
42 static const QRectF minimumSize(0, 0, 100, 20);
43
44 Q_ASSERT(state);
45 if (!state) {
46 return minimumSize;
47 }
48
49 const QString label = state->label();
50 const QFontMetricsF fm(prop->regionLabelFont());
51 const qreal width = fm.horizontalAdvance(label);
52 const qreal height = fm.height();
53 const qreal margin = prop->regionLabelMargins();
54 return QRectF(0, 0, width + prop->regionLabelButtonBoxSize().width() + 2 * margin, height + 2 * margin);
55}
56
57}
58
60 : QObject(parent)
61{
62}
63
64void RegionLayouter::layoutRegion(State *state, const QRectF &boundingRectHint, const LayoutProperties *properties)
65{
66 Q_ASSERT(state);
67 if (state->childStates().isEmpty()) {
68 return; // no composite state, do nothing
69 }
70
71 // Calculate bounding rect on our own in case it is empty
72 Q_ASSERT(!boundingRectHint.isEmpty());
73 const QRectF boundingRect = boundingRectHint;
74
75 const qreal fontHeight = properties->regionLabelFont().pointSizeF();
76 const qreal regionLabelMargin = properties->regionMargins();
77 const qreal regionLabelHeight = regionLabelMargin + fontHeight + regionLabelMargin;
78 const qreal innerMargin = properties->regionMargins();
79
80 const QPointF offset = QPointF(innerMargin, innerMargin + regionLabelHeight);
81 state->setWidth(boundingRect.width() + (2 * innerMargin));
82 state->setHeight(boundingRect.height() + (2 * innerMargin) + regionLabelHeight);
83 LayoutUtils::moveInner(state, offset);
84}
85
87 : Layouter(parent)
88#if HAVE_GRAPHVIZ
89 , m_layerLayouter(new GraphvizLayerLayouter(this))
90#else
91 , m_layerLayouter(nullptr)
92#endif
93 , m_regionLayouter(new RegionLayouter(this))
94 , m_properties(nullptr)
95{
96 qCDebug(KDSME_CORE) << "Using" << m_layerLayouter << "as layouter";
97}
98
99QRectF LayerwiseLayouter::layout(State *state, const LayoutProperties *properties)
100{
101 Q_ASSERT(state);
102 m_properties = properties;
103
105 walker.walkItems(state, [&](Element *element) { return layoutState(element); });
106
107 return QRect();
108}
109
110ElementWalker::VisitResult LayerwiseLayouter::layoutState(Element *element)
111{
112 auto *state = qobject_cast<State *>(element);
113 if (!state || state->childStates().isEmpty()) {
115 }
116
117 QRectF boundingRect;
118#if HAVE_GRAPHVIZ
119 if (state->isExpanded()) {
120 boundingRect = m_layerLayouter->layout(state, m_properties);
121 } else {
122 boundingRect = boundingRectForCollapsedRegion(state, m_properties);
123 }
124#else
125 // let's just show collapsed states in this case
126 boundingRect = boundingRectForCollapsedRegion(state, m_properties);
127#endif
128
129 m_regionLayouter->layoutRegion(state, boundingRect, m_properties);
130
132}
void setWidth(qreal width)
Definition element.cpp:111
QString label
Definition element.h:40
void setHeight(qreal height)
Definition element.cpp:125
QRectF layout(State *state, const LayoutProperties *properties) override
LayerwiseLayouter(QObject *parent=nullptr)
QRectF layout(State *state, const LayoutProperties *properties) override
static bool moveInner(State *state, const QPointF &offset)
Move all direct children of state state by offset offset.
RegionLayouter(QObject *parent=nullptr)
void layoutRegion(State *state, const QRectF &boundingRectHint, const LayoutProperties *properties)
bool isExpanded() const
Definition state.cpp:181
QList< State * > childStates() const
Definition state.cpp:93
bool walkItems(T item, const VisitFunction &visit)
Definition treewalker.h:74
@ RecursiveWalk
Traverse the children of this item.
Definition treewalker.h:49
QRectF boundingRect(QSGGeometry *geometry)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8