ctkExampleHostControlWidget.cpp 5.3 KB

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