32#include <QWidgetAction>
40QStringList availableThemeNames()
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"));
52struct StateMachineToolBar::Private
61 void exportToFile(
StateMachine *machine,
const QString &fileName);
66 QAction *m_exportAction;
72 , m_exportAction(nullptr)
78 , d(new Private(this))
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()));
87 addAction(d->m_exportAction);
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);
100 themeSelectionMenu->addAction(action);
102 themeSelectionButton->setMenu(themeSelectionMenu);
103 addWidget(themeSelectionButton);
110void StateMachineToolBar::Private::handleExport()
112 auto stateMachine = m_view->scene()->rootState()->machine();
114 QMessageBox::information(q, QString(), QStringLiteral(
"State machine unavailable"));
118 const QString fileName = QFileDialog::getSaveFileName(q, tr(
"Save to File"), QString(), tr(
"SCXML/QML/SVG files (*.scxml, *.qml, *.svg)"));
119 exportToFile(stateMachine, fileName);
122void StateMachineToolBar::Private::exportToFile(
StateMachine *machine,
const QString &fileName)
124 if (!machine || fileName.isEmpty())
127 QFile file(fileName);
128 const bool success = file.open(QIODevice::WriteOnly);
130 qCWarning(KDSME_VIEW) <<
"Failed to open file:" << fileName;
134 const QString suffix = QFileInfo(fileName).suffix();
136 if (suffix == u
"qml") {
138 }
else if (suffix == u
"svg") {
147#include "moc_statemachinetoolbar.cpp"
virtual bool exportMachine(StateMachine *machine)=0
Widget for displaying a KDSME::StateMachine in a Qt Quick based view.