ctkCmdLineModuleProcessWatcher.cpp 5.5 KB

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