summaryrefslogtreecommitdiffstats
path: root/src/qml-module/statemachine.cpp
blob: 2adec4c4f23c408f508822d2f35057319605ee06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/****************************************************************************
 **
 ** Copyright (c) 2015 Digia Plc
 ** For any questions to Digia, please use contact form at http://qt.digia.com/
 **
 ** All Rights Reserved.
 **
 ** NOTICE: All information contained herein is, and remains
 ** the property of Digia Plc and its suppliers,
 ** if any. The intellectual and technical concepts contained
 ** herein are proprietary to Digia Plc
 ** and its suppliers and may be covered by Finnish and Foreign Patents,
 ** patents in process, and are protected by trade secret or copyright law.
 ** Dissemination of this information or reproduction of this material
 ** is strictly forbidden unless prior written permission is obtained
 ** from Digia Plc.
 ****************************************************************************/

#include "signalevent.h"
#include "state.h"
#include "statemachine.h"

#include <QScxml/ecmascriptdatamodel.h>
#include <QScxml/scxmlstatetable.h>
#include <QScxml/scxmlparser.h>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQmlInfo>
#include <QFile>

static void append(QQmlListProperty<QObject> *prop, QObject *o)
{
    if (!o)
        return;

    if (State *state = qobject_cast<State *>(o)) {
        state->setParent(prop->object);
        static_cast<StateMachine::Kids *>(prop->data)->append(state);
        emit static_cast<StateMachine *>(prop->object)->statesChanged();
    } else if (SignalEvent *event = qobject_cast<SignalEvent *>(o)) {
        event->setParent(prop->object);
        static_cast<StateMachine::Kids *>(prop->data)->append(event);
        emit static_cast<StateMachine *>(prop->object)->statesChanged();
    }
}

static int count(QQmlListProperty<QObject> *prop)
{
    return static_cast<StateMachine::Kids *>(prop->data)->count();
}

static QObject *at(QQmlListProperty<QObject> *prop, int index)
{
    return static_cast<StateMachine::Kids *>(prop->data)->at(index);
}

static void clear(QQmlListProperty<QObject> *prop)
{
    static_cast<StateMachine::Kids *>(prop->data)->clear();
    emit static_cast<StateMachine *>(prop->object)->statesChanged();
}

StateMachine::StateMachine(QObject *parent)
    : QObject(parent)
{
}

void StateMachine::componentComplete()
{
    if (m_table == nullptr) {
         qmlInfo(this) << "No state machine loaded.";
         return;
    }

    bool ok = false;
    bool moreOk = QMetaObject::invokeMethod(m_table, "init", Qt::DirectConnection, Q_RETURN_ARG(bool, ok));
    if (ok && moreOk) {
        QMetaObject::invokeMethod(m_table, "start");
    } else {
        qmlInfo(this) << "Failed to initialize the state machine.";
    }
}

QQmlListProperty<QObject> StateMachine::states()
{
    return QQmlListProperty<QObject>(this, &m_children, append, count, at, clear);
}

Scxml::StateTable *StateMachine::stateMachine() const
{
    return m_table;
}

void StateMachine::setStateMachine(Scxml::StateTable *table)
{
    qDebug()<<"setting state machine to"<<table;
    if (m_table == nullptr && table != nullptr) {
        m_table = table;
    } else if (m_table) {
        qmlInfo(this) << "Can set the table only once";
    }
}

QString StateMachine::filename()
{
    return m_filename;
}

void StateMachine::setFilename(const QString filename)
{
    QString oldFilename = m_filename;
    if (m_table) {
        delete m_table;
        m_table = nullptr;
    }

    if (parse(filename)) {
        m_filename = filename;
        emit filenameChanged();
    } else {
        m_filename.clear();
        if (!oldFilename.isEmpty()) {
            emit filenameChanged();
        }
    }
}

bool StateMachine::parse(const QString &filename)
{
    QFile scxmlFile(filename);
    if (!scxmlFile.open(QIODevice::ReadOnly)) {
        qmlInfo(this) << QStringLiteral("ERROR: cannot open '%1' for reading!").arg(filename);
        return false;
    }

    QXmlStreamReader xmlReader(&scxmlFile);
    Scxml::ScxmlParser parser(&xmlReader);
    parser.parse();
    scxmlFile.close();
    setStateMachine(parser.table());

    if (parser.state() != Scxml::ScxmlParser::FinishedParsing || m_table == nullptr) {
        qmlInfo(this) << QStringLiteral("Something went wrong while parsing '%1':").arg(filename) << endl;
        foreach (const Scxml::ErrorMessage &msg, parser.errors()) {
            qmlInfo(this) << msg.fileName << QStringLiteral(":") << msg.line
                          << QStringLiteral(":") << msg.column
                          << QStringLiteral(": ") << msg.severityString()
                          << QStringLiteral(": ") << msg.msg;
        }

        return false;
    }

    return true;
}