ctkEventAdminService.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "ctkEventAdminService_p.h"
  16. #include "handler/ctkEASlotHandler_p.h"
  17. #include <ctkPluginConstants.h>
  18. ctkEventAdminService::ctkEventAdminService(ctkPluginContext* context,
  19. HandlerTasksInterface* managers,
  20. ctkEADefaultThreadPool* syncPool,
  21. ctkEADefaultThreadPool* asyncPool,
  22. int timeout,
  23. const QStringList& ignoreTimeout)
  24. : impl(managers, syncPool, asyncPool, timeout, ignoreTimeout),
  25. context(context), signalPublisher(this)
  26. {
  27. }
  28. ctkEventAdminService::~ctkEventAdminService()
  29. {
  30. qDeleteAll(slotHandler);
  31. }
  32. void ctkEventAdminService::postEvent(const ctkEvent& event)
  33. {
  34. impl.postEvent(event);
  35. }
  36. void ctkEventAdminService::sendEvent(const ctkEvent& event)
  37. {
  38. impl.sendEvent(event);
  39. }
  40. void ctkEventAdminService::publishSignal(const QObject* publisher, const char* signal,
  41. Qt::ConnectionType type)
  42. {
  43. if (type == Qt::DirectConnection)
  44. {
  45. connect(publisher, signal, &signalPublisher, SLOT(publishSyncSignal(ctkEvent)), Qt::DirectConnection);
  46. }
  47. else if (type == Qt::QueuedConnection)
  48. {
  49. connect(publisher, signal, &signalPublisher, SLOT(publishAsyncSignal(ctkEvent)), Qt::DirectConnection);
  50. }
  51. else
  52. {
  53. throw std::invalid_argument("type must be either Qt::DirectConnection or Qt::QueuedConnection");
  54. }
  55. }
  56. qlonglong ctkEventAdminService::subscribeSlot(const QObject* subscriber, const char* member, const ctkDictionary& properties)
  57. {
  58. ctkEASlotHandler* handler = new ctkEASlotHandler();
  59. connect(handler, SIGNAL(eventOccured(ctkEvent)), subscriber, member, Qt::DirectConnection);
  60. ctkServiceRegistration reg = context->registerService<ctkEventHandler>(handler, properties);
  61. handler->reg = reg;
  62. qlonglong id = reg.getReference().getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  63. slotHandler.insert(id, handler);
  64. return id;
  65. }
  66. void ctkEventAdminService::unsubscribeSlot(qlonglong subscriptionId)
  67. {
  68. ctkEASlotHandler* handler = slotHandler.take(subscriptionId);
  69. if (handler)
  70. {
  71. handler->reg.unregister();
  72. delete handler;
  73. }
  74. }
  75. bool ctkEventAdminService::updateProperties(qlonglong subscriptionId, const ctkDictionary& properties)
  76. {
  77. ctkEASlotHandler* handler = slotHandler.value(subscriptionId);
  78. if (handler)
  79. {
  80. handler->updateProperties(properties);
  81. return true;
  82. }
  83. return false;
  84. }
  85. void ctkEventAdminService::stop()
  86. {
  87. impl.stop();
  88. }
  89. void ctkEventAdminService::update(HandlerTasksInterface* managers, int timeout,
  90. const QStringList& ignoreTimeout)
  91. {
  92. impl.update(managers, timeout, ignoreTimeout);
  93. }