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()
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 id
= event
->id();
64 // Verify that this event is registered
65 QHash
<uint
, QString
>::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 QHash
<uint
, QVector
<QWeakPointer
<QObject
> > >::const_iterator subscribersIt
= mSubscribers
.constFind(id
);
72 if (subscribersIt
!= mSubscribers
.constEnd()) {
73 QVector
<QWeakPointer
<QObject
> > subscribers
= *subscribersIt
;
74 int sz
= subscribers
.size();
75 for (int i
= 0; i
< sz
; ++i
) {
77 // Get the subscriber object and make sure that it is still alive
78 QWeakPointer
<QObject
> obj
= subscribers
.at(i
);
83 // Notify the subscriber
84 bool rval
= QCoreApplication::sendEvent(obj
.data(), e
);
87 // The event was consumed and should be sent to any other subscribers
96 return iEventQueue::event(e
);
99 uint
EventQueue::registerEvent(QString
const & name
)
101 uint id
= queryEvent(name
);
104 mEvents
.insert(mNextEventId
, name
);
111 uint
EventQueue::queryEvent(QString
const & name
) const
113 return mEvents
.key(name
, 0);
116 void EventQueue::unregisterEvent(uint id
)
119 mSubscribers
.remove(id
);
122 uint
EventQueue::subscribeEvent(uint id
, QObject
* obj
)
127 // Only registered events please
128 if (mEvents
.constFind(id
) == mEvents
.constEnd()) {
132 // Check for duplicates
133 if (mSubscribers
[id
].indexOf(obj
) != -1)
136 mSubscribers
[id
].append(obj
);
141 void EventQueue::unsubscribeEvent(uint id
, QObject
* obj
)
146 // Is the event registered?
147 if (mEvents
.constFind(id
) == mEvents
.constEnd())
150 // Remove from the list of subscribers
151 QVector
<QWeakPointer
<QObject
> >::iterator it
= mSubscribers
[id
].begin();
152 QVector
<QWeakPointer
<QObject
> >::iterator e
= mSubscribers
[id
].end();
154 if (!it
->isNull() && it
->data() == obj
)
155 it
= mSubscribers
[id
].erase(it
);
161 void EventQueue::broadcastEvent(Event
* event
)
163 QCoreApplication::postEvent(this, event
);