2 * @file Common/eventqueue.cpp
3 * @brief Event queue interface implementation
6 * Copyright (c) 2011-2019 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #include "eventqueue.h"
23 #include "iregistry.h"
29 //-------------------------------------------------------------------
31 using namespace eVaf::Common
;
33 iEventQueue
* iEventQueue::instance()
35 static Internal::EventQueue singleton
;
40 //-------------------------------------------------------------------
42 using namespace eVaf::Common::Internal
;
44 EventQueue::EventQueue()
48 iRegistry::instance()->registerInterface("iEventQueue", this);
51 EventQueue::~EventQueue()
55 bool EventQueue::event(QEvent
* e
)
57 // Is it an eVaf event?
58 if (e
->type() == Event::eVafEvent
) {
60 Event
* event
= static_cast<Event
*>(e
);
62 uint
const id
= event
->id();
64 // Verify that this event is registered
65 Events::const_iterator eventsIt
= mEvents
.constFind(id
);
66 if (eventsIt
== mEvents
.constEnd()) {
67 return true; // We don't know it, but it is an eVaf event and we should handle it
70 // Send the event to all the subscribers
71 Subscribers::const_iterator subscribersIt
= mSubscribers
.constFind(id
);
72 if (subscribersIt
!= mSubscribers
.constEnd()) {
73 QVector
<QObject
*> subscribers
= *subscribersIt
;
74 int const sz
= subscribers
.size();
75 for (int i
= 0; i
< sz
; ++i
) {
77 // Notify the subscriber
78 bool const rval
= QCoreApplication::sendEvent(subscribers
.at(i
), e
);
81 // The event was consumed and should be sent to any other subscribers
90 return iEventQueue::event(e
);
93 uint
EventQueue::registerEvent(QString
const & name
)
95 uint id
= queryEvent(name
);
98 mEvents
.insert(mNextEventId
, name
);
105 uint
EventQueue::queryEvent(QString
const & name
) const
107 return mEvents
.key(name
, 0);
110 void EventQueue::unregisterEvent(uint id
)
113 mSubscribers
.remove(id
);
116 uint
EventQueue::subscribeEvent(uint id
, QObject
* obj
)
121 // Only registered events please
122 if (mEvents
.constFind(id
) == mEvents
.constEnd()) {
126 // Check for duplicates
127 if (mSubscribers
[id
].indexOf(obj
) != -1)
130 // Add to the list of subscribers and connect to the destroyed() signal
131 mSubscribers
[id
].append(obj
);
132 connect(obj
, SIGNAL(destroyed(QObject
*)), this, SLOT(subscriberDestroyed(QObject
*)));
137 void EventQueue::unsubscribeEvent(uint id
, QObject
* obj
)
142 // Is the event registered?
143 if (mEvents
.constFind(id
) == mEvents
.constEnd())
146 // Remove from the list of subscribers
147 QVector
<QObject
*>::iterator it
= mSubscribers
[id
].begin();
148 QVector
<QObject
*>::const_iterator e
= mSubscribers
[id
].end();
151 it
= mSubscribers
[id
].erase(it
);
152 disconnect(*it
, SIGNAL(destroyed(QObject
*)), this, SLOT(subscriberDestroyed(QObject
*)));
160 void EventQueue::broadcastEvent(Event
* event
)
162 QCoreApplication::postEvent(this, event
);
165 void EventQueue::subscriberDestroyed(QObject
* obj
)
167 // Remove the subscriber from all the subscriber lists
168 Subscribers::iterator it
= mSubscribers
.begin();
169 Subscribers::const_iterator e
= mSubscribers
.end();
170 for (; it
!= e
; ++it
) {
171 int const idx
= it
->indexOf(obj
);