KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
statemachinepalettewidget.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 "element.h"
19#include "kdsmeconstants.h"
20
21#include "debug.h"
22#include <QListView>
23#include <QMimeData>
24#include <QUrl>
25#include <QRegularExpression>
26#include <QVBoxLayout>
27
28using namespace KDSME;
29
30namespace KDSME {
31
32class PaletteModel : public QAbstractListModel
33{
34 Q_OBJECT
35
36public:
37 enum Role
38 {
39 ElementTypeRole = Qt::UserRole + 1
40 };
41
42 explicit PaletteModel(QObject *parent = nullptr);
43
44 [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
45 [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
46 [[nodiscard]] QMimeData *mimeData(const QModelIndexList &indexes) const override;
47 [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
48
49private:
50 struct Entry
51 {
52 Entry(Element::Type type_, const QString &iconName_, const QString &name_)
53 : type(type_)
54 , iconName(iconName_)
55 , name(name_)
56 {
57 }
58 Entry()
59 : type(Element::ElementType)
60 {
61 }
62
63 Element::Type type;
64 QString iconName;
65 QString name;
66 };
67
68 QVector<Entry> m_entries;
69};
70
71}
72
73PaletteModel::PaletteModel(QObject *parent)
74 : QAbstractListModel(parent)
75{
76 m_entries << Entry(Element::StateMachineType, QStringLiteral(":/kdsme/icons/state_machine.png"), tr("State Machine"));
77 m_entries << Entry(Element::StateType, QStringLiteral(":/kdsme/icons/state.png"), tr("State"));
78 m_entries << Entry(Element::FinalStateType, QStringLiteral(":/kdsme/icons/final_state.png"), tr("Final State"));
79 m_entries << Entry(Element::HistoryStateType, QStringLiteral(":/kdsme/icons/shallow_history.png"), tr("History State"));
80
81 m_entries << Entry(Element::SignalTransitionType, QStringLiteral(":/kdsme/icons/transition.png"), tr("Signal Transition"));
82 m_entries << Entry(Element::TimeoutTransitionType, QStringLiteral(":/kdsme/icons/transition.png"), tr("Timeout Transition"));
83}
84
85int PaletteModel::rowCount(const QModelIndex &parent) const
86{
87 if (parent.isValid())
88 return 0;
89
90 return static_cast<int>(m_entries.count());
91}
92
93QVariant PaletteModel::data(const QModelIndex &index, int role) const
94{
95 if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
96 return QVariant();
97
98 const Entry &entry = m_entries.at(index.row());
99 switch (role) {
100 case Qt::DisplayRole:
101 return entry.name;
102 case Qt::DecorationRole: {
103 return QIcon(entry.iconName);
104 }
105 case ElementTypeRole:
106 return QVariant::fromValue<Element::Type>(entry.type);
107 default:
108 return QVariant();
109 }
110}
111
112QMimeData *PaletteModel::mimeData(const QModelIndexList &indexes) const
113{
114 Q_ASSERT(indexes.size() == 1); // we only allow single selection
115
116 const QModelIndex index = indexes.first();
117 auto type = index.data(ElementTypeRole).value<Element::Type>();
118 const QString typeString = QString::fromLatin1(Element::typeToString(type));
119
120 auto *mimeData = new QMimeData;
121
122 mimeData->setUrls({ QUrl(QStringLiteral("%1:Element/%2").arg(QStringLiteral(KDSME_QML_URI_PREFIX)).arg(typeString)) }); // clazy:exclude=qstring-arg
123
124 // Following setData calls are used in QML DropArea.keys to accept/reject a drag and drop
125 // depending on the data given. We are using this to allow for example "TransitionType"
126 // elements not to be placed on a UmlStateMachine.qml but only "StateType" elements.
127
128 static const QRegularExpression stateTypeRE(QStringLiteral(".+StateType$"));
129 if (typeString.contains(stateTypeRE)) {
130 mimeData->setData(QStringLiteral("StateType"), "");
131 }
132
133 static const QRegularExpression transitionTypeRE(QStringLiteral(".+TransitionType$"));
134 if (typeString.contains(transitionTypeRE)) {
135 mimeData->setData(QStringLiteral("TransitionType"), "");
136 }
137
138 mimeData->setData(QStringLiteral("external"), "");
139 mimeData->setData(typeString, "");
140
141 return mimeData;
142}
143
144Qt::ItemFlags PaletteModel::flags(const QModelIndex &index) const
145{
146 auto flags = QAbstractListModel::flags(index);
147 return Qt::ItemIsDragEnabled | flags;
148}
149
150struct StateMachinePaletteWidget::Private
151{
152};
153
155 : QWidget(parent)
156 , d(new Private)
157{
158 auto *layout = new QVBoxLayout(this);
159 layout->setContentsMargins(0, 0, 0, 0);
160
161 auto *view = new QListView(this);
162 view->setDragEnabled(true);
163 view->setViewMode(QListView::IconMode);
164 view->setFlow(QListView::LeftToRight);
165 view->setGridSize(QSize(128, 48));
166 view->setSpacing(10);
167 view->setIconSize(QSize(32, 32));
168 view->viewport()->setAcceptDrops(false); // disallow internal drops
169 layout->addWidget(view);
170 view->setModel(new PaletteModel);
171}
172
176
177#include "statemachinepalettewidget.moc"
static Q_INVOKABLE const char * typeToString(KDSME::Element::Type type)
Definition element.cpp:229
@ TimeoutTransitionType
Definition element.h:58
@ SignalTransitionType
Definition element.h:57
#define KDSME_QML_URI_PREFIX

© 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