ctkWorkflowTransitions.h 5.8 KB

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