summaryrefslogtreecommitdiffstats
path: root/qmlscxml/qmlscxml.cpp
blob: 218394513f53485cf6de15676696f412caff8798 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "qmlscxml.h"
#include "qscxml.h"
#include <QDebug>
#include "qmlpropertymap.h"

class QmlScxmlEventProxy : public QObject
{
    Q_OBJECT
    QScxml* scxml;
    QString eventName;
    public:
            QmlScxmlEventProxy (const QString & name, QScxml* scx): QObject(scx)
            ,scxml(scx),eventName(name)
    {
        connect(scxml,SIGNAL(eventTriggered(QString)),this,SLOT(onEvent(QString)));
    }

    Q_SIGNALS:
        void triggered();

    public Q_SLOTS:
        void raise()
        {
            scxml->postNamedEvent(eventName);
        }

        void onEvent(const QString & e)
        {
            if (e == eventName) {
                emit triggered();
            }
        }
};
QmlScxml::QmlScxml(QObject* o) : QObject(o)
        ,m_states(NULL),m_events(NULL),m_data(NULL),m_cur(NULL),scxml(NULL)
{
}
QmlScxml::~QmlScxml()
{
}
QObject* QmlScxml::states() const
{
    return m_states;
}
QObject* QmlScxml::events() const
{
    return m_events;
}
QObject* QmlScxml::data() const
{
    return m_data;
}
QObject* QmlScxml::current() const
{
    return m_cur;
}
QUrl QmlScxml::source() const
{
    return m_source;
}

namespace {
    QString _q_fixName(QString oldName)  {
        QString newName;
        bool beginWord = false;
        if (oldName == oldName.toUpper())
            oldName = oldName.toLower();

        foreach (QChar c, oldName) {
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                newName += beginWord ? c.toUpper() : (newName=="" ? c.toLower() :c);
                beginWord = false;
            } else if (newName != "") {
                if (c >= '0' && c <= '9') {
                    newName += c;
                } else
                    beginWord = true;
            }
        }
        return newName;
    }
};
void QmlScxml::setSource(const QUrl & u)
{
    qDebug() << u;
    if (u != m_source) {
        m_source = u;
        if (scxml) {
            delete scxml;
            scxml= NULL;
        }
        scxml = QScxml::load(u.toLocalFile(),this);
        if (scxml) {
            connect(scxml,SIGNAL(started()),this,SIGNAL(runningChanged()));
            connect(scxml,SIGNAL(stopped()),this,SIGNAL(runningChanged()));
            connect(scxml,SIGNAL(finished()),this,SIGNAL(finished()));
            connect(scxml,SIGNAL(eventTriggered(QString)),this,SIGNAL(trigger(QString)));
            connect(scxml,SIGNAL(configurationChanged()),this,SLOT(checkConfig()));
            if (m_states)
                delete m_states;
            if (m_events)
                delete m_events;
            if (m_data)
                delete m_data;
            if (m_cur)
                delete m_cur;
            m_idForState.clear();
            m_states = new QmlPropertyMap(this);
            m_events = new QmlPropertyMap(this);
            m_data = new QmlPropertyMap(this);
            m_cur = new QmlPropertyMap(this);
            QMap<QString,QVariant> curData = scxml->data();
            for (QMap<QString,QVariant>::iterator it = curData.begin(); it != curData.end(); ++it) {
                m_data->insert(it.key(),it.value());
            }

            connect(m_data,SIGNAL(valueChanged(QString)),this,SLOT(onDataChanged(QString)));
            QList<QAbstractState*> states = scxml->findChildren<QAbstractState*>();
            m_oldConfig = scxml->configuration();
            foreach (QAbstractState* s, states) {
                QString name = _q_fixName(s->objectName());
                if (name != "") {
                   m_states->insert(name,QVariant::fromValue<QObject*>(s));
                   m_cur->insert(name,m_oldConfig.contains(s));
                   m_idForState[s] = name;
                }
            }
            QMap<QString,QString> prefixMap;
            QStringList knownEvents = scxml->knownEventNames();
            foreach (QString ev, knownEvents) {
                prefixMap[_q_fixName(ev)] = ev;
            }
            for (QMap<QString,QString>::iterator it = prefixMap.begin(); it != prefixMap.end(); ++it) {
                if (it.key() != "" && it.value() != "") {
                   m_events->insert(it.key(),QVariant::fromValue<QObject*>(new QmlScxmlEventProxy(it.value(),scxml)));
               }
            }
            emit statesChanged(m_states);
            emit eventsChanged(m_events);
            emit dataChanged(m_data);
            emit currentChanged(m_cur);
            scxml->start();
        }
        emit sourceChanged(u);
    }
}

void QmlScxml::onDataChanged(const QString & key)
{
    m_data->blockSignals(true);
    scxml->setData(key,(*m_data)[key]);
    m_data->blockSignals(false);
}
void QmlScxml::onDataChanged(const QString & key, const QVariant & value)
{
    if (m_data) {
        (*m_data)[key] = value;
    }
}

void QmlScxml::checkConfig()
{
    QSet<QAbstractState*> config = scxml->configuration();
    if (config != m_oldConfig) {
        QSet<QAbstractState*> intersecting = config & m_oldConfig;
        QSet<QAbstractState*> toRemove = m_oldConfig - intersecting;
        QSet<QAbstractState*> toAdd = config - intersecting;

        for (QSet<QAbstractState*>::iterator it = toRemove.begin(); it != toRemove.end(); ++it) {
            QString name = m_idForState[*it];
            if (name != "") {
                (*m_cur)[name] = false;
             }
        }
        for (QSet<QAbstractState*>::iterator it = toAdd.begin(); it != toAdd.end(); ++it) {
            QString name = m_idForState[*it];
            if (name != "") {
                (*m_cur)[name] = true;
             }
        }
        m_oldConfig = config;
        emit currentChanged(m_cur);
    }
}


bool QmlScxml::isRunning() const
{
    return scxml?scxml->isRunning():false;
}
void QmlScxml::setRunning(bool r)
{
    if (scxml) {
        if (r) {
            scxml->start();
        } else {
            scxml->stop();
        }
    }
}
void QmlScxml::raise(const QString & e)
{
    if (scxml) {
        scxml->postNamedEvent(e);
    }
}
void QmlScxml::stop()
{
    if (scxml) {
        scxml->stop();
    }
}
void QmlScxml::start()
{
    if (scxml) {
        scxml->start();
    }
}
QML_DEFINE_TYPE(Scxml, 1,0, Scxml, QmlScxml);

#include <qmlscxml.moc>