ctkSnippetReportManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef CTKSNIPPETREPORTMANAGER_H
  16. #define CTKSNIPPETREPORTMANAGER_H
  17. #include <ctkPluginContext.h>
  18. #include <ctkServiceReference.h>
  19. #include <service/event/ctkEventAdmin.h>
  20. #include <service/event/ctkEventHandler.h>
  21. #include <ctkPluginFrameworkLauncher.h>
  22. #include <QTime>
  23. class Report
  24. {
  25. public:
  26. QString getTitle() const { return "dummy"; }
  27. QString getAbsolutePath() const { return "dummy"; }
  28. };
  29. //! [Event Handler service]
  30. class ReportEventHandler : public QObject, public ctkEventHandler
  31. {
  32. Q_OBJECT
  33. Q_INTERFACES(ctkEventHandler)
  34. public:
  35. void handleEvent(const ctkEvent& event)
  36. {
  37. QString reportTitle = event.getProperty("title").toString();
  38. QString reportPath = event.getProperty("path").toString();
  39. // sendReportByEmail(reportTitle, reportPath);
  40. qDebug() << "title:" << reportTitle << "path:" << reportPath;
  41. }
  42. };
  43. //! [Event Handler service]
  44. //! [Event Handler slot]
  45. class ReportEventHandlerUsingSlots : public QObject
  46. {
  47. Q_OBJECT
  48. public slots:
  49. void handleEvent(const ctkEvent& event)
  50. {
  51. QString reportTitle = event.getProperty("title").toString();
  52. QString reportPath = event.getProperty("path").toString();
  53. // sendReportByEmail(reportTitle, reportPath);
  54. qDebug() << "[slot] title:" << reportTitle << "path:" << reportPath;
  55. }
  56. };
  57. //! [Event Handler slot]
  58. class ReportManager : public QObject
  59. {
  60. Q_OBJECT
  61. public:
  62. //! [Register signal]
  63. ReportManager(ctkPluginContext* context)
  64. {
  65. ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
  66. if (ref)
  67. {
  68. ctkEventAdmin* eventAdmin = context->getService<ctkEventAdmin>(ref);
  69. // Using Qt::DirectConnection is equivalent to ctkEventAdmin::sendEvent()
  70. eventAdmin->publishSignal(this, SIGNAL(reportGeneratedSignal(ctkDictionary)),
  71. "com/acme/reportgenerator/GENERATED", Qt::DirectConnection);
  72. }
  73. }
  74. //! [Register signal]
  75. //! [Emit signal]
  76. void reportGenerated(const Report& report)
  77. {
  78. ctkDictionary properties;
  79. properties["title"] = report.getTitle();
  80. properties["path"] = report.getAbsolutePath();
  81. properties["time"] = QTime::currentTime();
  82. emit reportGeneratedSignal(properties);
  83. }
  84. //! [Emit signal]
  85. //! [Publish event]
  86. void reportGenerated(const Report& report, ctkPluginContext* context)
  87. {
  88. ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
  89. if (ref)
  90. {
  91. ctkEventAdmin* eventAdmin = context->getService<ctkEventAdmin>(ref);
  92. ctkDictionary properties;
  93. properties["title"] = report.getTitle();
  94. properties["path"] = report.getAbsolutePath();
  95. properties["time"] = QTime::currentTime();
  96. ctkEvent reportGeneratedEvent("com/acme/reportgenerator/GENERATED", properties);
  97. eventAdmin->sendEvent(reportGeneratedEvent);
  98. }
  99. }
  100. //! [Publish event]
  101. void reportGeneratedAsync(const Report& report, ctkPluginContext* context)
  102. {
  103. ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
  104. if (ref)
  105. {
  106. ctkEventAdmin* eventAdmin = context->getService<ctkEventAdmin>(ref);
  107. ctkDictionary properties;
  108. properties["title"] = report.getTitle();
  109. properties["path"] = report.getAbsolutePath();
  110. properties["time"] = QTime::currentTime();
  111. //! [Publish event async]
  112. ctkEvent reportGeneratedEvent("com/acme/reportgenerator/GENERATED", properties);
  113. eventAdmin->postEvent(reportGeneratedEvent);
  114. //! [Publish event async]
  115. }
  116. }
  117. //! [Declare signal]
  118. signals:
  119. void reportGeneratedSignal(const ctkDictionary&);
  120. //! [Declare signal]
  121. };
  122. #endif // CTKSNIPPETREPORTMANAGER_H