statemachine: Emit finished() signal when the initial state is final

It's legal to set a QFinalState as the initial state. The state
machine should correctly emit the finished() signal upon entering
such a state in the initial transition, and don't do any further
processing.

Change-Id: Ica8d3fadbbde604512ea1136624af54eb3b13b11
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
This commit is contained in:
Kent Hansen 2012-07-07 05:43:11 +02:00 committed by Qt by Nokia
parent 0d8789ad30
commit 28e9a602cb
2 changed files with 24 additions and 1 deletions

View File

@ -1341,6 +1341,9 @@ void QStateMachinePrivate::_q_start()
#ifndef QT_NO_ANIMATION #ifndef QT_NO_ANIMATION
QList<QAbstractAnimation*> selectedAnimations = selectAnimations(transitions); QList<QAbstractAnimation*> selectedAnimations = selectAnimations(transitions);
#endif #endif
// enterStates() will set stopProcessingReason to Finished if a final
// state is entered.
stopProcessingReason = EventQueueEmpty;
enterStates(&nullEvent, exitedStates, enteredStates, statesForDefaultEntry, enterStates(&nullEvent, exitedStates, enteredStates, statesForDefaultEntry,
assignmentsForEnteredStates assignmentsForEnteredStates
#ifndef QT_NO_ANIMATION #ifndef QT_NO_ANIMATION
@ -1355,7 +1358,15 @@ void QStateMachinePrivate::_q_start()
emit q->started(); emit q->started();
_q_process(); if (stopProcessingReason == Finished) {
// The state machine immediately reached a final state.
processingScheduled = false;
state = NotRunning;
unregisterAllTransitions();
emit q->finished();
} else {
_q_process();
}
} }
void QStateMachinePrivate::_q_process() void QStateMachinePrivate::_q_process()

View File

@ -193,6 +193,7 @@ private slots:
void setPropertyAfterRestore(); void setPropertyAfterRestore();
void transitionWithNoTarget_data(); void transitionWithNoTarget_data();
void transitionWithNoTarget(); void transitionWithNoTarget();
void initialStateIsFinal();
void restorePropertiesSimple(); void restorePropertiesSimple();
void restoreProperties2(); void restoreProperties2();
@ -4196,6 +4197,17 @@ void tst_QStateMachine::transitionWithNoTarget()
delete object; delete object;
} }
void tst_QStateMachine::initialStateIsFinal()
{
QStateMachine machine;
QFinalState *f = new QFinalState(&machine);
machine.setInitialState(f);
QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
machine.start();
QTRY_VERIFY(machine.configuration().contains(f));
QTRY_COMPARE(finishedSpy.count(), 1);
}
class PropertyObject : public QObject class PropertyObject : public QObject
{ {
Q_OBJECT Q_OBJECT