ctkCmdLineModuleExplorerOutputText.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "ctkCmdLineModuleExplorerOutputText.h"
  16. #include "ctkCmdLineModuleFrontend.h"
  17. #include "ctkCmdLineModuleFuture.h"
  18. #include "ctkCmdLineModuleFutureWatcher.h"
  19. ctkCmdLineModuleExplorerOutputText::ctkCmdLineModuleExplorerOutputText(QWidget* parent)
  20. : QTextEdit(parent)
  21. , CurrentWatcher(NULL)
  22. , CurrentFrontend(NULL)
  23. {
  24. }
  25. ctkCmdLineModuleExplorerOutputText::~ctkCmdLineModuleExplorerOutputText()
  26. {
  27. qDeleteAll(this->FrontendToWatcherMap);
  28. }
  29. void ctkCmdLineModuleExplorerOutputText::setActiveFrontend(ctkCmdLineModuleFrontend* moduleFrontend)
  30. {
  31. if (this->CurrentFrontend == moduleFrontend) return;
  32. if (this->CurrentFrontend)
  33. {
  34. if (this->CurrentWatcher)
  35. {
  36. this->CurrentWatcher->disconnect();
  37. }
  38. this->CurrentFrontend->disconnect();
  39. // save the current output text
  40. this->FrontendToOutputMap[this->CurrentFrontend] = this->toHtml();
  41. this->clear();
  42. }
  43. this->CurrentFrontend = moduleFrontend;
  44. if (moduleFrontend)
  45. {
  46. // restore previous content
  47. this->setHtml(this->FrontendToOutputMap[moduleFrontend]);
  48. QTextCursor endCursor = this->textCursor();
  49. endCursor.movePosition(QTextCursor::End);
  50. this->setTextCursor(endCursor);
  51. this->CurrentWatcher = FrontendToWatcherMap[moduleFrontend];
  52. if (this->CurrentWatcher == NULL)
  53. {
  54. this->CurrentWatcher = new ctkCmdLineModuleFutureWatcher;
  55. this->FrontendToWatcherMap[moduleFrontend] = this->CurrentWatcher;
  56. }
  57. connect(this->CurrentFrontend, SIGNAL(started()), SLOT(frontendStarted()));
  58. connect(this->CurrentWatcher, SIGNAL(outputDataReady()), SLOT(outputDataReady()));
  59. connect(this->CurrentWatcher, SIGNAL(errorDataReady()), SLOT(errorDataReady()));
  60. this->CurrentWatcher->setFuture(moduleFrontend->future());
  61. // if the frontend is already finished get any output we have not yet fetched
  62. if (moduleFrontend->future().isFinished())
  63. {
  64. this->outputDataReady();
  65. this->errorDataReady();
  66. }
  67. }
  68. else
  69. {
  70. if (this->CurrentWatcher)
  71. {
  72. this->CurrentWatcher->disconnect();
  73. this->CurrentWatcher = NULL;
  74. }
  75. if (this->CurrentFrontend)
  76. {
  77. this->CurrentFrontend->disconnect();
  78. this->CurrentFrontend = NULL;
  79. }
  80. this->clear();
  81. }
  82. }
  83. void ctkCmdLineModuleExplorerOutputText::frontendRemoved(ctkCmdLineModuleFrontend *frontend)
  84. {
  85. delete this->FrontendToWatcherMap[frontend];
  86. this->FrontendToWatcherMap.remove(frontend);
  87. this->FrontendToOutputMap.remove(frontend);
  88. }
  89. void ctkCmdLineModuleExplorerOutputText::frontendStarted()
  90. {
  91. this->clear();
  92. this->FrontendToOutputMap[this->CurrentFrontend].clear();
  93. this->CurrentWatcher->setFuture(this->CurrentFrontend->future());
  94. }
  95. void ctkCmdLineModuleExplorerOutputText::outputDataReady()
  96. {
  97. QByteArray newOutput = this->CurrentWatcher->readPendingOutputData();
  98. this->setTextColor(QColor(Qt::black));
  99. this->insertPlainText(newOutput.data());
  100. }
  101. void ctkCmdLineModuleExplorerOutputText::errorDataReady()
  102. {
  103. QByteArray newOutput = this->CurrentWatcher->readPendingErrorData();
  104. this->setTextColor(QColor(Qt::darkRed));
  105. this->insertPlainText(newOutput.data());
  106. }