ctkCmdLineModuleProcessWatcher.cpp 4.8 KB

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