diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-05-22 14:31:40 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-05-22 14:31:40 (GMT) |
commit | 1602820bb4d2b13f843594194ace2186f4f72806 (patch) | |
tree | b00d38441edb1029b6e9f59f298c24785519c7ba | |
parent | 24d798694b794732240e85fa65b958b8c97cbef5 (diff) | |
download | Qt-1602820bb4d2b13f843594194ace2186f4f72806.zip Qt-1602820bb4d2b13f843594194ace2186f4f72806.tar.gz Qt-1602820bb4d2b13f843594194ace2186f4f72806.tar.bz2 |
make sure signal transition is correctly unregistered/re-registered
When the signal or senderObject is changed while the state is active,
the transition needs to be unregistered _before_ either property is
changed, only _then_ can it be re-registered.
-rw-r--r-- | src/corelib/statemachine/qsignaltransition.cpp | 29 | ||||
-rw-r--r-- | src/corelib/statemachine/qsignaltransition_p.h | 3 | ||||
-rw-r--r-- | tests/auto/qstatemachine/tst_qstatemachine.cpp | 35 |
3 files changed, 45 insertions, 22 deletions
diff --git a/src/corelib/statemachine/qsignaltransition.cpp b/src/corelib/statemachine/qsignaltransition.cpp index 4caa917..9ffcb9c 100644 --- a/src/corelib/statemachine/qsignaltransition.cpp +++ b/src/corelib/statemachine/qsignaltransition.cpp @@ -118,19 +118,20 @@ QSignalTransitionPrivate *QSignalTransitionPrivate::get(QSignalTransition *q) return q->d_func(); } -void QSignalTransitionPrivate::invalidate() +void QSignalTransitionPrivate::unregister() { Q_Q(QSignalTransition); - if (signalIndex != -1) { - QState *source = sourceState(); - QStatePrivate *source_d = QStatePrivate::get(source); - QStateMachinePrivate *mach = QStateMachinePrivate::get(source_d->machine()); - if (mach) { - mach->unregisterSignalTransition(q); - if (mach->configuration.contains(source)) - mach->registerSignalTransition(q); - } - } + if ((signalIndex == -1) || !machine()) + return; + QStateMachinePrivate::get(machine())->unregisterSignalTransition(q); +} + +void QSignalTransitionPrivate::maybeRegister() +{ + Q_Q(QSignalTransition); + if (!machine() || !machine()->configuration().contains(sourceState())) + return; + QStateMachinePrivate::get(machine())->registerSignalTransition(q); } /*! @@ -193,8 +194,9 @@ void QSignalTransition::setSenderObject(QObject *sender) Q_D(QSignalTransition); if (sender == d->sender) return; + d->unregister(); d->sender = sender; - d->invalidate(); + d->maybeRegister(); } /*! @@ -214,8 +216,9 @@ void QSignalTransition::setSignal(const QByteArray &signal) Q_D(QSignalTransition); if (signal == d->signal) return; + d->unregister(); d->signal = signal; - d->invalidate(); + d->maybeRegister(); } /*! diff --git a/src/corelib/statemachine/qsignaltransition_p.h b/src/corelib/statemachine/qsignaltransition_p.h index a23e58c..339de63 100644 --- a/src/corelib/statemachine/qsignaltransition_p.h +++ b/src/corelib/statemachine/qsignaltransition_p.h @@ -66,7 +66,8 @@ public: static QSignalTransitionPrivate *get(QSignalTransition *q); - void invalidate(); + void unregister(); + void maybeRegister(); QObject *sender; QByteArray signal; diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp index a5070cb..6de7e1a 100644 --- a/tests/auto/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -1666,14 +1666,12 @@ void tst_QStateMachine::signalTransitions() QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: no such signal SignalEmitter::noSuchSignal()"); QCOMPARE(s0->addTransition(&emitter, SIGNAL(noSuchSignal()), s1), (QObject*)0); - { - QSignalTransition *trans = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); - QVERIFY(trans != 0); - QCOMPARE(trans->sourceState(), s0); - QCOMPARE(trans->targetState(), (QAbstractState*)s1); - QCOMPARE(trans->senderObject(), (QObject*)&emitter); - QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); - } + QSignalTransition *trans = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); + QVERIFY(trans != 0); + QCOMPARE(trans->sourceState(), s0); + QCOMPARE(trans->targetState(), (QAbstractState*)s1); + QCOMPARE(trans->senderObject(), (QObject*)&emitter); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); QSignalSpy finishedSpy(&machine, SIGNAL(finished())); machine.setInitialState(s0); @@ -1685,6 +1683,27 @@ void tst_QStateMachine::signalTransitions() QTRY_COMPARE(finishedSpy.count(), 1); emitter.emitSignalWithNoArg(); + + trans->setSignal(SIGNAL(signalWithIntArg(int))); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithIntArg(int)))); + machine.start(); + QCoreApplication::processEvents(); + emitter.emitSignalWithIntArg(123); + QTRY_COMPARE(finishedSpy.count(), 2); + + machine.start(); + QCoreApplication::processEvents(); + trans->setSignal(SIGNAL(signalWithNoArg())); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); + emitter.emitSignalWithNoArg(); + QTRY_COMPARE(finishedSpy.count(), 3); + + SignalEmitter emitter2; + machine.start(); + QCoreApplication::processEvents(); + trans->setSenderObject(&emitter2); + emitter2.emitSignalWithNoArg(); + QTRY_COMPARE(finishedSpy.count(), 4); } { QStateMachine machine; |