diff options
author | Mathias Hasselmann <mathias.hasselmann@kdab.com> | 2013-10-19 23:18:23 +0200 |
---|---|---|
committer | Mathias Hasselmann <mathias.hasselmann@kdab.com> | 2013-10-19 23:18:23 +0200 |
commit | 8dc7502ac0a86569945769d790ed79b4cd966079 (patch) | |
tree | 14fe506c9d02164fc358ae1988197008b0d5815d | |
parent | 05c12c6d9af430c3637d43866059e88af28c034b (diff) | |
download | qtquickstreamer-8dc7502ac0a86569945769d790ed79b4cd966079.tar.gz qtquickstreamer-8dc7502ac0a86569945769d790ed79b4cd966079.tar.xz |
Emit signals upon property change
-rw-r--r-- | src/object.cpp | 32 | ||||
-rw-r--r-- | src/object.h | 2 |
2 files changed, 28 insertions, 6 deletions
diff --git a/src/object.cpp b/src/object.cpp index 31c6504..b722bee 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -16,7 +16,7 @@ struct PropertyInfo typedef std::function<void(GstObject *target, QVariant *value)> ReadDelegate; typedef std::function<void(GstObject *target, const QVariant &value)> WriteDelegate; - QByteArray name; + const char *name; ReadDelegate read; WriteDelegate write; }; @@ -55,6 +55,20 @@ struct TypeInfo return id - properties.size(); } + void emitPropertyChanged(Object *object, const GParamSpec *pspec) const + { + for (int i = 0; i < properties.size(); ++i) { + // can compare by pointer because GParamSpec::name is interned + if (pspec->name == properties.at(i).name) { + QMetaObject::activate(object, metaObject, i, Q_NULLPTR); + return; + } + } + + if (parent) + parent->emitPropertyChanged(object, pspec); + } + const TypeInfo *parent; const QMetaObject *metaObject; QVector<PropertyInfo> properties; @@ -370,11 +384,11 @@ static const TypeInfo *makeTypeInfo(GType gtype) continue; } - qDebug("Adding %s::%s", g_type_name(gtype), pSpec->name); - auto notifier = objectBuilder.addSignal(name + QByteArrayLiteral("Changed()")); auto property = objectBuilder.addProperty(name, type, notifier.index()); + qDebug("%s %d %d", name.constData(), notifier.index(), property.index()); + property.setReadable(pSpec->flags & G_PARAM_READABLE); property.setWritable(pSpec->flags & G_PARAM_WRITABLE); @@ -416,8 +430,10 @@ Object::Object(GstObject *target) , m_target(target) { Q_ASSERT(m_target != Q_NULLPTR); + g_object_ref_sink(m_target); g_object_set_qdata(G_OBJECT(target), Private::dataKeyQuark(), this); + g_signal_connect_swapped(target, "notify", reinterpret_cast<GCallback>(&Object::emitPropertyChanged), this); } Object::~Object() @@ -446,14 +462,12 @@ void *Object::qt_metacast(const char *className) int Object::qt_metacall(QMetaObject::Call call, int id, void **args) { - qDebug("%s: call=%d, id=%d", Q_FUNC_INFO, call, id); + qDebug("%s: %d(%d)", Q_FUNC_INFO, call, id); id = QObject::qt_metacall(call, id, args); if (id < 0) return id; - qDebug("-> id=%d", id); - switch(call) { case QMetaObject::ReadProperty: id = readProperty(id, static_cast<QVariant *>(args[0])); @@ -492,4 +506,10 @@ int Object::writeProperty(int id, const QVariant &value) return typeInfo->writeProperty(m_target, id, value); } +void Object::emitPropertyChanged(Object *self, GParamSpec *pspec) +{ + const auto *const typeInfo = Private::findTypeInfo(G_OBJECT_TYPE(self->m_target)); + typeInfo->emitPropertyChanged(self, pspec); +} + } // namespace QQuickStreamer diff --git a/src/object.h b/src/object.h index 43fe227..60b8e0e 100644 --- a/src/object.h +++ b/src/object.h @@ -3,6 +3,7 @@ #include <QObject> +typedef struct _GParamSpec GParamSpec; typedef struct _GstObject GstObject; namespace QQuickStreamer { @@ -28,6 +29,7 @@ public: protected: int readProperty(int id, QVariant *value); int writeProperty(int id, const QVariant &value); + static void emitPropertyChanged(Object *self, GParamSpec *pspec); private: GstObject *const m_target; |