ctkCmdLineModuleFutureWatcher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "ctkCmdLineModuleFutureWatcher.h"
  16. #include "ctkCmdLineModuleFuture.h"
  17. #include "ctkCmdLineModuleFutureInterface_p.h"
  18. #include <QThread>
  19. #include <QCoreApplication>
  20. //----------------------------------------------------------------------------
  21. struct ctkCmdLineModuleFutureWatcherPrivate : public ctkCmdLineModuleFutureCallOutInterface
  22. {
  23. ctkCmdLineModuleFutureWatcherPrivate(ctkCmdLineModuleFutureWatcher* q)
  24. : q(q)
  25. , pendingOutputReadyEvent(NULL)
  26. , pendingErrorReadyEvent(NULL)
  27. , outputPos(0)
  28. , errorPos(0)
  29. {}
  30. void connectOutputInterface()
  31. {
  32. q->futureInterface().d->connectOutputInterface(this);
  33. }
  34. void disconnectOutputInterface(bool pendingAssignment = false)
  35. {
  36. if (pendingAssignment)
  37. {
  38. delete this->pendingOutputReadyEvent;
  39. this->pendingOutputReadyEvent = NULL;
  40. delete this->pendingErrorReadyEvent;
  41. this->pendingErrorReadyEvent = NULL;
  42. }
  43. q->futureInterface().d->disconnectOutputInterface(this);
  44. }
  45. void postCmdLineModuleCallOutEvent(const ctkCmdLineModuleFutureCallOutEvent& callOutEvent)
  46. {
  47. QCoreApplication::postEvent(q, callOutEvent.clone());
  48. }
  49. void cmdLineModuleCallOutInterfaceDisconnected()
  50. {
  51. QCoreApplication::removePostedEvents(q, ctkCmdLineModuleFutureCallOutEvent::TypeId);
  52. }
  53. void sendCmdLineModuleCallOutEvent(ctkCmdLineModuleFutureCallOutEvent* event)
  54. {
  55. if (q->futureInterface().isCanceled()) return;
  56. switch (event->callOutType)
  57. {
  58. case ctkCmdLineModuleFutureCallOutEvent::OutputReady:
  59. emit q->outputDataReady();
  60. break;
  61. case ctkCmdLineModuleFutureCallOutEvent::ErrorReady:
  62. emit q->errorDataReady();
  63. break;
  64. default: break;
  65. }
  66. }
  67. ctkCmdLineModuleFutureWatcher* q;
  68. ctkCmdLineModuleFuture Future;
  69. ctkCmdLineModuleFutureCallOutEvent* pendingOutputReadyEvent;
  70. ctkCmdLineModuleFutureCallOutEvent* pendingErrorReadyEvent;
  71. int outputPos;
  72. int errorPos;
  73. };
  74. //----------------------------------------------------------------------------
  75. ctkCmdLineModuleFutureWatcher::ctkCmdLineModuleFutureWatcher(QObject* parent)
  76. : QFutureWatcher<ctkCmdLineModuleResult>(parent)
  77. , d(new ctkCmdLineModuleFutureWatcherPrivate(this))
  78. {
  79. }
  80. //----------------------------------------------------------------------------
  81. ctkCmdLineModuleFutureWatcher::~ctkCmdLineModuleFutureWatcher()
  82. {
  83. disconnectOutputInterface();
  84. }
  85. //----------------------------------------------------------------------------
  86. void ctkCmdLineModuleFutureWatcher::setFuture(const ctkCmdLineModuleFuture& future)
  87. {
  88. if (d->Future == future) return;
  89. d->outputPos = 0;
  90. d->errorPos = 0;
  91. d->disconnectOutputInterface(true);
  92. d->Future = future;
  93. QFutureWatcher<ctkCmdLineModuleResult>::setFuture(future);
  94. d->connectOutputInterface();
  95. }
  96. //----------------------------------------------------------------------------
  97. ctkCmdLineModuleFuture ctkCmdLineModuleFutureWatcher::future() const
  98. {
  99. return d->Future;
  100. }
  101. //----------------------------------------------------------------------------
  102. bool ctkCmdLineModuleFutureWatcher::event(QEvent *event)
  103. {
  104. if (event->type() == ctkCmdLineModuleFutureCallOutEvent::TypeId)
  105. {
  106. ctkCmdLineModuleFutureCallOutEvent* callOutEvent = static_cast<ctkCmdLineModuleFutureCallOutEvent*>(event);
  107. if (futureInterface().isPaused())
  108. {
  109. if (callOutEvent->callOutType == ctkCmdLineModuleFutureCallOutEvent::OutputReady &&
  110. d->pendingOutputReadyEvent == NULL)
  111. {
  112. d->pendingOutputReadyEvent = callOutEvent->clone();
  113. }
  114. if (callOutEvent->callOutType == ctkCmdLineModuleFutureCallOutEvent::ErrorReady &&
  115. d->pendingErrorReadyEvent == NULL)
  116. {
  117. d->pendingErrorReadyEvent = callOutEvent->clone();
  118. }
  119. return true;
  120. }
  121. d->sendCmdLineModuleCallOutEvent(callOutEvent);
  122. return true;
  123. }
  124. else if (event->type() == QEvent::FutureCallOut)
  125. {
  126. bool result = QFutureWatcher<ctkCmdLineModuleResult>::event(event);
  127. if (futureInterface().isRunning())
  128. {
  129. // send all pending call outs
  130. if (d->pendingOutputReadyEvent)
  131. {
  132. d->sendCmdLineModuleCallOutEvent(d->pendingOutputReadyEvent);
  133. delete d->pendingOutputReadyEvent;
  134. d->pendingOutputReadyEvent = NULL;
  135. }
  136. if (d->pendingErrorReadyEvent)
  137. {
  138. d->sendCmdLineModuleCallOutEvent(d->pendingErrorReadyEvent);
  139. delete d->pendingErrorReadyEvent;
  140. d->pendingErrorReadyEvent = NULL;
  141. }
  142. }
  143. return result;
  144. }
  145. return QFutureWatcher<ctkCmdLineModuleResult>::event(event);
  146. }
  147. //----------------------------------------------------------------------------
  148. QByteArray ctkCmdLineModuleFutureWatcher::readPendingOutputData() const
  149. {
  150. QByteArray output = futureInterface().outputData(d->outputPos);
  151. d->outputPos += output.size();
  152. return output;
  153. }
  154. //----------------------------------------------------------------------------
  155. QByteArray ctkCmdLineModuleFutureWatcher::readPendingErrorData() const
  156. {
  157. QByteArray errorOutput = futureInterface().errorData(d->errorPos);
  158. d->errorPos += errorOutput.size();
  159. return errorOutput;
  160. }
  161. //----------------------------------------------------------------------------
  162. QByteArray ctkCmdLineModuleFutureWatcher::readAllOutputData() const
  163. {
  164. return d->Future.readAllOutputData();
  165. }
  166. //----------------------------------------------------------------------------
  167. QByteArray ctkCmdLineModuleFutureWatcher::readAllErrorData() const
  168. {
  169. return d->Future.readAllErrorData();
  170. }
  171. //----------------------------------------------------------------------------
  172. const ctkCmdLineModuleFutureInterface& ctkCmdLineModuleFutureWatcher::futureInterface() const
  173. {
  174. return d->Future.d;
  175. }
  176. //----------------------------------------------------------------------------
  177. ctkCmdLineModuleFutureInterface& ctkCmdLineModuleFutureWatcher::futureInterface()
  178. {
  179. return d->Future.d;
  180. }