-
Notifications
You must be signed in to change notification settings - Fork 8
/
mafEventDispatcher.cpp
executable file
·239 lines (205 loc) · 8.61 KB
/
mafEventDispatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* mafEventDispatcher.cpp
* mafEventBus
*
* Created by Paolo Quadrani on 27/03/09.
* Copyright 2009 B3C. All rights reserved.
*
* See Licence at: http://tiny.cc/QXJ4D
*
*/
#include "mafEventDispatcher.h"
#include "mafEvent.h"
#define CALLBACK_SIGNATURE "1"
#define SIGNAL_SIGNATURE "2"
using namespace mafEventBus;
mafEventDispatcher::mafEventDispatcher() {
}
mafEventDispatcher::~mafEventDispatcher() {
// delete all lists present into the hash.
mafHash<mafString, mafEvent *>::iterator i;
for (i = m_CallbacksHash.begin(); i != m_CallbacksHash.end(); ++i) {
delete i.value();
}
m_CallbacksHash.clear();
for (i = m_SignalsHash.begin(); i != m_SignalsHash.end(); ++i) {
delete i.value();
}
m_SignalsHash.clear();
}
void mafEventDispatcher::initializeGlobalEvents() {
mafEvent *remote_done = new mafEvent();
mafString eventId = "maf.remote.eventBus.comunicationDone";
(*remote_done)[TOPIC] = eventId;
(*remote_done)[TYPE] = mafEventTypeLocal;
(*remote_done)[SIGTYPE] = mafSignatureTypeSignal;
mafVariant var;
var.setValue((QObject*)this);
(*remote_done)[OBJECT] = var;
(*remote_done)[SIGNATURE] = "remoteCommunicationDone()";
this->registerSignal(*remote_done);
mafEvent *remote_failed = new mafEvent();
(*remote_failed)[TOPIC] = "maf.remote.eventBus.comunicationFalied";
(*remote_failed)[TYPE] = mafEventTypeLocal;
(*remote_failed)[SIGTYPE] = mafSignatureTypeSignal;
var.setValue((QObject*)this);
(*remote_failed)[OBJECT] = var;
(*remote_failed)[SIGNATURE] = "remoteCommunicationFailed()";
this->registerSignal(*remote_failed);
}
bool mafEventDispatcher::isSignaturePresent(const mafEvent &props) const {
mafString topic = props[TOPIC].toString();
mafEventItemListType itemEventPropList;
mafEvent *itemEventProp;
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
itemEventPropList = m_CallbacksHash.values(topic);
} else {
itemEventPropList = m_SignalsHash.values(topic);
}
QObject *objParameter = props[OBJECT].value<QObject *>();
foreach(itemEventProp, itemEventPropList) {
QObject *objInList = (*itemEventProp)[OBJECT].value<QObject *>();
if(objInList == objParameter && (*itemEventProp)[SIGNATURE].toString() == props[SIGNATURE].toString()) {
return true;
}
}
return false;
}
bool mafEventDispatcher::disconnectSignal(const mafEvent &props) {
mafEventItemListType observerIdList = m_CallbacksHash.values(props[TOPIC].toString());
mafEvent *item;
bool result = true;
foreach(item, observerIdList) {
result = result && disconnectCallback(*item);
}
return result;
}
bool mafEventDispatcher::disconnectCallback(const mafEvent &props) {
//need to disconnect observer from the signal
mafString observer_sig = CALLBACK_SIGNATURE;
observer_sig.append(props[SIGNATURE].toString());
mafEvent *itemSignal = m_SignalsHash.value(props[TOPIC].toString());
mafString event_sig = SIGNAL_SIGNATURE;
event_sig.append((*itemSignal)[SIGNATURE].toString());
QObject *objSignal = (*itemSignal)[OBJECT].value<QObject *>();
QObject *objSlot = props[OBJECT].value<QObject *>();
return disconnect(objSignal, event_sig.toAscii(), objSlot, observer_sig.toAscii());
}
bool mafEventDispatcher::removeEventItem(const mafEvent &props) {
bool isDisconnected = false;
bool isPresent = isSignaturePresent(props);
if(isPresent == true) {
//mafEventItemListType itemEventPropList;
if(props[SIGTYPE].toInt() == mafSignatureTypeCallback) {
isDisconnected = disconnectCallback(props);
//iterator for erasing hash entry
mafEventsHashType::iterator i = m_CallbacksHash.find(props[TOPIC].toString());
while (i != m_CallbacksHash.end() && i.key() == props[TOPIC]) {
QObject *obj = (*(i.value()))[OBJECT].value<QObject *>();
QObject *objCheck = props[OBJECT].value<QObject *>();
if (obj == objCheck && (*(i.value()))[SIGNATURE].toString() == props[SIGNATURE].toString()) {
delete i.value();
i = m_CallbacksHash.erase(i);
} else {
++i;
}
}
} else {
//itemEventPropList = m_SignalsHash.values();
isDisconnected = disconnectSignal(props);
mafEventsHashType::iterator i = m_CallbacksHash.find(props[TOPIC].toString());
while (i != m_CallbacksHash.end() && i.key() == props[TOPIC].toString()) {
delete i.value();
i++;
}
i = m_SignalsHash.find(props[TOPIC].toString());
while (i != m_SignalsHash.end() && i.key() == props[TOPIC].toString()) {
delete i.value();
i++;
}
m_SignalsHash.remove(props[TOPIC].toString()); //in signal hash the id is unique
m_CallbacksHash.remove(props[TOPIC].toString()); //remove also all the id associated in callback
}
//itemEventPropList.removeAt(idx);
}
return isDisconnected;
}
bool mafEventDispatcher::addObserver(const mafEvent &props) {
//QObject *obj = props[OBJECT].value<QObject *>();
//REQUIRE(obj != NULL);
mafString topic = props[TOPIC].toString();
// check if the object has been already registered with the same signature to avoid duplicates.
if(m_CallbacksHash.contains(topic) && this->isSignaturePresent(props) == true) {
return false;
}
mafEvent *itemEventProp;
itemEventProp = m_SignalsHash.value(topic);
if(itemEventProp == NULL) {
mafMsgDebug() << mafTr("Signal not present for topic %1").arg(topic);
return false;
}
mafVariant sigVariant = (*itemEventProp)[SIGNATURE];
mafString sig = sigVariant.toString();
if(sig.length() > 0) {
mafString observer_sig = CALLBACK_SIGNATURE;
observer_sig.append(props[SIGNATURE].toString());
mafString event_sig = SIGNAL_SIGNATURE;
event_sig.append(sig);
// Add the new observer to the Hash.
mafEvent *dict = const_cast<mafEvent *>(&props);
this->m_CallbacksHash.insertMulti(topic, dict);
QObject *objSignal = (*itemEventProp)[OBJECT].value<QObject *>();
QObject *objSlot = props[OBJECT].value<QObject *>();
return connect(objSignal, event_sig.toAscii(), objSlot, observer_sig.toAscii());
}
return false;
}
bool mafEventDispatcher::removeObserver(const mafEvent &props) {
QObject *objSlot = props[OBJECT].value<QObject *>();
if (objSlot == NULL) {
// remove all observer for that 'id'
bool result = disconnectSignal(props);
int num = m_CallbacksHash.remove(props[TOPIC].toString());
return result && num > 0;
}
return removeEventItem(props);
}
bool mafEventDispatcher::registerSignal(const mafEvent &props) {
// check if the object has been already registered with the same signature to avoid duplicates.
if(props["Signature"].toString().length() == 0) {
QVariant var;
var.setValue((QObject *)this);
props[OBJECT] = var;
props[SIGNATURE] = "notifyDefaultEvent()";
}
mafString topic = props[TOPIC].toString();
// Check if a signal (corresponding to a mafID) already is present.
if(m_SignalsHash.contains(topic)) {// && (this->isSignaturePresent(signal_props) == true)) {
// Only one signal for a given id can be registered!!
QObject *obj = props[OBJECT].value<QObject *>();
if(obj != NULL) {
mafMsgWarning("%s", mafTr("Object %1 is trying to register a signal with ID '%2' that has been already registered!!").arg(obj->metaObject()->className(), topic).toAscii().data());
} else {
mafMsgWarning("%s", mafTr("NULL is trying to register a signal with ID '%2' that has been already registered!!").arg(topic).toAscii().data());
}
return false;
}
// Add the new signal to the Hash.
mafEvent *dict = const_cast<mafEvent *>(&props);
this->m_SignalsHash.insert(topic, dict);
return true;
}
bool mafEventDispatcher::removeSignal(const mafEvent &props) {
QObject *obj = props[OBJECT].value<QObject *>();
if(obj == NULL) {
// Remove all events corresponding to particular id
int num = m_SignalsHash.remove(props[TOPIC].toString());
return num > 0;
}
return removeEventItem(props);
}
void mafEventDispatcher::notifyEvent(const mafEvent &event_dictionary, mafEventArgumentsList *argList, mafGenericReturnArgument *returnArg) const {
Q_UNUSED(event_dictionary);
Q_UNUSED(argList);
Q_UNUSED(returnArg);
}