summaryrefslogtreecommitdiffstats
path: root/src/qscxml.h
blob: bfecc43b2de720ed92723f181eea88140b7dc725 (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
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the SCXML project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QSCXML_H
#define QSCXML_H
#ifndef QT_NO_STATEMACHINE

#include <QStateMachine>
#include <QAbstractTransition>
#include <QVariant>
#include <QEvent>
#include <QStringList>
#include <QScriptValue>
#include <QUrl>

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(QString eventPrefix READ eventPrefix WRITE setEventPrefix)

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

        QString conditionExpression () const { return cond; }
        void setConditionExpression (const QString & c) { cond = c; }
        QString eventPrefix () const { return ev; }
        void setEventPrefix (const QString & e) { ev = e; }

    Q_SIGNALS:
        void activated ();
    protected:
        bool eventTest(QEvent*);
        void onTransition (QEvent*) { emit activated(); }
    private:
        QScxml* scxml;
        QString ev,cond;
};

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(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);

    public Q_SLOTS:
        void postNamedEvent(const QString &);
        void executeScript (const QString &);

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

    Q_SIGNALS:
        void eventTriggered(const QString &);

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