ctkHostAppExampleWidget.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <QDebug>
  17. #include <QFileDialog>
  18. #include <QProcess>
  19. // CTK includes
  20. #include "ctkHostAppExampleWidget.h"
  21. #include "ui_ctkHostAppExampleWidget.h"
  22. #include "ctkExampleDicomHost.h"
  23. #include "ctkDicomAppService.h"
  24. #include <ctkDicomAppHostingTypesHelper.h>
  25. //----------------------------------------------------------------------------
  26. ctkHostAppExampleWidget::ctkHostAppExampleWidget(QWidget *parent) :
  27. QWidget(parent),
  28. ui(new Ui::ctkHostAppExampleWidget)
  29. {
  30. qDebug() << "setup ui";
  31. ui->setupUi(this);
  32. ui->crashLabel->setVisible(false);
  33. ui->messageOutput->setVisible(false);
  34. this->Host = new ctkExampleDicomHost(ui->placeholderFrame);
  35. connect(&this->Host->getAppProcess(),SIGNAL(error(QProcess::ProcessError)),SLOT(appProcessError(QProcess::ProcessError)));
  36. connect(&this->Host->getAppProcess(),SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(appProcessStateChanged(QProcess::ProcessState)));
  37. connect(ui->placeholderFrame,SIGNAL(resized()),SLOT(placeholderResized()));
  38. connect(this->Host,SIGNAL( stateChangedReceived(ctkDicomAppHosting::State)),SLOT(appStateChanged(ctkDicomAppHosting::State)));
  39. }
  40. //----------------------------------------------------------------------------
  41. ctkHostAppExampleWidget::~ctkHostAppExampleWidget()
  42. {
  43. delete this->Host;
  44. this->Host = 0;
  45. delete this->ui;
  46. this->ui = 0;
  47. }
  48. //----------------------------------------------------------------------------
  49. void ctkHostAppExampleWidget::startButtonClicked()
  50. {
  51. qDebug() << "start button clicked";
  52. if (this->Host)
  53. {
  54. this->Host->StartApplication(this->AppFileName);
  55. //forward output to textedit
  56. connect(&this->Host->getAppProcess(),SIGNAL(readyReadStandardOutput()),this,SLOT(outputMessage()));
  57. }
  58. }
  59. //----------------------------------------------------------------------------
  60. void ctkHostAppExampleWidget::runButtonClicked()
  61. {
  62. qDebug() << "run button clicked";
  63. if (this->Host)
  64. {
  65. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  66. qDebug() << " setState(INPROGRESS) returned: " << reply;
  67. }
  68. }
  69. //----------------------------------------------------------------------------
  70. void ctkHostAppExampleWidget::stopButtonClicked()
  71. {
  72. qDebug() << "stop button clicked";
  73. this->Host->getDicomAppService ()->setState (ctkDicomAppHosting::CANCELED);
  74. }
  75. //----------------------------------------------------------------------------
  76. void ctkHostAppExampleWidget::loadButtonClicked()
  77. {
  78. qDebug() << "load button clicked";
  79. this->setAppFileName(QFileDialog::getOpenFileName(this,"Choose hosted application",QApplication::applicationDirPath()));
  80. }
  81. //----------------------------------------------------------------------------
  82. void ctkHostAppExampleWidget::setAppFileName(QString name)
  83. {
  84. this->AppFileName = name;
  85. if (QFile(this->AppFileName).permissions() & QFile::ExeUser )
  86. {
  87. this->ui->applicationPathLabel->setText(this->AppFileName);
  88. }
  89. else
  90. {
  91. this->ui->applicationPathLabel->setText(
  92. QString("<font color='red'>Not executable:</font>").append(this->AppFileName));
  93. }
  94. }
  95. //----------------------------------------------------------------------------
  96. void ctkHostAppExampleWidget::appProcessError(QProcess::ProcessError error)
  97. {
  98. if (error == QProcess::Crashed)
  99. {
  100. qDebug() << "crash detected";
  101. ui->crashLabel->setVisible(true);
  102. }
  103. }
  104. //----------------------------------------------------------------------------
  105. void ctkHostAppExampleWidget::appProcessStateChanged(QProcess::ProcessState state)
  106. {
  107. QString labelText;
  108. switch (state)
  109. {
  110. case QProcess::Running:
  111. ui->processStateLabel->setText("Running");
  112. break;
  113. case QProcess::NotRunning:
  114. if (this->Host->getAppProcess().exitStatus() == QProcess::CrashExit )
  115. {
  116. labelText = "crashed";
  117. }
  118. else
  119. {
  120. labelText = "Not Running, last exit code ";
  121. labelText.append(QString::number(this->Host->getAppProcess().exitCode()));
  122. }
  123. ui->processStateLabel->setText(labelText);
  124. break;
  125. case QProcess::Starting:
  126. ui->processStateLabel->setText("Starting");
  127. break;
  128. default:
  129. ;
  130. }
  131. }
  132. //----------------------------------------------------------------------------
  133. void ctkHostAppExampleWidget::placeholderResized()
  134. {
  135. qDebug() << "resized";
  136. //ui->placeholderFrame->printPosition();
  137. }
  138. //----------------------------------------------------------------------------
  139. void ctkHostAppExampleWidget::appStateChanged(ctkDicomAppHosting::State state)
  140. {
  141. ui->statusLabel->setText(ctkDicomSoapState::toStringValue(state));
  142. bool reply;
  143. ctkDicomAppHosting::ObjectDescriptor ourObjectDescriptor;
  144. QList<ctkDicomAppHosting::Study> studies;
  145. ctkDicomAppHosting::AvailableData data;
  146. ctkDicomAppHosting::Patient patient;
  147. //TODO put the state changed routine back in notifyStateChanged for the state machine part.
  148. switch (state)
  149. {
  150. case ctkDicomAppHosting::IDLE:
  151. if (this->Host->getApplicationState() != ctkDicomAppHosting::IDLE)
  152. {
  153. qDebug()<<"state was not IDLE before -> setState EXIT ";
  154. this->Host->getDicomAppService()->setState(ctkDicomAppHosting::EXIT);
  155. }
  156. break;
  157. case ctkDicomAppHosting::INPROGRESS:
  158. patient.name = "John Doe";
  159. patient.id = "0000";
  160. patient.assigningAuthority = "authority";
  161. patient.sex = "male";
  162. patient.birthDate = "today";
  163. patient.objectDescriptors = QList<ctkDicomAppHosting::ObjectDescriptor>();
  164. patient.studies = studies;
  165. ourObjectDescriptor.descriptorUUID = QUuid("{11111111-1111-1111-1111-111111111111}");
  166. ourObjectDescriptor.mimeType = "text/plain";
  167. ourObjectDescriptor.classUID = "lovelyClass";
  168. ourObjectDescriptor.transferSyntaxUID = "transSyntaxUId";
  169. ourObjectDescriptor.modality = "modMod";
  170. data.objectDescriptors = QList<ctkDicomAppHosting::ObjectDescriptor>();
  171. data.objectDescriptors.append (ourObjectDescriptor);
  172. data.patients = QList<ctkDicomAppHosting::Patient>();
  173. data.patients.append (patient);
  174. qDebug()<<"send dataDescriptors";
  175. reply = this->Host->getDicomAppService()->notifyDataAvailable (data,true);
  176. qDebug() << " notifyDataAvailable(1111) returned: " << reply;
  177. break;
  178. case ctkDicomAppHosting::COMPLETED:
  179. case ctkDicomAppHosting::SUSPENDED:
  180. case ctkDicomAppHosting::CANCELED:
  181. case ctkDicomAppHosting::EXIT:
  182. //shouldn't happen, when exiting the application just dies
  183. default:
  184. //do nothing
  185. break;
  186. }
  187. this->Host->setApplicationState(state);
  188. }
  189. //----------------------------------------------------------------------------
  190. void ctkHostAppExampleWidget::outputMessage ()
  191. {
  192. ui->messageOutput->append (this->Host->processReadAll ());
  193. }