Make it possible to connect to QAbstract{State,Transtion} private signals.

Use the same trick as used for private signals in the models.

Change-Id: I4235788490cae0e3d554565621d145652dc5b0ca
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Stephen Kelly 2012-07-09 09:56:58 +02:00 committed by Qt by Nokia
parent 536ec793b6
commit f108fceb2c
5 changed files with 137 additions and 10 deletions

View File

@ -119,13 +119,13 @@ void QAbstractStatePrivate::callOnExit(QEvent *e)
void QAbstractStatePrivate::emitEntered()
{
Q_Q(QAbstractState);
emit q->entered();
emit q->entered(QAbstractState::QPrivateSignal());
}
void QAbstractStatePrivate::emitExited()
{
Q_Q(QAbstractState);
emit q->exited();
emit q->exited(QAbstractState::QPrivateSignal());
}
/*!

View File

@ -65,11 +65,16 @@ public:
QStateMachine *machine() const;
Q_SIGNALS:
#if !defined(Q_MOC_RUN) && !defined(qdoc)
private: // can only be emitted by QAbstractState
void entered(
#if !defined(qdoc)
QPrivateSignal
#endif
void entered();
void exited();
);
void exited(
#if !defined(qdoc)
QPrivateSignal
#endif
);
protected:
QAbstractState(QState *parent = 0);

View File

@ -145,7 +145,7 @@ QState *QAbstractTransitionPrivate::sourceState() const
void QAbstractTransitionPrivate::emitTriggered()
{
Q_Q(QAbstractTransition);
emit q->triggered();
emit q->triggered(QAbstractTransition::QPrivateSignal());
}
/*!

View File

@ -88,10 +88,11 @@ public:
#endif
Q_SIGNALS:
#if !defined(Q_MOC_RUN) && !defined(qdoc)
private: // can only be emitted by QAbstractTransition
void triggered(
#if !defined(qdoc)
QPrivateSignal
#endif
void triggered();
);
protected:
virtual bool eventTest(QEvent *event) = 0;

View File

@ -57,6 +57,7 @@ private slots:
void assignPropertyTwice();
void historyInitialState();
void transitions();
void privateSignals();
private:
bool functionCalled;
@ -237,5 +238,125 @@ void tst_QState::transitions()
QCOMPARE(s21->transitions().first(), t3);
}
class MyState : public QState
{
Q_OBJECT
public:
MyState(QState *parent = 0)
: QState(parent)
{
}
void emitPrivateSignals()
{
// These deliberately do not compile
// emit entered();
// emit exited();
//
// emit entered(QPrivateSignal());
// emit exited(QPrivateSignal());
//
// emit entered(QAbstractState::QPrivateSignal());
// emit exited(QAbstractState::QPrivateSignal());
}
};
class MyTransition : public QSignalTransition
{
Q_OBJECT
public:
MyTransition(QObject * sender, const char * signal, QState *sourceState = 0)
: QSignalTransition(sender, signal, sourceState)
{
}
void emitPrivateSignals()
{
// These deliberately do not compile
// emit triggered();
//
// emit triggered(QPrivateSignal());
//
// emit triggered(QAbstractTransition::QPrivateSignal());
}
};
class SignalConnectionTester : public QObject
{
Q_OBJECT
public:
SignalConnectionTester(QObject *parent = 0)
: QObject(parent), testPassed(false)
{
}
public Q_SLOTS:
void testSlot()
{
testPassed = true;
}
public:
bool testPassed;
};
class TestTrigger : public QObject
{
Q_OBJECT
public:
TestTrigger(QObject *parent = 0)
: QObject(parent)
{
}
void emitTrigger()
{
emit trigger();
}
signals:
void trigger();
};
void tst_QState::privateSignals()
{
QStateMachine machine;
QState *s1 = new QState(&machine);
MyState *s2 = new MyState(&machine);
TestTrigger testTrigger;
MyTransition *t1 = new MyTransition(&testTrigger, SIGNAL(trigger()), s1);
s1->addTransition(t1);
t1->setTargetState(s2);
machine.setInitialState(s1);
machine.start();
QCoreApplication::processEvents();
SignalConnectionTester s1Tester;
SignalConnectionTester s2Tester;
SignalConnectionTester t1Tester;
QObject::connect(s1, &QState::exited, &s1Tester, &SignalConnectionTester::testSlot);
QObject::connect(s2, &QState::entered, &s2Tester, &SignalConnectionTester::testSlot);
QObject::connect(t1, &QSignalTransition::triggered, &t1Tester, &SignalConnectionTester::testSlot);
testTrigger.emitTrigger();
QCoreApplication::processEvents();
QVERIFY(s1Tester.testPassed);
QVERIFY(s2Tester.testPassed);
QVERIFY(t1Tester.testPassed);
}
QTEST_MAIN(tst_QState)
#include "tst_qstate.moc"