ctkWorkflowStep.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QMetaType>
  17. #include <QObject>
  18. #include <QState>
  19. // CTK includes
  20. #include "ctkWorkflowStep.h"
  21. #include "ctkWorkflowStep_p.h"
  22. #include "ctkWorkflow.h"
  23. #include "ctkLogger.h"
  24. // STD includes
  25. #include <iostream>
  26. //--------------------------------------------------------------------------
  27. static ctkLogger logger("org.commontk.core.ctkWorkflowStep");
  28. //--------------------------------------------------------------------------
  29. // --------------------------------------------------------------------------
  30. // ctkWorkflowStepPrivate methods
  31. // --------------------------------------------------------------------------
  32. ctkWorkflowStepPrivate::ctkWorkflowStepPrivate(ctkWorkflowStep& object)
  33. :q_ptr(&object)
  34. {
  35. qRegisterMetaType<ctkWorkflowStep*>("ctkWorkflowStep*");
  36. this->Workflow = 0;
  37. this->WidgetType = false;
  38. this->HasValidateCommand = false;
  39. this->HasOnEntryCommand = false;
  40. this->HasOnExitCommand = false;
  41. // Create state
  42. this->ProcessingState = new QState();
  43. this->ValidationState = new QState();
  44. // Create 'validation' transition
  45. this->ValidationTransition =
  46. new ctkWorkflowIntrastepTransition(ctkWorkflowIntrastepTransition::ValidationTransition);
  47. this->ValidationTransition->setTargetState(this->ValidationState);
  48. this->ProcessingState->addTransition(this->ValidationTransition);
  49. // Create 'failed validation' transation
  50. this->ValidationFailedTransition = 0;
  51. this->ValidationFailedTransition =
  52. new ctkWorkflowIntrastepTransition(ctkWorkflowIntrastepTransition::ValidationFailedTransition);
  53. this->ValidationFailedTransition->setTargetState(this->ProcessingState);
  54. this->ValidationState->addTransition(this->ValidationFailedTransition);
  55. }
  56. // --------------------------------------------------------------------------
  57. ctkWorkflowStepPrivate::~ctkWorkflowStepPrivate()
  58. {
  59. if (!this->ValidationState.isNull())
  60. {
  61. delete this->ValidationState;
  62. }
  63. if (!this->ProcessingState.isNull())
  64. {
  65. delete this->ProcessingState;
  66. }
  67. // If we delete the states, then Qt will handle deleting the transitions
  68. }
  69. // --------------------------------------------------------------------------
  70. void ctkWorkflowStepPrivate::validationCompleteInternal(bool validationResults, const QString& branchId)const
  71. {
  72. emit validationComplete(validationResults, branchId);
  73. }
  74. // --------------------------------------------------------------------------
  75. void ctkWorkflowStepPrivate::onEntryCompleteInternal()const
  76. {
  77. emit onEntryComplete();
  78. }
  79. // --------------------------------------------------------------------------
  80. void ctkWorkflowStepPrivate::onExitCompleteInternal()const
  81. {
  82. emit onExitComplete();
  83. }
  84. // --------------------------------------------------------------------------
  85. void ctkWorkflowStepPrivate::invokeValidateCommandInternal(const QString& desiredBranchId)const
  86. {
  87. emit invokeValidateCommand(desiredBranchId);
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkWorkflowStepPrivate::invokeOnEntryCommandInternal(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  91. {
  92. emit invokeOnEntryCommand(comingFrom, transitionType);
  93. }
  94. // --------------------------------------------------------------------------
  95. void ctkWorkflowStepPrivate::invokeOnExitCommandInternal(const ctkWorkflowStep* goingTo, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  96. {
  97. emit invokeOnExitCommand(goingTo, transitionType);
  98. }
  99. // --------------------------------------------------------------------------
  100. // ctkWorkflowStep methods
  101. // --------------------------------------------------------------------------
  102. ctkWorkflowStep::ctkWorkflowStep(): d_ptr(new ctkWorkflowStepPrivate(*this))
  103. {
  104. }
  105. // --------------------------------------------------------------------------
  106. ctkWorkflowStep::ctkWorkflowStep(const QString& newId)
  107. : d_ptr(new ctkWorkflowStepPrivate(*this))
  108. {
  109. Q_D(ctkWorkflowStep);
  110. d->Id = newId;
  111. }
  112. // --------------------------------------------------------------------------
  113. ctkWorkflowStep::ctkWorkflowStep(ctkWorkflowStepPrivate * pimpl,
  114. const QString& newId):d_ptr(pimpl)
  115. {
  116. Q_D(ctkWorkflowStep);
  117. d->Id = newId;
  118. }
  119. // --------------------------------------------------------------------------
  120. ctkWorkflowStep::~ctkWorkflowStep()
  121. {
  122. }
  123. // --------------------------------------------------------------------------
  124. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflow*, workflow, Workflow);
  125. CTK_SET_CPP(ctkWorkflowStep, ctkWorkflow*, setWorkflow, Workflow);
  126. // --------------------------------------------------------------------------
  127. CTK_GET_CPP(ctkWorkflowStep, QString, id, Id);
  128. // --------------------------------------------------------------------------
  129. void ctkWorkflowStep::setId(const QString& newId)
  130. {
  131. Q_D(ctkWorkflowStep);
  132. if (d->Workflow && d->Workflow->hasStep(newId) && !this->id().isEmpty())
  133. {
  134. logger.error(QString("ctkWorkflowStep - Failed to change id from '%1' to '%2' - "
  135. "Step already added to a workflow !").arg(this->id()).arg(newId));
  136. return;
  137. }
  138. d->Id = newId;
  139. }
  140. // --------------------------------------------------------------------------
  141. CTK_GET_CPP(ctkWorkflowStep, QString, name, Name);
  142. CTK_SET_CPP(ctkWorkflowStep, const QString&, setName, Name);
  143. // --------------------------------------------------------------------------
  144. CTK_GET_CPP(ctkWorkflowStep, QString, description, Description);
  145. CTK_SET_CPP(ctkWorkflowStep, const QString&, setDescription, Description);
  146. // --------------------------------------------------------------------------
  147. CTK_GET_CPP(ctkWorkflowStep, QString, statusText, StatusText);
  148. CTK_SET_CPP(ctkWorkflowStep, const QString&, setStatusText, StatusText);
  149. // --------------------------------------------------------------------------
  150. CTK_GET_CPP(ctkWorkflowStep, bool, hasValidateCommand, HasValidateCommand);
  151. CTK_SET_CPP(ctkWorkflowStep, bool, setHasValidateCommand, HasValidateCommand);
  152. // --------------------------------------------------------------------------
  153. CTK_GET_CPP(ctkWorkflowStep, bool, hasOnEntryCommand, HasOnEntryCommand);
  154. CTK_SET_CPP(ctkWorkflowStep, bool, setHasOnEntryCommand, HasOnEntryCommand);
  155. // --------------------------------------------------------------------------
  156. CTK_GET_CPP(ctkWorkflowStep, bool, hasOnExitCommand, HasOnExitCommand);
  157. CTK_SET_CPP(ctkWorkflowStep, bool, setHasOnExitCommand, HasOnExitCommand);
  158. // --------------------------------------------------------------------------
  159. CTK_GET_CPP(ctkWorkflowStep, QState*, processingState, ProcessingState);
  160. CTK_GET_CPP(ctkWorkflowStep, QState*, validationState, ValidationState);
  161. // --------------------------------------------------------------------------
  162. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflowIntrastepTransition*, validationTransition, ValidationTransition);
  163. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflowIntrastepTransition*,
  164. validationFailedTransition, ValidationFailedTransition);
  165. // --------------------------------------------------------------------------
  166. CTK_GET_CPP(ctkWorkflowStep, bool, isWidgetType, WidgetType);
  167. // --------------------------------------------------------------------------
  168. QObject* ctkWorkflowStep::ctkWorkflowStepQObject()
  169. {
  170. Q_D(ctkWorkflowStep);
  171. return d;
  172. }
  173. // --------------------------------------------------------------------------
  174. void ctkWorkflowStep::validationComplete(bool validationResults, const QString& branchId)const
  175. {
  176. Q_D(const ctkWorkflowStep);
  177. d->validationCompleteInternal(validationResults, branchId);
  178. }
  179. // --------------------------------------------------------------------------
  180. void ctkWorkflowStep::onEntryComplete()const
  181. {
  182. Q_D(const ctkWorkflowStep);
  183. d->onEntryCompleteInternal();
  184. }
  185. // --------------------------------------------------------------------------
  186. void ctkWorkflowStep::onExitComplete()const
  187. {
  188. Q_D(const ctkWorkflowStep);
  189. d->onExitCompleteInternal();
  190. }
  191. // --------------------------------------------------------------------------
  192. void ctkWorkflowStep::invokeValidateCommand(const QString& desiredBranchId)const
  193. {
  194. Q_D(const ctkWorkflowStep);
  195. d->invokeValidateCommandInternal(desiredBranchId);
  196. }
  197. // --------------------------------------------------------------------------
  198. void ctkWorkflowStep::invokeOnEntryCommand(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  199. {
  200. Q_D(const ctkWorkflowStep);
  201. d->invokeOnEntryCommandInternal(comingFrom, transitionType);
  202. }
  203. // --------------------------------------------------------------------------
  204. void ctkWorkflowStep::invokeOnExitCommand(const ctkWorkflowStep* goingTo, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  205. {
  206. Q_D(const ctkWorkflowStep);
  207. d->invokeOnExitCommandInternal(goingTo, transitionType);
  208. }
  209. // --------------------------------------------------------------------------
  210. void ctkWorkflowStep::validate(const QString& desiredBranchId)
  211. {
  212. Q_D(ctkWorkflowStep);
  213. logger.info(QString("validate - validating the input from %1").arg(d->Name));
  214. this->validationComplete(true, desiredBranchId);
  215. }
  216. // --------------------------------------------------------------------------
  217. void ctkWorkflowStep::onEntry(const ctkWorkflowStep* comingFrom,
  218. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  219. {
  220. Q_UNUSED(comingFrom);
  221. Q_UNUSED(transitionType);
  222. // Signals that we are finished
  223. this->onEntryComplete();
  224. }
  225. // --------------------------------------------------------------------------
  226. void ctkWorkflowStep::onExit(const ctkWorkflowStep* goingTo,
  227. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  228. {
  229. Q_UNUSED(goingTo);
  230. Q_UNUSED(transitionType);
  231. // Signals that we are finished
  232. this->onExitComplete();
  233. }