1
/****************************************************************************
2
**
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of SCXML on Qt labs
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QSCXML_H
43
#define QSCXML_H
44
#ifndef QT_NO_STATEMACHINE
45
46
#include <QStateMachine>
47
#include <QAbstractTransition>
48
#include <QVariant>
49
#include <QEvent>
50
#include <QStringList>
51
#include <QScriptValue>
52
#include <QUrl>
53
#include <QScriptProgram>
54
class QScriptEngine;
55
class QScxml;
56
57
class QScxmlEvent : public QEvent
58
{
59
    public:
60
        static QEvent::Type eventType();
61
        QString eventName() const;
62
        QStringList paramNames () const;
63
        QVariantList paramValues () const;
64
        QScriptValue content () const;
65
        QVariant param (const QString & name) const;
66
        QScxmlEvent (
67
                const QString & name,
68
                const QStringList & paramNames = QStringList(),
69
                const QVariantList & paramValues = QVariantList(),
70
                const QScriptValue & content = QScriptValue());
71
72
        struct MetaData
73
        {
74
            QUrl origin,target;
75
            QString originType, targetType;
76
            QString invokeID;
77
            enum Kind { Platform, Internal, External } kind;
78
        };
79
80
        MetaData metaData;
81
82
    private:
83
        QString ename;
84
        QStringList pnames;
85
        QVariantList pvals;
86
        QScriptValue cnt;
87
};
88
89
90
class   QScxmlTransition : public QAbstractTransition
91
{
92
    Q_OBJECT
93
    Q_PROPERTY(QString conditionExpression READ conditionExpression WRITE setConditionExpression)
94
    Q_PROPERTY(QStringList eventPrefixes READ eventPrefixes WRITE setEventPrefixes)
95
96
    public:
97
        QScxmlTransition (QState* state, QScxml* machine);
98
99
        QString conditionExpression () const;
100
        void setConditionExpression (const QString & c);
101
        QStringList eventPrefixes () const { return ev; }
102
        void setEventPrefixes (const QStringList & e) { ev = e; }
103
104
    protected:
105
        bool eventTest(QEvent*);
106
        void onTransition (QEvent*);
107
    private:
108
        QScxml* scxml;
109
        QStringList ev;
110
        QScriptProgram prog;
111
};
112
113
class QScxmlInvoker : public QObject
114
{
115
    Q_OBJECT
116
    Q_PROPERTY (QString id READ id WRITE setID)
117
118
    protected:
119
        QScxmlInvoker(QScxmlEvent* ievent, QStateMachine* p) : QObject(p), initEvent(ievent),cancelled(false) {}
120
121
    public:
122
        virtual ~QScxmlInvoker();
123
        QString id () const;
124
        void setID(const QString &);
125
126
    public Q_SLOTS:
127
        virtual void activate() = 0;
128
        virtual void cancel() { deleteLater(); }
129
130
    protected Q_SLOTS:
131
        void postParentEvent (const QString & event);
132
133
    protected:
134
        QScxml* parentStateMachine() { return (QScxml*)parent(); }
135
        void postParentEvent (QScxmlEvent* ev);
136
        QScxmlEvent* initEvent;
137
        bool cancelled;
138
139
    friend struct QScxmlFunctions;
140
};
141
142
struct QScxmlInvokerFactory
143
{
144
    virtual QScxmlInvoker* createInvoker (QScxmlEvent* event, QScxml* stateMachine) = 0;
145
    virtual bool isTypeSupported (const QString & type) const = 0;
146
    virtual void init (QScxml*) = 0;
147
};
148
149
template <class T>
150
class QScxmlAutoInvokerFactory : public QScxmlInvokerFactory
151
{
152
    QScxmlInvoker* createInvoker (QScxmlEvent* _e, QScxml* _sm) { return new T(_e,_sm); }
153
    bool isTypeSupported(const QString & _s) const { return T::isTypeSupported(_s); }
154
    void init (QScxml* sm) { T::initInvokerFactory(sm); }
155
};
156
157
158
class QScxml : public QStateMachine
159
{
160
    Q_OBJECT
161
162
    Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl)
163
164
165
    public:
166
        QScxml(QScriptEngine* engine, QObject* o = NULL);
167
        QScxml(QObject* o = NULL);
168
        virtual ~QScxml();
169
    protected:
170
        // overloaded to store the event for the script environment's use (_event), and to convert
171
        // StateFinished events to "done." named events
172
        virtual void beginSelectTransitions(QEvent*);
173
        virtual void endMicrostep(QEvent*);
174
175
    public:
176
        QScriptEngine* scriptEngine () const;
177
        void registerObject (QObject* object, const QString & name = QString(), bool recursive = false);
178
        void registerInvokerFactory (QScxmlInvokerFactory* f);
179
        void setBaseUrl (const QUrl &);
180
        QUrl baseUrl () const;
181
        static QScxml* load (const QString & filename, QObject* o = NULL);
182
        QMap<QString,QVariant> data() const;
183
        QStringList knownEventNames() const;
184
185
    public Q_SLOTS:
186
        void postNamedEvent(const QString &);
187
        void executeScript (const QString &);
188
        void executeScript (const QScriptProgram &);
189
        void setData(const QString & id, const QVariant & value);
190
191
    private Q_SLOTS:
192
        void registerSession();
193
        void unregisterSession();
194
        void handleStateFinished();
195
196
    Q_SIGNALS:
197
        void eventTriggered(const QString &);
198
        void dataChanged (const QString &, const QVariant &);
199
        void configurationChanged();
200
201
    private:
202
        class QScxmlPrivate* pvt;
203
        friend class QScxmlLoader;
204
        friend struct QScxmlFunctions;
205
        void init();
206
};
207
#endif
208
#endif // QSCXML_H