diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-07-22 13:10:35 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-07-22 13:23:06 (GMT) |
commit | 1a55f40b6223511d0eb388064597ab38a0d37627 (patch) | |
tree | 522d9fafe3bef5049b75471b266ab2f614dfa412 | |
parent | df2a1de1720476d096ad9be0a8cf5a0410206d82 (diff) | |
download | Qt-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
-rw-r--r-- | examples/animation/appchooser/main.cpp | 2 | ||||
-rw-r--r-- | examples/animation/moveblocks/main.cpp | 6 | ||||
-rw-r--r-- | examples/animation/states/main.cpp | 7 | ||||
-rw-r--r-- | examples/animation/stickman/lifecycle.cpp | 6 | ||||
-rw-r--r-- | examples/animation/sub-attaq/boat.cpp | 10 | ||||
-rw-r--r-- | examples/animation/sub-attaq/bomb.cpp | 4 | ||||
-rw-r--r-- | examples/animation/sub-attaq/graphicsscene.cpp | 8 | ||||
-rw-r--r-- | examples/animation/sub-attaq/states.cpp | 8 | ||||
-rw-r--r-- | examples/animation/sub-attaq/submarine.cpp | 6 | ||||
-rw-r--r-- | examples/animation/sub-attaq/torpedo.cpp | 4 | ||||
-rw-r--r-- | examples/statemachine/factorial/main.cpp | 4 | ||||
-rw-r--r-- | examples/statemachine/tankgame/mainwindow.cpp | 6 | ||||
-rw-r--r-- | src/corelib/statemachine/qabstracttransition.cpp | 15 | ||||
-rw-r--r-- | src/corelib/statemachine/qstate.cpp | 24 | ||||
-rw-r--r-- | src/corelib/statemachine/qstatemachine.cpp | 255 | ||||
-rw-r--r-- | src/corelib/statemachine/qstatemachine.h | 22 | ||||
-rw-r--r-- | src/corelib/statemachine/qstatemachine_p.h | 17 | ||||
-rw-r--r-- | tests/auto/qstate/tst_qstate.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qstatemachine/tst_qstatemachine.cpp | 511 |
19 files changed, 428 insertions, 499 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())); diff --git a/src/corelib/statemachine/qabstracttransition.cpp b/src/corelib/statemachine/qabstracttransition.cpp index 670aa7d..0004d3e 100644 --- a/src/corelib/statemachine/qabstracttransition.cpp +++ b/src/corelib/statemachine/qabstracttransition.cpp @@ -115,13 +115,10 @@ QAbstractTransitionPrivate *QAbstractTransitionPrivate::get(QAbstractTransition QStateMachine *QAbstractTransitionPrivate::machine() const { - QObject *par = parent; - while (par != 0) { - if (QStateMachine *mach = qobject_cast<QStateMachine*>(par)) - return mach; - par = par->parent(); - } - return 0; + QState *source = sourceState(); + if (!source) + return 0; + return source->machine(); } bool QAbstractTransitionPrivate::callEventTest(QEvent *e) @@ -256,10 +253,6 @@ void QAbstractTransition::setTargetStates(const QList<QAbstractState*> &targets) qWarning("QAbstractTransition::setTargetStates: target state(s) cannot be null"); return; } - if (target->machine() != 0 && target->machine()->rootState() == target) { - qWarning("QAbstractTransition::setTargetStates: root state cannot be target of transition"); - return; - } } d->targetStates.clear(); diff --git a/src/corelib/statemachine/qstate.cpp b/src/corelib/statemachine/qstate.cpp index 83dd869..f74edc3 100644 --- a/src/corelib/statemachine/qstate.cpp +++ b/src/corelib/statemachine/qstate.cpp @@ -71,7 +71,7 @@ QT_BEGIN_NAMESPACE The assignProperty() function is used for defining property assignments that should be performed when a state is entered. - Top-level states must be passed QStateMachine::rootState() as their parent + Top-level states must be passed a QStateMachine object as their parent state, or added to a state machine using QStateMachine::addState(). \section1 States with Child States @@ -242,7 +242,7 @@ void QState::assignProperty(QObject *object, const char *name, /*! Returns this state's error state. - \sa QStateMachine::errorState(), QStateMachine::setErrorState() + \sa QStateMachine::error() */ QAbstractState *QState::errorState() const { @@ -256,19 +256,17 @@ QAbstractState *QState::errorState() const state recursively. If no error state is set for the state itself or any of its ancestors, an error will cause the machine to stop executing and an error will be printed to the console. - - \sa QStateMachine::setErrorState(), QStateMachine::errorState() */ void QState::setErrorState(QAbstractState *state) { Q_D(QState); - if (state != 0 && state->machine() != machine()) { - qWarning("QState::setErrorState: error state cannot belong " - "to a different state machine"); + if (state != 0 && qobject_cast<QStateMachine*>(state)) { + qWarning("QStateMachine::setErrorState: root state cannot be error state"); return; } - if (state != 0 && state->machine() != 0 && state->machine()->rootState() == state) { - qWarning("QStateMachine::setErrorState: root state cannot be error state"); + if (state != 0 && (!state->machine() || ((state->machine() != machine()) && !qobject_cast<QStateMachine*>(this)))) { + qWarning("QState::setErrorState: error state cannot belong " + "to a different state machine"); return; } @@ -288,12 +286,7 @@ QAbstractTransition *QState::addTransition(QAbstractTransition *transition) return 0; } - // machine() will always be non-null for root state - if (machine() != 0 && machine()->rootState() == this) { - qWarning("QState::addTransition: cannot add transition from root state"); - return 0; - } - + transition->setParent(this); const QList<QPointer<QAbstractState> > &targets = QAbstractTransitionPrivate::get(transition)->targetStates; for (int i = 0; i < targets.size(); ++i) { QAbstractState *t = targets.at(i); @@ -308,7 +301,6 @@ QAbstractTransition *QState::addTransition(QAbstractTransition *transition) return 0; } } - transition->setParent(this); if (machine() != 0 && machine()->configuration().contains(this)) QStateMachinePrivate::get(machine())->registerTransitions(this); return transition; diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp index a00e7e1..5402b04 100644 --- a/src/corelib/statemachine/qstatemachine.cpp +++ b/src/corelib/statemachine/qstatemachine.cpp @@ -102,14 +102,9 @@ QT_BEGIN_NAMESPACE Framework}{overview} gives several state graphs and the code to build them. - The rootState() is the parent of all top-level states in the - machine; it is used, for instance, when the state graph is - deleted. It is created by the machine. - - Use the addState() function to add a state to the state machine. - All top-level states are added to the root state. States are - removed with the removeState() function. Removing states while the - machine is running is discouraged. + Use the addState() function to add a top-level state to the state machine. + States are removed with the removeState() function. Removing states while + the machine is running is discouraged. Before the machine can be started, the \l{initialState}{initial state} must be set. The initial state is the state that the @@ -179,26 +174,6 @@ This is */ /*! - \property QStateMachine::rootState - - \brief the root state of this state machine -*/ - -/*! - \property QStateMachine::initialState - - \brief the initial state of this state machine - - The initial state must be one of the rootState()'s child states. -*/ - -/*! - \property QStateMachine::errorState - - \brief the error state of this state machine -*/ - -/*! \property QStateMachine::errorString \brief the error string of this state machine @@ -235,7 +210,6 @@ QStateMachinePrivate::QStateMachinePrivate() stop = false; error = QStateMachine::NoError; globalRestorePolicy = QStateMachine::DoNotRestoreProperties; - rootState = 0; signalEventGenerator = 0; #ifndef QT_NO_ANIMATION animationsEnabled = true; @@ -255,6 +229,11 @@ QStateMachinePrivate *QStateMachinePrivate::get(QStateMachine *q) return 0; } +QState *QStateMachinePrivate::rootState() const +{ + return const_cast<QStateMachine*>(q_func()); +} + static QEvent *cloneEvent(QEvent *e) { switch (e->type()) { @@ -302,7 +281,9 @@ bool QStateMachinePrivate::stateEntryLessThan(QAbstractState *s1, QAbstractState } else if (isDescendantOf(s2, s1)) { return true; } else { - QState *lca = findLCA(QList<QAbstractState*>() << s1 << s2); + Q_ASSERT(s1->machine() != 0); + QStateMachinePrivate *mach = QStateMachinePrivate::get(s1->machine()); + QState *lca = mach->findLCA(QList<QAbstractState*>() << s1 << s2); Q_ASSERT(lca != 0); return (indexOfDescendant(lca, s1) < indexOfDescendant(lca, s2)); } @@ -318,17 +299,19 @@ bool QStateMachinePrivate::stateExitLessThan(QAbstractState *s1, QAbstractState } else if (isDescendantOf(s2, s1)) { return false; } else { - QState *lca = findLCA(QList<QAbstractState*>() << s1 << s2); + Q_ASSERT(s1->machine() != 0); + QStateMachinePrivate *mach = QStateMachinePrivate::get(s1->machine()); + QState *lca = mach->findLCA(QList<QAbstractState*>() << s1 << s2); Q_ASSERT(lca != 0); return (indexOfDescendant(lca, s1) < indexOfDescendant(lca, s2)); } } -QState *QStateMachinePrivate::findLCA(const QList<QAbstractState*> &states) +QState *QStateMachinePrivate::findLCA(const QList<QAbstractState*> &states) const { if (states.isEmpty()) return 0; - QList<QState*> ancestors = properAncestors(states.at(0), 0); + QList<QState*> ancestors = properAncestors(states.at(0), rootState()->parentState()); for (int i = 0; i < ancestors.size(); ++i) { QState *anc = ancestors.at(i); bool ok = true; @@ -376,7 +359,7 @@ QSet<QAbstractTransition*> QStateMachinePrivate::selectTransitions(QEvent *event continue; if (isPreempted(state, enabledTransitions)) continue; - QList<QState*> lst = properAncestors(state, 0); + QList<QState*> lst = properAncestors(state, rootState()->parentState()); if (QState *grp = qobject_cast<QState*>(state)) lst.prepend(grp); bool found = false; @@ -557,11 +540,13 @@ QList<QAbstractState*> QStateMachinePrivate::enterStates(QEvent *event, const QL if (isFinal(s)) { QState *parent = s->parentState(); if (parent) { - QState *grandparent = parent->parentState(); + if (parent != rootState()) { #ifdef QSTATEMACHINE_DEBUG - qDebug() << q << ": emitting finished signal for" << parent; + qDebug() << q << ": emitting finished signal for" << parent; #endif - QStatePrivate::get(parent)->emitFinished(); + QStatePrivate::get(parent)->emitFinished(); + } + QState *grandparent = parent->parentState(); if (grandparent && isParallel(grandparent)) { bool allChildStatesFinal = true; QList<QAbstractState*> childStates = QStatePrivate::get(grandparent)->childStates(); @@ -572,7 +557,7 @@ QList<QAbstractState*> QStateMachinePrivate::enterStates(QEvent *event, const QL break; } } - if (allChildStatesFinal) { + if (allChildStatesFinal && (grandparent != rootState())) { #ifdef QSTATEMACHINE_DEBUG qDebug() << q << ": emitting finished signal for" << grandparent; #endif @@ -585,7 +570,7 @@ QList<QAbstractState*> QStateMachinePrivate::enterStates(QEvent *event, const QL { QSet<QAbstractState*>::const_iterator it; for (it = configuration.constBegin(); it != configuration.constEnd(); ++it) { - if (isFinal(*it) && (*it)->parentState() == rootState) { + if (isFinal(*it) && (*it)->parentState() == rootState()) { processing = false; stopProcessingReason = Finished; break; @@ -630,6 +615,11 @@ void QStateMachinePrivate::addStatesToEnter(QAbstractState *s, QState *root, } } } else { + if (s == rootState()) { + // Error has already been set by exitStates(). + Q_ASSERT(error != QStateMachine::NoError); + return; + } statesToEnter.insert(s); if (isParallel(s)) { QState *grp = qobject_cast<QState*>(s); @@ -643,6 +633,7 @@ void QStateMachinePrivate::addStatesToEnter(QAbstractState *s, QState *root, QState *grp = qobject_cast<QState*>(s); QAbstractState *initial = grp->initialState(); if (initial != 0) { + Q_ASSERT(initial->machine() == q_func()); addStatesToEnter(initial, grp, statesToEnter, statesForDefaultEntry); } else { setError(QStateMachine::NoInitialStateError, grp); @@ -873,20 +864,26 @@ bool QStateMachinePrivate::isParallel(const QAbstractState *s) return ss && (QStatePrivate::get(ss)->childMode == QState::ParallelStates); } -bool QStateMachinePrivate::isCompound(const QAbstractState *s) +bool QStateMachinePrivate::isCompound(const QAbstractState *s) const { const QState *group = qobject_cast<const QState*>(s); if (!group) return false; + bool isMachine = (qobject_cast<const QStateMachine*>(group) != 0); + // Don't treat the machine as compound if it's a sub-state of this machine + if (isMachine && (group != rootState())) + return false; return (!isParallel(group) && !QStatePrivate::get(group)->childStates().isEmpty()) - || (qobject_cast<QStateMachine*>(group->parent()) != 0); + || isMachine; } -bool QStateMachinePrivate::isAtomic(const QAbstractState *s) +bool QStateMachinePrivate::isAtomic(const QAbstractState *s) const { const QState *ss = qobject_cast<const QState*>(s); return (ss && QStatePrivate::get(ss)->childStates().isEmpty()) - || isFinal(s); + || isFinal(s) + // Treat the machine as atomic if it's a sub-state of this machine + || (ss && (qobject_cast<const QStateMachine*>(ss) != 0) && (ss != rootState())); } @@ -1034,7 +1031,7 @@ void QStateMachinePrivate::setError(QStateMachine::Error errorCode, QAbstractSta if (currentContext == currentErrorState) currentErrorState = 0; - Q_ASSERT(currentErrorState != rootState); + Q_ASSERT(currentErrorState != rootState()); if (currentErrorState != 0) { QState *lca = findLCA(QList<QAbstractState*>() << currentErrorState << currentContext); @@ -1141,11 +1138,8 @@ void QStateMachinePrivate::_q_start() { Q_Q(QStateMachine); Q_ASSERT(state == Starting); - if (!rootState) { - state = NotRunning; - return; - } - QAbstractState *initial = rootState->initialState(); + Q_ASSERT(rootState() != 0); + QAbstractState *initial = rootState()->initialState(); configuration.clear(); qDeleteAll(internalEventQueue); internalEventQueue.clear(); @@ -1159,7 +1153,7 @@ void QStateMachinePrivate::_q_start() processingScheduled = true; // we call _q_process() below emit q->started(); - StartState *start = new StartState(rootState); + StartState *start = new StartState(rootState()); QAbstractTransition *initialTransition = new InitialTransition(initial); start->addTransition(initialTransition); QList<QAbstractTransition*> transitions; @@ -1371,15 +1365,22 @@ void QStateMachinePrivate::unregisterSignalTransition(QSignalTransition *transit void QStateMachinePrivate::unregisterAllTransitions() { + Q_Q(QStateMachine); { - QList<QSignalTransition*> transitions = qFindChildren<QSignalTransition*>(rootState); - for (int i = 0; i < transitions.size(); ++i) - unregisterSignalTransition(transitions.at(i)); + QList<QSignalTransition*> transitions = qFindChildren<QSignalTransition*>(rootState()); + for (int i = 0; i < transitions.size(); ++i) { + QSignalTransition *t = transitions.at(i); + if (t->machine() == q) + unregisterSignalTransition(t); + } } { - QList<QEventTransition*> transitions = qFindChildren<QEventTransition*>(rootState); - for (int i = 0; i < transitions.size(); ++i) - unregisterEventTransition(transitions.at(i)); + QList<QEventTransition*> transitions = qFindChildren<QEventTransition*>(rootState()); + for (int i = 0; i < transitions.size(); ++i) { + QEventTransition *t = transitions.at(i); + if (t->machine() == q) + unregisterEventTransition(t); + } } } @@ -1457,16 +1458,20 @@ void QStateMachinePrivate::handleTransitionSignal(const QObject *sender, int sig Constructs a new state machine with the given \a parent. */ QStateMachine::QStateMachine(QObject *parent) - : QObject(*new QStateMachinePrivate, parent) + : QState(*new QStateMachinePrivate, /*parentState=*/0) { + // Can't pass the parent to the QState constructor, as it expects a QState + // But this works as expected regardless of whether parent is a QState or not + setParent(parent); } /*! \internal */ QStateMachine::QStateMachine(QStateMachinePrivate &dd, QObject *parent) - : QObject(dd, parent) + : QState(dd, /*parentState=*/0) { + setParent(parent); } /*! @@ -1476,69 +1481,6 @@ QStateMachine::~QStateMachine() { } -namespace { - -class RootState : public QState -{ -public: - RootState(QState *parent) - : QState(parent) - { - } - - void onEntry(QEvent *) {} - void onExit(QEvent *) {} -}; - -} // namespace - -/*! - Returns this state machine's root state. -*/ -QState *QStateMachine::rootState() const -{ - Q_D(const QStateMachine); - if (!d->rootState) { - const_cast<QStateMachinePrivate*>(d)->rootState = new RootState(0); - d->rootState->setParent(const_cast<QStateMachine*>(this)); - } - return d->rootState; -} - -/*! - Returns the error state of the state machine's root state. - - \sa QState::errorState() -*/ -QAbstractState *QStateMachine::errorState() const -{ - return rootState()->errorState(); -} - -/*! - Sets the error state of this state machine's root state to be \a state. When a running state - machine encounters an error which puts it in an undefined state, it will enter an error state - based on the context of the error that occurred. It will enter this state regardless of what - is currently in the event queue. - - If the erroneous state has an error state set, this will be entered by the machine. If no error - state has been set, the state machine will search the parent hierarchy recursively for an - error state. The error state of the root state can thus be seen as a global error state that - applies for all states for which a more specific error state has not been set. - - Before entering the error state, the state machine will set the error code returned by error() and - error message returned by errorString(). - - If there is no error state available for the erroneous state, the state machine will print a - warning message on the console and stop executing. - - \sa QState::setErrorState(), rootState() -*/ -void QStateMachine::setErrorState(QAbstractState *state) -{ - rootState()->setErrorState(state); -} - /*! \enum QStateMachine::Error This enum type defines errors that can occur in the state machine at run time. When the state @@ -1640,39 +1582,13 @@ void QStateMachine::setGlobalRestorePolicy(QStateMachine::RestorePolicy restoreP } /*! - Returns this state machine's initial state, or 0 if no initial state has - been set. -*/ -QAbstractState *QStateMachine::initialState() const -{ - Q_D(const QStateMachine); - if (!d->rootState) - return 0; - return d->rootState->initialState(); -} - -/*! - Sets this state machine's initial \a state. -*/ -void QStateMachine::setInitialState(QAbstractState *state) -{ - Q_D(QStateMachine); - if (!d->rootState) { - if (!state) - return; - rootState()->setInitialState(state); - } - d->rootState->setInitialState(state); -} - -/*! Adds the given \a state to this state machine. The state becomes a top-level - state (i.e. a child of the rootState()). + state. If the state is already in a different machine, it will first be removed from its old machine, and then added to this machine. - \sa removeState(), rootState(), setInitialState() + \sa removeState(), setInitialState() */ void QStateMachine::addState(QAbstractState *state) { @@ -1684,7 +1600,7 @@ void QStateMachine::addState(QAbstractState *state) qWarning("QStateMachine::addState: state has already been added to this machine"); return; } - state->setParent(rootState()); + state->setParent(this); } /*! @@ -1730,7 +1646,7 @@ void QStateMachine::start() { Q_D(QStateMachine); - if (rootState()->initialState() == 0) { + if (initialState() == 0) { qWarning("QStateMachine::start: No initial state set for machine. Refusing to start."); return; } @@ -1821,7 +1737,7 @@ void QStateMachine::postInternalEvent(QEvent *event) Returns the maximal consistent set of states (including parallel and final states) that this state machine is currently in. If a state \c s is in the configuration, it is always the case that the parent of \c s is also in - c. Note, however, that the rootState() is not an explicit member of the + c. Note, however, that the machine itself is not an explicit member of the configuration. */ QSet<QAbstractState*> QStateMachine::configuration() const @@ -1840,15 +1756,6 @@ QSet<QAbstractState*> QStateMachine::configuration() const */ /*! - \fn QStateMachine::finished() - - This signal is emitted when the state machine has reached a top-level final - state (QFinalState). - - \sa QStateMachine::started() -*/ - -/*! \fn QStateMachine::stopped() This signal is emitted when the state machine has stopped. @@ -1872,14 +1779,6 @@ bool QStateMachine::event(QEvent *e) d->scheduleProcess(); return true; } - } else if (e->type() == QEvent::ChildAdded) { - QChildEvent *ce = static_cast<QChildEvent*>(e); - if (QAbstractState *state = qobject_cast<QAbstractState*>(ce->child())) { - if (state != rootState()) { - state->setParent(rootState()); - return true; - } - } } return QObject::event(e); } @@ -1949,6 +1848,24 @@ void QStateMachine::endMicrostep(QEvent *event) Q_UNUSED(event); } +/*! + \reimp +*/ +void QStateMachine::onEntry(QEvent *event) +{ + start(); + QState::onEntry(event); +} + +/*! + \reimp +*/ +void QStateMachine::onExit(QEvent *event) +{ + stop(); + QState::onExit(event); +} + #ifndef QT_NO_ANIMATION /*! diff --git a/src/corelib/statemachine/qstatemachine.h b/src/corelib/statemachine/qstatemachine.h index 30d0e3a..230d852 100644 --- a/src/corelib/statemachine/qstatemachine.h +++ b/src/corelib/statemachine/qstatemachine.h @@ -42,7 +42,7 @@ #ifndef QSTATEMACHINE_H #define QSTATEMACHINE_H -#include <QtCore/qabstractstate.h> +#include <QtCore/qstate.h> #include <QtCore/qlist.h> #include <QtCore/qobject.h> @@ -57,18 +57,12 @@ QT_MODULE(Core) #ifndef QT_NO_STATEMACHINE class QEvent; -class QAbstractState; -class QState; class QStateMachinePrivate; class QAbstractAnimation; -class QAbstractState; -class Q_CORE_EXPORT QStateMachine : public QObject +class Q_CORE_EXPORT QStateMachine : public QState { Q_OBJECT - Q_PROPERTY(QState* rootState READ rootState) - Q_PROPERTY(QAbstractState* initialState READ initialState WRITE setInitialState) - Q_PROPERTY(QAbstractState* errorState READ errorState WRITE setErrorState) Q_PROPERTY(QString errorString READ errorString) Q_PROPERTY(RestorePolicy globalRestorePolicy READ globalRestorePolicy WRITE setGlobalRestorePolicy) Q_ENUMS(RestorePolicy) @@ -94,14 +88,6 @@ public: void addState(QAbstractState *state); void removeState(QAbstractState *state); - QState *rootState() const; - - QAbstractState *initialState() const; - void setInitialState(QAbstractState *state); - - QAbstractState *errorState() const; - void setErrorState(QAbstractState *state); - Error error() const; QString errorString() const; void clearError(); @@ -135,9 +121,11 @@ public Q_SLOTS: Q_SIGNALS: void started(); void stopped(); - void finished(); protected: + void onEntry(QEvent *event); + void onExit(QEvent *event); + void postInternalEvent(QEvent *event); virtual void beginSelectTransitions(QEvent *event); diff --git a/src/corelib/statemachine/qstatemachine_p.h b/src/corelib/statemachine/qstatemachine_p.h index 1335b93..21e405d 100644 --- a/src/corelib/statemachine/qstatemachine_p.h +++ b/src/corelib/statemachine/qstatemachine_p.h @@ -53,7 +53,8 @@ // We mean it. // -#include <private/qobject_p.h> +#include "private/qstate_p.h" + #include <QtCore/qcoreevent.h> #include <QtCore/qhash.h> #include <QtCore/qlist.h> @@ -61,9 +62,6 @@ #include <QtCore/qset.h> #include <QtCore/qvector.h> -#include "qstate.h" -#include "private/qstate_p.h" - QT_BEGIN_NAMESPACE class QEvent; @@ -81,7 +79,7 @@ class QAbstractAnimation; #endif class QStateMachine; -class QStateMachinePrivate : public QObjectPrivate +class QStateMachinePrivate : public QStatePrivate { Q_DECLARE_PUBLIC(QStateMachine) public: @@ -101,7 +99,7 @@ public: static QStateMachinePrivate *get(QStateMachine *q); - static QState *findLCA(const QList<QAbstractState*> &states); + QState *findLCA(const QList<QAbstractState*> &states) const; static bool stateEntryLessThan(QAbstractState *s1, QAbstractState *s2); static bool stateExitLessThan(QAbstractState *s1, QAbstractState *s2); @@ -116,6 +114,8 @@ public: void _q_animationFinished(); #endif + QState *rootState() const; + void microstep(QEvent *event, const QList<QAbstractTransition*> &transitionList); bool isPreempted(const QAbstractState *s, const QSet<QAbstractTransition*> &transitions) const; QSet<QAbstractTransition*> selectTransitions(QEvent *event) const; @@ -133,8 +133,8 @@ public: bool isInFinalState(QAbstractState *s) const; static bool isFinal(const QAbstractState *s); static bool isParallel(const QAbstractState *s); - static bool isCompound(const QAbstractState *s); - static bool isAtomic(const QAbstractState *s); + bool isCompound(const QAbstractState *s) const; + bool isAtomic(const QAbstractState *s) const; static bool isDescendantOf(const QAbstractState *s, const QAbstractState *other); static QList<QState*> properAncestors(const QAbstractState *s, const QState *upperBound); @@ -164,7 +164,6 @@ public: bool processingScheduled; bool stop; StopProcessingReason stopProcessingReason; - QState *rootState; QSet<QAbstractState*> configuration; QList<QEvent*> internalEventQueue; QList<QEvent*> externalEventQueue; diff --git a/tests/auto/qstate/tst_qstate.cpp b/tests/auto/qstate/tst_qstate.cpp index ab87767..78b9853 100644 --- a/tests/auto/qstate/tst_qstate.cpp +++ b/tests/auto/qstate/tst_qstate.cpp @@ -60,10 +60,10 @@ tst_QState::~tst_QState() void tst_QState::test() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); QCOMPARE(s1->machine(), &machine); - QCOMPARE(s1->parentState(), machine.rootState()); + QCOMPARE(s1->parentState(), &machine); QCOMPARE(s1->initialState(), (QState*)0); QVERIFY(s1->childStates().isEmpty()); QVERIFY(s1->transitions().isEmpty()); @@ -218,7 +218,7 @@ void tst_QState::assignProperty() QObject *object = new QObject(); object->setProperty("fooBar", 10); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(object, "fooBar", 20); machine.setInitialState(s1); @@ -235,7 +235,7 @@ void tst_QState::assignPropertyTwice() QObject *object = new QObject(); object->setProperty("fooBar", 10); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(object, "fooBar", 20); s1->assignProperty(object, "fooBar", 30); @@ -271,9 +271,9 @@ void tst_QState::historyInitialState() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); QHistoryState *h1 = new QHistoryState(s2); s2->setInitialState(h1); diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp index 44fc998..7f4d9f5 100644 --- a/tests/auto/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -190,6 +190,8 @@ private slots: // void overrideDefaultSourceAnimationWithSpecific(); // void overrideDefaultTargetAnimationWithSpecific(); // void overrideDefaultTargetAnimationWithSource(); + + void nestedStateMachines(); }; tst_QStateMachine::tst_QStateMachine() @@ -259,13 +261,17 @@ private: void tst_QStateMachine::transitionToRootState() { QStateMachine machine; + machine.setObjectName("machine"); QState *initialState = new QState(); + initialState->setObjectName("initial"); machine.addState(initialState); machine.setInitialState(initialState); - QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::setTargetStates: root state cannot be target of transition"); - initialState->addTransition(new EventTransition(QEvent::User, machine.rootState())); + QAbstractTransition *trans = initialState->addTransition(new EventTransition(QEvent::User, &machine)); + QVERIFY(trans != 0); + QCOMPARE(trans->sourceState(), initialState); + QCOMPARE(trans->targetState(), &machine); machine.start(); QCoreApplication::processEvents(); @@ -274,22 +280,21 @@ void tst_QStateMachine::transitionToRootState() QVERIFY(machine.configuration().contains(initialState)); machine.postEvent(new QEvent(QEvent::User)); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 'initial'"); QCoreApplication::processEvents(); - - QCOMPARE(machine.configuration().count(), 1); - QVERIFY(machine.configuration().contains(initialState)); + QVERIFY(machine.configuration().isEmpty()); + QVERIFY(!machine.isRunning()); } void tst_QStateMachine::transitionFromRootState() { QStateMachine machine; - QState *root = machine.rootState(); + QState *root = &machine; QState *s1 = new QState(root); EventTransition *trans = new EventTransition(QEvent::User, s1); - QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition from root state"); - root->addTransition(trans); - QCOMPARE(trans->sourceState(), (QState*)0); - delete trans; + QCOMPARE(root->addTransition(trans), trans); + QCOMPARE(trans->sourceState(), root); + QCOMPARE(trans->targetState(), s1); } void tst_QStateMachine::transitionEntersParent() @@ -671,7 +676,7 @@ void tst_QStateMachine::errorStateIsRootState() { QStateMachine machine; QTest::ignoreMessage(QtWarningMsg, "QStateMachine::setErrorState: root state cannot be error state"); - machine.setErrorState(machine.rootState()); + machine.setErrorState(&machine); QState *initialState = new QState(); initialState->setObjectName("initialState"); @@ -772,7 +777,7 @@ void tst_QStateMachine::errorStateEntersParentFirst() void tst_QStateMachine::customErrorStateIsNull() { QStateMachine machine; - machine.rootState()->setErrorState(0); + machine.setErrorState(0); QState *initialState = new QState(); machine.addState(initialState); @@ -798,9 +803,9 @@ void tst_QStateMachine::customErrorStateIsNull() void tst_QStateMachine::clearError() { QStateMachine machine; - machine.setErrorState(new QState(machine.rootState())); // avoid warnings + machine.setErrorState(new QState(&machine)); // avoid warnings - QState *brokenState = new QState(machine.rootState()); + QState *brokenState = new QState(&machine); brokenState->setObjectName("brokenState"); machine.setInitialState(brokenState); new QState(brokenState); @@ -822,13 +827,13 @@ void tst_QStateMachine::historyStateAsInitialState() { QStateMachine machine; - QHistoryState *hs = new QHistoryState(machine.rootState()); + QHistoryState *hs = new QHistoryState(&machine); machine.setInitialState(hs); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); hs->setDefaultState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); QHistoryState *s2h = new QHistoryState(s2); s2->setInitialState(s2h); @@ -856,11 +861,11 @@ void tst_QStateMachine::historyStateHasNowhereToGo() { QStateMachine machine; - QState *initialState = new QState(machine.rootState()); + QState *initialState = new QState(&machine); machine.setInitialState(initialState); - machine.setErrorState(new QState(machine.rootState())); // avoid warnings + machine.setErrorState(new QState(&machine)); // avoid warnings - QState *brokenState = new QState(machine.rootState()); + QState *brokenState = new QState(&machine); brokenState->setObjectName("brokenState"); brokenState->setInitialState(new QState(brokenState)); @@ -890,14 +895,14 @@ void tst_QStateMachine::brokenStateIsNeverEntered() entryController->setProperty("childStateEntered", false); entryController->setProperty("errorStateEntered", false); - QState *initialState = new QState(machine.rootState()); + QState *initialState = new QState(&machine); machine.setInitialState(initialState); - QState *errorState = new QState(machine.rootState()); + QState *errorState = new QState(&machine); errorState->assignProperty(entryController, "errorStateEntered", true); machine.setErrorState(errorState); - QState *brokenState = new QState(machine.rootState()); + QState *brokenState = new QState(&machine); brokenState->assignProperty(entryController, "brokenStateEntered", true); brokenState->setObjectName("brokenState"); @@ -921,7 +926,7 @@ void tst_QStateMachine::transitionToStateNotInGraph() { QStateMachine machine; - QState *initialState = new QState(machine.rootState()); + QState *initialState = new QState(&machine); initialState->setObjectName("initialState"); machine.setInitialState(initialState); @@ -946,7 +951,7 @@ void tst_QStateMachine::customErrorStateNotInGraph() machine.setErrorState(&errorState); QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); - QState *initialBrokenState = new QState(machine.rootState()); + QState *initialBrokenState = new QState(&machine); initialBrokenState->setObjectName("initialBrokenState"); machine.setInitialState(initialBrokenState); new QState(initialBrokenState); @@ -1012,25 +1017,22 @@ void tst_QStateMachine::restoreProperties() void tst_QStateMachine::rootState() { QStateMachine machine; - QVERIFY(machine.rootState() != 0); - QVERIFY(qobject_cast<QState*>(machine.rootState()) != 0); - QCOMPARE(qobject_cast<QState*>(machine.rootState())->parentState(), (QState*)0); - QCOMPARE(machine.rootState()->parent(), (QObject*)&machine); - QCOMPARE(machine.rootState()->machine(), &machine); + QCOMPARE(qobject_cast<QState*>(machine.parentState()), (QState*)0); + QCOMPARE(machine.machine(), (QStateMachine*)0); - QState *s1 = new QState(machine.rootState()); - QCOMPARE(s1->parentState(), machine.rootState()); + QState *s1 = new QState(&machine); + QCOMPARE(s1->parentState(), &machine); QState *s2 = new QState(); s2->setParent(&machine); - QCOMPARE(s2->parentState(), machine.rootState()); + QCOMPARE(s2->parentState(), &machine); } void tst_QStateMachine::addAndRemoveState() { #ifdef QT_BUILD_INTERNAL QStateMachine machine; - QStatePrivate *root_d = QStatePrivate::get(machine.rootState()); + QStatePrivate *root_d = QStatePrivate::get(&machine); QCOMPARE(root_d->childStates().size(), 0); QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: cannot add null state"); @@ -1041,7 +1043,7 @@ void tst_QStateMachine::addAndRemoveState() QCOMPARE(s1->machine(), (QStateMachine*)0); machine.addState(s1); QCOMPARE(s1->machine(), &machine); - QCOMPARE(s1->parentState(), machine.rootState()); + QCOMPARE(s1->parentState(), &machine); QCOMPARE(root_d->childStates().size(), 1); QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); @@ -1051,7 +1053,7 @@ void tst_QStateMachine::addAndRemoveState() QState *s2 = new QState(); QCOMPARE(s2->parentState(), (QState*)0); machine.addState(s2); - QCOMPARE(s2->parentState(), machine.rootState()); + QCOMPARE(s2->parentState(), &machine); QCOMPARE(root_d->childStates().size(), 2); QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); QCOMPARE(root_d->childStates().at(1), (QAbstractState*)s2); @@ -1076,13 +1078,13 @@ void tst_QStateMachine::addAndRemoveState() { QString warning; warning.sprintf("QStateMachine::removeState: state %p's machine (%p) is different from this machine (%p)", - machine2.rootState(), &machine2, &machine); + &machine2, (void*)0, &machine); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); - machine.removeState(machine2.rootState()); + machine.removeState(&machine2); } // ### check this behavior - machine.addState(machine2.rootState()); - QCOMPARE(machine2.rootState()->parent(), (QObject*)machine.rootState()); + machine.addState(&machine2); + QCOMPARE(machine2.parent(), (QObject*)&machine); } delete s1; @@ -1098,7 +1100,7 @@ void tst_QStateMachine::stateEntryAndExit() { QStateMachine machine; - TestState *s1 = new TestState(machine.rootState()); + TestState *s1 = new TestState(&machine); QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); s1->addTransition((QAbstractState*)0); QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add null transition"); @@ -1106,8 +1108,8 @@ void tst_QStateMachine::stateEntryAndExit() QTest::ignoreMessage(QtWarningMsg, "QState::removeTransition: cannot remove null transition"); s1->removeTransition((QAbstractTransition*)0); - TestState *s2 = new TestState(machine.rootState()); - QFinalState *s3 = new QFinalState(machine.rootState()); + TestState *s2 = new TestState(&machine); + QFinalState *s3 = new QFinalState(&machine); TestTransition *t = new TestTransition(s2); QCOMPARE(t->machine(), (QStateMachine*)0); @@ -1156,9 +1158,9 @@ void tst_QStateMachine::stateEntryAndExit() QCOMPARE(machine.initialState(), (QAbstractState*)s1); { QString warning; - warning.sprintf("QState::setInitialState: state %p is not a child of this state (%p)", machine.rootState(), machine.rootState()); + warning.sprintf("QState::setInitialState: state %p is not a child of this state (%p)", &machine, &machine); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); - machine.setInitialState(machine.rootState()); + machine.setInitialState(&machine); QCOMPARE(machine.initialState(), (QAbstractState*)s1); } QVERIFY(machine.configuration().isEmpty()); @@ -1205,11 +1207,11 @@ void tst_QStateMachine::stateEntryAndExit() { QStateMachine machine; - TestState *s1 = new TestState(machine.rootState()); + TestState *s1 = new TestState(&machine); TestState *s11 = new TestState(s1); TestState *s12 = new TestState(s1); - TestState *s2 = new TestState(machine.rootState()); - QFinalState *s3 = new QFinalState(machine.rootState()); + TestState *s2 = new TestState(&machine); + QFinalState *s3 = new QFinalState(&machine); s1->setInitialState(s11); TestTransition *t1 = new TestTransition(s12); s11->addTransition(t1); @@ -1268,13 +1270,13 @@ void tst_QStateMachine::stateEntryAndExit() void tst_QStateMachine::assignProperty() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); QTest::ignoreMessage(QtWarningMsg, "QState::assignProperty: cannot assign property 'foo' of null object"); s1->assignProperty(0, "foo", QVariant()); s1->assignProperty(s1, "objectName", "s1"); - QFinalState *s2 = new QFinalState(machine.rootState()); + QFinalState *s2 = new QFinalState(&machine); s1->addTransition(s2); machine.setInitialState(s1); machine.start(); @@ -1320,9 +1322,9 @@ void tst_QStateMachine::assignPropertyWithAnimation() QObject obj; obj.setProperty("foo", 321); obj.setProperty("bar", 654); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(&obj, "foo", 123); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(&obj, "foo", 456); s2->assignProperty(&obj, "bar", 789); QAbstractTransition *trans = s1->addTransition(s2); @@ -1342,7 +1344,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() trans->addAnimation(&anim); QCOMPARE(trans->animations().size(), 1); QCOMPARE(trans->animations().at(0), (QAbstractAnimation*)&anim); - QFinalState *s3 = new QFinalState(machine.rootState()); + QFinalState *s3 = new QFinalState(&machine); s2->addTransition(s2, SIGNAL(polished()), s3); machine.setInitialState(s1); @@ -1358,9 +1360,9 @@ void tst_QStateMachine::assignPropertyWithAnimation() QObject obj; obj.setProperty("foo", 321); obj.setProperty("bar", 654); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(&obj, "foo", 123); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(&obj, "foo", 456); s2->assignProperty(&obj, "bar", 789); QAbstractTransition *trans = s1->addTransition(s2); @@ -1370,7 +1372,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() QPropertyAnimation anim2(&obj, "bar"); anim2.setDuration(150); trans->addAnimation(&anim2); - QFinalState *s3 = new QFinalState(machine.rootState()); + QFinalState *s3 = new QFinalState(&machine); s2->addTransition(s2, SIGNAL(polished()), s3); machine.setInitialState(s1); @@ -1386,10 +1388,10 @@ void tst_QStateMachine::assignPropertyWithAnimation() QObject obj; obj.setProperty("foo", 321); obj.setProperty("bar", 654); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(&obj, "foo", 123); s1->assignProperty(&obj, "bar", 321); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(&obj, "foo", 456); s2->assignProperty(&obj, "bar", 654); s2->assignProperty(&obj, "baz", 789); @@ -1398,7 +1400,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() group.addAnimation(new QPropertyAnimation(&obj, "foo")); group.addAnimation(new QPropertyAnimation(&obj, "bar")); trans->addAnimation(&group); - QFinalState *s3 = new QFinalState(machine.rootState()); + QFinalState *s3 = new QFinalState(&machine); s2->addTransition(s2, SIGNAL(polished()), s3); machine.setInitialState(s1); @@ -1415,7 +1417,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() QObject obj; obj.setProperty("foo", 321); obj.setProperty("bar", 654); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); QCOMPARE(s1->childMode(), QState::ExclusiveStates); s1->setChildMode(QState::ParallelStates); QCOMPARE(s1->childMode(), QState::ParallelStates); @@ -1425,7 +1427,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() s1->setObjectName("s1"); s1->assignProperty(&obj, "foo", 123); s1->assignProperty(&obj, "bar", 456); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->setObjectName("s2"); s2->assignProperty(&obj, "foo", 321); QState *s21 = new QState(s2); @@ -1447,7 +1449,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() s21->addTransition(s21, SIGNAL(polished()), s22); - QFinalState *s3 = new QFinalState(machine.rootState()); + QFinalState *s3 = new QFinalState(&machine); s22->addTransition(s2, SIGNAL(polished()), s3); machine.setInitialState(s1); @@ -1464,7 +1466,7 @@ void tst_QStateMachine::assignPropertyWithAnimation() QObject obj; obj.setProperty("foo", 321); obj.setProperty("bar", 654); - QState *group = new QState(machine.rootState()); + QState *group = new QState(&machine); QState *s1 = new QState(group); group->setInitialState(s1); s1->assignProperty(&obj, "foo", 123); @@ -1590,12 +1592,12 @@ void tst_QStateMachine::postEvent() void tst_QStateMachine::stateFinished() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); QState *s1_1 = new QState(s1); QFinalState *s1_2 = new QFinalState(s1); s1_1->addTransition(s1_2); s1->setInitialState(s1_1); - QFinalState *s2 = new QFinalState(machine.rootState()); + QFinalState *s2 = new QFinalState(&machine); s1->addTransition(s1, SIGNAL(finished()), s2); machine.setInitialState(s1); QSignalSpy finishedSpy(&machine, SIGNAL(finished())); @@ -1645,7 +1647,7 @@ void tst_QStateMachine::parallelStates() void tst_QStateMachine::parallelRootState() { QStateMachine machine; - QState *root = machine.rootState(); + QState *root = &machine; QCOMPARE(root->childMode(), QState::ExclusiveStates); root->setChildMode(QState::ParallelStates); QCOMPARE(root->childMode(), QState::ParallelStates); @@ -1668,7 +1670,7 @@ void tst_QStateMachine::parallelRootState() void tst_QStateMachine::allSourceToTargetConfigurations() { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); + QState *s0 = new QState(&machine); s0->setObjectName("s0"); QState *s1 = new QState(s0); s1->setObjectName("s1"); @@ -1680,7 +1682,7 @@ void tst_QStateMachine::allSourceToTargetConfigurations() s21->setObjectName("s21"); QState *s211 = new QState(s21); s211->setObjectName("s211"); - QFinalState *f = new QFinalState(machine.rootState()); + QFinalState *f = new QFinalState(&machine); f->setObjectName("f"); s0->setInitialState(s1); @@ -1754,7 +1756,7 @@ void tst_QStateMachine::signalTransitions() { { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); + QState *s0 = new QState(&machine); QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: sender cannot be null"); QCOMPARE(s0->addTransition(0, SIGNAL(noSuchSignal()), 0), (QSignalTransition*)0); @@ -1765,7 +1767,7 @@ void tst_QStateMachine::signalTransitions() QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); QCOMPARE(s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), 0), (QSignalTransition*)0); - QFinalState *s1 = new QFinalState(machine.rootState()); + QFinalState *s1 = new QFinalState(&machine); QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: no such signal SignalEmitter::noSuchSignal()"); QCOMPARE(s0->addTransition(&emitter, SIGNAL(noSuchSignal()), s1), (QSignalTransition*)0); @@ -1816,8 +1818,8 @@ void tst_QStateMachine::signalTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); SignalEmitter emitter; QSignalTransition *trans = s0->addTransition(&emitter, "signalWithNoArg()", s1); QVERIFY(trans != 0); @@ -1844,8 +1846,8 @@ void tst_QStateMachine::signalTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); SignalEmitter emitter; TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithIntArg(int)), s1); s0->addTransition(trans); @@ -1863,8 +1865,8 @@ void tst_QStateMachine::signalTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); SignalEmitter emitter; TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s1); s0->addTransition(trans); @@ -1883,8 +1885,8 @@ void tst_QStateMachine::signalTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); TestSignalTransition *trans = new TestSignalTransition(); QCOMPARE(trans->senderObject(), (QObject*)0); @@ -1911,8 +1913,8 @@ void tst_QStateMachine::signalTransitions() { QStateMachine machine; SignalEmitter emitter; - QState *s0 = new QState(machine.rootState()); - QState *s1 = new QState(machine.rootState()); + QState *s0 = new QState(&machine); + QState *s1 = new QState(&machine); QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); QSignalTransition *t1 = s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s0); @@ -1956,12 +1958,12 @@ void tst_QStateMachine::signalTransitions() { QStateMachine machine; SignalEmitter emitter; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); - QFinalState *s2 = new QFinalState(machine.rootState()); + QFinalState *s2 = new QFinalState(&machine); s0->addTransition(&emitter, SIGNAL(signalWithIntArg(int)), s2); - QFinalState *s3 = new QFinalState(machine.rootState()); + QFinalState *s3 = new QFinalState(&machine); s0->addTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s3); QSignalSpy startedSpy(&machine, SIGNAL(started())); @@ -1993,8 +1995,8 @@ void tst_QStateMachine::signalTransitions() { QStateMachine machine; SignalEmitter emitter; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL( signalWithNoArg( ) ), s1); QVERIFY(t0 != 0); QCOMPARE(t0->signal(), QByteArray(SIGNAL( signalWithNoArg( ) ))); @@ -2021,8 +2023,8 @@ void tst_QStateMachine::eventTransitions() QPushButton button; for (int x = 0; x < 2; ++x) { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QMouseEventTransition *trans; if (x == 0) { @@ -2070,8 +2072,8 @@ void tst_QStateMachine::eventTransitions() } for (int x = 0; x < 3; ++x) { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QEventTransition *trans; if (x == 0) { @@ -2105,8 +2107,8 @@ void tst_QStateMachine::eventTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QMouseEventTransition *trans = new QMouseEventTransition(); QCOMPARE(trans->eventObject(), (QObject*)0); @@ -2131,8 +2133,8 @@ void tst_QStateMachine::eventTransitions() { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QKeyEventTransition *trans = new QKeyEventTransition(&button, QEvent::KeyPress, Qt::Key_A); QCOMPARE(trans->eventType(), QEvent::KeyPress); @@ -2152,8 +2154,8 @@ void tst_QStateMachine::eventTransitions() } { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QKeyEventTransition *trans = new QKeyEventTransition(); QCOMPARE(trans->eventObject(), (QObject*)0); @@ -2178,8 +2180,8 @@ void tst_QStateMachine::eventTransitions() // Multiple transitions for same (object,event) { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QState *s1 = new QState(machine.rootState()); + QState *s0 = new QState(&machine); + QState *s1 = new QState(&machine); QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); t0->setTargetState(s1); s0->addTransition(t0); @@ -2226,9 +2228,9 @@ void tst_QStateMachine::eventTransitions() // multiple event transitions from same source { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); - QFinalState *s2 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); + QFinalState *s2 = new QFinalState(&machine); QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); t0->setTargetState(s1); s0->addTransition(t0); @@ -2257,8 +2259,8 @@ void tst_QStateMachine::eventTransitions() // custom event { QStateMachine machine; - QState *s0 = new QState(machine.rootState()); - QFinalState *s1 = new QFinalState(machine.rootState()); + QState *s0 = new QState(&machine); + QFinalState *s1 = new QFinalState(&machine); QEventTransition *trans = new QEventTransition(&button, QEvent::Type(QEvent::User+1)); trans->setTargetState(s1); @@ -2276,7 +2278,7 @@ void tst_QStateMachine::historyStates() { for (int x = 0; x < 2; ++x) { QStateMachine machine; - QState *root = machine.rootState(); + QState *root = &machine; QState *s0 = new QState(root); QState *s00 = new QState(s0); QState *s01 = new QState(s0); @@ -2360,7 +2362,7 @@ void tst_QStateMachine::startAndStop() QCOMPARE(stoppedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); machine.start(); QTRY_COMPARE(machine.isRunning(), true); @@ -2391,7 +2393,7 @@ void tst_QStateMachine::startAndStop() void tst_QStateMachine::targetStateWithNoParent() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->setObjectName("s1"); QState s2; s1->addTransition(&s2); @@ -2411,9 +2413,9 @@ void tst_QStateMachine::targetStateWithNoParent() void tst_QStateMachine::targetStateDeleted() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->setObjectName("s1"); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); QAbstractTransition *trans = s1->addTransition(s2); delete s2; QCOMPARE(trans->targetState(), (QAbstractState*)0); @@ -2428,13 +2430,13 @@ void tst_QStateMachine::defaultGlobalRestorePolicy() propertyHolder->setProperty("a", 1); propertyHolder->setProperty("b", 2); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(propertyHolder, "a", 3); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "b", 4); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); s1->addTransition(new EventTransition(QEvent::User, s2)); s2->addTransition(new EventTransition(QEvent::User, s3)); @@ -2463,11 +2465,12 @@ void tst_QStateMachine::noInitialStateForInitialState() { QStateMachine machine; - QState *initialState = new QState(machine.rootState()); + QState *initialState = new QState(&machine); initialState->setObjectName("initialState"); machine.setInitialState(initialState); QState *childState = new QState(initialState); + (void)childState; QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: " "Missing initial state in compound state 'initialState'"); @@ -2487,7 +2490,7 @@ void tst_QStateMachine::restorePolicyNotInherited() propertyHolder->setProperty("a", 1); propertyHolder->setProperty("b", 2); - QState *parentState = new QState(machine.rootState()); + QState *parentState = new QState(&machine); parentState->setObjectName("parentState"); parentState->setRestorePolicy(QState::RestoreProperties); @@ -2536,13 +2539,13 @@ void tst_QStateMachine::globalRestorePolicySetToDoNotRestore() propertyHolder->setProperty("a", 1); propertyHolder->setProperty("b", 2); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(propertyHolder, "a", 3); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "b", 4); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); s1->addTransition(new EventTransition(QEvent::User, s2)); s2->addTransition(new EventTransition(QEvent::User, s3)); @@ -2646,7 +2649,7 @@ void tst_QStateMachine::restorePolicyOnChildState() propertyHolder->setProperty("a", 1); propertyHolder->setProperty("b", 2); - QState *parentState = new QState(machine.rootState()); + QState *parentState = new QState(&machine); parentState->setObjectName("parentState"); QState *s1 = new QState(parentState); @@ -2697,13 +2700,13 @@ void tst_QStateMachine::globalRestorePolicySetToRestore() propertyHolder->setProperty("a", 1); propertyHolder->setProperty("b", 2); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->assignProperty(propertyHolder, "a", 3); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "b", 4); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); s1->addTransition(new EventTransition(QEvent::User, s2)); s2->addTransition(new EventTransition(QEvent::User, s3)); @@ -2736,19 +2739,19 @@ void tst_QStateMachine::mixedRestoreProperties() QObject *propertyHolder = new QObject(); propertyHolder->setProperty("a", 1); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); s1->setRestorePolicy(QState::RestoreProperties); s1->assignProperty(propertyHolder, "a", 3); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "a", 4); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); - QState *s4 = new QState(machine.rootState()); + QState *s4 = new QState(&machine); s4->assignProperty(propertyHolder, "a", 5); - QState *s5 = new QState(machine.rootState()); + QState *s5 = new QState(&machine); s5->setRestorePolicy(QState::RestoreProperties); s5->assignProperty(propertyHolder, "a", 6); @@ -2800,8 +2803,8 @@ void tst_QStateMachine::mixedRestoreProperties() void tst_QStateMachine::transitionWithParent() { QStateMachine machine; - QState *s1 = new QState(machine.rootState()); - QState *s2 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); + QState *s2 = new QState(&machine); EventTransition *trans = new EventTransition(QEvent::User, s2, s1); QCOMPARE(trans->sourceState(), s1); QCOMPARE(trans->targetState(), (QAbstractState*)s2); @@ -2816,8 +2819,8 @@ void tst_QStateMachine::simpleAnimation() QObject *object = new QObject(&machine); object->setProperty("fooBar", 1.0); - QState *s1 = new QState(machine.rootState()); - QState *s2 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); + QState *s2 = new QState(&machine); s2->assignProperty(object, "fooBar", 2.0); EventTransition *et = new EventTransition(QEvent::User, s2); @@ -2825,7 +2828,7 @@ void tst_QStateMachine::simpleAnimation() et->addAnimation(animation); s1->addTransition(et); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); s2->addTransition(animation, SIGNAL(finished()), s3); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); @@ -2860,8 +2863,8 @@ void tst_QStateMachine::twoAnimations() object->setProperty("foo", 1.0); object->setProperty("bar", 3.0); - QState *s1 = new QState(machine.rootState()); - QState *s2 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); s2->assignProperty(object, "bar", 10.0); @@ -2878,7 +2881,7 @@ void tst_QStateMachine::twoAnimations() et->addAnimation(animationBar); s1->addTransition(et); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s2->addTransition(s2, SIGNAL(polished()), s3); @@ -2903,23 +2906,23 @@ void tst_QStateMachine::twoAnimatedTransitions() QObject *object = new QObject(&machine); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 5.0); QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(fooAnimation); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s2->addTransition(fooAnimation, SIGNAL(finished()), s3); - QState *s4 = new QState(machine.rootState()); + QState *s4 = new QState(&machine); s4->assignProperty(object, "foo", 2.0); QPropertyAnimation *fooAnimation2 = new QPropertyAnimation(object, "foo", s4); s3->addTransition(new EventTransition(QEvent::User, s4))->addAnimation(fooAnimation2); - QState *s5 = new QState(machine.rootState()); + QState *s5 = new QState(&machine); QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); s4->addTransition(fooAnimation2, SIGNAL(finished()), s5); @@ -2947,22 +2950,22 @@ void tst_QStateMachine::playAnimationTwice() QObject *object = new QObject(&machine); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 5.0); QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(fooAnimation); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s2->addTransition(fooAnimation, SIGNAL(finished()), s3); - QState *s4 = new QState(machine.rootState()); + QState *s4 = new QState(&machine); s4->assignProperty(object, "foo", 2.0); s3->addTransition(new EventTransition(QEvent::User, s4))->addAnimation(fooAnimation); - QState *s5 = new QState(machine.rootState()); + QState *s5 = new QState(&machine); QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); s4->addTransition(fooAnimation, SIGNAL(finished()), s5); @@ -2993,8 +2996,8 @@ void tst_QStateMachine::nestedTargetStateForAnimation() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); - QState *s2 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); @@ -3021,7 +3024,7 @@ void tst_QStateMachine::nestedTargetStateForAnimation() connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); at->addAnimation(animation); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); s2->addTransition(s2Child, SIGNAL(polished()), s3); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); @@ -3049,13 +3052,13 @@ void tst_QStateMachine::animatedGlobalRestoreProperty() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); - QState *s2 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); - QState *s4 = new QState(machine.rootState()); + QState *s4 = new QState(&machine); QObject::connect(s4, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3093,16 +3096,16 @@ void tst_QStateMachine::specificTargetValueOfAnimation() QObject *object = new QObject(&machine); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); QPropertyAnimation *anim = new QPropertyAnimation(object, "foo"); anim->setEndValue(10.0); s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(anim); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s2->addTransition(anim, SIGNAL(finished()), s3); @@ -3127,12 +3130,12 @@ void tst_QStateMachine::addDefaultAnimation() QObject *object = new QObject(); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3164,12 +3167,12 @@ void tst_QStateMachine::addDefaultAnimationWithUnusedAnimation() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3245,13 +3248,13 @@ void tst_QStateMachine::overrideDefaultAnimationWithSpecific() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3287,12 +3290,12 @@ void tst_QStateMachine::addDefaultAnimationForSource() QObject *object = new QObject(); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3319,12 +3322,12 @@ void tst_QStateMachine::addDefaultAnimationForTarget() QObject *object = new QObject(); object->setProperty("foo", 1.0); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3348,88 +3351,88 @@ void tst_QStateMachine::removeDefaultAnimationForSource() { QStateMachine machine; - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); - machine.addDefaultAnimationForSourceState(machine.rootState(), anim); + machine.addDefaultAnimationForSourceState(&machine, anim); QCOMPARE(machine.defaultAnimations().size(), 0); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); - machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + machine.removeDefaultAnimationForTargetState(&machine, anim); QCOMPARE(machine.defaultAnimations().size(), 0); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); - machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + machine.removeDefaultAnimationForSourceState(&machine, anim); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); - machine.addDefaultAnimationForSourceState(machine.rootState(), anim); + machine.addDefaultAnimationForSourceState(&machine, anim); QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); - machine.addDefaultAnimationForSourceState(machine.rootState(), anim2); + machine.addDefaultAnimationForSourceState(&machine, anim2); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 2); - QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); - QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim2)); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 2); + QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); + QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim2)); - machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + machine.removeDefaultAnimationForSourceState(&machine, anim); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim2)); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim2)); - machine.removeDefaultAnimationForSourceState(machine.rootState(), anim2); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + machine.removeDefaultAnimationForSourceState(&machine, anim2); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); } void tst_QStateMachine::removeDefaultAnimationForTarget() { QStateMachine machine; - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); - machine.addDefaultAnimationForTargetState(machine.rootState(), anim); + machine.addDefaultAnimationForTargetState(&machine, anim); QCOMPARE(machine.defaultAnimations().size(), 0); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); - machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + machine.removeDefaultAnimationForSourceState(&machine, anim); QCOMPARE(machine.defaultAnimations().size(), 0); - QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); + QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); - machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + machine.removeDefaultAnimationForTargetState(&machine, anim); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); - machine.addDefaultAnimationForTargetState(machine.rootState(), anim); + machine.addDefaultAnimationForTargetState(&machine, anim); QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); - machine.addDefaultAnimationForTargetState(machine.rootState(), anim2); + machine.addDefaultAnimationForTargetState(&machine, anim2); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 2); - QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); - QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim2)); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 2); + QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); + QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim2)); - machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + machine.removeDefaultAnimationForTargetState(&machine, anim); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); - QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim2)); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim2)); - machine.removeDefaultAnimationForTargetState(machine.rootState(), anim2); - QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + machine.removeDefaultAnimationForTargetState(&machine, anim2); + QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); } void tst_QStateMachine::overrideDefaultAnimationWithSource() @@ -3441,13 +3444,13 @@ void tst_QStateMachine::overrideDefaultAnimationWithSource() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3481,13 +3484,13 @@ void tst_QStateMachine::overrideDefaultAnimationWithTarget() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3522,13 +3525,13 @@ void tst_QStateMachine::overrideDefaultSourceAnimationWithSpecific() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3562,13 +3565,13 @@ void tst_QStateMachine::overrideDefaultTargetAnimationWithSpecific() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3602,13 +3605,13 @@ void tst_QStateMachine::overrideDefaultTargetAnimationWithSource() SlotCalledCounter counter; - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(object, "foo", 2.0); - QState *s3 = new QState(machine.rootState()); + QState *s3 = new QState(&machine); QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); s1->addTransition(new EventTransition(QEvent::User, s2)); @@ -3644,10 +3647,10 @@ void tst_QStateMachine::parallelStateAssignmentsDone() propertyHolder->setProperty("bar", 456); propertyHolder->setProperty("zoot", 789); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + QState *parallelState = new QState(QState::ParallelStates, &machine); parallelState->assignProperty(propertyHolder, "foo", 321); QState *s2 = new QState(parallelState); @@ -3676,10 +3679,10 @@ void tst_QStateMachine::transitionsFromParallelStateWithNoChildren() { QStateMachine machine; - QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + QState *parallelState = new QState(QState::ParallelStates, &machine); machine.setInitialState(parallelState); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); parallelState->addTransition(new EventTransition(QEvent::User, s1)); machine.start(); @@ -3700,7 +3703,7 @@ void tst_QStateMachine::parallelStateTransition() { QStateMachine machine; - QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + QState *parallelState = new QState(QState::ParallelStates, &machine); machine.setInitialState(parallelState); QState *s1 = new QState(parallelState); @@ -3749,10 +3752,10 @@ void tst_QStateMachine::nestedRestoreProperties() propertyHolder->setProperty("foo", 1); propertyHolder->setProperty("bar", 2); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "foo", 3); QState *s21 = new QState(s2); @@ -3801,10 +3804,10 @@ void tst_QStateMachine::nestedRestoreProperties2() propertyHolder->setProperty("foo", 1); propertyHolder->setProperty("bar", 2); - QState *s1 = new QState(machine.rootState()); + QState *s1 = new QState(&machine); machine.setInitialState(s1); - QState *s2 = new QState(machine.rootState()); + QState *s2 = new QState(&machine); s2->assignProperty(propertyHolder, "foo", 3); QState *s21 = new QState(s2); @@ -3856,6 +3859,46 @@ void tst_QStateMachine::nestedRestoreProperties2() } +void tst_QStateMachine::nestedStateMachines() +{ + QStateMachine machine; + QState *group = new QState(&machine); + group->setChildMode(QState::ParallelStates); + QStateMachine *subMachines[3]; + for (int i = 0; i < 3; ++i) { + QState *subGroup = new QState(group); + QStateMachine *subMachine = new QStateMachine(subGroup); + { + QState *initial = new QState(subMachine); + QFinalState *done = new QFinalState(subMachine); + initial->addTransition(new EventTransition(QEvent::User, done)); + subMachine->setInitialState(initial); + } + QFinalState *subMachineDone = new QFinalState(subGroup); + subMachine->addTransition(subMachine, SIGNAL(finished()), subMachineDone); + subGroup->setInitialState(subMachine); + subMachines[i] = subMachine; + } + QFinalState *final = new QFinalState(&machine); + group->addTransition(group, SIGNAL(finished()), final); + machine.setInitialState(group); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + QTRY_COMPARE(machine.configuration().count(), 1+2*3); + QVERIFY(machine.configuration().contains(group)); + for (int i = 0; i < 3; ++i) + QVERIFY(machine.configuration().contains(subMachines[i])); + + QCoreApplication::processEvents(); // starts the submachines + + for (int i = 0; i < 3; ++i) + subMachines[i]->postEvent(new QEvent(QEvent::User)); + + QTRY_COMPARE(finishedSpy.count(), 1); +} QTEST_MAIN(tst_QStateMachine) #include "tst_qstatemachine.moc" |