summaryrefslogtreecommitdiffstats
path: root/src/qscxml.h
blob: 4cf4365faa8b6803974e6476238147107c384d54 (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
/****************************************************************************
**
** Copyright (C) 2008-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of the SCXML project on Trolltech Labs.
**
** $TROLLTECH_GPL_LICENSE$
**
****************************************************************************/

#ifndef QSCXML_H
#define QSCXML_H
#ifndef QT_NO_STATEMACHINE

#include <QStateMachine>
#include <QAbstractTransition>
#include <QVariant>
#include <QEvent>
#include <QStringList>
#include <QScriptValue>
#include <QUrl>
#include <QScriptProgram>
class QScriptEngine;
class QScxml;

class QScxmlEvent : public QEvent
{
    public:
        static QEvent::Type eventType();
        QString eventName() const;
        QStringList paramNames () const;
        QVariantList paramValues () const;
        QScriptValue content () const;
        QVariant param (const QString & name) const;
        QScxmlEvent (
                const QString & name,
                const QStringList & paramNames = QStringList(),
                const QVariantList & paramValues = QVariantList(),
                const QScriptValue & content = QScriptValue());

        struct MetaData
        {
            QUrl origin,target;
            QString originType, targetType;
            QString invokeID;
            enum Kind { Platform, Internal, External } kind;
        };

        MetaData metaData;

    private:
        QString ename;
        QStringList pnames;
        QVariantList pvals;
        QScriptValue cnt;
};


class   QScxmlTransition : public QAbstractTransition
{
    Q_OBJECT
    Q_PROPERTY(QString conditionExpression READ conditionExpression WRITE setConditionExpression)
    Q_PROPERTY(QStringList eventPrefixes READ eventPrefixes WRITE setEventPrefixes)

    public:
        QScxmlTransition (QState* state, QScxml* machine);

        QString conditionExpression () const;
        void setConditionExpression (const QString & c);
        QStringList eventPrefixes () const { return ev; }
        void setEventPrefixes (const QStringList & e) { ev = e; }

    protected:
        bool eventTest(QEvent*);
        void onTransition (QEvent*);
    private:
        QScxml* scxml;
        QStringList ev;
        QScriptProgram prog;
};

class QScxmlInvoker : public QObject
{
    Q_OBJECT
    Q_PROPERTY (QString id READ id WRITE setID)

    protected:
        QScxmlInvoker(QScxmlEvent* ievent, QStateMachine* p) : QObject(p), initEvent(ievent),cancelled(false) {}

    public:
        virtual ~QScxmlInvoker();
        QString id () const;
        void setID(const QString &);

    public Q_SLOTS:
        virtual void activate() = 0;
        virtual void cancel() { deleteLater(); }

    protected Q_SLOTS:
        void postParentEvent (const QString & event);

    protected:
        QScxml* parentStateMachine() { return (QScxml*)parent(); }
        void postParentEvent (QScxmlEvent* ev);
        QScxmlEvent* initEvent;
        bool cancelled;

    friend struct QScxmlFunctions;
};

struct QScxmlInvokerFactory
{
    virtual QScxmlInvoker* createInvoker (QScxmlEvent* event, QScxml* stateMachine) = 0;
    virtual bool isTypeSupported (const QString & type) const = 0;
    virtual void init (QScxml*) = 0;
};

template <class T>
class QScxmlAutoInvokerFactory : public QScxmlInvokerFactory
{
    QScxmlInvoker* createInvoker (QScxmlEvent* _e, QScxml* _sm) { return new T(_e,_sm); }
    bool isTypeSupported(const QString & _s) const { return T::isTypeSupported(_s); }
    void init (QScxml* sm) { T::initInvokerFactory(sm); }
};


class QScxml : public QStateMachine
{
    Q_OBJECT

    Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl)


    public:
        QScxml(QScriptEngine* engine, QObject* o = NULL);
        QScxml(QObject* o = NULL);
        virtual ~QScxml();
    protected:
        // overloaded to store the event for the script environment's use (_event), and to convert
        // StateFinished events to "done." named events
        virtual void beginSelectTransitions(QEvent*);
        virtual void endMicrostep(QEvent*);

    public:
        QScriptEngine* scriptEngine () const;
        void registerObject (QObject* object, const QString & name = QString(), bool recursive = false);
        void registerInvokerFactory (QScxmlInvokerFactory* f);
        void setBaseUrl (const QUrl &);
        QUrl baseUrl () const;
        static QScxml* load (const QString & filename, QObject* o = NULL);
        QMap<QString,QVariant> data() const;
        QStringList knownEventNames() const;

    public Q_SLOTS:
        void postNamedEvent(const QString &);
        void executeScript (const QString &);
        void executeScript (const QScriptProgram &);
        void setData(const QString & id, const QVariant & value);

    private Q_SLOTS:
        void registerSession();
        void unregisterSession();
        void handleStateFinished();

    Q_SIGNALS:
        void eventTriggered(const QString &);
        void dataChanged (const QString &, const QVariant &);
        void configurationChanged();

    private:
        class QScxmlPrivate* pvt;
        friend class QScxmlLoader;
        friend struct QScxmlFunctions;
        void init();
};
#endif
#endif // QSCXML_H