ctkWorkflowStep.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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. #ifndef __ctkWorkflowStep_h
  15. #define __ctkWorkflowStep_h
  16. // Qt includes
  17. #include <QObject>
  18. // CTK includes
  19. #include "ctkPimpl.h"
  20. #include "CTKCoreExport.h"
  21. #include "ctkWorkflowTransitions.h"
  22. class ctkWorkflow;
  23. class ctkWorkflowStepPrivate;
  24. class QState;
  25. /// \brief ctkWorkflowStep is the basis for a workflow step.
  26. ///
  27. /// A workflow step is a placeholder for various states and transitions that are used in a
  28. /// typical workflow. Such steps can be added to instances of the ctkWorkflow class.
  29. class CTK_CORE_EXPORT ctkWorkflowStep : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. typedef QObject Superclass;
  34. explicit ctkWorkflowStep(ctkWorkflow* newWorkflow, const QString& newId = QString());
  35. virtual ~ctkWorkflowStep(){}
  36. /// Get the \a workflow associated with this step
  37. ctkWorkflow* workflow()const;
  38. /// Get id
  39. QString id()const;
  40. /// Set/get \a name
  41. QString name()const;
  42. void setName(const QString& newName);
  43. /// Set/get \a description
  44. QString description()const;
  45. void setDescription(const QString& newDescription);
  46. ///
  47. /// Set/get \a statusText
  48. QString statusText()const;
  49. void setStatusText(const QString& newText);
  50. ///
  51. /// Set/get whether a validationCommand has been provided in a separate QObject
  52. /// \note see method 2 described for validation()
  53. /// \sa validation()
  54. bool hasValidateCommand()const;
  55. void setHasValidateCommand(bool newHasValidateCommand);
  56. /// Set/get whether an onEntryCommand has been provided in a separate QObject
  57. /// \note See method2 in onEntry()
  58. /// \sa onEntry()
  59. bool hasOnEntryCommand()const;
  60. void setHasOnEntryCommand(bool newHasOnEntryCommand);
  61. ///
  62. /// Set/get whether an onExitCommand has been provided in a separate QObject
  63. /// \note See method2 in onExit()
  64. /// \sa onExit()
  65. bool hasOnExitCommand()const;
  66. void setHasOnExitCommand(bool newHasOnExitCommand);
  67. protected:
  68. /// Set step Id
  69. void setId(const QString& newStepId);
  70. /// \brief Get the step's processing state.
  71. ///
  72. /// This state is used to perform the processing associated with this step.
  73. QState* processingState()const;
  74. /// \brief Get the step's validation state.
  75. ///
  76. /// The validate(const QString&) method is the key component to define for this state to work as expected.
  77. /// This state is used to validate the processing pertaining to this
  78. /// step, then branch to the next step's processingState state on
  79. /// success, or back to the current step's processingState state on
  80. /// error.
  81. ///
  82. /// When the validation state emits its entered() signal, the corresponding workflow's
  83. /// attemptToGoToNextStep() slot is called.
  84. ///
  85. /// This function calls the step's validate(const QString&) method to evaluate
  86. /// whether one can transition to the next step.
  87. QState* validationState()const;
  88. /// \brief Get the step's validation transition.
  89. ///
  90. /// The validation transition is used to bring the state machine
  91. /// from the step's processingState to its validationState.
  92. /// More specifically:
  93. /// <ul>
  94. /// <li>its origin state is the processingState state</li>
  95. /// <li>its destination state is the validationState state</li>
  96. /// </ul>
  97. ///
  98. /// The transition is of type ctkWorkflowTransition with the value
  99. /// ctkWorkflowTransitionType::ValidationTransition.
  100. ctkWorkflowIntrastepTransition* validationTransition()const;
  101. /// \brief Get the step's validation failed transition.
  102. ///
  103. /// The validationFailed transition is used to bring the state
  104. /// machine from the step's validationState state back to its
  105. /// processingState, when validation of the processing step fails
  106. /// (i.e. validate(const QString&) returns false).
  107. ///
  108. /// More specifically:
  109. /// <ul>
  110. /// <li>its origin state is the validatationState state</li>
  111. /// <li>its destination state is the processingState state</li>
  112. /// </ul>
  113. ///
  114. /// The transition is of type ctkWorkflowTransition with the value
  115. /// ctkWorkflowTransitionType::ValidationFailedTransition.
  116. ctkWorkflowIntrastepTransition* validationFailedTransition()const;
  117. ///\brief Reimplement this function for step-specific processing when entering a step.
  118. ///
  119. /// To define a custom step, developers can either reimplement the onEntry() method in a subclass
  120. /// of ctkWorkflowStep, or create a ctkWorkflowStep instance and set the onEntryCommand to point
  121. /// to a callback of their choice.
  122. ///
  123. /// Each step should be self-contained, \a comingFrom and \a transitionType may
  124. /// be used only to decide on how processing should be done for the current step.
  125. ///
  126. /// \param comingFrom gives the step that the state machine was in before
  127. /// transitioning to this step.
  128. ///
  129. /// \param transitionType gives the type of the transition used to get to this step.
  130. virtual void onEntry(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType);
  131. /// \brief Reimplement this function for step-specific processing when exiting a step.
  132. ///
  133. /// To define a custom step, developers can either reimplement the onExit() method
  134. /// in a subclass of ctkWorkflowStep, or create a ctkWorkflowStep instance and set the
  135. /// onExitCommand to point to a callback of their choice.
  136. ///
  137. /// Each step should be self-contained, \a goingTo and \a transitionType may be used only to
  138. /// decide on how processing should be done for the current step.
  139. ///
  140. /// \param goingTo gives the step that the state machine will go to after
  141. /// transitioning from this step.
  142. /// \param transitionType gives the type of the transition used to get to this step.
  143. virtual void onExit(const ctkWorkflowStep* goingTo,
  144. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType);
  145. /// \brief Validates the computation performed in this step's processing state.
  146. ///
  147. /// The validate(const QString&) function is called from the workflow's attemptToGoToNextStep() slot,
  148. /// which is invoked by the validatationState state's entered() signal. It must emit a
  149. /// signal with a single integer parameter. This signal is connected to the workflow's
  150. /// evaluateValidationResults(int) slot, which then performs conditional transition to
  151. /// the next state.
  152. ///
  153. /// When creating a custom step, developers can create a validate(const QString&) method is one of two ways:
  154. /// 1) Reimplement the validate(const QString&) method in a subclass of
  155. /// ctkWorkflowStep, following these instructions:
  156. /// <ul>
  157. /// <li>*do* call the Superclass's validate(const QString&) function, and fail if it returns 0</li>
  158. /// <li>emit the signal ctkWorkflowStep::validateComplete(int) (1 on successful validation,
  159. /// 0 on failure)</li>
  160. /// <li>return an int (1 on successful validation, 0 otherwise)</li>
  161. /// </ul>
  162. //
  163. /// OR:
  164. ///
  165. /// 2) Create an instance of a ctkWorkflowStep then:
  166. /// <ul>
  167. /// <li>Call setHasValidateCommand(1) on the step *before* adding the step to the workflow</li>
  168. /// <li>Create a slot foo() associated with any QObject*, following these instructions:</li>
  169. /// <ul>
  170. /// <li>There is *NO* need to call the Superclass's validate(const QString&) function
  171. /// (error checking is performed before the invokeValidateCommand(const QString&)
  172. /// signal is emitted)</li>
  173. /// <li>Emit a signal bar(int) (1 on successful validation, 0 on
  174. /// failure)</li>
  175. /// <li>Return: 1 on successful validation, 0 otherwise</li>
  176. /// <li>Set the following two connections:</li>
  177. /// <ul>
  178. /// <li>QObject::connect(step, SIGNAL(invokeValidateCommand(const QString&)), object,
  179. /// SLOT(foo()))</li>
  180. /// <li>QObject::connect(object, SIGNAl(bar(int)), workflow,
  181. /// SLOT(evaluateValidationResults(int)))</li>
  182. /// </ul>
  183. /// </ul>
  184. /// </ul>
  185. ///
  186. /// \note ctkWorkflowStep does not function within a workflow as implemented here,
  187. /// one of the above two methods must be followed
  188. virtual void validate(const QString& desiredBranchId = QString());
  189. signals:
  190. /// \brief Signal indicating that validation of this step's processing should be performed.
  191. ///
  192. /// \note Should be used if a validationCommand has been provided
  193. /// See method 2 described in validation()
  194. ///
  195. /// \sa validation()
  196. void invokeValidateCommand(const QString& desiredBranchId = QString())const;
  197. /// \brief Signal indicating that validation of this step's processing has completed.
  198. ///
  199. /// \note Should be used if a validationCommand has not been provided
  200. /// See method 1 described in validation()
  201. ///
  202. /// \param validationSuceeded 1 on successful validation, 0 on failure
  203. void validationComplete(bool validationSuceeded, const QString& branchId = QString())const;
  204. void invokeOnEntryCommand(const ctkWorkflowStep* comingFrom, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const;
  205. void onEntryComplete()const;
  206. void invokeOnExitCommand(const ctkWorkflowStep* goingTo, const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)const;
  207. void onExitComplete()const;
  208. private:
  209. CTK_DECLARE_PRIVATE(ctkWorkflowStep);
  210. friend class ctkWorkflow; // For access to processingState, validationState, setWorkflow, validate
  211. friend class ctkWorkflowPrivate; // For access to invokeOn{Entry,Exit}Command, on{Entry,Exit}
  212. };
  213. #endif