summaryrefslogtreecommitdiffstats
path: root/src/corelib/statemachine/qstate.cpp
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-07-22 13:10:35 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-07-22 13:23:06 (GMT)
commit1a55f40b6223511d0eb388064597ab38a0d37627 (patch)
tree522d9fafe3bef5049b75471b266ab2f614dfa412 /src/corelib/statemachine/qstate.cpp
parentdf2a1de1720476d096ad9be0a8cf5a0410206d82 (diff)
downloadQt-1a55f40b6223511d0eb388064597ab38a0d37627.zip
Qt-1a55f40b6223511d0eb388064597ab38a0d37627.tar.gz
Qt-1a55f40b6223511d0eb388064597ab38a0d37627.tar.bz2
Make QStateMachine inherit QState
This removes the need for a "root state" in the machine; or rather, the machine _is_ the root state. User code can now pass in a QStateMachine directly to the QState constructor, instead of machine->rootState(). This also means we could get rid of the "proxying" from the machine to the root state for things like properties (initialState et al), finished() signal and auto-reparenting of states (the ChildAdded event hack). A fun little side-effect of this change is that it's now possible to embed state machines within state machines. We can't think of a good use case yet where you would rather embed a stand-alone state machine (with its own event processing etc.) rather than having just a regular nested state, but it's neat and it works. Reviewed-by: Eskil Abrahamsen Blomfeldt
Diffstat (limited to 'src/corelib/statemachine/qstate.cpp')
-rw-r--r--src/corelib/statemachine/qstate.cpp24
1 files changed, 8 insertions, 16 deletions
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;