ctkExampleDicomHost.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // Qt includes
  16. #include <QProcess>
  17. #include <QtDebug>
  18. #include <QRect>
  19. #include <QStringList>
  20. #include <QDir>
  21. // CTK includes
  22. #include "ctkExampleDicomHost.h"
  23. #include "ctkDicomAppHostingTypesHelper.h"
  24. #include "ctkDicomAvailableDataHelper.h"
  25. // STD includes
  26. #include <iostream>
  27. // DCMTK includes
  28. #include <dcmtk/dcmdata/dcuid.h>
  29. //----------------------------------------------------------------------------
  30. ctkExampleDicomHost::ctkExampleDicomHost(ctkHostedAppPlaceholderWidget* placeholderWidget, int hostPort, int appPort) :
  31. ctkDicomAbstractHost(hostPort, appPort),
  32. PlaceholderWidget(placeholderWidget),
  33. exitingApplication(false)
  34. {
  35. connect(this,SIGNAL(appReady()),SLOT(onAppReady()), Qt::QueuedConnection);
  36. connect(this,SIGNAL(startProgress()),this,SLOT(onStartProgress()));
  37. connect(this,SIGNAL(releaseAvailableResources()),this,SLOT(onReleaseAvailableResources()));
  38. connect(this,SIGNAL(resumed()),this,SLOT(onResumed()));
  39. connect(this,SIGNAL(completed()),this,SLOT(onCompleted()));
  40. connect(this,SIGNAL(suspended()),this,SLOT(onSuspended()));
  41. connect(this,SIGNAL(canceled()),this,SLOT(onCanceled()));
  42. connect(this,SIGNAL(exited()),this,SLOT(onExited()));
  43. }
  44. //----------------------------------------------------------------------------
  45. void ctkExampleDicomHost::StartApplication(QString AppPath)
  46. {
  47. QStringList arguments;
  48. arguments.append("--hostURL");
  49. arguments.append(QString("http://localhost:") + QString::number(this->getHostPort()) + "/HostInterface" );
  50. arguments.append("--applicationURL");
  51. arguments.append(QString("http://localhost:") + QString::number(this->getAppPort()) + "/ApplicationInterface" );
  52. //by default, the ctkExampleHostedApp uses the org.commontk.dah.exampleapp plugin
  53. //arguments.append("dicomapp"); // the app plugin to use - has to be changed later
  54. //if (!QProcess::startDetached (
  55. //{
  56. // qCritical() << "application failed to start!";
  57. //}
  58. //qDebug() << "starting application: " << AppPath << " " << arguments;
  59. qDebug() << "starting application: " << AppPath << " " << arguments;
  60. this->AppProcess.setProcessChannelMode(QProcess::MergedChannels);
  61. this->AppProcess.start(AppPath, arguments);
  62. }
  63. //----------------------------------------------------------------------------
  64. QRect ctkExampleDicomHost::getAvailableScreen(const QRect& preferredScreen)
  65. {
  66. qDebug()<< "Application asked for this area:"<< preferredScreen;
  67. QRect rect (this->PlaceholderWidget->getAbsolutePosition());
  68. emit giveAvailableScreen(rect);
  69. return rect;
  70. }
  71. //----------------------------------------------------------------------------
  72. void ctkExampleDicomHost::notifyStatus(const ctkDicomAppHosting::Status& status)
  73. {
  74. qDebug()<< "new status received:"<<status.codeMeaning;
  75. emit statusReceived(status);;
  76. }
  77. //----------------------------------------------------------------------------
  78. ctkExampleDicomHost::~ctkExampleDicomHost()
  79. {
  80. if(this->AppProcess.state()!=QProcess::NotRunning)
  81. {
  82. qDebug() << "Exiting host: trying to terminate app";
  83. this->AppProcess.terminate();
  84. qDebug() << "Exiting host: trying to kill app";
  85. this->AppProcess.kill();
  86. }
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkExampleDicomHost::onAppReady()
  90. {
  91. //prepare some resources...
  92. //tell app to start
  93. //getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  94. qDebug() << "App ready to work";
  95. if (this->exitingApplication)
  96. {
  97. this->exitingApplication = false;
  98. getDicomAppService ()->setState (ctkDicomAppHosting::EXIT);
  99. }
  100. }
  101. //----------------------------------------------------------------------------
  102. void ctkExampleDicomHost::onStartProgress()
  103. {
  104. ctkDicomAppHosting::AvailableData data;
  105. ctkDicomAvailableDataHelper::addToAvailableData(data,
  106. this->objectLocatorCache(),
  107. "C:/XIP/XIPHost/dicom-dataset-demo/1.3.6.1.4.1.9328.50.1.10698.dcm");
  108. qDebug()<<"send dataDescriptors";
  109. bool success = this->publishData(data, true);
  110. if(!success)
  111. {
  112. qCritical() << "Failed to publish data";
  113. }
  114. qDebug() << " notifyDataAvailable(1111) returned: " << success;
  115. }
  116. //----------------------------------------------------------------------------
  117. void ctkExampleDicomHost::onResumed()
  118. {
  119. qDebug() << "App resumed work";
  120. }
  121. //----------------------------------------------------------------------------
  122. void ctkExampleDicomHost::onCompleted()
  123. {
  124. qDebug() << "App finished processing";
  125. }
  126. //----------------------------------------------------------------------------
  127. void ctkExampleDicomHost::onSuspended()
  128. {
  129. qDebug() << "App paused";
  130. }
  131. //----------------------------------------------------------------------------
  132. void ctkExampleDicomHost::onCanceled()
  133. {
  134. qDebug() << "App canceled";
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkExampleDicomHost::onExited()
  138. {
  139. qDebug() << "App exited";
  140. cleanIncomingData();
  141. }
  142. //----------------------------------------------------------------------------
  143. void ctkExampleDicomHost::onReleaseAvailableResources()
  144. {
  145. qDebug() << "Should release resources put at the disposition of the app";
  146. }
  147. //----------------------------------------------------------------------------
  148. void ctkExampleDicomHost::exitApplication()
  149. {
  150. this->exitingApplication=true;
  151. if(this->getAppProcess().state() == QProcess::Running)
  152. {
  153. if(this->getApplicationState() == ctkDicomAppHosting::EXIT)
  154. return;
  155. if(this->getApplicationState() == ctkDicomAppHosting::IDLE)
  156. {
  157. getDicomAppService ()->setState (ctkDicomAppHosting::EXIT);
  158. return;
  159. }
  160. getDicomAppService ()->setState (ctkDicomAppHosting::CANCELED);
  161. }
  162. }
  163. //----------------------------------------------------------------------------
  164. QString ctkExampleDicomHost::getOutputLocation(const QStringList& preferredProtocols)
  165. {
  166. if(preferredProtocols.indexOf("file:")>=0)
  167. return QDir::temp().absolutePath();
  168. return "";
  169. }
  170. //----------------------------------------------------------------------------
  171. ctkDicomAppHosting::State ctkExampleDicomHost::getApplicationState()const
  172. {
  173. if(this->getAppProcess().state() == QProcess::NotRunning)
  174. return ctkDicomAppHosting::EXIT;
  175. return ctkDicomAbstractHost::getApplicationState();
  176. }
  177. //----------------------------------------------------------------------------
  178. QString ctkExampleDicomHost::generateUID()
  179. {
  180. char uid[100];
  181. dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
  182. return uid;
  183. }
  184. //----------------------------------------------------------------------------
  185. void ctkExampleDicomHost::exitApplicationBlocking(int timeout)
  186. {
  187. connect(&this->AppProcess,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(onBlockingExiting(QProcess::ProcessState)));
  188. if(this->getApplicationState() != ctkDicomAppHosting::EXIT)
  189. {
  190. this->exitApplication();
  191. QTimer::singleShot(timeout,this,SLOT(onBlockingExiting()));
  192. BlockingLoopForExiting.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);
  193. }
  194. if(this->AppProcess.state()!=QProcess::NotRunning)
  195. {
  196. this->AppProcess.kill();
  197. }
  198. }
  199. //----------------------------------------------------------------------------
  200. void ctkExampleDicomHost::onBlockingExiting(QProcess::ProcessState)
  201. {
  202. this->AppProcess.disconnect(SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onBlockingExiting(QProcess::ProcessState)));
  203. BlockingLoopForExiting.exit(0);
  204. }
  205. //----------------------------------------------------------------------------
  206. void ctkExampleDicomHost::onBlockingExiting()
  207. {
  208. BlockingLoopForExiting.exit(0);
  209. }