diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-04-28 12:05:15 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-04-29 06:29:47 (GMT) |
commit | 2c22f8748f62db3cd4c2782abcd19c273f4a4426 (patch) | |
tree | 7042ed9d22fc694aca7651d938496a16c369a58e | |
parent | 6293521a6e5ee0ce010180a40e6fda3ef3a8b245 (diff) | |
download | Qt-2c22f8748f62db3cd4c2782abcd19c273f4a4426.zip Qt-2c22f8748f62db3cd4c2782abcd19c273f4a4426.tar.gz Qt-2c22f8748f62db3cd4c2782abcd19c273f4a4426.tar.bz2 |
say hello (again) to QAbstract{State,Transition}::machine()
It's useful and it's simple for us to expose, so let's.
-rw-r--r-- | examples/statemachine/clockticking/main.cpp | 24 | ||||
-rw-r--r-- | examples/statemachine/pingpong/main.cpp | 31 | ||||
-rw-r--r-- | src/corelib/statemachine/qabstractstate.cpp | 13 | ||||
-rw-r--r-- | src/corelib/statemachine/qabstractstate.h | 2 | ||||
-rw-r--r-- | src/corelib/statemachine/qabstracttransition.cpp | 13 | ||||
-rw-r--r-- | src/corelib/statemachine/qabstracttransition.h | 3 |
6 files changed, 47 insertions, 39 deletions
diff --git a/examples/statemachine/clockticking/main.cpp b/examples/statemachine/clockticking/main.cpp index 5501e2c..301060b 100644 --- a/examples/statemachine/clockticking/main.cpp +++ b/examples/statemachine/clockticking/main.cpp @@ -57,25 +57,21 @@ public: class ClockState : public QState { public: - ClockState(QStateMachine *machine, QState *parent) - : QState(parent), m_machine(machine) {} + ClockState(QState *parent) + : QState(parent) {} protected: virtual void onEntry() { fprintf(stdout, "ClockState entered; posting the initial tick\n"); - m_machine->postEvent(new ClockEvent()); + machine()->postEvent(new ClockEvent()); } - -private: - QStateMachine *m_machine; }; class ClockTransition : public QAbstractTransition { public: - ClockTransition(QStateMachine *machine) - : QAbstractTransition(), m_machine(machine) { } + ClockTransition() {} protected: virtual bool eventTest(QEvent *e) const { @@ -84,18 +80,14 @@ protected: virtual void onTransition() { fprintf(stdout, "ClockTransition triggered; posting another tick with a delay of 1 second\n"); - m_machine->postEvent(new ClockEvent(), 1000); + machine()->postEvent(new ClockEvent(), 1000); } - -private: - QStateMachine *m_machine; }; class ClockListener : public QAbstractTransition { public: - ClockListener() - : QAbstractTransition() {} + ClockListener() {} protected: virtual bool eventTest(QEvent *e) const { @@ -115,9 +107,9 @@ int main(int argc, char **argv) QState *group = new QState(QState::ParallelGroup); group->setObjectName("group"); - ClockState *clock = new ClockState(&machine, group); + ClockState *clock = new ClockState(group); clock->setObjectName("clock"); - clock->addTransition(new ClockTransition(&machine)); + clock->addTransition(new ClockTransition()); QState *listener = new QState(group); listener->setObjectName("listener"); diff --git a/examples/statemachine/pingpong/main.cpp b/examples/statemachine/pingpong/main.cpp index 00ff643..ec1ebf2 100644 --- a/examples/statemachine/pingpong/main.cpp +++ b/examples/statemachine/pingpong/main.cpp @@ -64,25 +64,21 @@ public: class Pinger : public QState { public: - Pinger(QStateMachine *machine, QState *parent) - : QState(parent), m_machine(machine) {} + Pinger(QState *parent) + : QState(parent) {} protected: virtual void onEntry() { - m_machine->postEvent(new PingEvent()); + machine()->postEvent(new PingEvent()); fprintf(stdout, "ping?\n"); } - -private: - QStateMachine *m_machine; }; class PongTransition : public QAbstractTransition { public: - PongTransition(QStateMachine *machine) - : QAbstractTransition(), m_machine(machine) {} + PongTransition() {} protected: virtual bool eventTest(QEvent *e) const { @@ -90,19 +86,15 @@ protected: } virtual void onTransition() { - m_machine->postEvent(new PingEvent(), 500); + machine()->postEvent(new PingEvent(), 500); fprintf(stdout, "ping?\n"); } - -private: - QStateMachine *m_machine; }; class PingTransition : public QAbstractTransition { public: - PingTransition(QStateMachine *machine) - : QAbstractTransition(), m_machine(machine) {} + PingTransition() {} protected: virtual bool eventTest(QEvent *e) const { @@ -110,12 +102,9 @@ protected: } virtual void onTransition() { - m_machine->postEvent(new PongEvent(), 500); + machine()->postEvent(new PongEvent(), 500); fprintf(stdout, "pong!\n"); } - -private: - QStateMachine *m_machine; }; int main(int argc, char **argv) @@ -126,13 +115,13 @@ int main(int argc, char **argv) QState *group = new QState(QState::ParallelGroup); group->setObjectName("group"); - Pinger *pinger = new Pinger(&machine, group); + Pinger *pinger = new Pinger(group); pinger->setObjectName("pinger"); - pinger->addTransition(new PongTransition(&machine)); + pinger->addTransition(new PongTransition()); QState *ponger = new QState(group); ponger->setObjectName("ponger"); - ponger->addTransition(new PingTransition(&machine)); + ponger->addTransition(new PingTransition()); machine.addState(group); machine.setInitialState(group); diff --git a/src/corelib/statemachine/qabstractstate.cpp b/src/corelib/statemachine/qabstractstate.cpp index 12f14d7..cc6f0f9 100644 --- a/src/corelib/statemachine/qabstractstate.cpp +++ b/src/corelib/statemachine/qabstractstate.cpp @@ -63,7 +63,8 @@ QT_BEGIN_NAMESPACE The entered() signal is emitted when the state has been entered. The exited() signal is emitted when the state has been exited. - The parentState() function returns the state's parent state. + The parentState() function returns the state's parent state. The machine() + function returns the state machine that the state is part of. \section1 Subclassing @@ -179,6 +180,16 @@ QState *QAbstractState::parentState() const } /*! + Returns the state machine that this state is part of, or 0 if the state is + not part of a state machine. +*/ +QStateMachine *QAbstractState::machine() const +{ + Q_D(const QAbstractState); + return d->machine(); +} + +/*! \fn QAbstractState::onExit() This function is called when the state is exited. Reimplement this function diff --git a/src/corelib/statemachine/qabstractstate.h b/src/corelib/statemachine/qabstractstate.h index a5f1440..30a68ff 100644 --- a/src/corelib/statemachine/qabstractstate.h +++ b/src/corelib/statemachine/qabstractstate.h @@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Core) class QState; +class QStateMachine; class QAbstractStatePrivate; class Q_CORE_EXPORT QAbstractState : public QObject @@ -60,6 +61,7 @@ public: ~QAbstractState(); QState *parentState() const; + QStateMachine *machine() const; Q_SIGNALS: void entered(); diff --git a/src/corelib/statemachine/qabstracttransition.cpp b/src/corelib/statemachine/qabstracttransition.cpp index dfce310..731871c 100644 --- a/src/corelib/statemachine/qabstracttransition.cpp +++ b/src/corelib/statemachine/qabstracttransition.cpp @@ -61,7 +61,8 @@ QT_BEGIN_NAMESPACE Framework}. The sourceState() function returns the source of the transition. The - targetStates() function returns the targets of the transition. + targetStates() function returns the targets of the transition. The machine() + function returns the state machine that the transition is part of. Transitions can cause animations to be played. Use the addAnimation() function to add an animation to the transition. @@ -286,6 +287,16 @@ void QAbstractTransition::setTargetStates(const QList<QAbstractState*> &targets) d->targetStates = targets; } +/*! + Returns the state machine that this transition is part of, or 0 if the + transition is not part of a state machine. +*/ +QStateMachine *QAbstractTransition::machine() const +{ + Q_D(const QAbstractTransition); + return d->machine(); +} + #ifndef QT_NO_ANIMATION /*! diff --git a/src/corelib/statemachine/qabstracttransition.h b/src/corelib/statemachine/qabstracttransition.h index c49731f..37365c5 100644 --- a/src/corelib/statemachine/qabstracttransition.h +++ b/src/corelib/statemachine/qabstracttransition.h @@ -55,6 +55,7 @@ QT_MODULE(Core) class QEvent; class QAbstractState; class QState; +class QStateMachine; #ifndef QT_NO_ANIMATION class QAbstractAnimation; @@ -78,6 +79,8 @@ public: QList<QAbstractState*> targetStates() const; void setTargetStates(const QList<QAbstractState*> &targets); + QStateMachine *machine() const; + #ifndef QT_NO_ANIMATION void addAnimation(QAbstractAnimation *animation); void removeAnimation(QAbstractAnimation *animation); |