StateMachine: remove initial state for parallel states.

A parallel state cannot have an initial state, as all children of the
parallel state will be entered. Setting such an initial state on a
QState marked as ParallelStates would already produce a warning and
ignore the initial state. Now any initial state that has been set before
changing the child-mode to ParallelStates will also produce a warning
and remove the previously set initial state.

Change-Id: Ie5fcd44b03516744f785f2d1880bf806918c44d4
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Erik Verbruggen 2015-06-19 11:11:31 +02:00 committed by Erik Verbruggen
parent 67638d08b4
commit 0cd34a0c39
2 changed files with 39 additions and 0 deletions

View File

@ -518,6 +518,14 @@ QState::ChildMode QState::childMode() const
void QState::setChildMode(ChildMode mode)
{
Q_D(QState);
if (mode == QState::ParallelStates && d->initialState) {
qWarning("QState::setChildMode: setting the child-mode of state %p to "
"parallel removes the initial state", this);
d->initialState = Q_NULLPTR;
emit initialStateChanged(QState::QPrivateSignal());
}
if (d->childMode != mode) {
d->childMode = mode;
emit childModeChanged(QState::QPrivateSignal());

View File

@ -47,6 +47,7 @@ private slots:
void historyInitialState();
void transitions();
void privateSignals();
void parallelStateAndInitialState();
};
class TestClass: public QObject
@ -344,5 +345,35 @@ void tst_QState::privateSignals()
}
void tst_QState::parallelStateAndInitialState()
{
QStateMachine machine;
{ // setting an initial state on a parallel state:
QState a(QState::ParallelStates, &machine);
QState b(&a);
QVERIFY(!a.initialState());
const QString warning
= QString::asprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", &a);
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
a.setInitialState(&b); // should produce a warning and do nothing.
QVERIFY(!a.initialState());
}
{ // setting the child-mode from ExclusiveStates to ParallelStates should remove the initial state:
QState a(QState::ExclusiveStates, &machine);
QState b(&a);
a.setInitialState(&b);
QCOMPARE(a.initialState(), &b);
const QString warning
= QString::asprintf("QState::setChildMode: setting the child-mode of state %p to "
"parallel removes the initial state", &a);
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
a.setChildMode(QState::ParallelStates); // should produce a warning and remove the initial state
QVERIFY(!a.initialState());
QCOMPARE(a.childMode(), QState::ParallelStates);
}
}
QTEST_MAIN(tst_QState)
#include "tst_qstate.moc"