KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
createelementcommand.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
17
18#include "elementfactory.h"
19#include "elementmodel.h"
20#include "state.h"
21
22#include "debug.h"
23
24using namespace KDSME;
25
27 : Command(model, parent)
28 , m_parentElement(nullptr)
29 , m_type(type)
30 , m_createdElement(nullptr)
31{
32 updateText();
33}
34
36{
37 if (m_createdElement && !m_createdElement->parent())
38 delete m_createdElement;
39}
40
42{
43 return m_parentElement;
44}
45
47{
48 if (m_parentElement == parentElement)
49 return;
50
51 m_parentElement = parentElement;
52 Q_EMIT parentElementChanged(m_parentElement);
53}
54
56{
57 return m_type;
58}
59
61{
62 if (m_type == type)
63 return;
64
65 m_type = type;
66 Q_EMIT typeChanged(m_type);
67}
68
70{
71 return m_createdElement;
72}
73
75{
76 static const ElementFactory factory;
77
78 if (!model() || m_type == Element::ElementType) {
79 qCDebug(KDSME_VIEW) << "Can't redo, invalid model or type";
80 return;
81 }
82
83 Element *parentElementL = m_parentElement ? m_parentElement : model()->state();
84 const StateModel::AppendOperation append(model(), parentElementL);
85 if (m_createdElement) {
86 m_createdElement->setParent(parentElementL);
87 } else {
88 Element *element = factory.create(m_type);
89 if (!element) {
90 qCDebug(KDSME_VIEW) << "Element could not be instantiated, type:" << m_type;
91 return;
92 }
93
94 element->setLabel(tr("Unnamed"));
95 element->setParent(parentElementL);
96 m_createdElement = element;
97 }
98 updateText();
99}
100
102{
103 if (!m_createdElement) {
104 qCDebug(KDSME_VIEW) << "Aborting undo, element was never created";
105 return;
106 }
107
108 {
109 const StateModel::RemoveOperation remove(model(), m_createdElement);
110 m_createdElement->setParent(nullptr);
111 }
112}
113
114void CreateElementCommand::updateText()
115{
116 setText(tr("Create %1").arg(m_createdElement ? m_createdElement->toDisplayString() : QStringLiteral("<No element>")));
117}
StateModel * model() const
Definition command.cpp:34
void setType(Element::Type type)
void parentElementChanged(KDSME::Element *parentElement)
void setParentElement(Element *parentElement)
void typeChanged(KDSME::Element::Type type)
CreateElementCommand(StateModel *model=nullptr, Element::Type type=Element::ElementType, QUndoCommand *parent=nullptr)
Element * create(Element::Type type) const
void setLabel(const QString &label)
Definition element.cpp:63
void setParent(QObject *object)
Definition element.cpp:240
virtual QString toDisplayString() const
Definition element.cpp:220
KDSME::Element * parent
Definition element.h:37
KDSME::State * state

© 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