ctkCmdLineModuleProcessTask.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "ctkCmdLineModuleProcessTask.h"
  16. #include "ctkCmdLineModuleRunException.h"
  17. #include "ctkCmdLineModuleXmlProgressWatcher.h"
  18. #include "ctkCmdLineModuleFuture.h"
  19. #include <QDebug>
  20. #include <QEventLoop>
  21. #include <QThreadPool>
  22. #include <QProcess>
  23. ctkCmdLineModuleProcessProgressWatcher::ctkCmdLineModuleProcessProgressWatcher(QProcess& process, const QString& location,
  24. ctkCmdLineModuleFutureInterface &futureInterface)
  25. : process(process), location(location), futureInterface(futureInterface), processXmlWatcher(&process),
  26. progressValue(0)
  27. {
  28. futureInterface.setProgressRange(0, 1000);
  29. connect(&processXmlWatcher, SIGNAL(filterStarted(QString,QString)), SLOT(filterStarted(QString,QString)));
  30. connect(&processXmlWatcher, SIGNAL(filterProgress(float)), SLOT(filterProgress(float)));
  31. connect(&processXmlWatcher, SIGNAL(filterFinished(QString)), SLOT(filterFinished(QString)));
  32. connect(&processXmlWatcher, SIGNAL(filterXmlError(QString)), SLOT(filterXmlError(QString)));
  33. }
  34. void ctkCmdLineModuleProcessProgressWatcher::filterStarted(const QString& name, const QString& comment)
  35. {
  36. Q_UNUSED(comment)
  37. futureInterface.setProgressValueAndText(incrementProgress(), name);
  38. }
  39. void ctkCmdLineModuleProcessProgressWatcher::filterProgress(float progress)
  40. {
  41. futureInterface.setProgressValue(updateProgress(progress));
  42. }
  43. void ctkCmdLineModuleProcessProgressWatcher::filterFinished(const QString& name)
  44. {
  45. futureInterface.setProgressValueAndText(incrementProgress(), "Finished: " + name);
  46. }
  47. void ctkCmdLineModuleProcessProgressWatcher::filterXmlError(const QString &error)
  48. {
  49. qDebug().nospace() << "[Module " << location << "]: " << error;
  50. }
  51. int ctkCmdLineModuleProcessProgressWatcher::updateProgress(float progress)
  52. {
  53. progressValue = static_cast<int>(progress * 1000.0f);
  54. // normalize the value to lie between 0 and 1000.
  55. // 0 is reported when the process starts and 1000 is reserved for
  56. // reporting completion + standard output text
  57. if (progressValue < 1) progressValue = 1;
  58. if (progressValue > 999) progressValue = 999;
  59. return progressValue;
  60. }
  61. int ctkCmdLineModuleProcessProgressWatcher::incrementProgress()
  62. {
  63. if (++progressValue > 999) progressValue = 999;
  64. return progressValue;
  65. }
  66. ctkCmdLineModuleProcessTask::ctkCmdLineModuleProcessTask(const QString& location, const QStringList& args)
  67. : location(location), args(args)
  68. {
  69. this->setCanCancel(true);
  70. }
  71. ctkCmdLineModuleProcessTask::~ctkCmdLineModuleProcessTask()
  72. {
  73. }
  74. ctkCmdLineModuleFuture ctkCmdLineModuleProcessTask::start()
  75. {
  76. this->setRunnable(this);
  77. this->reportStarted();
  78. ctkCmdLineModuleFuture future = this->future();
  79. QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
  80. return future;
  81. }
  82. void ctkCmdLineModuleProcessTask::run()
  83. {
  84. if (this->isCanceled())
  85. {
  86. this->reportFinished();
  87. return;
  88. }
  89. QProcess process;
  90. process.setReadChannel(QProcess::StandardOutput);
  91. QEventLoop localLoop;
  92. QObject::connect(&process, SIGNAL(finished(int)), &localLoop, SLOT(quit()));
  93. QObject::connect(&process, SIGNAL(error(QProcess::ProcessError)), &localLoop, SLOT(quit()));
  94. process.start(location, args);
  95. ctkCmdLineModuleProcessProgressWatcher progressWatcher(process, location, *this);
  96. Q_UNUSED(progressWatcher)
  97. localLoop.exec();
  98. if (process.error() != QProcess::UnknownError)
  99. {
  100. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString()));
  101. }
  102. else if (process.exitCode() != 0)
  103. {
  104. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.readAllStandardError()));
  105. }
  106. this->setProgressValueAndText(1000, process.readAllStandardError());
  107. //this->reportResult(result);
  108. this->reportFinished();
  109. }