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 | //! \instantiates 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 event \a event occurrs. |
40 | |
41 | The corresponding signal handler is \c onOccurred. |
42 | |
43 | \sa QScxmlEvent |
44 | */ |
45 | |
46 | |
47 | QScxmlEventConnection::QScxmlEventConnection(QObject *parent) : |
48 | QObject(parent) |
49 | { |
50 | } |
51 | |
52 | QStringList QScxmlEventConnection::events() const |
53 | { |
54 | return m_events; |
55 | } |
56 | |
57 | void QScxmlEventConnection::setEvents(const QStringList &events) |
58 | { |
59 | if (events == m_events.value()) { |
60 | m_events.removeBindingUnlessInWrapper(); |
61 | return; |
62 | } |
63 | m_events = events; |
64 | doConnect(); |
65 | m_events.notify(); |
66 | } |
67 | |
68 | QBindable<QStringList> QScxmlEventConnection::bindableEvents() |
69 | { |
70 | return &m_events; |
71 | } |
72 | |
73 | QScxmlStateMachine *QScxmlEventConnection::stateMachine() const |
74 | { |
75 | return m_stateMachine; |
76 | } |
77 | |
78 | void QScxmlEventConnection::setStateMachine(QScxmlStateMachine *stateMachine) |
79 | { |
80 | if (stateMachine == m_stateMachine.value()) { |
81 | m_stateMachine.removeBindingUnlessInWrapper(); |
82 | return; |
83 | } |
84 | m_stateMachine = stateMachine; |
85 | doConnect(); |
86 | m_stateMachine.notify(); |
87 | } |
88 | |
89 | QBindable<QScxmlStateMachine*> QScxmlEventConnection::bindableStateMachine() |
90 | { |
91 | return &m_stateMachine; |
92 | } |
93 | |
94 | void QScxmlEventConnection::doConnect() |
95 | { |
96 | for (const QMetaObject::Connection &connection : std::as_const(t&: m_connections)) |
97 | disconnect(connection); |
98 | m_connections.clear(); |
99 | if (m_stateMachine) { |
100 | for (const QString &event : std::as_const(t: m_events.value())) { |
101 | m_connections.append(t: m_stateMachine->connectToEvent(scxmlEventSpec: event, context: this, |
102 | functor: &QScxmlEventConnection::occurred)); |
103 | } |
104 | } |
105 | } |
106 | |
107 | void QScxmlEventConnection::classBegin() |
108 | { |
109 | } |
110 | |
111 | void QScxmlEventConnection::componentComplete() |
112 | { |
113 | auto parentStateMachine = qobject_cast<QScxmlStateMachine *>(object: parent()); |
114 | if (!m_stateMachine.value() && parentStateMachine) |
115 | setStateMachine(parentStateMachine); |
116 | } |
117 | |
118 | QT_END_NAMESPACE |
119 |