summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-07-22 13:10:35 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-07-22 13:23:06 (GMT)
commit1a55f40b6223511d0eb388064597ab38a0d37627 (patch)
tree522d9fafe3bef5049b75471b266ab2f614dfa412 /examples
parentdf2a1de1720476d096ad9be0a8cf5a0410206d82 (diff)
downloadQt-1a55f40b6223511d0eb388064597ab38a0d37627.zip
Qt-1a55f40b6223511d0eb388064597ab38a0d37627.tar.gz
Qt-1a55f40b6223511d0eb388064597ab38a0d37627.tar.bz2
Make QStateMachine inherit QState
This removes the need for a "root state" in the machine; or rather, the machine _is_ the root state. User code can now pass in a QStateMachine directly to the QState constructor, instead of machine->rootState(). This also means we could get rid of the "proxying" from the machine to the root state for things like properties (initialState et al), finished() signal and auto-reparenting of states (the ChildAdded event hack). A fun little side-effect of this change is that it's now possible to embed state machines within state machines. We can't think of a good use case yet where you would rather embed a stand-alone state machine (with its own event processing etc.) rather than having just a regular nested state, but it's neat and it works. Reviewed-by: Eskil Abrahamsen Blomfeldt
Diffstat (limited to 'examples')
-rw-r--r--examples/animation/appchooser/main.cpp2
-rw-r--r--examples/animation/moveblocks/main.cpp6
-rw-r--r--examples/animation/states/main.cpp7
-rw-r--r--examples/animation/stickman/lifecycle.cpp6
-rw-r--r--examples/animation/sub-attaq/boat.cpp10
-rw-r--r--examples/animation/sub-attaq/bomb.cpp4
-rw-r--r--examples/animation/sub-attaq/graphicsscene.cpp8
-rw-r--r--examples/animation/sub-attaq/states.cpp8
-rw-r--r--examples/animation/sub-attaq/submarine.cpp6
-rw-r--r--examples/animation/sub-attaq/torpedo.cpp4
-rw-r--r--examples/statemachine/factorial/main.cpp4
-rw-r--r--examples/statemachine/tankgame/mainwindow.cpp6
12 files changed, 34 insertions, 37 deletions
diff --git a/examples/animation/appchooser/main.cpp b/examples/animation/appchooser/main.cpp
index fe4be1f..97751b2 100644
--- a/examples/animation/appchooser/main.cpp
+++ b/examples/animation/appchooser/main.cpp
@@ -134,7 +134,7 @@ int main(int argc, char **argv)
QStateMachine machine;
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties);
- QState *group = new QState(machine.rootState());
+ QState *group = new QState(&machine);
group->setObjectName("group");
QRect selectedRect(86, 86, 128, 128);
diff --git a/examples/animation/moveblocks/main.cpp b/examples/animation/moveblocks/main.cpp
index c43e841..97d3f81 100644
--- a/examples/animation/moveblocks/main.cpp
+++ b/examples/animation/moveblocks/main.cpp
@@ -108,8 +108,7 @@ class StateSwitcher : public QState
Q_OBJECT
public:
StateSwitcher(QStateMachine *machine)
- : QState(machine->rootState()), m_machine(machine),
- m_stateCount(0), m_lastIndex(0)
+ : QState(machine), m_stateCount(0), m_lastIndex(0)
{ }
//![10]
@@ -120,7 +119,7 @@ public:
while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex)
{ }
m_lastIndex = n;
- m_machine->postEvent(new StateSwitchEvent(n));
+ machine()->postEvent(new StateSwitchEvent(n));
}
virtual void onExit(QEvent *) {}
//![11]
@@ -135,7 +134,6 @@ public:
//![12]
private:
- QStateMachine *m_machine;
int m_stateCount;
int m_lastIndex;
};
diff --git a/examples/animation/states/main.cpp b/examples/animation/states/main.cpp
index b3c28f2..99e04c3 100644
--- a/examples/animation/states/main.cpp
+++ b/examples/animation/states/main.cpp
@@ -124,10 +124,9 @@ int main(int argc, char *argv[])
scene.addItem(p6);
QStateMachine machine;
- QState *root = machine.rootState();
- QState *state1 = new QState(root);
- QState *state2 = new QState(root);
- QState *state3 = new QState(root);
+ QState *state1 = new QState(&machine);
+ QState *state2 = new QState(&machine);
+ QState *state3 = new QState(&machine);
machine.setInitialState(state1);
// State 1
diff --git a/examples/animation/stickman/lifecycle.cpp b/examples/animation/stickman/lifecycle.cpp
index 2a54c82..c761d87 100644
--- a/examples/animation/stickman/lifecycle.cpp
+++ b/examples/animation/stickman/lifecycle.cpp
@@ -108,11 +108,11 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
m_machine->addDefaultAnimation(m_animationGroup);
//! [3]
- m_alive = new QState(m_machine->rootState());
+ m_alive = new QState(m_machine);
m_alive->setObjectName("alive");
// Make it blink when lightning strikes before entering dead animation
- QState *lightningBlink = new QState(m_machine->rootState());
+ QState *lightningBlink = new QState(m_machine);
lightningBlink->assignProperty(m_stickMan->scene(), "backgroundBrush", Qt::white);
lightningBlink->assignProperty(m_stickMan, "penColor", Qt::black);
lightningBlink->assignProperty(m_stickMan, "fillColor", Qt::white);
@@ -126,7 +126,7 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
QObject::connect(lightningBlink, SIGNAL(exited()), timer, SLOT(stop()));
//! [5]
- m_dead = new QState(m_machine->rootState());
+ m_dead = new QState(m_machine);
m_dead->assignProperty(m_stickMan->scene(), "backgroundBrush", Qt::black);
m_dead->assignProperty(m_stickMan, "penColor", Qt::white);
m_dead->assignProperty(m_stickMan, "fillColor", Qt::black);
diff --git a/examples/animation/sub-attaq/boat.cpp b/examples/animation/sub-attaq/boat.cpp
index d286be5..68e646e 100644
--- a/examples/animation/sub-attaq/boat.cpp
+++ b/examples/animation/sub-attaq/boat.cpp
@@ -142,14 +142,14 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
//We setup the state machien of the boat
machine = new QStateMachine(this);
- QState *moving = new QState(machine->rootState());
+ QState *moving = new QState(machine);
StopState *stopState = new StopState(this, moving);
machine->setInitialState(moving);
moving->setInitialState(stopState);
MoveStateRight *moveStateRight = new MoveStateRight(this, moving);
MoveStateLeft *moveStateLeft = new MoveStateLeft(this, moving);
- LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine->rootState());
- LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine->rootState());
+ LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine);
+ LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine);
//then setup the transitions for the rightMove state
KeyStopTransition *leftStopRight = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Left);
@@ -216,10 +216,10 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags)
launchStateLeft->addTransition(historyState);
launchStateRight->addTransition(historyState);
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
//This state play the destroyed animation
- QAnimationState *destroyedState = new QAnimationState(machine->rootState());
+ QAnimationState *destroyedState = new QAnimationState(machine);
destroyedState->setAnimation(destroyAnimation);
//Play a nice animation when the boat is destroyed
diff --git a/examples/animation/sub-attaq/bomb.cpp b/examples/animation/sub-attaq/bomb.cpp
index 454970a..e92a723 100644
--- a/examples/animation/sub-attaq/bomb.cpp
+++ b/examples/animation/sub-attaq/bomb.cpp
@@ -85,11 +85,11 @@ void Bomb::launch(Bomb::Direction direction)
QStateMachine *machine = new QStateMachine(this);
//This state is when the launch animation is playing
- QAnimationState *launched = new QAnimationState(machine->rootState());
+ QAnimationState *launched = new QAnimationState(machine);
launched->setAnimation(launchAnimation);
//End
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
machine->setInitialState(launched);
diff --git a/examples/animation/sub-attaq/graphicsscene.cpp b/examples/animation/sub-attaq/graphicsscene.cpp
index bd37ce2..fcbc1b3 100644
--- a/examples/animation/sub-attaq/graphicsscene.cpp
+++ b/examples/animation/sub-attaq/graphicsscene.cpp
@@ -230,17 +230,17 @@ void GraphicsScene::setupScene(const QList<QAction *> &actions)
QStateMachine *machine = new QStateMachine(this);
//This state is when the player is playing
- PlayState *gameState = new PlayState(this,machine->rootState());
+ PlayState *gameState = new PlayState(this,machine);
//Final state
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
//Animation when the player enter in the game
- QAnimationState *lettersMovingState = new QAnimationState(machine->rootState());
+ QAnimationState *lettersMovingState = new QAnimationState(machine);
lettersMovingState->setAnimation(lettersGroupMoving);
//Animation when the welcome screen disappear
- QAnimationState *lettersFadingState = new QAnimationState(machine->rootState());
+ QAnimationState *lettersFadingState = new QAnimationState(machine);
lettersFadingState->setAnimation(lettersGroupFading);
//if new game then we fade out the welcome screen and start playing
diff --git a/examples/animation/sub-attaq/states.cpp b/examples/animation/sub-attaq/states.cpp
index 81fd2de..d63737f 100644
--- a/examples/animation/sub-attaq/states.cpp
+++ b/examples/animation/sub-attaq/states.cpp
@@ -83,7 +83,7 @@ void PlayState::onEntry(QEvent *)
machine = new QStateMachine(this);
//This state is when player is playing
- LevelState *levelState = new LevelState(scene, this, machine->rootState());
+ LevelState *levelState = new LevelState(scene, this, machine);
//This state is when the player is actually playing but the game is not paused
QState *playingState = new QState(levelState);
@@ -105,10 +105,10 @@ void PlayState::onEntry(QEvent *)
pauseState->addTransition(pressPpause);
//This state is when player have lost
- LostState *lostState = new LostState(scene, this, machine->rootState());
+ LostState *lostState = new LostState(scene, this, machine);
//This state is when player have won
- WinState *winState = new WinState(scene, this, machine->rootState());
+ WinState *winState = new WinState(scene, this, machine);
//The boat has been destroyed then the game is finished
levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState);
@@ -136,7 +136,7 @@ void PlayState::onEntry(QEvent *)
machine->setInitialState(levelState);
//Final state
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
//This transition is triggered when the player press space after completing a level
CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space);
diff --git a/examples/animation/sub-attaq/submarine.cpp b/examples/animation/sub-attaq/submarine.cpp
index 04b7916..78a9539 100644
--- a/examples/animation/sub-attaq/submarine.cpp
+++ b/examples/animation/sub-attaq/submarine.cpp
@@ -115,7 +115,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem *
QStateMachine *machine = new QStateMachine(this);
//This state is when the boat is moving/rotating
- QState *moving = new QState(machine->rootState());
+ QState *moving = new QState(machine);
//This state is when the boat is moving from left to right
MovementState *movement = new MovementState(this, moving);
@@ -132,7 +132,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem *
machine->setInitialState(moving);
//End
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
//If the moving animation is finished we move to the return state
movement->addTransition(movement, SIGNAL(animationFinished()), rotation);
@@ -141,7 +141,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem *
rotation->addTransition(rotation, SIGNAL(animationFinished()), movement);
//This state play the destroyed animation
- QAnimationState *destroyedState = new QAnimationState(machine->rootState());
+ QAnimationState *destroyedState = new QAnimationState(machine);
destroyedState->setAnimation(setupDestroyAnimation(this));
//Play a nice animation when the submarine is destroyed
diff --git a/examples/animation/sub-attaq/torpedo.cpp b/examples/animation/sub-attaq/torpedo.cpp
index 5ef237a..fe79488 100644
--- a/examples/animation/sub-attaq/torpedo.cpp
+++ b/examples/animation/sub-attaq/torpedo.cpp
@@ -74,11 +74,11 @@ void Torpedo::launch()
QStateMachine *machine = new QStateMachine(this);
//This state is when the launch animation is playing
- QAnimationState *launched = new QAnimationState(machine->rootState());
+ QAnimationState *launched = new QAnimationState(machine);
launched->setAnimation(launchAnimation);
//End
- QFinalState *final = new QFinalState(machine->rootState());
+ QFinalState *final = new QFinalState(machine);
machine->setInitialState(launched);
diff --git a/examples/statemachine/factorial/main.cpp b/examples/statemachine/factorial/main.cpp
index 18a9521..5050347 100644
--- a/examples/statemachine/factorial/main.cpp
+++ b/examples/statemachine/factorial/main.cpp
@@ -151,14 +151,14 @@ int main(int argc, char **argv)
//! [3]
//! [4]
- QState *compute = new QState(machine.rootState());
+ QState *compute = new QState(&machine);
compute->assignProperty(&factorial, "fac", 1);
compute->assignProperty(&factorial, "x", 6);
compute->addTransition(new FactorialLoopTransition(&factorial));
//! [4]
//! [5]
- QFinalState *done = new QFinalState(machine.rootState());
+ QFinalState *done = new QFinalState(&machine);
FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&factorial);
doneTransition->setTargetState(done);
compute->addTransition(doneTransition);
diff --git a/examples/statemachine/tankgame/mainwindow.cpp b/examples/statemachine/tankgame/mainwindow.cpp
index 68a8d68..596cdfe 100644
--- a/examples/statemachine/tankgame/mainwindow.cpp
+++ b/examples/statemachine/tankgame/mainwindow.cpp
@@ -160,7 +160,7 @@ void MainWindow::init()
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
m_machine = new QStateMachine(this);
- QState *stoppedState = new QState(m_machine->rootState());
+ QState *stoppedState = new QState(m_machine);
stoppedState->setObjectName("stoppedState");
stoppedState->assignProperty(runGameAction, "enabled", true);
stoppedState->assignProperty(stopGameAction, "enabled", false);
@@ -188,14 +188,14 @@ void MainWindow::init()
stoppedState->setInitialState(hs);
//! [0]
- m_runningState = new QState(QState::ParallelStates, m_machine->rootState());
+ m_runningState = new QState(QState::ParallelStates, m_machine);
//! [0]
m_runningState->setObjectName("runningState");
m_runningState->assignProperty(addTankAction, "enabled", false);
m_runningState->assignProperty(runGameAction, "enabled", false);
m_runningState->assignProperty(stopGameAction, "enabled", true);
- QState *gameOverState = new QState(m_machine->rootState());
+ QState *gameOverState = new QState(m_machine);
gameOverState->setObjectName("gameOverState");
gameOverState->assignProperty(stopGameAction, "enabled", false);
connect(gameOverState, SIGNAL(entered()), this, SLOT(gameOver()));