KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
statemachinetoolbar.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 "statemachinetoolbar.h"
17
18#include "debug.h"
20#include "export/qmlexporter.h"
21#include "export/svgexporter.h"
22#include "state.h"
23#include "statemachineview.h"
24#include "statemachinescene.h"
25
26#include <QAction>
27#include <QDir>
28#include <QMenu>
29#include <QFileDialog>
30#include <QMessageBox>
31#include <QToolButton>
32#include <QWidgetAction>
33
34#include <algorithm>
35
36using namespace KDSME;
37
38namespace {
39
40QStringList availableThemeNames()
41{
42 const QDir dir(QStringLiteral(":/kdsme/qml/themes"));
43 auto entries = dir.entryList();
44 std::transform(entries.cbegin(), entries.cend(), entries.begin(), [](const QString &x) {
45 return QString(x).remove(QStringLiteral(".qml"));
46 });
47 return entries;
48}
49
50}
51
52struct StateMachineToolBar::Private
53{
54 Private(StateMachineToolBar *q);
55
56 // slots
57 void handleExport();
58
59 void init();
60
61 void exportToFile(StateMachine *machine, const QString &fileName);
62
64 StateMachineView *m_view;
65
66 QAction *m_exportAction;
67};
68
69StateMachineToolBar::Private::Private(StateMachineToolBar *q)
70 : q(q)
71 , m_view(nullptr)
72 , m_exportAction(nullptr)
73{
74}
75
77 : QToolBar(parent)
78 , d(new Private(this))
79{
80 d->m_view = view;
81
82 setWindowTitle(tr("State Machine Tool Bar"));
83 d->m_exportAction = new QAction(tr("Export to File..."), this);
84 d->m_exportAction->setObjectName(QStringLiteral("actionExportToFile"));
85 d->m_exportAction->setStatusTip(QStringLiteral("Export current state machine to a file."));
86 connect(d->m_exportAction, SIGNAL(triggered()), this, SLOT(handleExport())); // clazy:exclude=old-style-connect
87 addAction(d->m_exportAction);
88
89 auto *themeSelectionButton = new QToolButton(this);
90 themeSelectionButton->setText(tr("Theme"));
91 themeSelectionButton->setPopupMode(QToolButton::InstantPopup);
92 auto *themeSelectionMenu = new QMenu(themeSelectionButton);
93 const auto themes = availableThemeNames();
94 for (const QString &themeName : themes) {
95 auto action = new QAction(themeName, this);
96 action->setObjectName(QStringLiteral("action%1").arg(themeName));
97 connect(action, &QAction::triggered, this, [this, themeName]() {
98 d->m_view->setThemeName(themeName);
99 });
100 themeSelectionMenu->addAction(action);
101 }
102 themeSelectionButton->setMenu(themeSelectionMenu);
103 addWidget(themeSelectionButton);
104}
105
109
110void StateMachineToolBar::Private::handleExport()
111{
112 auto stateMachine = m_view->scene()->rootState()->machine();
113 if (!stateMachine) {
114 QMessageBox::information(q, QString(), QStringLiteral("State machine unavailable"));
115 return;
116 }
117
118 const QString fileName = QFileDialog::getSaveFileName(q, tr("Save to File"), QString(), tr("SCXML/QML/SVG files (*.scxml, *.qml, *.svg)"));
119 exportToFile(stateMachine, fileName);
120}
121
122void StateMachineToolBar::Private::exportToFile(StateMachine *machine, const QString &fileName)
123{
124 if (!machine || fileName.isEmpty())
125 return;
126
127 QFile file(fileName);
128 const bool success = file.open(QIODevice::WriteOnly);
129 if (!success) {
130 qCWarning(KDSME_VIEW) << "Failed to open file:" << fileName;
131 return;
132 }
133
134 const QString suffix = QFileInfo(fileName).suffix();
135 AbstractExporter *exporter;
136 if (suffix == u"qml") {
137 exporter = new QmlExporter(&file);
138 } else if (suffix == u"svg") {
139 exporter = new SvgExporter(&file);
140 } else {
141 // fallback
142 exporter = new ScxmlExporter(&file);
143 }
144 exporter->exportMachine(machine);
145}
146
147#include "moc_statemachinetoolbar.cpp"
virtual bool exportMachine(StateMachine *machine)=0
StateMachineToolBar(StateMachineView *view, QWidget *parent=nullptr)
Widget for displaying a KDSME::StateMachine in a Qt Quick based view.

© 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