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