summaryrefslogtreecommitdiffstats
path: root/examples/scxml/invoke
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scxml/invoke')
-rw-r--r--examples/scxml/invoke/Button.qml37
-rw-r--r--examples/scxml/invoke/CMakeLists.txt53
-rw-r--r--examples/scxml/invoke/MainView.qml63
-rw-r--r--examples/scxml/invoke/SubView.qml31
-rw-r--r--examples/scxml/invoke/doc/images/invoke.pngbin0 -> 7170 bytes
-rw-r--r--examples/scxml/invoke/doc/src/invoke.qdoc75
-rw-r--r--examples/scxml/invoke/invoke-qml.h20
-rw-r--r--examples/scxml/invoke/invoke.cpp17
-rw-r--r--examples/scxml/invoke/invoke.pro27
-rw-r--r--examples/scxml/invoke/qmldir5
-rw-r--r--examples/scxml/invoke/statemachine.scxml32
11 files changed, 360 insertions, 0 deletions
diff --git a/examples/scxml/invoke/Button.qml b/examples/scxml/invoke/Button.qml
new file mode 100644
index 0000000..5be5665
--- /dev/null
+++ b/examples/scxml/invoke/Button.qml
@@ -0,0 +1,37 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+pragma ComponentBehavior: Bound
+
+import QtQuick
+
+Item {
+ id: button
+ signal clicked
+ property string text: "hello"
+ property bool enabled: true
+ opacity: enabled ? 1.0 : 0.5
+
+ Rectangle {
+ x: 5
+ y: 5
+ width: parent.width - 10
+ height: parent.height - 10
+ radius: 5
+ color: "lightsteelblue"
+
+ Text {
+ anchors.fill: parent
+ color: "white"
+ text: button.text
+ horizontalAlignment: Text.AlignHCenter
+ verticalAlignment: Text.AlignVCenter
+ }
+
+ MouseArea {
+ id: mouse
+ anchors.fill: parent
+ onClicked: if (button.enabled) button.clicked()
+ }
+ }
+}
diff --git a/examples/scxml/invoke/CMakeLists.txt b/examples/scxml/invoke/CMakeLists.txt
new file mode 100644
index 0000000..d8c858a
--- /dev/null
+++ b/examples/scxml/invoke/CMakeLists.txt
@@ -0,0 +1,53 @@
+# Copyright (C) 2023 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+cmake_minimum_required(VERSION 3.16)
+project(invoke LANGUAGES CXX)
+
+if(NOT DEFINED INSTALL_EXAMPLESDIR)
+ set(INSTALL_EXAMPLESDIR "examples")
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/scxml/invoke")
+
+find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)
+
+qt_standard_project_setup(REQUIRES 6.5)
+
+qt_add_executable(invoke
+ invoke.cpp
+)
+
+set_target_properties(invoke PROPERTIES
+ WIN32_EXECUTABLE TRUE
+ MACOSX_BUNDLE TRUE
+)
+
+target_link_libraries(invoke PRIVATE
+ Qt6::Core
+ Qt6::Gui
+ Qt6::Qml
+ Qt6::Scxml
+)
+
+# Statecharts:
+qt6_add_statecharts(invoke
+ statemachine.scxml
+)
+
+qt_add_qml_module(invoke
+ URI InvokeExample
+ VERSION 1.0
+ QML_FILES
+ Button.qml
+ MainView.qml
+ SubView.qml
+ SOURCES
+ invoke-qml.h
+)
+
+install(TARGETS invoke
+ RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
+ LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+)
diff --git a/examples/scxml/invoke/MainView.qml b/examples/scxml/invoke/MainView.qml
new file mode 100644
index 0000000..c7a4400
--- /dev/null
+++ b/examples/scxml/invoke/MainView.qml
@@ -0,0 +1,63 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+pragma ComponentBehavior: Bound
+
+import QtQuick
+import QtQuick.Window
+import QtScxml
+import InvokeExample
+
+Window {
+ id: window
+ visible: true
+ color: "black"
+ width: 400
+ height: 300
+
+ DirectionsStateMachine {
+ id: stateMachine
+ running: true
+ }
+
+ Item {
+ width: parent.width / 2
+ height: parent.height
+
+ Button {
+ id: nowhere
+ text: "Go Nowhere"
+ width: parent.width
+ height: parent.height / 2
+ onClicked: stateMachine.submitEvent("goNowhere")
+ enabled: stateMachine.somewhere
+ }
+
+ Button {
+ id: somewhere
+ text: "Go Somewhere"
+ width: parent.width
+ height: parent.height / 2
+ y: parent.height / 2
+ onClicked: stateMachine.submitEvent("goSomewhere")
+ enabled: stateMachine.nowhere
+ }
+ }
+
+ Loader {
+ sourceComponent: SubView {
+ anywhere: services.children.anywhere ? services.children.anywhere.stateMachine : null
+ }
+ active: stateMachine.somewhere
+
+ x: parent.width / 2
+ width: parent.width / 2
+ height: parent.height
+
+ InvokedServices {
+ id: services
+ stateMachine: stateMachine
+ }
+ }
+}
+
diff --git a/examples/scxml/invoke/SubView.qml b/examples/scxml/invoke/SubView.qml
new file mode 100644
index 0000000..31dbf06
--- /dev/null
+++ b/examples/scxml/invoke/SubView.qml
@@ -0,0 +1,31 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+pragma ComponentBehavior: Bound
+
+import QtQuick
+import QtScxml
+
+Item {
+ id: root
+ required property StateMachine anywhere
+
+ Button {
+ id: here
+ enabled: root.anywhere ? root.anywhere.here : false
+ text: "Go There"
+ width: parent.width / 2
+ height: parent.height
+ onClicked: root.anywhere.submitEvent("goThere")
+ }
+
+ Button {
+ id: there
+ enabled: root.anywhere ? root.anywhere.there : false
+ text: "Go Here"
+ width: parent.width / 2
+ height: parent.height
+ x: width
+ onClicked: root.anywhere.submitEvent("goHere")
+ }
+}
diff --git a/examples/scxml/invoke/doc/images/invoke.png b/examples/scxml/invoke/doc/images/invoke.png
new file mode 100644
index 0000000..fa73ebe
--- /dev/null
+++ b/examples/scxml/invoke/doc/images/invoke.png
Binary files differ
diff --git a/examples/scxml/invoke/doc/src/invoke.qdoc b/examples/scxml/invoke/doc/src/invoke.qdoc
new file mode 100644
index 0000000..8e8d903
--- /dev/null
+++ b/examples/scxml/invoke/doc/src/invoke.qdoc
@@ -0,0 +1,75 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
+
+/*!
+ \example invoke
+ \title SCXML Invoke
+ \examplecategory {Data Processing & I/O}
+ \ingroup examples-qtscxml
+
+ \brief Invokes a compiled nested state machine.
+
+ \image invoke.png
+
+ \e{Invoke} demonstrates how to use the \c <invoke> element
+ with generated nested state-machines, where the SCXML file is compiled to
+ a C++ class. The \c <invoke> element is used to create an instance of an
+ external service.
+
+ \include examples-run.qdocinc
+
+ \section1 Invoking the State Machine
+
+ In \e statemachine.scxml, we specify a state machine with the name
+ \e DirectionsStateMachine of type \e http://www.w3.org/TR/scxml/ to invoke:
+
+ \quotefromfile invoke/statemachine.scxml
+ \skipto scxml
+ \printuntil
+
+ \section1 Compiling the State Machine
+ We link against the Qt SCXML module by adding the following lines to the
+ example's build files.
+
+ \section2 \e invoke.pro when using qmake:
+
+ \quotefromfile invoke/invoke.pro
+ \skipto QT
+ \printline scxml
+
+ We then specify the state machine to compile:
+ \skipto STATECHARTS
+ \printline scxml
+
+ \section2 \e CMakeLists.txt when using cmake:
+ \quotefromfile invoke/CMakeLists.txt
+ \skipto find_package
+ \printline Scxml
+ \skipto target_link_libraries
+ \printuntil )
+
+ We then specify the state machine to compile:
+ \skipto qt6_add_statecharts
+ \printuntil )
+
+ The statechart directives \e STATECHARTS or \e qt6_add_statecharts invoke the Qt SCXML
+ Compiler, \c qscxmlc, which is run automatically to generate \e statemachine.h and
+ \e statemachine.cpp, which are then added appropriately as headers and sources for
+ compilation.
+
+ \section1 Declaring the state machine as QML element
+
+ The state machine is declared as a QML element as follows:
+ \quotefromfile invoke/invoke-qml.h
+ \skipto struct
+ \printuntil }
+
+ \section1 Instantiating the State Machine
+
+ We instantiate the generated \c DirectionsStateMachine element in the \e MainView.qml
+ file, as follows:
+
+ \quotefromfile invoke/MainView.qml
+ \skipto DirectionsStateMachine
+ \printuntil }
+*/
diff --git a/examples/scxml/invoke/invoke-qml.h b/examples/scxml/invoke/invoke-qml.h
new file mode 100644
index 0000000..e0735a8
--- /dev/null
+++ b/examples/scxml/invoke/invoke-qml.h
@@ -0,0 +1,20 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef INVOKE_QML
+#define INVOKE_QML
+
+#include "statemachine.h"
+
+#include <QtQml/qqml.h>
+#include <QtCore/qobject.h>
+
+struct DirectionsStateMachineRegistration
+{
+ Q_GADGET
+ QML_FOREIGN(DirectionsStateMachine)
+ QML_NAMED_ELEMENT(DirectionsStateMachine)
+ QML_ADDED_IN_VERSION(1, 0)
+};
+
+#endif // INVOKE_QML
diff --git a/examples/scxml/invoke/invoke.cpp b/examples/scxml/invoke/invoke.cpp
new file mode 100644
index 0000000..6680396
--- /dev/null
+++ b/examples/scxml/invoke/invoke.cpp
@@ -0,0 +1,17 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include <QtGui/qguiapplication.h>
+#include <QtQml/qqmlapplicationengine.h>
+
+int main(int argc, char *argv[])
+{
+ QGuiApplication app(argc, argv);
+
+ QQmlApplicationEngine engine;
+ QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app,
+ [](){ QCoreApplication::exit(EXIT_FAILURE); }, Qt::QueuedConnection);
+ engine.loadFromModule("InvokeExample", "MainView");
+
+ return app.exec();
+}
diff --git a/examples/scxml/invoke/invoke.pro b/examples/scxml/invoke/invoke.pro
new file mode 100644
index 0000000..d23a0af
--- /dev/null
+++ b/examples/scxml/invoke/invoke.pro
@@ -0,0 +1,27 @@
+TEMPLATE = app
+
+QT += qml scxml
+CONFIG += c++11
+CONFIG += qmltypes
+
+SOURCES += invoke.cpp
+HEADERS += invoke-qml.h
+
+QML_IMPORT_NAME = InvokeExample
+QML_IMPORT_MAJOR_VERSION = 1
+
+qml_resources.files = \
+ qmldir \
+ Button.qml \
+ MainView.qml \
+ SubView.qml
+
+qml_resources.prefix = /qt/qml/InvokeExample
+
+RESOURCES += qml_resources
+
+STATECHARTS = statemachine.scxml
+
+target.path = $$[QT_INSTALL_EXAMPLES]/scxml/invoke
+INSTALLS += target
+
diff --git a/examples/scxml/invoke/qmldir b/examples/scxml/invoke/qmldir
new file mode 100644
index 0000000..6816682
--- /dev/null
+++ b/examples/scxml/invoke/qmldir
@@ -0,0 +1,5 @@
+module InvokeExample
+prefer :/qt/qml/InvokeExample/
+Button 1.0 Button.qml
+MainView 1.0 MainView.qml
+SubView 1.0 SubView.qml
diff --git a/examples/scxml/invoke/statemachine.scxml b/examples/scxml/invoke/statemachine.scxml
new file mode 100644
index 0000000..bd31dca
--- /dev/null
+++ b/examples/scxml/invoke/statemachine.scxml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+-->
+<scxml
+ xmlns="http://www.w3.org/2005/07/scxml"
+ version="1.0"
+ name="DirectionsStateMachine"
+ initial="anyplace"
+>
+ <state id="anyplace">
+ <transition event="goNowhere" target="nowhere"/>
+ <transition event="goSomewhere" target="somewhere"/>
+
+ <state id="nowhere"/>
+ <state id="somewhere">
+ <invoke type="http://www.w3.org/TR/scxml/">
+ <content>
+ <scxml name="anywhere" version="1.0">
+ <state id="here">
+ <transition event="goThere" target="there"/>
+ </state>
+ <state id="there">
+ <transition event="goHere" target="here"/>
+ </state>
+ </scxml>
+ </content>
+ </invoke>
+ </state>
+ </state>
+</scxml>