X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;ds=inline;f=src%2Flibs%2FCommon%2Feventqueue.cpp;h=91539a4b1698b7e600816df45fc365bbde57855b;hb=de270ece1b764b19968e14420f538321f1c06b15;hp=33e73c0d6e54e26888f3fd1e7651f3fe1435c5a8;hpb=8e0779e2e5a9f947f79c28e2ff121f6ffdd78b3f;p=evaf
diff --git a/src/libs/Common/eventqueue.cpp b/src/libs/Common/eventqueue.cpp
index 33e73c0..91539a4 100644
--- a/src/libs/Common/eventqueue.cpp
+++ b/src/libs/Common/eventqueue.cpp
@@ -3,7 +3,7 @@
* @brief Event queue interface implementation
* @author Enar Vaikene
*
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2019 Enar Vaikene
*
* This file is part of the eVaf C++ cross-platform application development framework.
*
@@ -45,6 +45,7 @@ EventQueue::EventQueue()
: iEventQueue()
, mNextEventId(1)
{
+ iRegistry::instance()->registerInterface("iEventQueue", this);
}
EventQueue::~EventQueue()
@@ -58,29 +59,23 @@ bool EventQueue::event(QEvent * e)
Event * event = static_cast(e);
- uint id = event->id();
+ uint const id = event->id();
// Verify that this event is registered
- QHash::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 > >::const_iterator subscribersIt = mSubscribers.constFind(id);
+ Subscribers::const_iterator subscribersIt = mSubscribers.constFind(id);
if (subscribersIt != mSubscribers.constEnd()) {
- QVector > subscribers = *subscribersIt;
- int sz = subscribers.size();
+ QVector 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 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
@@ -132,7 +127,9 @@ uint EventQueue::subscribeEvent(uint id, QObject * obj)
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;
}
@@ -147,13 +144,16 @@ void EventQueue::unsubscribeEvent(uint id, QObject * obj)
return;
// Remove from the list of subscribers
- QVector >::iterator it = mSubscribers[id].begin();
- QVector >::iterator e = mSubscribers[id].end();
+ QVector::iterator it = mSubscribers[id].begin();
+ QVector::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;
+ }
}
}
@@ -161,3 +161,16 @@ void EventQueue::broadcastEvent(Event * event)
{
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);
+ }
+ }
+}