ctkWorkflowTransitions.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 __ctkWorkflowTransition_h
  15. #define __ctkWorkflowTransition_h
  16. // Qt includes
  17. #include <QEvent>
  18. #include <QAbstractTransition>
  19. #include <QString>
  20. // CTK includes
  21. #include "CTKCoreExport.h"
  22. /// \brief Custom transitions for use with ctkWorkflow.
  23. ///
  24. /// ctkWorkflowIntrastepTransition: for transition between states of the same step. The transition
  25. /// occurs only when the transition event's EventTransitionType matches the transition's
  26. /// TransitionType).
  27. ///
  28. /// ctkWorkflowInterstepTransition: for transition between states of different steps (i.e. when
  29. /// transitioning between steps). The transition occurs only when the transition event's
  30. /// EventTransitionType matches the transition's TransitionType, and when the transition event's
  31. /// EventId matches the transition's Id.
  32. ///
  33. /// Example:
  34. /// \code
  35. /// ctkWorkflowInterstepTransition *transition = new
  36. /// ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToNextStep, "branchId");
  37. ///
  38. /// workflow.postEvent(new
  39. /// ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToNextStep, "branchId")
  40. /// \endcode
  41. //-----------------------------------------------------------------------------
  42. struct CTK_CORE_EXPORT ctkWorkflowIntrastepTransitionEvent : public QEvent
  43. {
  44. /// EventTransitionType is the value of a transition event, used to conditionally trigger transitions
  45. ctkWorkflowIntrastepTransitionEvent(int newTransitionType)
  46. : QEvent(QEvent::Type(getWorkflowIntrastepTransitionEventType())),
  47. EventTransitionType(newTransitionType){}
  48. /// Reserve a custom event type, ensuring that we are not re-using an
  49. /// event type that was previously used
  50. static inline int getWorkflowIntrastepTransitionEventType()
  51. {
  52. static int workflowIntrastepTransitionEventType = QEvent::registerEventType(QEvent::User+1);
  53. return workflowIntrastepTransitionEventType;
  54. }
  55. int EventTransitionType;
  56. };
  57. //-----------------------------------------------------------------------------
  58. class CTK_CORE_EXPORT ctkWorkflowIntrastepTransition : public QAbstractTransition
  59. {
  60. Q_OBJECT
  61. public:
  62. enum IntrastepTransitionType
  63. {
  64. ValidationTransition = 0,
  65. ValidationFailedTransition
  66. };
  67. ctkWorkflowIntrastepTransition(IntrastepTransitionType newTransitionType)
  68. : TransitionType(newTransitionType){}
  69. IntrastepTransitionType transitionType() {return this->TransitionType;}
  70. protected:
  71. virtual bool eventTest(QEvent* e)
  72. {
  73. // check the event type
  74. if (e->type() != ctkWorkflowIntrastepTransitionEvent::getWorkflowIntrastepTransitionEventType())
  75. {
  76. return false;
  77. }
  78. // check the event value (i.e. the TransitionType)
  79. ctkWorkflowIntrastepTransitionEvent* workflowEvent = static_cast<ctkWorkflowIntrastepTransitionEvent*>(e);
  80. return (this->TransitionType == workflowEvent->EventTransitionType);
  81. }
  82. void onTransition(QEvent*){}
  83. private:
  84. IntrastepTransitionType TransitionType;
  85. };
  86. //-----------------------------------------------------------------------------
  87. struct CTK_CORE_EXPORT ctkWorkflowInterstepTransitionEvent : public QEvent
  88. {
  89. /// EventTransitionType is the value of a transition event, used to conditionally trigger transitions
  90. ctkWorkflowInterstepTransitionEvent(int newTransitionType)
  91. : QEvent(QEvent::Type(getWorkflowInterstepTransitionEventType())),
  92. EventTransitionType(newTransitionType){}
  93. ctkWorkflowInterstepTransitionEvent(int newTransitionType, const QString& newId)
  94. : QEvent(QEvent::Type(getWorkflowInterstepTransitionEventType())),
  95. EventTransitionType(newTransitionType),
  96. EventId(newId){}
  97. /// Reserve a custom event type, ensuring that we are not re-using an
  98. /// event type that was previously used
  99. static inline int getWorkflowInterstepTransitionEventType()
  100. {
  101. static int workflowInterstepTransitionEventType = QEvent::registerEventType(QEvent::User+1);
  102. return workflowInterstepTransitionEventType;
  103. }
  104. int EventTransitionType;
  105. QString EventId;
  106. };
  107. //-----------------------------------------------------------------------------
  108. class CTK_CORE_EXPORT ctkWorkflowInterstepTransition : public QAbstractTransition
  109. {
  110. Q_OBJECT
  111. public:
  112. enum InterstepTransitionType
  113. {
  114. TransitionToNextStep = 0,
  115. TransitionToPreviousStep,
  116. StartingWorkflow,
  117. StoppingWorkflow,
  118. TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep
  119. };
  120. ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType)
  121. : TransitionType(newTransitionType){}
  122. ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType, const QString& newId)
  123. : TransitionType(newTransitionType),
  124. Id(newId) {}
  125. InterstepTransitionType transitionType() {return this->TransitionType;}
  126. QString& id() {return this->Id;}
  127. protected:
  128. virtual bool eventTest(QEvent* e)
  129. {
  130. // check the event type
  131. if (e->type() != ctkWorkflowInterstepTransitionEvent::getWorkflowInterstepTransitionEventType())
  132. {
  133. return false;
  134. }
  135. // check the event value (i.e. the TransitionType)
  136. ctkWorkflowInterstepTransitionEvent* workflowEvent = static_cast<ctkWorkflowInterstepTransitionEvent*>(e);
  137. return (this->TransitionType == workflowEvent->EventTransitionType
  138. && this->Id == workflowEvent->EventId);
  139. }
  140. void onTransition(QEvent*){}
  141. private:
  142. InterstepTransitionType TransitionType;
  143. QString Id;
  144. };
  145. #endif