ctkEventBusImpl.cxx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "ctkEventBusImpl_p.h"
  2. #include <QSetIterator>
  3. #include "ctkEventHandlerWrapper_p.h"
  4. namespace ctk {
  5. EventBusImpl* EventBusImpl::instance()
  6. {
  7. static EventBusImpl inst;
  8. return &inst;
  9. }
  10. EventBusImpl::EventBusImpl()
  11. {
  12. }
  13. void EventBusImpl::postEvent(const Event& event)
  14. {
  15. dispatchEvent(event, true);
  16. }
  17. void EventBusImpl::sendEvent(const Event& event)
  18. {
  19. dispatchEvent(event, false);
  20. }
  21. void EventBusImpl::publishSignal(const QObject* publisher, const char* signal)
  22. {
  23. }
  24. void EventBusImpl::subscribeSlot(const QObject* subscriber, const char* member, const Properties& properties)
  25. {
  26. // TODO check for duplicates
  27. EventHandlerWrapper* wrapper = new EventHandlerWrapper(subscriber, member, properties);
  28. if (wrapper->init())
  29. {
  30. bucket(wrapper);
  31. }
  32. }
  33. void EventBusImpl::dispatchEvent(const Event& event, bool isAsync)
  34. {
  35. QString topic = event.topic();
  36. QSet<EventHandlerWrapper*> eventHandlers = this->handlers(topic);
  37. if (eventHandlers.empty()) return;
  38. QSetIterator<EventHandlerWrapper*> iter(eventHandlers);
  39. while (iter.hasNext())
  40. {
  41. iter.next()->handleEvent(event);
  42. }
  43. }
  44. void EventBusImpl::bucket(EventHandlerWrapper* wrapper)
  45. {
  46. // TODO bucket logic
  47. globalWildcard.push_back(wrapper);
  48. }
  49. QSet<EventHandlerWrapper*> EventBusImpl::handlers(const QString& topic)
  50. {
  51. // TODO
  52. return globalWildcard.toSet();
  53. }
  54. }