/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \page qtscxml-instantiating-state-machines.html
    \title Instantiating State Machines
    \brief Instantiating dynamically created and compiled state machines in C++
    and QML

    Both the dynamically created and the compiled state machines behave in the
    same way, have the same properties, states, data model, and so on. They only
    differ in the way they are instantiated. To dynamically create one in C++
    from an SCXML file, you can use:

    \code
    auto *stateMachine = QScxmlStateMachine::fromFile("MyStatemachine.scxml");
    \endcode

    Or, in QML:

    \qml
    import QtScxml 5.7

    Item {
        property QtObject stateMachine: scxmlLoader.stateMachine

        StateMachineLoader {
            id: scxmlLoader
            filename: "statemachine.scxml"
        }
    }
    \endqml

    A compiled state machine can be instantiated the same way as any C++
    object:

    \code
    auto *stateMachine = new MyStatemachine;
    \endcode

    Or:

    \code
    MyStatemachine stateMachine;
    \endcode

    To use a compiled state machine in QML, you can assign it to a context
    property:

    \code
    MyStatemachine stateMachine;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("stateMachine", &stateMachine);
    \endcode

    To compile a state machine, the following lines have to be added to a
    .pro file:

    \badcode
    QT += scxml
    STATECHARTS = MyStatemachine.scxml
    \endcode

    This will tell qmake to run \e qscxmlc which generates MyStatemachine.h
    and MyStatemachine.cpp, and adds them to \l [QMake] HEADERS and
    \l [QMake] SOURCES variables.

    After instantiating a state machine, you can connect to any state's
    active property as follows. For example, if the state machine for a
    traffic light has a state indicating that the light is red (which has the
    convenient id "red" in the scxml file), you can write:

    \code
    QObject::connect(stateMachine, &TrafficLightStateMachine::redChanged,
        [](bool active) {
       qDebug() << (active ? "entered" : "exited") << "the red state";
    \endcode

    And in QML:

    \qml
    Light {
        id: greenLight
        color: "green"
        visible: stateMachine.green
    }
    \endqml

    If you want to be notified when a state machine sends out an event, you
    can connect to the corresponding signal. For example, for a media player
    state machine which indicates that playback has stopped by sending an
    event, you can write:

    \code
    QObject::connect(stateMachine, &MediaPlayer::playbackStopped, [](){
        qDebug() << "Stopped!";
    });
    \endcode

    And in QML:

    \qml
    Connections {
        target: stateMachine
        onPlaybackStopped: console.log("Stopped!")
    }
    \endqml

    Sending events to a state machine is equally simple. You can call (or
    connect to) the slot:

    \code
    stateMachine->tap(QVariantMap({
        std::make_pair("artist", "Fatboy Slim"),
        std::make_pair("title", "The Rockafeller Skank")
    });
    \endcode

    This will generate a "tap" event with the map contents available in
    _event.data inside the state machine. In QML:

    \code
    stateMacine.tap({
        "artist": "Fatboy Slim"
        "title": "The Rockafeller Skank"
    })
    \endcode

    Any invoked state machine with a name property will also show up as a
    property on its parent state machine.
*/
