2 * @file Common/eventqueue.cpp
3 * @brief Event queue interface implementation
6 * Copyright (c) 2011 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()
50 EventQueue::~EventQueue()
54 bool EventQueue::event(QEvent
* e
)
56 // Is it an eVaf event?
57 if (e
->type() == Event::eVafEvent
) {
59 Event
* event
= static_cast<Event
*>(e
);
61 uint id
= event
->id();
63 // Verify that this event is registered
64 QHash
<uint
, QString
>::const_iterator eventsIt
= mEvents
.constFind(id
);
65 if (eventsIt
== mEvents
.constEnd()) {
66 return true; // We don't know it, but it is an eVaf event and we should handle it
69 // Send the event to all the subscribers
70 QHash
<uint
, QList
<QPointer
<QObject
> > >::const_iterator subscribersIt
= mSubscribers
.constFind(id
);
71 if (subscribersIt
!= mSubscribers
.constEnd()) {
72 QList
<QPointer
<QObject
> > subscribers
= *subscribersIt
;
73 int sz
= subscribers
.size();
74 for (int i
= 0; i
< sz
; ++i
) {
76 // Get the subscriber object and make sure that it is still alive
77 QPointer
<QObject
> obj
= subscribers
.at(i
);
82 // Notify the subscriber
83 bool rval
= QCoreApplication::sendEvent(obj
, e
);
86 // The event was consumed and should be sent to any other subscribers
95 return iEventQueue::event(e
);
98 uint
EventQueue::registerEvent(QString
const & name
)
100 uint id
= queryEvent(name
);
103 mEvents
.insert(mNextEventId
, name
);
110 uint
EventQueue::queryEvent(QString
const & name
) const
112 return mEvents
.key(name
, 0);
115 void EventQueue::unregisterEvent(uint id
)
118 mSubscribers
.remove(id
);
121 uint
EventQueue::subscribeEvent(uint id
, QObject
* obj
)
126 // Only registered events please
127 if (mEvents
.constFind(id
) == mEvents
.constEnd()) {
131 // Check for duplicates
132 if (mSubscribers
[id
].indexOf(obj
) != -1)
135 mSubscribers
[id
].append(obj
);
140 void EventQueue::unsubscribeEvent(uint id
, QObject
* obj
)
145 // Is the event registered?
146 if (mEvents
.constFind(id
) == mEvents
.constEnd())
149 mSubscribers
[id
].removeAll(obj
);
152 void EventQueue::broadcastEvent(Event
* event
)
154 QCoreApplication::postEvent(this, event
);