KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
layoututils.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 "layoututils.h"
17
18#include "element.h"
19#include "state.h"
20#include "transition.h"
21
22#include <QGuiApplication>
23#include "debug.h"
24#include <QFontMetricsF>
25#include <QSizeF>
26
27using namespace KDSME;
28
29QSizeF LayoutUtils::sizeForLabel(const QString &label)
30{
31 // TODO: Get LayoutProperties here
32 const qreal margin = 10;
33
34 // TODO: Improve
35 const QFontMetricsF fm(QGuiApplication::font());
36 const qreal width = fm.horizontalAdvance(label);
37 return QSizeF(width + (2 * margin), fm.height() + (2 * margin));
38}
39
40bool LayoutUtils::moveInner(State *state, const QPointF &offset) // cppcheck-suppress constParameterPointer // clazy:exclude=function-args-by-value
41{
42 if (!state) {
43 return false;
44 }
45
46 const auto childStates = state->childStates();
47 for (State *childState : childStates) {
48 childState->setPos(childState->pos() + offset);
49 }
50 return true;
51}
52
54{
55 /*
56 LayoutWalker walker;
57 walker.walkItems(state, [&](Element* obj) -> LayoutWalker::VisitResult {
58 return LayoutWalker::RecursiveWalk;
59 });
60 */
61 return true;
62}
63
65{
66 if (!item || !parentItem)
67 return false;
68
69 const auto *oldParent = item->parentElement();
70 if (oldParent == parentItem)
71 return false; // do nothing
72
73 // map item coordinates to new parent item's coordinate system
74 const QPointF oldParentPos = (oldParent ? oldParent->absolutePos() : QPointF(0, 0));
75 const QPointF newParentPos = parentItem->absolutePos();
76 const QPointF delta = oldParentPos - newParentPos;
77
78 item->setParent(parentItem);
79 const QPointF itemRelPos = item->pos();
80 item->setPos(itemRelPos + delta);
81 return true;
82}
void setPos(const QPointF &pos)
Definition element.cpp:97
QPointF absolutePos() const
Definition element.cpp:134
QPointF pos
The position of the element from the top-left corner.
Definition element.h:42
void setParent(QObject *object)
Definition element.cpp:240
Element * parentElement() const
Definition element.cpp:205
static bool fixupLayout(State *)
static QSizeF sizeForLabel(const QString &label)
static bool moveInner(State *state, const QPointF &offset)
Move all direct children of state state by offset offset.
static bool moveToParent(Element *item, Element *parentItem)
QList< State * > childStates() const
Definition state.cpp:93

© 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