KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
mainwindow.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 "mainwindow.h"
19#include "ui_mainwindow.h"
20
21#include "editcontroller.h"
22#include "layouter.h"
23#include "elementmodel.h"
24#include "scxmlimporter.h"
25#include "state.h"
26#include "transition.h"
27#include "commandcontroller.h"
28#include "parsehelper.h"
29#include "statemachinescene.h"
32
33#include <QCoreApplication>
34#include <QDebug>
35#include <QDir>
36#include <QLayout>
37#include <QSettings>
38#include <QStandardItemModel>
39#include <QStandardPaths>
40#include <QUndoStack>
41
42using namespace KDSME;
43
44namespace {
45
46enum PresetsModelDataRoles : quint16
47{
48 AbsoluteFilePathRole = Qt::UserRole + 1
49};
50
51}
52
53MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags f)
54 : QMainWindow(parent, f)
55 , ui(new Ui::MainWindow)
56 , m_presetsModel(new QStandardItemModel(this))
57 , m_transitionsModel(new TransitionListModel(this))
58 , m_stateMachineView(nullptr)
59{
60 ui->setupUi(this);
61
62 setupPresetsView();
63 setupStateMachineView();
64 setupObjectInspector();
65 setupActions();
66
67 addToolBar(new StateMachineToolBar(m_stateMachineView));
68
69 setWindowTitle(tr("State Machine Editor"));
70
71 // initialize with an empty state machine
72 setStateMachine(nullptr);
73}
74
76{
77 m_stateMachineView->scene()->setRootState(nullptr);
78
79 delete ui;
80}
81
82void MainWindow::loadPresets(const QString &presetsDir)
83{
84 m_presetsModel->clear();
85
86 const QDir dir(presetsDir);
87 if (!dir.exists()) {
88 qWarning() << "Non-existent presets location:" << presetsDir;
89 }
90
91 const QStringList files = dir.entryList(QDir::Files);
92 for (const QString &file : files) {
93 if (!file.endsWith(u".scxml"))
94 continue;
95
96 auto *item = new QStandardItem(file);
97 item->setData(dir.absoluteFilePath(file), AbsoluteFilePathRole);
98 m_presetsModel->appendRow(item);
99 }
100
101 m_presetsModel->setHeaderData(0, Qt::Horizontal, tr("Preset"));
102}
103
104void MainWindow::setupStateMachineView()
105{
106 m_stateMachineView = new StateMachineView;
107 m_stateMachineView->editController()->setEditModeEnabled(true);
108 setCentralWidget(m_stateMachineView);
109}
110
111void MainWindow::setupPresetsView()
112{
113 ui->presetsTreeView->setModel(m_presetsModel);
114 ui->presetsTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
115 connect(ui->presetsTreeView, &QTreeView::clicked, this, &MainWindow::handlePresetActivated);
116}
117
118void MainWindow::setupObjectInspector()
119{
120 // object inspectors for the state machine object tree
121 ui->statesView->setModel(m_stateMachineView->scene()->stateModel());
122 ui->transitionsView->setModel(m_transitionsModel);
123
124 ui->undoView->setStack(m_stateMachineView->commandController()->undoStack());
125}
126
127void MainWindow::setupActions()
128{
129 auto action = new QAction(tr("New"), this);
130 connect(action, &QAction::triggered, this, &MainWindow::createNew);
131 ui->mainToolBar->addAction(action);
132}
133
135{
136 m_stateMachineView->scene()->setRootState(nullptr);
137
138 if (!stateMachine) {
139 stateMachine = new StateMachine;
140 stateMachine->setLabel(tr("New"));
141 m_owningStateMachine.reset(stateMachine);
142 }
143
144 // update state chart
145 m_stateMachineView->scene()->setRootState(stateMachine);
146 m_stateMachineView->scene()->layout();
147
148 m_transitionsModel->setState(stateMachine);
149
150 auto selectionModel = m_stateMachineView->scene()->selectionModel();
151 Q_ASSERT(selectionModel);
152 ui->statesView->setSelectionModel(selectionModel);
153 ui->propertyEditorWidget->setSelectionModel(selectionModel);
154 ui->propertyEditorWidget->setCommandController(m_stateMachineView->commandController());
155
156 ui->statesView->expandAll();
157}
158
160{
161 if (mode == PresetsInputMode) {
162 importFromScxmlFile(selectedFile());
163 }
164}
165
167{
168 setStateMachine(nullptr);
169 ui->presetsTreeView->setCurrentIndex(QModelIndex());
170}
171
173{
174 const QModelIndex selected = ui->presetsTreeView->currentIndex();
175 return selected.data(AbsoluteFilePathRole).toString();
176}
177
178void MainWindow::importFromScxmlFile(const QString &filePath)
179{
180 if (!filePath.isEmpty()) {
181 ScxmlImporter parser(ParseHelper::readFile(filePath));
182 StateMachine *stateMachine = parser.import();
183 setStateMachine(stateMachine);
184 if (stateMachine) {
185 m_owningStateMachine.reset(stateMachine);
186 }
187 } else {
188 setStateMachine(nullptr);
189 }
190
191 // update view
192 const QModelIndex match = m_presetsModel->match(m_presetsModel->index(0, 0), AbsoluteFilePathRole, QFileInfo(filePath).absoluteFilePath(), 1, Qt::MatchExactly).value(0);
193 ui->presetsTreeView->setCurrentIndex(match);
194}
195
196void MainWindow::handlePresetActivated(const QModelIndex &index)
197{
198 const QString filePath = index.data(AbsoluteFilePathRole).toString();
199 if (filePath.isEmpty())
200 return;
201
202 importFromScxmlFile(filePath);
203}
QItemSelectionModel * selectionModel() const
void setEditModeEnabled(bool editModeEnabled)
void setLabel(const QString &label)
Definition element.cpp:63
StateModel * stateModel() const
void setRootState(State *rootState)
Widget for displaying a KDSME::StateMachine in a Qt Quick based view.
KDSME::StateMachineScene * scene
KDSME::CommandController * commandController
KDSME::EditController * editController
void setState(State *state)
QString selectedFile() const
When in PresetsInputMode, return the currently selected file name.
void setStateMachine(KDSME::StateMachine *stateMachine)
MainWindow(QWidget *parent=nullptr, Qt::WindowFlags f={})
void loadPresets(const QString &presetsDir)
void setInputMode(MainWindow::InputMode mode)
void createNew()
@ PresetsInputMode
Definition mainwindow.h:44

© 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