ctkDicomAbstractApp.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <ctkDicomAppHostingTypesHelper.h>
  22. class ctkDicomAbstractAppPrivate
  23. {
  24. public:
  25. ctkDicomAbstractAppPrivate(ctkPluginContext* context);
  26. ctkServiceTracker<ctkDicomHostInterface*> HostTracker;
  27. ctkDicomAppHosting::State currentState;
  28. ctkDicomObjectLocatorCache ObjectLocatorCache;
  29. };
  30. //----------------------------------------------------------------------------
  31. // ctkDicomAbstractAppPrivate methods
  32. //----------------------------------------------------------------------------
  33. ctkDicomAbstractAppPrivate::ctkDicomAbstractAppPrivate(ctkPluginContext * context):HostTracker(context),currentState(ctkDicomAppHosting::IDLE)
  34. {
  35. //perhaps notStarted or some dummy state instead of IDLE?
  36. }
  37. //----------------------------------------------------------------------------
  38. // ctkDicomAbstractApp methods
  39. //----------------------------------------------------------------------------
  40. ctkDicomAbstractApp::ctkDicomAbstractApp(ctkPluginContext* context) : d_ptr(new ctkDicomAbstractAppPrivate(context))
  41. {
  42. d_ptr->HostTracker.open();
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkDicomAbstractApp::~ctkDicomAbstractApp()
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. bool ctkDicomAbstractApp::setState(ctkDicomAppHosting::State newState)
  50. {
  51. qDebug()<<"treating new state: "<< ctkDicomSoapState::toStringValue(newState);
  52. bool result = false;
  53. //received a new state,
  54. switch (newState){
  55. case ctkDicomAppHosting::IDLE:
  56. if (d_ptr->currentState == ctkDicomAppHosting::COMPLETED)
  57. {
  58. emit releaseResources();
  59. //inform the host that now the app is idle
  60. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
  61. //also change the internal state
  62. d_ptr->currentState = ctkDicomAppHosting::IDLE;
  63. result = true;
  64. }
  65. break;
  66. case ctkDicomAppHosting::INPROGRESS:
  67. if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
  68. {
  69. emit startProgress();
  70. result = true;
  71. }
  72. else if(d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
  73. {
  74. emit resumeProgress();
  75. result = true;
  76. }
  77. break;
  78. case ctkDicomAppHosting::COMPLETED:
  79. qDebug() << "Hosting system shouldn't send completed";
  80. break;
  81. case ctkDicomAppHosting::SUSPENDED:
  82. //suspend computation, release as much resource as possible with possible resuming of computation
  83. emit suspendProgress();
  84. result = true;
  85. break;
  86. case ctkDicomAppHosting::CANCELED:
  87. //stop and release everything.
  88. if (d_ptr->currentState == ctkDicomAppHosting::INPROGRESS
  89. || d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
  90. {
  91. //special state, a transitional state, so we notify straight away the new state.
  92. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::CANCELED);
  93. d_ptr->currentState = ctkDicomAppHosting::CANCELED;
  94. //releasing resources
  95. emit cancelProgress();
  96. result = true;
  97. }
  98. break;
  99. case ctkDicomAppHosting::EXIT:
  100. //check if current state is IDLE
  101. if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
  102. {
  103. //maybe not useful:
  104. //getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
  105. emit exitHostedApp();
  106. result = true;
  107. }
  108. break;
  109. default:
  110. //should never happen
  111. qDebug() << "unexisting state Code, do nothing";
  112. }
  113. if (!result)
  114. {
  115. qDebug()<<"illegal transition to: "<< static_cast<int>(newState) <<
  116. "Current state is:" << static_cast<int>(d_ptr->currentState);
  117. qDebug()<<"illegal transition to: "<< ctkDicomSoapState::toStringValue(newState) <<
  118. "Current state is:" << ctkDicomSoapState::toStringValue(d_ptr->currentState);
  119. }
  120. return result;
  121. }
  122. //----------------------------------------------------------------------------
  123. ctkDicomHostInterface* ctkDicomAbstractApp::getHostInterface() const
  124. {
  125. ctkDicomHostInterface* host = d_ptr->HostTracker.getService();
  126. if (!host) throw ctkRuntimeException("DICOM Host Interface not available");
  127. return host;
  128. }
  129. //----------------------------------------------------------------------------
  130. ctkDicomExchangeInterface* ctkDicomAbstractApp::getOtherSideExchangeService() const
  131. {
  132. return getHostInterface();
  133. }
  134. //----------------------------------------------------------------------------
  135. ctkDicomAppHosting::State ctkDicomAbstractApp::getState()
  136. {
  137. return d_ptr->currentState;
  138. }
  139. void ctkDicomAbstractApp::setInternalState(ctkDicomAppHosting::State state)
  140. {
  141. d_ptr->currentState = state;
  142. }