ctkWorkflowTransitions.h 5.6 KB

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