ctkHostAppExampleWidget.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(true);
  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->exitApplication();
  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. void ctkHostAppExampleWidget::appStateChanged(ctkDicomAppHosting::State state)
  139. {
  140. ui->statusLabel->setText(ctkDicomSoapState::toStringValue(state));
  141. }
  142. //----------------------------------------------------------------------------
  143. void ctkHostAppExampleWidget::outputMessage ()
  144. {
  145. ui->messageOutput->append (this->Host->processReadAll ());
  146. }
  147. //----------------------------------------------------------------------------
  148. void ctkHostAppExampleWidget::suspendButtonClicked()
  149. {
  150. this->Host->getDicomAppService()->setState(ctkDicomAppHosting::SUSPENDED);
  151. }
  152. void ctkHostAppExampleWidget::cancelButtonClicked()
  153. {
  154. this->Host->getDicomAppService()->setState(ctkDicomAppHosting::CANCELED);
  155. }