WvStreams
wvqthook.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * A Qt object that invokes its callback whenever it receives
6 * an event. This is useful for deferring processing to the
7 * Qt event loop. Use it to avoid problems resulting from the
8 * non-reentrant nature of WvStream::execute().
9 */
10#include "wvqthook.moc"
11
12WvQtHook::WvQtHook(WvQtHookCallback _callback) :
13 callback(_callback)
14{
15}
16
17
18void WvQtHook::setcallback(WvQtHookCallback _callback)
19{
20 callback = _callback;
21}
22
23
24bool WvQtHook::event(QEvent *event)
25{
26 if (! callback)
27 return false;
28 QEvent::Type eventtype = event->type();
29 if (eventtype < QEvent::User || eventtype > QEvent::MaxUser)
30 return false;
31 QCustomEvent *ce = static_cast<QCustomEvent*>(event);
32 callback(*this, eventtype - QEvent::User, ce->data());
33 return true;
34}
35
36
37void WvQtHook::post(int type, void *data)
38{
39 // event must be allocated on heap for postEvent
40 QEvent::Type eventtype = QEvent::Type(QEvent::User + type);
41 QCustomEvent *event = new QCustomEvent(eventtype, data);
42 QApplication::postEvent(this, event);
43}
44
45
46void WvQtHook::send(int type, void *data)
47{
48 QEvent::Type eventtype = QEvent::Type(QEvent::User + type);
49 QCustomEvent event(eventtype, data);
50 QApplication::sendEvent(this, & event);
51}