| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "eventconnection_p.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | /*! |
| 9 | \qmltype EventConnection |
| 10 | //! \nativetype QScxmlEventConnection |
| 11 | \inqmlmodule QtScxml |
| 12 | \since QtScxml 5.8 |
| 13 | |
| 14 | \brief Connects to events sent out by state machines. |
| 15 | |
| 16 | To receive a notification when a state machine sends out an event, a |
| 17 | connection can be created to the corresponding signal. |
| 18 | */ |
| 19 | |
| 20 | /*! |
| 21 | \qmlproperty stringlist EventConnection::events |
| 22 | |
| 23 | The list of SCXML event specifiers that describe the events to listen for. |
| 24 | |
| 25 | Even though spaces are allowed in event specifications in SCXML documents, |
| 26 | they are not allowed in this list. However, the list can contain multiple |
| 27 | specifiers, to the same effect. |
| 28 | */ |
| 29 | |
| 30 | /*! |
| 31 | \qmlproperty ScxmlStateMachine EventConnection::stateMachine |
| 32 | |
| 33 | The state machine that sends out the event. |
| 34 | */ |
| 35 | |
| 36 | /*! |
| 37 | \qmlsignal EventConnection::occurred(event) |
| 38 | |
| 39 | This signal is emitted when the SCXML event \a event occurs. |
| 40 | |
| 41 | \sa QScxmlEvent |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | QScxmlEventConnection::QScxmlEventConnection(QObject *parent) : |
| 46 | QObject(parent) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | QStringList QScxmlEventConnection::events() const |
| 51 | { |
| 52 | return m_events; |
| 53 | } |
| 54 | |
| 55 | void QScxmlEventConnection::setEvents(const QStringList &events) |
| 56 | { |
| 57 | m_events.removeBindingUnlessInWrapper(); |
| 58 | if (events == m_events.valueBypassingBindings()) { |
| 59 | return; |
| 60 | } |
| 61 | m_events.setValueBypassingBindings(events); |
| 62 | doConnect(); |
| 63 | m_events.notify(); |
| 64 | } |
| 65 | |
| 66 | QBindable<QStringList> QScxmlEventConnection::bindableEvents() |
| 67 | { |
| 68 | return &m_events; |
| 69 | } |
| 70 | |
| 71 | QScxmlStateMachine *QScxmlEventConnection::stateMachine() const |
| 72 | { |
| 73 | return m_stateMachine; |
| 74 | } |
| 75 | |
| 76 | void QScxmlEventConnection::setStateMachine(QScxmlStateMachine *stateMachine) |
| 77 | { |
| 78 | m_stateMachine.removeBindingUnlessInWrapper(); |
| 79 | if (stateMachine == m_stateMachine.valueBypassingBindings()) |
| 80 | return; |
| 81 | m_stateMachine.setValueBypassingBindings(stateMachine); |
| 82 | doConnect(); |
| 83 | m_stateMachine.notify(); |
| 84 | } |
| 85 | |
| 86 | QBindable<QScxmlStateMachine*> QScxmlEventConnection::bindableStateMachine() |
| 87 | { |
| 88 | return &m_stateMachine; |
| 89 | } |
| 90 | |
| 91 | void QScxmlEventConnection::doConnect() |
| 92 | { |
| 93 | for (const QMetaObject::Connection &connection : std::as_const(t&: m_connections)) |
| 94 | disconnect(connection); |
| 95 | m_connections.clear(); |
| 96 | const auto stateMachine = m_stateMachine.valueBypassingBindings(); |
| 97 | if (stateMachine) { |
| 98 | const auto events = m_events.valueBypassingBindings(); |
| 99 | for (const QString &event : events) { |
| 100 | m_connections.append(t: stateMachine->connectToEvent(scxmlEventSpec: event, context: this, |
| 101 | functor: &QScxmlEventConnection::occurred)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void QScxmlEventConnection::classBegin() |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | void QScxmlEventConnection::componentComplete() |
| 111 | { |
| 112 | auto parentStateMachine = qobject_cast<QScxmlStateMachine *>(object: parent()); |
| 113 | if (!m_stateMachine.value() && parentStateMachine) |
| 114 | setStateMachine(parentStateMachine); |
| 115 | } |
| 116 | |
| 117 | QT_END_NAMESPACE |
| 118 |
