KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
elementmodel.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 "elementmodel.h"
17
18#include "objecthelper.h"
19#include "kdsmeconstants.h"
20#include "state.h"
21#include "transition.h"
22
23#include <QAbstractTransition>
24#include "debug.h"
25#include <QVariant>
26
27using namespace KDSME;
28
29namespace {
30
31Qt::ItemFlags toItemFlags(Element::Flags flags)
32{
33 Qt::ItemFlags result;
34 if (flags.testFlag(Element::ElementIsDragEnabled))
35 result |= Qt::ItemIsDragEnabled;
36 if (flags.testFlag(Element::ElementIsSelectable))
37 result |= Qt::ItemIsSelectable;
38 if (flags.testFlag(Element::ElementIsEditable))
39 result |= Qt::ItemIsEditable;
40 return result;
41}
42
43}
44
45struct StateModel::Private
46{
47 Private();
48};
49
50StateModel::Private::Private()
51{
52}
53
54StateModel::StateModel(QObject *parent)
55 : ObjectTreeModel(parent)
56 , d(new Private)
57{
58}
59
63
65{
66 return qobject_cast<State *>(rootObjects().value(0));
67}
68
70{
72 Q_EMIT stateChanged();
73}
74
75QVariant StateModel::data(const QModelIndex &index, int role) const
76{
77 auto *element = qobject_cast<Element *>(ObjectTreeModel::data(index, ObjectRole).value<QObject *>());
78 if (!element) {
79 return ObjectTreeModel::data(index, role);
80 }
81
82 switch (role) {
83 case Qt::DisplayRole:
84 return element->toDisplayString();
85 case ElementRole:
86 return QVariant::fromValue<Element *>(element);
87 case InternalIdRole:
88 return element->internalId();
89 case Qt::EditRole:
90 return element->label();
91 }
92
93 return ObjectTreeModel::data(index, role);
94}
95
96QVariant StateModel::headerData(int section, Qt::Orientation orientation, int role) const
97{
98 Q_UNUSED(section);
99 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
100 switch (section) {
101 case 0:
102 default:
103 return tr("State");
104 }
105 }
106 return QAbstractItemModel::headerData(section, orientation, role);
107}
108
109Qt::ItemFlags StateModel::flags(const QModelIndex &index) const
110{
111 const Element *element = index.data(ElementRole).value<Element *>();
112 Q_ASSERT(element);
113 if (!element) {
114 return QAbstractItemModel::flags(index);
115 }
116
117 return toItemFlags(element->flags()) | Qt::ItemIsEnabled;
118}
119
120struct TransitionModel::Private
121{
122};
123
125 : QSortFilterProxyModel(parent)
126 , d(new Private)
127{
128}
129
133
134void TransitionModel::setSourceModel(QAbstractItemModel *sourceModel)
135{
136 if (!sourceModel) {
137 QSortFilterProxyModel::setSourceModel(nullptr);
138 return;
139 }
140
141 auto *model = qobject_cast<StateModel *>(sourceModel);
142 if (!model) {
143 qCWarning(KDSME_CORE) << "called with invalid model instance:" << model;
144 return;
145 }
146
147 QSortFilterProxyModel::setSourceModel(sourceModel);
148}
149
150bool TransitionModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
151{
152 Q_UNUSED(source_row);
153
154 const auto *object = source_parent.data(StateModel::ObjectRole).value<QObject *>();
155 const auto *transition = qobject_cast<const Transition *>(object);
156
157 return transition != nullptr;
158}
159
160struct TransitionListModel::Private
161{
162 Private();
163
164 State *m_state;
165 QList<Transition *> m_transitions;
166};
167
168TransitionListModel::Private::Private()
169 : m_state(nullptr)
170{
171}
172
174 : QAbstractListModel(parent)
175 , d(new Private)
176{
177}
178
182
183int TransitionListModel::rowCount(const QModelIndex &parent) const
184{
185 if (parent.isValid())
186 return 0;
187
188 return static_cast<int>(d->m_transitions.size());
189}
190
191int TransitionListModel::columnCount(const QModelIndex &parent) const
192{
193 Q_UNUSED(parent);
194 return _LastColumn;
195}
196
197QVariant TransitionListModel::data(const QModelIndex &index, int role) const
198{
199 if (index.row() < 0 || index.row() >= rowCount()) {
200 return QVariant();
201 }
202
203 Transition *transition = d->m_transitions[index.row()];
204 Q_ASSERT(transition);
205 if (role == Qt::DisplayRole) {
206 const int column = index.column();
207 switch (column) {
208 case NameColumn:
209 return transition->toDisplayString();
211 return (transition->sourceState() ? transition->sourceState()->toDisplayString() : QStringLiteral("0x0"));
213 return (transition->targetState() ? transition->targetState()->toDisplayString() : QStringLiteral("0x0"));
214 default:
215 return QVariant();
216 }
217 } else if (role == ObjectRole) {
218 return QVariant::fromValue<Transition *>(transition);
219 }
220 return QVariant();
221}
222
223QVariant TransitionListModel::headerData(int section, Qt::Orientation orientation, int role) const
224{
225 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
226 switch (section) {
227 case NameColumn:
228 return tr("Transition");
230 return tr("Source State");
232 return tr("Target State");
233 default:
234 return QVariant();
235 }
236 }
237 return QAbstractListModel::headerData(section, orientation, role);
238}
239
240QHash<int, QByteArray> TransitionListModel::roleNames() const
241{
242 QHash<int, QByteArray> roleNames = QAbstractItemModel::roleNames();
243 roleNames.insert(ObjectRole, "object");
244 return roleNames;
245}
246
248{
249 return d->m_state;
250}
251
253{
254 beginResetModel();
255 d->m_state = state;
256 d->m_transitions = (state ? state->findChildren<Transition *>() : QList<Transition *>());
257 // TODO: Track updates to object (newly created/removed transitions)?
258 endResetModel();
259
260 Q_EMIT stateChanged();
261}
262
263#include "moc_elementmodel.cpp"
@ ElementIsSelectable
Definition element.h:73
@ ElementIsDragEnabled
Definition element.h:72
@ ElementIsEditable
Definition element.h:74
virtual QString toDisplayString() const
Definition element.cpp:220
Flags flags
Definition element.h:39
void setRootObject(QObject *rootObject)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QList< QObject * > rootObjects() const
QModelIndex index(int row, int column, const QModelIndex &parent={}) const override
@ ObjectRole
return QObject*
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
@ ElementRole
return Element*
@ InternalIdRole
return quint64
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
KDSME::State * state
StateModel(QObject *parent=nullptr)
void setState(State *state)
int columnCount(const QModelIndex &parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
TransitionListModel(QObject *parent=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setState(State *state)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames() const override
void setSourceModel(QAbstractItemModel *sourceModel) override
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
TransitionModel(QObject *parent=nullptr)
KDSME::State * sourceState
Definition transition.h:26
KDSME::State * targetState
Definition transition.h:27

© 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