ctkCmdLineModuleProcessWatcher.cpp 6.3 KB

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