From 955bac49404590e88360f855207d88eb21925a6d Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 5 Apr 2012 13:30:00 +1000 Subject: [PATCH] Provide receivers count from QQmlData. Change-Id: I70b06507158797df3083dc23a119935497aa19f4 Reviewed-by: Kent Hansen Reviewed-by: Aaron Kennedy --- src/qml/qml/qqmldata_p.h | 3 +++ src/qml/qml/qqmlengine.cpp | 19 +++++++++++++++++++ tests/auto/qml/qqmllanguage/data/receivers.qml | 8 ++++++++ tests/auto/qml/qqmllanguage/testtypes.cpp | 2 ++ tests/auto/qml/qqmllanguage/testtypes.h | 19 +++++++++++++++++++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 13 +++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 tests/auto/qml/qqmllanguage/data/receivers.qml diff --git a/src/qml/qml/qqmldata_p.h b/src/qml/qml/qqmldata_p.h index 3dd1c3c..b8eee47 100644 --- a/src/qml/qml/qqmldata_p.h +++ b/src/qml/qml/qqmldata_p.h @@ -91,12 +91,14 @@ public: QAbstractDeclarativeData::parentChanged = parentChanged; QAbstractDeclarativeData::objectNameChanged = objectNameChanged; QAbstractDeclarativeData::signalEmitted = signalEmitted; + QAbstractDeclarativeData::receivers = receivers; } static void destroyed(QAbstractDeclarativeData *, QObject *); static void parentChanged(QAbstractDeclarativeData *, QObject *, QObject *); static void objectNameChanged(QAbstractDeclarativeData *, QObject *); static void signalEmitted(QAbstractDeclarativeData *, QObject *, int, void **); + static int receivers(QAbstractDeclarativeData *, const QObject *, int); void destroyed(QObject *); void parentChanged(QObject *, QObject *); @@ -130,6 +132,7 @@ public: inline QQmlNotifierEndpoint *notify(int index); void addNotify(int index, QQmlNotifierEndpoint *); + int endpointCount(int index); // The context that created the C++ object QQmlContextData *context; diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 153d6b3..82eeb91 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -457,6 +457,25 @@ void QQmlData::signalEmitted(QAbstractDeclarativeData *, QObject *object, int in if (ep) QQmlNotifier::emitNotify(ep); } +int QQmlData::receivers(QAbstractDeclarativeData *d, const QObject *, int index) +{ + return static_cast(d)->endpointCount(index); +} + +int QQmlData::endpointCount(int index) +{ + int count = 0; + QQmlNotifierEndpoint *ep = notify(index); + if (!ep) + return count; + ++count; + while (ep->next) { + ++count; + ep = ep->next; + } + return count; +} + void QQmlEnginePrivate::init() { Q_Q(QQmlEngine); diff --git a/tests/auto/qml/qqmllanguage/data/receivers.qml b/tests/auto/qml/qqmllanguage/data/receivers.qml new file mode 100644 index 0000000..8828745 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/receivers.qml @@ -0,0 +1,8 @@ +import Test 1.0 + +MyReceiversTestObject { + property int dummy: prop + onPropChanged: { var a = 0; } //do nothing + onMySignal: { var a = 0; } //do nothing +} + diff --git a/tests/auto/qml/qqmllanguage/testtypes.cpp b/tests/auto/qml/qqmllanguage/testtypes.cpp index 5e94237..beb0f59 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.cpp +++ b/tests/auto/qml/qqmllanguage/testtypes.cpp @@ -81,6 +81,8 @@ void registerTypes() qmlRegisterType("Test",1,0,"MyEnum1Class"); qmlRegisterType("Test",1,0,"MyEnum2Class"); qmlRegisterType("Test",1,0,"MyEnumDerivedClass"); + + qmlRegisterType("Test",1,0,"MyReceiversTestObject"); } QVariant myCustomVariantTypeConverter(const QString &data) diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index 7b3265e..90dea4a 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -526,6 +526,24 @@ public: UnavailableType() {} }; +class MyReceiversTestObject : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int prop READ prop NOTIFY propChanged) +public: + MyReceiversTestObject() {} + + int prop() const { return 5; } + + int mySignalCount() { return receivers(SIGNAL(mySignal())); } + int propChangedCount() { return receivers(SIGNAL(propChanged())); } + +signals: + void mySignal(); + void propChanged(); +}; + class MyDotPropertyObject : public QObject { Q_OBJECT @@ -907,6 +925,7 @@ QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered) QML_DECLARE_TYPE(MyRevisionedClass) QML_DECLARE_TYPE(MyRevisionedSubclass) QML_DECLARE_TYPE(MySubclass) +QML_DECLARE_TYPE(MyReceiversTestObject) void registerTypes(); diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 4720269..e33f636 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -146,6 +146,7 @@ private slots: void nestedComponentRoots(); void registrationOrder(); void readonly(); + void receivers(); void basicRemote_data(); void basicRemote(); @@ -2285,6 +2286,18 @@ void tst_qqmllanguage::readonly() delete o; } +void tst_qqmllanguage::receivers() +{ + QQmlComponent component(&engine, TEST_FILE("receivers.qml")); + + MyReceiversTestObject *o = qobject_cast(component.create()); + QVERIFY(o != 0); + QCOMPARE(o->mySignalCount(), 1); + QCOMPARE(o->propChangedCount(), 2); + + delete o; +} + // QTBUG-18268 void tst_qqmllanguage::remoteLoadCrash() { -- 2.7.4