ctkWorkflowStep.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // STD includes
  24. #include <iostream>
  25. // --------------------------------------------------------------------------
  26. // ctkWorkflowStepPrivate methods
  27. // --------------------------------------------------------------------------
  28. ctkWorkflowStepPrivate::ctkWorkflowStepPrivate(ctkWorkflowStep& object)
  29. :q_ptr(&object)
  30. {
  31. qRegisterMetaType<ctkWorkflowStep*>("ctkWorkflowStep*");
  32. this->Workflow = 0;
  33. this->WidgetType = false;
  34. this->HasValidateCommand = false;
  35. this->HasOnEntryCommand = false;
  36. this->HasOnExitCommand = false;
  37. // Create state
  38. this->ProcessingState = new QState();
  39. this->ValidationState = new QState();
  40. // Create 'validation' transition
  41. this->ValidationTransition =
  42. new ctkWorkflowIntrastepTransition(ctkWorkflowIntrastepTransition::ValidationTransition);
  43. this->ValidationTransition->setTargetState(this->ValidationState);
  44. this->ProcessingState->addTransition(this->ValidationTransition);
  45. // Create 'failed validation' transation
  46. this->ValidationFailedTransition = 0;
  47. this->ValidationFailedTransition =
  48. new ctkWorkflowIntrastepTransition(ctkWorkflowIntrastepTransition::ValidationFailedTransition);
  49. this->ValidationFailedTransition->setTargetState(this->ProcessingState);
  50. this->ValidationState->addTransition(this->ValidationFailedTransition);
  51. }
  52. // --------------------------------------------------------------------------
  53. ctkWorkflowStepPrivate::~ctkWorkflowStepPrivate()
  54. {
  55. if (!this->ValidationState.isNull())
  56. {
  57. delete this->ValidationState;
  58. }
  59. if (!this->ProcessingState.isNull())
  60. {
  61. delete this->ProcessingState;
  62. }
  63. // If we delete the states, then Qt will handle deleting the transitions
  64. }
  65. // --------------------------------------------------------------------------
  66. void ctkWorkflowStepPrivate::validationCompleteInternal(bool validationResults, const QString& branchId)const
  67. {
  68. emit validationComplete(validationResults, branchId);
  69. }
  70. // --------------------------------------------------------------------------
  71. void ctkWorkflowStepPrivate::onEntryCompleteInternal()const
  72. {
  73. emit onEntryComplete();
  74. }
  75. // --------------------------------------------------------------------------
  76. void ctkWorkflowStepPrivate::onExitCompleteInternal()const
  77. {
  78. emit onExitComplete();
  79. }
  80. // --------------------------------------------------------------------------
  81. void ctkWorkflowStepPrivate::invokeValidateCommandInternal(const QString& desiredBranchId)const
  82. {
  83. emit invokeValidateCommand(desiredBranchId);
  84. }
  85. // --------------------------------------------------------------------------
  86. void ctkWorkflowStepPrivate::invokeOnEntryCommandInternal(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  87. {
  88. emit invokeOnEntryCommand(comingFrom, transitionType);
  89. }
  90. // --------------------------------------------------------------------------
  91. void ctkWorkflowStepPrivate::invokeOnExitCommandInternal(const ctkWorkflowStep* goingTo, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  92. {
  93. emit invokeOnExitCommand(goingTo, transitionType);
  94. }
  95. // --------------------------------------------------------------------------
  96. // ctkWorkflowStep methods
  97. // --------------------------------------------------------------------------
  98. ctkWorkflowStep::ctkWorkflowStep(): d_ptr(new ctkWorkflowStepPrivate(*this))
  99. {
  100. }
  101. // --------------------------------------------------------------------------
  102. ctkWorkflowStep::ctkWorkflowStep(const QString& newId)
  103. : d_ptr(new ctkWorkflowStepPrivate(*this))
  104. {
  105. Q_D(ctkWorkflowStep);
  106. d->Id = newId;
  107. }
  108. // --------------------------------------------------------------------------
  109. ctkWorkflowStep::ctkWorkflowStep(ctkWorkflowStepPrivate * pimpl,
  110. const QString& newId):d_ptr(pimpl)
  111. {
  112. Q_D(ctkWorkflowStep);
  113. d->Id = newId;
  114. }
  115. // --------------------------------------------------------------------------
  116. ctkWorkflowStep::~ctkWorkflowStep()
  117. {
  118. }
  119. // --------------------------------------------------------------------------
  120. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflow*, workflow, Workflow);
  121. CTK_SET_CPP(ctkWorkflowStep, ctkWorkflow*, setWorkflow, Workflow);
  122. // --------------------------------------------------------------------------
  123. CTK_GET_CPP(ctkWorkflowStep, QString, id, Id);
  124. // --------------------------------------------------------------------------
  125. void ctkWorkflowStep::setId(const QString& newId)
  126. {
  127. Q_D(ctkWorkflowStep);
  128. if (d->Workflow && d->Workflow->hasStep(newId) && !this->id().isEmpty())
  129. {
  130. qWarning() << QString("ctkWorkflowStep - Failed to change id from '%1' to '%2' - "
  131. "Step already added to a workflow !").arg(this->id()).arg(newId);
  132. return;
  133. }
  134. d->Id = newId;
  135. }
  136. // --------------------------------------------------------------------------
  137. CTK_GET_CPP(ctkWorkflowStep, QString, name, Name);
  138. CTK_SET_CPP(ctkWorkflowStep, const QString&, setName, Name);
  139. // --------------------------------------------------------------------------
  140. CTK_GET_CPP(ctkWorkflowStep, QString, description, Description);
  141. CTK_SET_CPP(ctkWorkflowStep, const QString&, setDescription, Description);
  142. // --------------------------------------------------------------------------
  143. CTK_GET_CPP(ctkWorkflowStep, QString, statusText, StatusText);
  144. CTK_SET_CPP(ctkWorkflowStep, const QString&, setStatusText, StatusText);
  145. // --------------------------------------------------------------------------
  146. CTK_GET_CPP(ctkWorkflowStep, bool, hasValidateCommand, HasValidateCommand);
  147. CTK_SET_CPP(ctkWorkflowStep, bool, setHasValidateCommand, HasValidateCommand);
  148. // --------------------------------------------------------------------------
  149. CTK_GET_CPP(ctkWorkflowStep, bool, hasOnEntryCommand, HasOnEntryCommand);
  150. CTK_SET_CPP(ctkWorkflowStep, bool, setHasOnEntryCommand, HasOnEntryCommand);
  151. // --------------------------------------------------------------------------
  152. CTK_GET_CPP(ctkWorkflowStep, bool, hasOnExitCommand, HasOnExitCommand);
  153. CTK_SET_CPP(ctkWorkflowStep, bool, setHasOnExitCommand, HasOnExitCommand);
  154. // --------------------------------------------------------------------------
  155. CTK_GET_CPP(ctkWorkflowStep, QState*, processingState, ProcessingState);
  156. CTK_GET_CPP(ctkWorkflowStep, QState*, validationState, ValidationState);
  157. // --------------------------------------------------------------------------
  158. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflowIntrastepTransition*, validationTransition, ValidationTransition);
  159. CTK_GET_CPP(ctkWorkflowStep, ctkWorkflowIntrastepTransition*,
  160. validationFailedTransition, ValidationFailedTransition);
  161. // --------------------------------------------------------------------------
  162. CTK_GET_CPP(ctkWorkflowStep, bool, isWidgetType, WidgetType);
  163. // --------------------------------------------------------------------------
  164. QObject* ctkWorkflowStep::ctkWorkflowStepQObject()
  165. {
  166. Q_D(ctkWorkflowStep);
  167. return d;
  168. }
  169. // --------------------------------------------------------------------------
  170. void ctkWorkflowStep::validationComplete(bool validationResults, const QString& branchId)const
  171. {
  172. Q_D(const ctkWorkflowStep);
  173. d->validationCompleteInternal(validationResults, branchId);
  174. }
  175. // --------------------------------------------------------------------------
  176. void ctkWorkflowStep::onEntryComplete()const
  177. {
  178. Q_D(const ctkWorkflowStep);
  179. d->onEntryCompleteInternal();
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkWorkflowStep::onExitComplete()const
  183. {
  184. Q_D(const ctkWorkflowStep);
  185. d->onExitCompleteInternal();
  186. }
  187. // --------------------------------------------------------------------------
  188. void ctkWorkflowStep::invokeValidateCommand(const QString& desiredBranchId)const
  189. {
  190. Q_D(const ctkWorkflowStep);
  191. d->invokeValidateCommandInternal(desiredBranchId);
  192. }
  193. // --------------------------------------------------------------------------
  194. void ctkWorkflowStep::invokeOnEntryCommand(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  195. {
  196. Q_D(const ctkWorkflowStep);
  197. d->invokeOnEntryCommandInternal(comingFrom, transitionType);
  198. }
  199. // --------------------------------------------------------------------------
  200. void ctkWorkflowStep::invokeOnExitCommand(const ctkWorkflowStep* goingTo, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const
  201. {
  202. Q_D(const ctkWorkflowStep);
  203. d->invokeOnExitCommandInternal(goingTo, transitionType);
  204. }
  205. // --------------------------------------------------------------------------
  206. void ctkWorkflowStep::validate(const QString& desiredBranchId)
  207. {
  208. Q_D(ctkWorkflowStep);
  209. if (this->workflow()->verbose())
  210. {
  211. qDebug() << QString("validate - validating the input from %1").arg(d->Name);
  212. }
  213. this->validationComplete(true, desiredBranchId);
  214. }
  215. // --------------------------------------------------------------------------
  216. void ctkWorkflowStep::onEntry(const ctkWorkflowStep* comingFrom,
  217. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  218. {
  219. Q_UNUSED(comingFrom);
  220. Q_UNUSED(transitionType);
  221. // Signals that we are finished
  222. this->onEntryComplete();
  223. }
  224. // --------------------------------------------------------------------------
  225. void ctkWorkflowStep::onExit(const ctkWorkflowStep* goingTo,
  226. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  227. {
  228. Q_UNUSED(goingTo);
  229. Q_UNUSED(transitionType);
  230. // Signals that we are finished
  231. this->onExitComplete();
  232. }