/**************************************************************************** ** ** 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 #include #include #include #include #include #include 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; } protected: bool eventTest(QEvent*); void onTransition (QEvent*); 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 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); 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; void init(); }; #endif #endif // QSCXML_H