1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __ctkWorkflowTransition_h
22 #define __ctkWorkflowTransition_h
23 
24 // Qt includes
25 #include <QEvent>
26 #include <QAbstractTransition>
27 #include <QString>
28 
29 // CTK includes
30 #include "ctkCoreExport.h"
31 
32 /// \ingroup Core
33 /// \brief Custom transitions for use with ctkWorkflow.
34 ///
35 /// ctkWorkflowIntrastepTransition: for transition between states of the same step.  The transition
36 /// occurs only when the transition event's EventTransitionType matches the transition's
37 /// TransitionType).
38 ///
39 /// ctkWorkflowInterstepTransition: for transition between states of different steps.  The
40 /// transition occurs only when the transition event's EventTransitionType matches the transition's
41 /// TransitionType and the transition event's EventId matches the transition's Id.
42 
43 //-----------------------------------------------------------------------------
44 struct CTK_CORE_EXPORT ctkWorkflowIntrastepTransitionEvent : public QEvent
45 {
46 
47   /// EventTransitionType is the value of a transition event, used to conditionally trigger transitions
ctkWorkflowIntrastepTransitionEventctkWorkflowIntrastepTransitionEvent48   ctkWorkflowIntrastepTransitionEvent(int newTransitionType)
49     : QEvent(QEvent::Type(getWorkflowIntrastepTransitionEventType())),
50       EventTransitionType(newTransitionType){}
51 
52   /// Reserve a custom event type, ensuring that we are not re-using an
53   /// event type that was previously used
getWorkflowIntrastepTransitionEventTypectkWorkflowIntrastepTransitionEvent54   static inline int getWorkflowIntrastepTransitionEventType()
55   {
56     static int workflowIntrastepTransitionEventType = QEvent::registerEventType(QEvent::User+1);
57     return workflowIntrastepTransitionEventType;
58   }
59 
60   int EventTransitionType;
61 };
62 
63 //-----------------------------------------------------------------------------
64 /// \ingroup Core
65 class CTK_CORE_EXPORT ctkWorkflowIntrastepTransition : public QAbstractTransition
66 {
67   Q_OBJECT
68 
69 public:
70 
71   enum IntrastepTransitionType
72   {
73     ValidationTransition = 0,
74     ValidationFailedTransition
75   };
76 
ctkWorkflowIntrastepTransition(IntrastepTransitionType newTransitionType)77   ctkWorkflowIntrastepTransition(IntrastepTransitionType newTransitionType)
78     : TransitionType(newTransitionType){}
79 
transitionType()80   IntrastepTransitionType transitionType() {return this->TransitionType;}
81 
82 protected:
eventTest(QEvent * e)83   virtual bool eventTest(QEvent* e)
84   {
85     // check the event type
86     if (e->type() != ctkWorkflowIntrastepTransitionEvent::getWorkflowIntrastepTransitionEventType())
87       {
88       return false;
89       }
90 
91     // check the event value (i.e. the TransitionType)
92     ctkWorkflowIntrastepTransitionEvent* workflowEvent = static_cast<ctkWorkflowIntrastepTransitionEvent*>(e);
93 
94     return (this->TransitionType == workflowEvent->EventTransitionType);
95   }
96 
onTransition(QEvent *)97   void onTransition(QEvent*){}
98 
99 private:
100   IntrastepTransitionType TransitionType;
101 
102 };
103 
104 //-----------------------------------------------------------------------------
105 /// \ingroup Core
106 struct CTK_CORE_EXPORT ctkWorkflowInterstepTransitionEvent : public QEvent
107 {
108 
109   /// EventTransitionType is the value of a transition event, used to conditionally trigger transitions
ctkWorkflowInterstepTransitionEventctkWorkflowInterstepTransitionEvent110   ctkWorkflowInterstepTransitionEvent(int newTransitionType)
111     : QEvent(QEvent::Type(getWorkflowInterstepTransitionEventType())),
112       EventTransitionType(newTransitionType){}
ctkWorkflowInterstepTransitionEventctkWorkflowInterstepTransitionEvent113   ctkWorkflowInterstepTransitionEvent(int newTransitionType, const QString& newId)
114     : QEvent(QEvent::Type(getWorkflowInterstepTransitionEventType())),
115     EventTransitionType(newTransitionType),
116     EventId(newId){}
117 
118   /// Reserve a custom event type, ensuring that we are not re-using an
119   /// event type that was previously used
getWorkflowInterstepTransitionEventTypectkWorkflowInterstepTransitionEvent120   static inline int getWorkflowInterstepTransitionEventType()
121   {
122     static int workflowInterstepTransitionEventType = QEvent::registerEventType(QEvent::User+1);
123     return workflowInterstepTransitionEventType;
124   }
125 
126   int     EventTransitionType;
127   QString EventId;
128 };
129 
130 //-----------------------------------------------------------------------------
131 /// \ingroup Core
132 class CTK_CORE_EXPORT ctkWorkflowInterstepTransition : public QAbstractTransition
133 {
134   Q_OBJECT
135   Q_ENUMS(InterstepTransitionType)
136 
137 public:
138 
139   enum InterstepTransitionType
140   {
141     TransitionToNextStep = 0,
142     TransitionToPreviousStep,
143     StartingWorkflow,
144     StoppingWorkflow,
145     TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep
146   };
147 
ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType)148   ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType)
149     : TransitionType(newTransitionType){}
ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType,const QString & newId)150   ctkWorkflowInterstepTransition(InterstepTransitionType newTransitionType, const QString& newId)
151     : TransitionType(newTransitionType),
152     Id(newId) {}
153 
transitionType()154   InterstepTransitionType transitionType() {return this->TransitionType;}
id()155   QString& id() {return this->Id;}
156 
157 protected:
eventTest(QEvent * e)158   virtual bool eventTest(QEvent* e)
159   {
160     // check the event type
161     if (e->type() != ctkWorkflowInterstepTransitionEvent::getWorkflowInterstepTransitionEventType())
162       {
163       return false;
164       }
165 
166     // check the event value (i.e. the TransitionType)
167     ctkWorkflowInterstepTransitionEvent* workflowEvent = static_cast<ctkWorkflowInterstepTransitionEvent*>(e);
168 
169     return (this->TransitionType == workflowEvent->EventTransitionType
170             && this->Id == workflowEvent->EventId);
171   }
172 
onTransition(QEvent *)173   void onTransition(QEvent*){}
174 
175 private:
176   InterstepTransitionType TransitionType;
177   QString                 Id;
178 
179 };
180 
181 #endif
182