ctkEventBusImpl.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkEventBusImpl_p.h"
  16. #include <QSetIterator>
  17. #include "ctkEventHandlerWrapper_p.h"
  18. ctkEventBusImpl* ctkEventBusImpl::instance()
  19. {
  20. static ctkEventBusImpl inst;
  21. return &inst;
  22. }
  23. ctkEventBusImpl::ctkEventBusImpl()
  24. {
  25. }
  26. void ctkEventBusImpl::postEvent(const ctkEvent& event)
  27. {
  28. dispatchEvent(event, true);
  29. }
  30. void ctkEventBusImpl::sendEvent(const ctkEvent& event)
  31. {
  32. dispatchEvent(event, false);
  33. }
  34. void ctkEventBusImpl::publishSignal(const QObject* publisher, const char* signal, const QString& topic,
  35. Qt::ConnectionType type)
  36. {
  37. Q_UNUSED(publisher)
  38. Q_UNUSED(signal)
  39. Q_UNUSED(topic)
  40. Q_UNUSED(type)
  41. }
  42. QString ctkEventBusImpl::subscribeSlot(const QObject* subscriber, const char* member, const ctkProperties& properties)
  43. {
  44. // TODO check for duplicates
  45. ctkEventHandlerWrapper* wrapper = new ctkEventHandlerWrapper(subscriber, member, properties);
  46. if (wrapper->init())
  47. {
  48. bucket(wrapper);
  49. }
  50. // TODO return id
  51. return QString();
  52. }
  53. void ctkEventBusImpl::updateProperties(const QString& subscriptionId, const ctkProperties& properties)
  54. {
  55. Q_UNUSED(subscriptionId)
  56. Q_UNUSED(properties)
  57. }
  58. void ctkEventBusImpl::dispatchEvent(const ctkEvent& event, bool isAsync)
  59. {
  60. Q_UNUSED(isAsync)
  61. QString topic = event.topic();
  62. QSet<ctkEventHandlerWrapper*> eventHandlers = this->handlers(topic);
  63. if (eventHandlers.empty()) return;
  64. QSetIterator<ctkEventHandlerWrapper*> iter(eventHandlers);
  65. while (iter.hasNext())
  66. {
  67. iter.next()->handleEvent(event);
  68. }
  69. }
  70. void ctkEventBusImpl::bucket(ctkEventHandlerWrapper* wrapper)
  71. {
  72. // TODO bucket logic
  73. globalWildcard.push_back(wrapper);
  74. }
  75. QSet<ctkEventHandlerWrapper*> ctkEventBusImpl::handlers(const QString& topic)
  76. {
  77. Q_UNUSED(topic)
  78. // TODO
  79. return globalWildcard.toSet();
  80. }