ctkExampleHostControlWidget.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "ctkExampleHostControlWidget.h"
  21. #include "ui_ctkExampleHostControlWidget.h"
  22. #include "ctkExampleDicomHost.h"
  23. #include "ctkDicomAppService.h"
  24. #include <ctkDicomAppHostingTypesHelper.h>
  25. //----------------------------------------------------------------------------
  26. ctkExampleHostControlWidget::ctkExampleHostControlWidget(ctkExampleDicomHost * host, QWidget *parent) :
  27. Host(host),
  28. QWidget(parent),
  29. ValidAppFileName(false),
  30. ui(new Ui::ctkExampleHostControlWidget)
  31. {
  32. qDebug() << "setup ui";
  33. ui->setupUi(this);
  34. // ui->crashLabel->setVisible(false);
  35. ui->messageOutput->setVisible(true);
  36. if(Host==NULL)
  37. qDebug() << "no host provided when creating ctkExampleHostControlWidget.";
  38. else
  39. {
  40. connect(&this->Host->getAppProcess(),SIGNAL(error(QProcess::ProcessError)),SLOT(appProcessError(QProcess::ProcessError)));
  41. connect(&this->Host->getAppProcess(),SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(appProcessStateChanged(QProcess::ProcessState)));
  42. connect(this->Host,SIGNAL(stateChangedReceived(ctkDicomAppHosting::State)),SLOT(appStateChanged(ctkDicomAppHosting::State)));
  43. }
  44. }
  45. //----------------------------------------------------------------------------
  46. ctkExampleHostControlWidget::~ctkExampleHostControlWidget()
  47. {
  48. delete this->ui;
  49. this->ui = 0;
  50. }
  51. //----------------------------------------------------------------------------
  52. void ctkExampleHostControlWidget::StartApplication(QString appFileName)
  53. {
  54. qDebug() << "ctkExampleHostControlWidget::StartApplication";
  55. if(appFileName.isEmpty()==false)
  56. this->setAppFileName(appFileName);
  57. if ((this->Host) && (validAppFileName()))
  58. {
  59. qDebug() << "Starting app";
  60. this->Host->StartApplication(this->AppFileName);
  61. //forward output to textedit
  62. connect(&this->Host->getAppProcess(),SIGNAL(readyReadStandardOutput()),this,SLOT(outputMessage()));
  63. }
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkExampleHostControlWidget::runButtonClicked()
  67. {
  68. qDebug() << "run button clicked";
  69. if (this->Host)
  70. {
  71. bool reply = this->Host->getDicomAppService()->setState(ctkDicomAppHosting::INPROGRESS);
  72. qDebug() << " setState(INPROGRESS) returned: " << reply;
  73. }
  74. }
  75. //----------------------------------------------------------------------------
  76. void ctkExampleHostControlWidget::stopButtonClicked()
  77. {
  78. qDebug() << "stop button clicked";
  79. this->Host->exitApplication();
  80. }
  81. //----------------------------------------------------------------------------
  82. void ctkExampleHostControlWidget::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. ValidAppFileName = true;
  89. }
  90. else
  91. {
  92. ValidAppFileName = false;
  93. this->ui->applicationPathLabel->setText(
  94. QString("<font color='red'>Not executable:</font>").append(this->AppFileName));
  95. }
  96. }
  97. //----------------------------------------------------------------------------
  98. void ctkExampleHostControlWidget::appProcessError(QProcess::ProcessError error)
  99. {
  100. if (error == QProcess::Crashed)
  101. {
  102. qDebug() << "crash detected";
  103. // ui->crashLabel->setVisible(true);
  104. }
  105. }
  106. //----------------------------------------------------------------------------
  107. void ctkExampleHostControlWidget::appProcessStateChanged(QProcess::ProcessState state)
  108. {
  109. QString labelText;
  110. switch (state)
  111. {
  112. case QProcess::Running:
  113. ui->processStateLabel->setText("Running");
  114. break;
  115. case QProcess::NotRunning:
  116. if (this->Host->getAppProcess().exitStatus() == QProcess::CrashExit )
  117. {
  118. labelText = "crashed";
  119. }
  120. else
  121. {
  122. labelText = "Not Running, last exit code ";
  123. labelText.append(QString::number(this->Host->getAppProcess().exitCode()));
  124. }
  125. ui->processStateLabel->setText(labelText);
  126. break;
  127. case QProcess::Starting:
  128. ui->processStateLabel->setText("Starting");
  129. break;
  130. default:
  131. ;
  132. }
  133. }
  134. void ctkExampleHostControlWidget::appStateChanged(ctkDicomAppHosting::State state)
  135. {
  136. ui->statusLabel->setText(ctkDicomSoapState::toStringValue(state));
  137. }
  138. //----------------------------------------------------------------------------
  139. void ctkExampleHostControlWidget::outputMessage ()
  140. {
  141. ui->messageOutput->append (this->Host->processReadAll ());
  142. }
  143. //----------------------------------------------------------------------------
  144. void ctkExampleHostControlWidget::suspendButtonClicked()
  145. {
  146. this->Host->getDicomAppService()->setState(ctkDicomAppHosting::SUSPENDED);
  147. }
  148. //----------------------------------------------------------------------------
  149. void ctkExampleHostControlWidget::cancelButtonClicked()
  150. {
  151. this->Host->getDicomAppService()->setState(ctkDicomAppHosting::CANCELED);
  152. }
  153. //----------------------------------------------------------------------------
  154. bool ctkExampleHostControlWidget::validAppFileName()
  155. {
  156. return ValidAppFileName;
  157. }