Provide receivers count from QQmlData.
authorMichael Brasser <michael.brasser@nokia.com>
Thu, 5 Apr 2012 03:30:00 +0000 (13:30 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 20 Apr 2012 02:09:37 +0000 (04:09 +0200)
Change-Id: I70b06507158797df3083dc23a119935497aa19f4
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/qml/qml/qqmldata_p.h
src/qml/qml/qqmlengine.cpp
tests/auto/qml/qqmllanguage/data/receivers.qml [new file with mode: 0644]
tests/auto/qml/qqmllanguage/testtypes.cpp
tests/auto/qml/qqmllanguage/testtypes.h
tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp

index 3dd1c3c..b8eee47 100644 (file)
@@ -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; 
index 153d6b3..82eeb91 100644 (file)
@@ -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<QQmlData *>(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 (file)
index 0000000..8828745
--- /dev/null
@@ -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
+}
+
index 5e94237..beb0f59 100644 (file)
@@ -81,6 +81,8 @@ void registerTypes()
     qmlRegisterType<MyEnum1Class>("Test",1,0,"MyEnum1Class");
     qmlRegisterType<MyEnum2Class>("Test",1,0,"MyEnum2Class");
     qmlRegisterType<MyEnumDerivedClass>("Test",1,0,"MyEnumDerivedClass");
+
+    qmlRegisterType<MyReceiversTestObject>("Test",1,0,"MyReceiversTestObject");
 }
 
 QVariant myCustomVariantTypeConverter(const QString &data)
index 7b3265e..90dea4a 100644 (file)
@@ -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();
 
index 4720269..e33f636 100644 (file)
@@ -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<MyReceiversTestObject*>(component.create());
+    QVERIFY(o != 0);
+    QCOMPARE(o->mySignalCount(), 1);
+    QCOMPARE(o->propChangedCount(), 2);
+
+    delete o;
+}
+
 // QTBUG-18268
 void tst_qqmllanguage::remoteLoadCrash()
 {