From 1ad46d8cc9fa30ef26c3eb109c5d75937ec78aac Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Mon, 22 Mar 2010 15:17:26 +0100 Subject: Add a function to get the transitions available from a state For introspection purposes. It's nicer than having to qobject_cast the state's children(). Task-number: QTBUG-7741 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/corelib/statemachine/qstate.cpp | 18 +++++++++++++++++- src/corelib/statemachine/qstate.h | 3 +++ tests/auto/qstate/tst_qstate.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/corelib/statemachine/qstate.cpp b/src/corelib/statemachine/qstate.cpp index b6b3281..69cca06 100644 --- a/src/corelib/statemachine/qstate.cpp +++ b/src/corelib/statemachine/qstate.cpp @@ -66,7 +66,8 @@ QT_BEGIN_NAMESPACE states. QState is part of \l{The State Machine Framework}. The addTransition() function adds a transition. The removeTransition() - function removes a transition. + function removes a transition. The transitions() function returns the + state's outgoing transitions. The assignProperty() function is used for defining property assignments that should be performed when a state is entered. @@ -408,6 +409,21 @@ void QState::removeTransition(QAbstractTransition *transition) } /*! + \since 4.7 + + Returns this state's outgoing transitions (i.e. transitions where + this state is the \l{QAbstractTransition::sourceState()}{source + state}), or an empty list if this state has no outgoing transitions. + + \sa addTransition() +*/ +QList QState::transitions() const +{ + Q_D(const QState); + return d->transitions(); +} + +/*! \reimp */ void QState::onEntry(QEvent *event) diff --git a/src/corelib/statemachine/qstate.h b/src/corelib/statemachine/qstate.h index 782a2a6..620104f 100644 --- a/src/corelib/statemachine/qstate.h +++ b/src/corelib/statemachine/qstate.h @@ -44,6 +44,8 @@ #include +#include + QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -80,6 +82,7 @@ public: QSignalTransition *addTransition(QObject *sender, const char *signal, QAbstractState *target); QAbstractTransition *addTransition(QAbstractState *target); void removeTransition(QAbstractTransition *transition); + QList transitions() const; QAbstractState *initialState() const; void setInitialState(QAbstractState *state); diff --git a/tests/auto/qstate/tst_qstate.cpp b/tests/auto/qstate/tst_qstate.cpp index b05aaa0..0d39c72 100644 --- a/tests/auto/qstate/tst_qstate.cpp +++ b/tests/auto/qstate/tst_qstate.cpp @@ -77,6 +77,7 @@ private slots: void assignProperty(); void assignPropertyTwice(); void historyInitialState(); + void transitions(); private: bool functionCalled; @@ -370,6 +371,38 @@ void tst_QState::historyInitialState() QVERIFY(machine.configuration().contains(s4)); } +void tst_QState::transitions() +{ + QState s1; + QState s2; + + QVERIFY(s1.transitions().isEmpty()); + + QAbstractTransition *t1 = s1.addTransition(this, SIGNAL(destroyed()), &s2); + QVERIFY(t1 != 0); + QCOMPARE(s1.transitions().count(), 1); + QCOMPARE(s1.transitions().first(), t1); + QVERIFY(s2.transitions().isEmpty()); + + s1.removeTransition(t1); + QVERIFY(s1.transitions().isEmpty()); + + s1.addTransition(t1); + QCOMPARE(s1.transitions().count(), 1); + QCOMPARE(s1.transitions().first(), t1); + + QAbstractTransition *t2 = new QEventTransition(&s1); + QCOMPARE(s1.transitions().count(), 2); + QVERIFY(s1.transitions().contains(t1)); + QVERIFY(s1.transitions().contains(t2)); + + // Transitions from child states should not be reported. + QState *s21 = new QState(&s2); + QAbstractTransition *t3 = s21->addTransition(this, SIGNAL(destroyed()), &s2); + QVERIFY(s2.transitions().isEmpty()); + QCOMPARE(s21->transitions().count(), 1); + QCOMPARE(s21->transitions().first(), t3); +} QTEST_MAIN(tst_QState) #include "tst_qstate.moc" -- cgit v0.12