summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp96
-rw-r--r--src/corelib/statemachine/qstatemachine_p.h8
-rw-r--r--tests/auto/qstatemachine/tst_qstatemachine.cpp41
3 files changed, 138 insertions, 7 deletions
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index d89ad98..5aa5917 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -205,6 +205,7 @@ This is
QStateMachinePrivate::QStateMachinePrivate()
{
state = NotRunning;
+ _startState = 0;
processing = false;
processingScheduled = false;
stop = false;
@@ -1146,6 +1147,20 @@ protected:
} // namespace
+QState *QStateMachinePrivate::startState()
+{
+ Q_Q(QStateMachine);
+ if (_startState == 0)
+ _startState = new StartState(q);
+ return _startState;
+}
+
+void QStateMachinePrivate::removeStartState()
+{
+ delete _startState;
+ _startState = 0;
+}
+
void QStateMachinePrivate::_q_start()
{
Q_Q(QStateMachine);
@@ -1165,11 +1180,19 @@ void QStateMachinePrivate::_q_start()
processingScheduled = true; // we call _q_process() below
emit q->started();
- StartState *start = new StartState(rootState());
- QAbstractTransition *initialTransition = new InitialTransition(initial);
- start->addTransition(initialTransition);
- QList<QAbstractTransition*> transitions;
- transitions.append(initialTransition);
+ QState *start = startState();
+ Q_ASSERT(start != 0);
+
+ QList<QAbstractTransition*> transitions = QStatePrivate::get(start)->transitions();
+
+ // If a transition has already been added, then we skip this step, as the
+ // initial transition in that case has been overridden.
+ if (transitions.isEmpty()) {
+ QAbstractTransition *initialTransition = new InitialTransition(initial);
+ start->addTransition(initialTransition);
+ transitions.append(initialTransition);
+ }
+
QEvent nullEvent(QEvent::None);
executeTransitionContent(&nullEvent, transitions);
QList<QAbstractState*> enteredStates = enterStates(&nullEvent, transitions);
@@ -1177,7 +1200,7 @@ void QStateMachinePrivate::_q_start()
applyProperties(transitions, QList<QAbstractState*>() << start,
enteredStates);
#endif
- delete start;
+ removeStartState();
#ifdef QSTATEMACHINE_DEBUG
qDebug() << q << ": initial configuration:" << configuration;
@@ -1277,6 +1300,67 @@ void QStateMachinePrivate::scheduleProcess()
QMetaObject::invokeMethod(q_func(), "_q_process", Qt::QueuedConnection);
}
+namespace {
+
+class GoToStateTransition : public QAbstractTransition
+{
+public:
+ GoToStateTransition(QAbstractState *target)
+ : QAbstractTransition(QList<QAbstractState*>() << target) {}
+protected:
+ void onTransition(QEvent *) { deleteLater(); }
+ bool eventTest(QEvent *) { return true; }
+};
+
+} // namespace
+
+/*!
+ \internal
+
+ Causes this state machine to unconditionally transition to the given
+ \a targetState.
+
+ Provides a backdoor for using the state machine "imperatively"; i.e. rather
+ than defining explicit transitions, you drive the machine's execution by
+ calling this function. It breaks the whole integrity of the
+ transition-driven model, but is provided for pragmatic reasons.
+*/
+void QStateMachinePrivate::goToState(QAbstractState *targetState)
+{
+ if (!targetState) {
+ qWarning("QStateMachine::goToState(): cannot go to null state");
+ return;
+ }
+
+ if (configuration.contains(targetState))
+ return;
+
+ QState *sourceState = 0;
+ if (state == Running) {
+ QSet<QAbstractState*>::const_iterator it;
+ for (it = configuration.constBegin(); it != configuration.constEnd(); ++it) {
+ sourceState = qobject_cast<QState*>(*it);
+ if (sourceState != 0)
+ break;
+ }
+ } else {
+ sourceState = startState();
+ }
+
+ Q_ASSERT(sourceState != 0);
+ // Reuse previous GoToStateTransition in case of several calls to
+ // goToState() in a row.
+ GoToStateTransition *trans = qFindChild<GoToStateTransition*>(sourceState);
+ if (!trans) {
+ trans = new GoToStateTransition(targetState);
+ sourceState->addTransition(trans);
+ } else {
+ trans->setTargetState(targetState);
+ }
+
+ scheduleProcess();
+}
+
void QStateMachinePrivate::registerTransitions(QAbstractState *state)
{
QState *group = qobject_cast<QState*>(state);
diff --git a/src/corelib/statemachine/qstatemachine_p.h b/src/corelib/statemachine/qstatemachine_p.h
index 590cffe..714a40b 100644
--- a/src/corelib/statemachine/qstatemachine_p.h
+++ b/src/corelib/statemachine/qstatemachine_p.h
@@ -79,7 +79,7 @@ class QAbstractAnimation;
#endif
class QStateMachine;
-class QStateMachinePrivate : public QStatePrivate
+class Q_AUTOTEST_EXPORT QStateMachinePrivate : public QStatePrivate
{
Q_DECLARE_PUBLIC(QStateMachine)
public:
@@ -116,6 +116,9 @@ public:
QState *rootState() const;
+ QState *startState();
+ void removeStartState();
+
void microstep(QEvent *event, const QList<QAbstractTransition*> &transitionList);
bool isPreempted(const QAbstractState *s, const QSet<QAbstractTransition*> &transitions) const;
QSet<QAbstractTransition*> selectTransitions(QEvent *event) const;
@@ -138,6 +141,8 @@ public:
static bool isDescendantOf(const QAbstractState *s, const QAbstractState *other);
static QList<QState*> properAncestors(const QAbstractState *s, const QState *upperBound);
+ void goToState(QAbstractState *targetState);
+
void registerTransitions(QAbstractState *state);
void registerSignalTransition(QSignalTransition *transition);
void unregisterSignalTransition(QSignalTransition *transition);
@@ -162,6 +167,7 @@ public:
#endif
State state;
+ QState *_startState;
bool processing;
bool processingScheduled;
bool stop;
diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp
index 7f4d9f5..32812e2 100644
--- a/tests/auto/qstatemachine/tst_qstatemachine.cpp
+++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp
@@ -192,6 +192,7 @@ private slots:
// void overrideDefaultTargetAnimationWithSource();
void nestedStateMachines();
+ void goToState();
};
tst_QStateMachine::tst_QStateMachine()
@@ -3900,5 +3901,45 @@ void tst_QStateMachine::nestedStateMachines()
QTRY_COMPARE(finishedSpy.count(), 1);
}
+void tst_QStateMachine::goToState()
+{
+ QStateMachine machine;
+ QState *s1 = new QState(&machine);
+ QState *s2 = new QState(&machine);
+ machine.setInitialState(s1);
+ QSignalSpy startedSpy(&machine, SIGNAL(started()));
+ machine.start();
+ QTRY_COMPARE(startedSpy.count(), 1);
+
+ QStateMachinePrivate::get(&machine)->goToState(s2);
+ QCoreApplication::processEvents();
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s2));
+
+ QStateMachinePrivate::get(&machine)->goToState(s2);
+ QCoreApplication::processEvents();
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s2));
+
+ QStateMachinePrivate::get(&machine)->goToState(s1);
+ QStateMachinePrivate::get(&machine)->goToState(s2);
+ QStateMachinePrivate::get(&machine)->goToState(s1);
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s2));
+
+ QCoreApplication::processEvents();
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s1));
+
+ // go to state in group
+ QState *s2_1 = new QState(s2);
+ s2->setInitialState(s2_1);
+ QStateMachinePrivate::get(&machine)->goToState(s2_1);
+ QCoreApplication::processEvents();
+ QCOMPARE(machine.configuration().size(), 2);
+ QVERIFY(machine.configuration().contains(s2));
+ QVERIFY(machine.configuration().contains(s2_1));
+}
+
QTEST_MAIN(tst_QStateMachine)
#include "tst_qstatemachine.moc"