uint id = event->id();
// Verify that this event is registered
- QHash<uint, QString>::const_iterator eventsIt = mEvents.constFind(id);
+ Events::const_iterator eventsIt = mEvents.constFind(id);
if (eventsIt == mEvents.constEnd()) {
return true; // We don't know it, but it is an eVaf event and we should handle it
}
// Send the event to all the subscribers
- QHash<uint, QVector<QWeakPointer<QObject> > >::const_iterator subscribersIt = mSubscribers.constFind(id);
+ Subscribers::const_iterator subscribersIt = mSubscribers.constFind(id);
if (subscribersIt != mSubscribers.constEnd()) {
- QVector<QWeakPointer<QObject> > subscribers = *subscribersIt;
- int sz = subscribers.size();
+ QVector<QObject *> subscribers = *subscribersIt;
+ int const sz = subscribers.size();
for (int i = 0; i < sz; ++i) {
- // Get the subscriber object and make sure that it is still alive
- QWeakPointer<QObject> obj = subscribers.at(i);
- if (obj.isNull()) {
- continue;
- }
-
// Notify the subscriber
- bool rval = QCoreApplication::sendEvent(obj.data(), e);
+ bool const rval = QCoreApplication::sendEvent(subscribers.at(i), e);
if (rval) {
// The event was consumed and should be sent to any other subscribers
if (mSubscribers[id].indexOf(obj) != -1)
return id;
+ // Add to the list of subscribers and connect to the destroyed() signal
mSubscribers[id].append(obj);
+ connect(obj, SIGNAL(destroyed(QObject *)), this, SLOT(subscriberDestroyed(QObject *)));
return id;
}
return;
// Remove from the list of subscribers
- QVector<QWeakPointer<QObject> >::iterator it = mSubscribers[id].begin();
- QVector<QWeakPointer<QObject> >::iterator e = mSubscribers[id].end();
+ QVector<QObject *>::iterator it = mSubscribers[id].begin();
+ QVector<QObject *>::const_iterator e = mSubscribers[id].end();
while (it != e) {
- if (!it->isNull() && it->data() == obj)
+ if (*it == obj) {
it = mSubscribers[id].erase(it);
- else
+ disconnect(*it, SIGNAL(destroyed(QObject *)), this, SLOT(subscriberDestroyed(QObject*)));
+ }
+ else {
++it;
+ }
}
}
{
QCoreApplication::postEvent(this, event);
}
+
+void EventQueue::subscriberDestroyed(QObject * obj)
+{
+ // Remove the subscriber from all the subscriber lists
+ Subscribers::iterator it = mSubscribers.begin();
+ Subscribers::const_iterator e = mSubscribers.end();
+ for (; it != e; ++it) {
+ int const idx = it->indexOf(obj);
+ if (idx != -1) {
+ it->remove(idx);
+ }
+ }
+}