ctkDicomAbstractApp.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // CTK includes
  16. #include "ctkDicomAbstractApp.h"
  17. #include <ctkDicomHostInterface.h>
  18. #include <ctkDicomObjectLocatorCache.h>
  19. #include <ctkPluginContext.h>
  20. #include <ctkServiceTracker.h>
  21. class ctkDicomAbstractAppPrivate
  22. {
  23. public:
  24. ctkDicomAbstractAppPrivate(ctkPluginContext* context);
  25. ctkServiceTracker<ctkDicomHostInterface*> HostTracker;
  26. ctkDicomAppHosting::State currentState;
  27. ctkDicomObjectLocatorCache ObjectLocatorCache;
  28. };
  29. //----------------------------------------------------------------------------
  30. // ctkDicomAbstractAppPrivate methods
  31. //----------------------------------------------------------------------------
  32. ctkDicomAbstractAppPrivate::ctkDicomAbstractAppPrivate(ctkPluginContext * context):HostTracker(context),currentState(ctkDicomAppHosting::IDLE)
  33. {
  34. //perhaps notStarted or some dummy state instead of IDLE?
  35. }
  36. //----------------------------------------------------------------------------
  37. // ctkDicomAbstractApp methods
  38. //----------------------------------------------------------------------------
  39. ctkDicomAbstractApp::ctkDicomAbstractApp(ctkPluginContext* context) : d_ptr(new ctkDicomAbstractAppPrivate(context))
  40. {
  41. d_ptr->HostTracker.open();
  42. }
  43. //----------------------------------------------------------------------------
  44. ctkDicomAbstractApp::~ctkDicomAbstractApp()
  45. {
  46. }
  47. /**
  48. * Method triggered by the host. Changes the state of the hosted application.
  49. *@return true if state received and not illegal in the transition diagram from the reference, false if illegal or not recognized.
  50. */
  51. bool ctkDicomAbstractApp::setState(ctkDicomAppHosting::State newState)
  52. {
  53. bool result = true;
  54. //received a new state,
  55. switch (newState){
  56. case ctkDicomAppHosting::IDLE:
  57. if (d_ptr->currentState == ctkDicomAppHosting::COMPLETED)
  58. {
  59. }
  60. else
  61. {
  62. result = false;
  63. }
  64. break;
  65. case ctkDicomAppHosting::INPROGRESS:
  66. if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
  67. {
  68. emit startProgress();
  69. }
  70. else if(d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
  71. {
  72. emit resumeProgress();
  73. }
  74. else
  75. {
  76. result = false;
  77. }
  78. break;
  79. case ctkDicomAppHosting::COMPLETED:
  80. qDebug() << "Hosting system shouldn't send completed";
  81. result = false;
  82. break;
  83. case ctkDicomAppHosting::SUSPENDED:
  84. //suspend computation, release as much resource as possible with possible resuming of computation
  85. emit suspendProgress();
  86. break;
  87. case ctkDicomAppHosting::CANCELED:
  88. //stop and release everything.
  89. if (d_ptr->currentState != ctkDicomAppHosting::INPROGRESS
  90. || d_ptr->currentState != ctkDicomAppHosting::SUSPENDED)
  91. {
  92. result = false;
  93. }
  94. else
  95. {
  96. //releasing resources
  97. emit cancelProgress();
  98. //special state, a transitional state, so we notify straight away the new state.
  99. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::CANCELED);
  100. d_ptr->currentState = ctkDicomAppHosting::CANCELED;
  101. }
  102. break;
  103. case ctkDicomAppHosting::EXIT:
  104. //check if current state is IDLE
  105. if (d_ptr->currentState != ctkDicomAppHosting::IDLE)
  106. {
  107. qDebug()<<"illegal transition to EXIT." <<
  108. "Current state is:" << d_ptr->currentState;
  109. result = false;
  110. }
  111. //maybe not useful:
  112. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
  113. emit exitHostedApp();
  114. break;
  115. default:
  116. //should never happen
  117. qDebug() << "unexisting state Code, do nothing";
  118. result = false;
  119. }
  120. if (!result)
  121. {
  122. qDebug()<<"illegal transition to: "<< newState <<
  123. "Current state is:" << d_ptr->currentState;
  124. }
  125. return result;
  126. }
  127. //----------------------------------------------------------------------------
  128. ctkDicomHostInterface* ctkDicomAbstractApp::getHostInterface() const
  129. {
  130. ctkDicomHostInterface* host = d_ptr->HostTracker.getService();
  131. if (!host) throw std::runtime_error("DICOM Host Interface not available");
  132. return host;
  133. }
  134. //----------------------------------------------------------------------------
  135. QList<ctkDicomAppHosting::ObjectLocator> ctkDicomAbstractApp::getData(
  136. const QList<QUuid>& objectUUIDs,
  137. const QList<QString>& acceptableTransferSyntaxUIDs,
  138. bool includeBulkData)
  139. {
  140. return this->objectLocatorCache()->getData(objectUUIDs, acceptableTransferSyntaxUIDs, includeBulkData);
  141. }
  142. //----------------------------------------------------------------------------
  143. ctkDicomObjectLocatorCache* ctkDicomAbstractApp::objectLocatorCache()const
  144. {
  145. Q_D(const ctkDicomAbstractApp);
  146. return const_cast<ctkDicomObjectLocatorCache*>(&d->ObjectLocatorCache);
  147. }
  148. //----------------------------------------------------------------------------
  149. bool ctkDicomAbstractApp::publishData(const ctkDicomAppHosting::AvailableData& availableData, bool lastData)
  150. {
  151. if (!this->objectLocatorCache()->isCached(availableData))
  152. {
  153. return false;
  154. }
  155. bool success = this->getHostInterface()->notifyDataAvailable(availableData, lastData);
  156. if(!success)
  157. {
  158. return false;
  159. }
  160. return true;
  161. }