ctkCmdLineModuleProcessWatcher.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "ctkCmdLineModuleProcessWatcher_p.h"
  16. #include "ctkCmdLineModuleFuture.h"
  17. #include <QProcess>
  18. #ifdef Q_OS_UNIX
  19. #include <signal.h>
  20. #endif
  21. //----------------------------------------------------------------------------
  22. ctkCmdLineModuleProcessWatcher::ctkCmdLineModuleProcessWatcher(QProcess& process, const QString& location,
  23. ctkCmdLineModuleFutureInterface &futureInterface)
  24. : process(process), location(location), futureInterface(futureInterface), processXmlWatcher(&process),
  25. processPaused(false), progressValue(0)
  26. {
  27. futureInterface.setProgressRange(0, 1000);
  28. connect(&processXmlWatcher, SIGNAL(filterStarted(QString,QString)), SLOT(filterStarted(QString,QString)));
  29. connect(&processXmlWatcher, SIGNAL(filterProgress(float)), SLOT(filterProgress(float)));
  30. connect(&processXmlWatcher, SIGNAL(filterResult(QString,QString)), SLOT(filterResult(QString,QString)));
  31. connect(&processXmlWatcher, SIGNAL(filterFinished(QString)), SLOT(filterFinished(QString)));
  32. connect(&processXmlWatcher, SIGNAL(filterXmlError(QString)), SLOT(filterXmlError(QString)));
  33. connect(&processXmlWatcher, SIGNAL(outputDataAvailable(QByteArray)), SLOT(outputDataAvailable(QByteArray)));
  34. connect(&processXmlWatcher, SIGNAL(errorDataAvailable(QByteArray)), SLOT(errorDataAvailable(QByteArray)));
  35. connect(&futureWatcher, SIGNAL(canceled()), SLOT(cancelProcess()));
  36. #ifdef Q_OS_UNIX
  37. connect(&futureWatcher, SIGNAL(resumed()), SLOT(resumeProcess()));
  38. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  39. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  40. // resuming the future, after the "resume" signal has been emitted...
  41. //connect(&futureWatcher, SIGNAL(paused()), SLOT(pauseProcess()));
  42. connect(&pollPauseTimer, SIGNAL(timeout()), this, SLOT(pauseProcess()));
  43. pollPauseTimer.start(500);
  44. #endif
  45. futureWatcher.setFuture(futureInterface.future());
  46. }
  47. //----------------------------------------------------------------------------
  48. void ctkCmdLineModuleProcessWatcher::filterStarted(const QString& name, const QString& comment)
  49. {
  50. Q_UNUSED(comment)
  51. futureInterface.setProgressValueAndText(incrementProgress(), name);
  52. }
  53. //----------------------------------------------------------------------------
  54. void ctkCmdLineModuleProcessWatcher::filterProgress(float progress)
  55. {
  56. futureInterface.setProgressValue(updateProgress(progress));
  57. }
  58. //----------------------------------------------------------------------------
  59. void ctkCmdLineModuleProcessWatcher::filterResult(const QString &parameter, const QString &value)
  60. {
  61. futureInterface.reportResult(ctkCmdLineModuleResult(parameter, value));
  62. }
  63. //----------------------------------------------------------------------------
  64. void ctkCmdLineModuleProcessWatcher::filterFinished(const QString& name)
  65. {
  66. futureInterface.setProgressValueAndText(incrementProgress(), "Finished: " + name);
  67. }
  68. //----------------------------------------------------------------------------
  69. void ctkCmdLineModuleProcessWatcher::filterXmlError(const QString &error)
  70. {
  71. qDebug().nospace() << "[Module " << location << "]: " << error;
  72. }
  73. //----------------------------------------------------------------------------
  74. void ctkCmdLineModuleProcessWatcher::pauseProcess()
  75. {
  76. if (processPaused || !futureInterface.isPaused()) return;
  77. #ifdef Q_OS_UNIX
  78. if (::kill(process.pid(), SIGSTOP))
  79. {
  80. // error
  81. futureInterface.setPaused(false);
  82. }
  83. else
  84. {
  85. processPaused = true;
  86. }
  87. #endif
  88. }
  89. //----------------------------------------------------------------------------
  90. void ctkCmdLineModuleProcessWatcher::resumeProcess()
  91. {
  92. if (!processPaused) return;
  93. #ifdef Q_OS_UNIX
  94. if(::kill(process.pid(), SIGCONT))
  95. {
  96. // error
  97. futureInterface.setPaused(true);
  98. }
  99. else
  100. {
  101. processPaused = false;
  102. }
  103. #endif
  104. }
  105. //----------------------------------------------------------------------------
  106. void ctkCmdLineModuleProcessWatcher::cancelProcess()
  107. {
  108. process.kill();
  109. }
  110. //----------------------------------------------------------------------------
  111. void ctkCmdLineModuleProcessWatcher::outputDataAvailable(const QByteArray &outputData)
  112. {
  113. futureInterface.reportOutputData(outputData);
  114. }
  115. //----------------------------------------------------------------------------
  116. void ctkCmdLineModuleProcessWatcher::errorDataAvailable(const QByteArray &errorData)
  117. {
  118. futureInterface.reportErrorData(errorData);
  119. }
  120. //----------------------------------------------------------------------------
  121. int ctkCmdLineModuleProcessWatcher::updateProgress(float progress)
  122. {
  123. progressValue = static_cast<int>(progress * 1000.0f);
  124. // normalize the value to lie between 0 and 1000.
  125. // 0 is reported when the process starts and 1000 is reserved for
  126. // reporting completion + standard output text
  127. if (progressValue < 1) progressValue = 1;
  128. if (progressValue > 999) progressValue = 999;
  129. return progressValue;
  130. }
  131. //----------------------------------------------------------------------------
  132. int ctkCmdLineModuleProcessWatcher::incrementProgress()
  133. {
  134. if (++progressValue > 999) progressValue = 999;
  135. return progressValue;
  136. }