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 "invokedservices_p.h" |
5 | #include <QtScxml/qscxmlinvokableservice.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | /*! |
10 | \qmltype InvokedServices |
11 | //! \nativetype QScxmlInvokedServices |
12 | \inqmlmodule QtScxml |
13 | \since QtScxml 5.8 |
14 | |
15 | \brief Provices access to the services invoked by state machines. |
16 | |
17 | Makes the invoked services easily accessible by their names, without |
18 | constantly iterating through QScxmlStateMachine::invokedServices. |
19 | |
20 | The services are called from state machines via the mechanism described in |
21 | \l{SCXML Specification - 6.4 <invoke>}. |
22 | */ |
23 | |
24 | QScxmlInvokedServices::QScxmlInvokedServices(QObject *parent) : QObject(parent) |
25 | { |
26 | } |
27 | |
28 | /*! |
29 | \qmlproperty var InvokedServices::children |
30 | |
31 | The services invoked by the state machine. |
32 | */ |
33 | |
34 | QVariantMap QScxmlInvokedServices::children() |
35 | { |
36 | return m_children.value(); |
37 | } |
38 | |
39 | QVariantMap QScxmlInvokedServices::childrenActualCalculation() const |
40 | { |
41 | QVariantMap ret; |
42 | if (m_stateMachine.value()) { |
43 | const QList<QScxmlInvokableService *> children = m_stateMachine->invokedServices(); |
44 | for (QScxmlInvokableService *service : children) |
45 | ret.insert(key: service->name(), value: QVariant::fromValue(value: service)); |
46 | } |
47 | return ret; |
48 | } |
49 | |
50 | QBindable<QVariantMap> QScxmlInvokedServices::bindableChildren() |
51 | { |
52 | return &m_children; |
53 | } |
54 | |
55 | void QScxmlInvokedServices::classBegin() |
56 | { |
57 | } |
58 | |
59 | /*! |
60 | \qmlproperty ScxmlStateMachine InvokedServices::stateMachine |
61 | |
62 | The state machine that invoked the services. |
63 | */ |
64 | |
65 | QScxmlStateMachine *QScxmlInvokedServices::stateMachine() const |
66 | { |
67 | return m_stateMachine; |
68 | } |
69 | |
70 | void QScxmlInvokedServices::setStateMachine(QScxmlStateMachine *stateMachine) |
71 | { |
72 | m_stateMachine.removeBindingUnlessInWrapper(); |
73 | if (stateMachine == m_stateMachine.valueBypassingBindings()) |
74 | return; |
75 | |
76 | QObject::disconnect(m_serviceConnection); |
77 | m_stateMachine.setValueBypassingBindings(stateMachine); |
78 | |
79 | if (stateMachine) { |
80 | m_serviceConnection = |
81 | QObject::connect(sender: stateMachine, |
82 | signal: &QScxmlStateMachine::invokedServicesChanged, context: this, slot: [this]() { |
83 | m_children.notify(); |
84 | emit childrenChanged(); |
85 | }); |
86 | } |
87 | m_stateMachine.notify(); |
88 | m_children.notify(); |
89 | emit childrenChanged(); |
90 | } |
91 | |
92 | QBindable<QScxmlStateMachine*> QScxmlInvokedServices::bindableStateMachine() |
93 | { |
94 | return &m_stateMachine; |
95 | } |
96 | |
97 | /*! |
98 | \qmlproperty list<QtObject> InvokedServices::qmlChildren |
99 | |
100 | A list of additional QtObject types nested in this type. |
101 | */ |
102 | |
103 | QQmlListProperty<QObject> QScxmlInvokedServices::qmlChildren() |
104 | { |
105 | return QQmlListProperty<QObject>(this, &m_qmlChildren); |
106 | } |
107 | |
108 | void QScxmlInvokedServices::componentComplete() |
109 | { |
110 | if (!m_stateMachine.value()) |
111 | setStateMachine(qobject_cast<QScxmlStateMachine *>(object: parent())); |
112 | } |
113 | |
114 | QT_END_NAMESPACE |
115 | |