X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;ds=sidebyside;f=src%2Flibs%2FCommon%2Feventqueue.cpp;h=647d892368ecbcdc13302da68086861ab795805b;hb=0c61450fcffcb36a6e23ca5b083c26543d956084;hp=bbf30ed8ae2f12d1ff213ea6845f3517c8781a91;hpb=5815060246f84e8efdf3143b4e8c7d00778168cf;p=evaf
diff --git a/src/libs/Common/eventqueue.cpp b/src/libs/Common/eventqueue.cpp
index bbf30ed..647d892 100644
--- a/src/libs/Common/eventqueue.cpp
+++ b/src/libs/Common/eventqueue.cpp
@@ -45,6 +45,7 @@ EventQueue::EventQueue()
: iEventQueue()
, mNextEventId(1)
{
+ iRegistry::instance()->registerInterface("iEventQueue", this);
}
EventQueue::~EventQueue()
@@ -61,26 +62,20 @@ bool EventQueue::event(QEvent * e)
uint 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()) {
- QList > 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
- QPointer obj = subscribers.at(i);
- if (obj.isNull()) {
- continue;
- }
-
// Notify the subscriber
- bool rval = QCoreApplication::sendEvent(obj, 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;
}
@@ -146,10 +143,34 @@ void EventQueue::unsubscribeEvent(uint id, QObject * obj)
if (mEvents.constFind(id) == mEvents.constEnd())
return;
- mSubscribers[id].removeAll(obj);
+ // Remove from the list of subscribers
+ QVector::iterator it = mSubscribers[id].begin();
+ QVector::const_iterator e = mSubscribers[id].end();
+ while (it != e) {
+ if (*it == obj) {
+ it = mSubscribers[id].erase(it);
+ disconnect(*it, SIGNAL(destroyed(QObject *)), this, SLOT(subscriberDestroyed(QObject*)));
+ }
+ else {
+ ++it;
+ }
+ }
}
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);
+ }
+ }
+}