ctkWorkflow.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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. // Qt includes
  15. #include <QStateMachine>
  16. #include <QState>
  17. // CTK includes
  18. #include "ctkWorkflow.h"
  19. #include "ctkWorkflowStep.h"
  20. #include "ctkWorkflowStep_p.h"
  21. #include "ctkWorkflowTransitions.h"
  22. #include "ctkLogger.h"
  23. // STD includes
  24. #include <iostream>
  25. //--------------------------------------------------------------------------
  26. static ctkLogger logger("org.commontk.libs.core.ctkWorkflow");
  27. //--------------------------------------------------------------------------
  28. namespace
  29. {
  30. //-----------------------------------------------------------------------------
  31. struct forwardAndBackwardSteps
  32. {
  33. QList<ctkWorkflowStep*> forwardSteps()
  34. {
  35. return this->ForwardSteps;
  36. }
  37. QList<ctkWorkflowStep*> backwardSteps()
  38. {
  39. return this->BackwardSteps;
  40. }
  41. QList<QString> forwardBranchIds()
  42. {
  43. return this->ForwardBranchIds;
  44. }
  45. QList<QString> backwardBranchIds()
  46. {
  47. return this->BackwardBranchIds;
  48. }
  49. void appendForwardStep(ctkWorkflowStep* step, QString id)
  50. {
  51. this->ForwardSteps.append(step);
  52. this->ForwardBranchIds.append(id);
  53. }
  54. void appendBackwardStep(ctkWorkflowStep* step, QString id)
  55. {
  56. this->BackwardSteps.append(step);
  57. this->BackwardBranchIds.append(id);
  58. }
  59. QString firstForwardBranchId()
  60. {
  61. if (this->ForwardBranchIds.isEmpty())
  62. {
  63. return QString();
  64. }
  65. else
  66. {
  67. return this->ForwardBranchIds.first();
  68. }
  69. }
  70. ctkWorkflowStep* forwardStep(QString branchId)
  71. {
  72. int index = this->ForwardBranchIds.indexOf(branchId);
  73. if (index != -1)
  74. {
  75. return ForwardSteps.at(index);
  76. }
  77. else
  78. {
  79. return 0;
  80. }
  81. }
  82. QString backwardBranchId(ctkWorkflowStep* step)
  83. {
  84. int index = this->BackwardSteps.indexOf(step);
  85. if (index != -1)
  86. {
  87. return BackwardBranchIds.at(index);
  88. }
  89. else
  90. {
  91. return QString();
  92. }
  93. }
  94. QString forwardBranchId(ctkWorkflowStep* step)
  95. {
  96. int index = this->ForwardSteps.indexOf(step);
  97. if (index != -1)
  98. {
  99. return ForwardBranchIds.at(index);
  100. }
  101. else
  102. {
  103. return QString();
  104. }
  105. }
  106. private:
  107. QList<ctkWorkflowStep*> ForwardSteps;
  108. QList<ctkWorkflowStep*> BackwardSteps;
  109. QList<QString> ForwardBranchIds;
  110. QList<QString> BackwardBranchIds;
  111. };
  112. }
  113. //-----------------------------------------------------------------------------
  114. class ctkWorkflowPrivate: public ctkPrivate<ctkWorkflow>
  115. {
  116. public:
  117. CTK_DECLARE_PUBLIC(ctkWorkflow);
  118. ctkWorkflowPrivate();
  119. ~ctkWorkflowPrivate();
  120. /// \brief Add a step to the workflow
  121. ///
  122. /// \note The step's components will be automatically be added to the state machine
  123. /// (i.e. the processingState state, validationState state, validationTransition transition
  124. /// and validationFailedtransition transition.
  125. ///
  126. /// \return True or False indicating whether the method was successful.
  127. void addStep(ctkWorkflowStep* step);
  128. /// \brief Returns whether a transition has been previously added with the same origin,
  129. /// destination and directionality
  130. bool hasDuplicateTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  131. const ctkWorkflow::TransitionDirectionality directionality);
  132. /// \brief Returns whether a transition has been previously added with the same origin and branch
  133. /// id (for forward transitions) or with the same destination and branch id (for backward transitions
  134. bool hasTransitionWithSameBranchId(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  135. const QString& branchId,
  136. const ctkWorkflow::TransitionDirectionality directionality);
  137. /// \brief Creates a transition from the origin to the destinatio.
  138. ///
  139. /// More specifically, the transition is from the \a origin's validation state to the \a
  140. /// destination's processing state, and is of type ctkWorkflowTransition::TransitionToNextStep
  141. ///
  142. /// The destination step should semantically be a next step, i.e. from a workflow perspective, the
  143. /// \a destination step is meant to appear after the \a origin step.
  144. ///
  145. /// Returns true/false indicating whether the method was successful.
  146. void createTransitionToNextStep(ctkWorkflowStep* origin,
  147. ctkWorkflowStep* destination,
  148. const QString& branchId = "");
  149. /// \brief Creates a transition from the destination to the origin
  150. ///
  151. /// More specifically, the transition is from the \a destination's processing state to the \a
  152. /// origin's processing state, and is of type ctkWorkflowTransition::TransitionToPreviousStep
  153. ///
  154. /// The destination step should semantically be a next step, i.e. from a workflow perspective, the
  155. /// \a destination step is meant to appear after the \a origin step.
  156. ///
  157. /// Returns true/false indicating whether the method was successful.
  158. void createTransitionToPreviousStep(ctkWorkflowStep* origin,
  159. ctkWorkflowStep* destination,
  160. const QString& branchId = "");
  161. /// \brief Creates a transition from the goTo step to the step from which the attempt to go to the
  162. /// goTo step was initiated.
  163. ///
  164. /// More specifically, the transition is from the \a goTo step's processing state to the \a
  165. /// starting step's processing state, and is of type ctkWorkflowTransition::TransitionToPreviousStartingStep
  166. ///
  167. /// Returns true/false indicating whether the method was successful.
  168. void createTransitionToPreviousStartingStep(ctkWorkflowStep* startingStep,
  169. ctkWorkflowStep* currentStep);
  170. ///
  171. void validateInternal(ctkWorkflowStep* step);
  172. /// \brief Performs computation required when entering this step.
  173. ///
  174. /// Does some sanity checks and then either calls onEntry() or emits the invokeOnEntryCommand(),
  175. /// depending on whether the user indicates that there is an onEntryCommand.
  176. void onEntryInternal(ctkWorkflowStep* step, ctkWorkflowStep* comingFrom,
  177. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType);
  178. /// \brief Performs computation required when exiting this step.
  179. ///
  180. /// Does some sanity checks and then either calls onExit() or emits the invokeOnExitCommand(),
  181. /// depending on whether the user indicates that there is an onExitCommand.
  182. void onExitInternal(ctkWorkflowStep* step, ctkWorkflowStep* goingTo,
  183. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType);
  184. /// Get the step in the workflow with a given id.
  185. ctkWorkflowStep* stepFromId(const QString& id)const;
  186. /// Get the step that a state belongs to (if any)
  187. ctkWorkflowStep* stepFromState(const QAbstractState* state);
  188. /// Get the number of forward steps from the given step
  189. int numberOfForwardSteps(ctkWorkflowStep* step);
  190. /// Get the number of backward steps from the given step
  191. int numberOfBackwardSteps(ctkWorkflowStep* step);
  192. /// Get the ids of the steps that directly follow the given step.
  193. QList<QString> forwardBranchIds(ctkWorkflowStep* step)const;
  194. /// Get the ids of the steps that directly preceed the given step.
  195. QList<QString> backwardBranchIds(ctkWorkflowStep* step)const;
  196. /// Determines whether there exists a path from the origin step (the current step by default) to
  197. /// the step with the given goalId
  198. bool pathExists(const QString& goalId, ctkWorkflowStep* origin = 0)const;
  199. /// Determines whether there exists a path from the current step's next step (as given by the
  200. /// branchId) to the step with the given goalId
  201. bool pathExistsFromNextStep(const QString& goalId, const QString& branchId)const;
  202. QStateMachine* StateMachine;
  203. // Maintain a list of pointers to the steps in the workflow,
  204. // along with their forward and backward transitions
  205. QMap<ctkWorkflowStep*, forwardAndBackwardSteps*> StepToForwardAndBackwardStepMap;
  206. // ... and its associated convenient typedef
  207. typedef QMap<ctkWorkflowStep*, forwardAndBackwardSteps*> StepToForwardAndBackwardStepMapType;
  208. typedef QList<ctkWorkflowStep*> StepListType;
  209. // Maintain a map of <state, step> key/value pairs, to find the step
  210. // that a given state belongs to
  211. typedef QMap<const QAbstractState*, ctkWorkflowStep*> StateToStepMapType;
  212. typedef QMap<const QAbstractState*, ctkWorkflowStep*>::iterator StateToStepMapIterator;
  213. StateToStepMapType StateToStepMap;
  214. ctkWorkflowStep* InitialStep;
  215. ctkWorkflowStep* CurrentStep;
  216. QMap<ctkWorkflowStep*, ctkWorkflowStep*> StepToPreviousStepMap;
  217. // Used when performing a transition
  218. ctkWorkflowStep* OriginStep;
  219. ctkWorkflowStep* DestinationStep;
  220. ctkWorkflowInterstepTransition::InterstepTransitionType TransitionType;
  221. QString DesiredBranchId; // Desired branchId specified when invoking goForward
  222. ctkWorkflowStep* GoToStep; // Desired step when attempting to go to a finish step
  223. ctkWorkflowStep* StartingStep; // Current step when we began the attempt to go to the desired finish step
  224. // Temporary transition after successfully going to finish step, to get us back to the starting step
  225. ctkWorkflowInterstepTransition* TransitionToPreviousStartingStep;
  226. QString ARTIFICIAL_BRANCH_ID_PREFIX;
  227. };
  228. // --------------------------------------------------------------------------
  229. // ctkWorkflowPrivate methods
  230. // --------------------------------------------------------------------------
  231. ctkWorkflowPrivate::ctkWorkflowPrivate()
  232. {
  233. this->InitialStep = 0;
  234. this->CurrentStep = 0;
  235. this->OriginStep = 0;
  236. this->DestinationStep = 0;
  237. this->GoToStep = 0;
  238. this->StartingStep = 0;
  239. this->TransitionToPreviousStartingStep = 0;
  240. this->ARTIFICIAL_BRANCH_ID_PREFIX = "ctkWorkflowArtificialBranchId_";
  241. }
  242. // --------------------------------------------------------------------------
  243. ctkWorkflowPrivate::~ctkWorkflowPrivate()
  244. {
  245. }
  246. // --------------------------------------------------------------------------
  247. void ctkWorkflowPrivate::addStep(ctkWorkflowStep* step)
  248. {
  249. CTK_P(ctkWorkflow);
  250. Q_ASSERT(step);
  251. Q_ASSERT(!p->hasStep(step->id()));
  252. Q_ASSERT(!this->StateMachine->isRunning());
  253. // Add the states, creating them if necessary
  254. this->StateMachine->addState(step->processingState());
  255. this->StateMachine->addState(step->validationState());
  256. // Update the map of steps to transitions and the <state,step> map
  257. this->StepToForwardAndBackwardStepMap.insert(step, new forwardAndBackwardSteps);
  258. this->StateToStepMap[step->processingState()] = step;
  259. this->StateToStepMap[step->validationState()] = step;
  260. // Setup the signal/slot that triggers the attempt to go to the next step
  261. QObject::connect(step->validationState(), SIGNAL(entered()),
  262. p, SLOT(attemptToGoToNextStep()));
  263. // Setup the signal/slot that triggers the evaluation of the validation results
  264. // after validate(const QString&) is called
  265. p->connectStep(step);
  266. }
  267. // --------------------------------------------------------------------------
  268. bool ctkWorkflowPrivate::hasDuplicateTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  269. const ctkWorkflow::TransitionDirectionality directionality)
  270. {
  271. CTK_P(ctkWorkflow);
  272. Q_ASSERT(origin);
  273. Q_ASSERT(destination);
  274. Q_ASSERT(directionality == ctkWorkflow::Forward || ctkWorkflow::Backward);
  275. ctkWorkflowPrivate::StepListType stepList;
  276. ctkWorkflowStep* targetStep = 0;
  277. if (directionality == ctkWorkflow::Forward)
  278. {
  279. stepList = p->forwardSteps(origin);
  280. targetStep = destination;
  281. }
  282. else if (directionality == ctkWorkflow::Backward)
  283. {
  284. stepList = p->backwardSteps(destination);
  285. targetStep = origin;
  286. }
  287. foreach(ctkWorkflowStep * step, stepList)
  288. {
  289. if (step == targetStep)
  290. {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. // --------------------------------------------------------------------------
  297. bool ctkWorkflowPrivate::hasTransitionWithSameBranchId(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  298. const QString& branchId,
  299. const ctkWorkflow::TransitionDirectionality directionality)
  300. {
  301. Q_ASSERT(origin);
  302. Q_ASSERT(destination);
  303. Q_ASSERT(directionality == ctkWorkflow::Forward || ctkWorkflow::Backward);
  304. Q_ASSERT(!branchId.isEmpty());
  305. QList<QString> branchIdList;
  306. if (directionality == ctkWorkflow::Forward)
  307. {
  308. branchIdList = this->forwardBranchIds(origin);
  309. }
  310. else if (directionality == ctkWorkflow::Backward)
  311. {
  312. branchIdList = this->backwardBranchIds(destination);
  313. }
  314. foreach(QString id, branchIdList)
  315. {
  316. if (QString::compare(id, branchId, Qt::CaseInsensitive) == 0)
  317. {
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. // --------------------------------------------------------------------------
  324. void ctkWorkflowPrivate::createTransitionToNextStep(ctkWorkflowStep* origin,
  325. ctkWorkflowStep* destination,
  326. const QString& branchId)
  327. {
  328. CTK_P(ctkWorkflow);
  329. Q_ASSERT(origin);
  330. Q_ASSERT(destination);
  331. Q_ASSERT(!p->hasTransition(origin, destination, branchId, ctkWorkflow::Forward));
  332. QString id;
  333. // create an artificial branchId if one is not given
  334. if (branchId.isEmpty())
  335. {
  336. id.setNum(this->numberOfForwardSteps(origin));
  337. id.prepend(this->ARTIFICIAL_BRANCH_ID_PREFIX);
  338. }
  339. else
  340. {
  341. id = branchId;
  342. }
  343. // Create the transition
  344. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToNextStep, id);
  345. transition->setTargetState(destination->processingState());
  346. origin->validationState()->addTransition(transition);
  347. // Update the step to transitions map
  348. this->StepToForwardAndBackwardStepMap.value(origin)->appendForwardStep(destination, id);
  349. // Setup the signal/slot that shows and hides the steps' user interfaces
  350. // on transition to the next step
  351. QObject::connect(transition, SIGNAL(triggered()), p, SLOT(performTransitionBetweenSteps()));
  352. }
  353. // --------------------------------------------------------------------------
  354. void ctkWorkflowPrivate::createTransitionToPreviousStep(ctkWorkflowStep* origin,
  355. ctkWorkflowStep* destination,
  356. const QString& branchId)
  357. {
  358. CTK_P(ctkWorkflow);
  359. Q_ASSERT(origin);
  360. Q_ASSERT(destination);
  361. Q_ASSERT(!p->hasTransition(origin, destination, branchId, ctkWorkflow::Backward));
  362. QString id;
  363. // create an artificial branchId if one is not given
  364. if (branchId.isEmpty())
  365. {
  366. id.setNum(this->numberOfBackwardSteps(destination));
  367. id.prepend(this->ARTIFICIAL_BRANCH_ID_PREFIX);
  368. }
  369. else
  370. {
  371. id = branchId;
  372. }
  373. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToPreviousStep, id);
  374. transition->setTargetState(origin->processingState());
  375. destination->processingState()->addTransition(transition);
  376. // Update the step to transitions map
  377. this->StepToForwardAndBackwardStepMap.value(destination)->appendBackwardStep(origin, id);
  378. // Setup the signal/slot that shows and hides the steps' user
  379. // interfaces on transition to the previous step
  380. QObject::connect(transition, SIGNAL(triggered()), p, SLOT(performTransitionBetweenSteps()));
  381. }
  382. // --------------------------------------------------------------------------
  383. void ctkWorkflowPrivate::createTransitionToPreviousStartingStep(ctkWorkflowStep* startingStep,
  384. ctkWorkflowStep* currentStep)
  385. {
  386. CTK_P(ctkWorkflow);
  387. Q_ASSERT(startingStep);
  388. Q_ASSERT(currentStep);
  389. if (!this->TransitionToPreviousStartingStep)
  390. {
  391. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(
  392. ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  393. // Setup the signal/slot that shows and hides the steps' user interfaces
  394. // on transition to the previous step
  395. QObject::connect(transition, SIGNAL(triggered()), p, SLOT(performTransitionBetweenSteps()));
  396. this->TransitionToPreviousStartingStep = transition;
  397. }
  398. QState* currentState;
  399. // looping on the finish step
  400. if (startingStep == currentStep)
  401. {
  402. currentState = currentStep->validationState();
  403. }
  404. else
  405. {
  406. currentState = currentStep->processingState();
  407. }
  408. this->TransitionToPreviousStartingStep->setTargetState(startingStep->processingState());
  409. currentState->addTransition(this->TransitionToPreviousStartingStep);
  410. }
  411. // --------------------------------------------------------------------------
  412. ctkWorkflowStep* ctkWorkflowPrivate::stepFromId(const QString& id)const
  413. {
  414. foreach(ctkWorkflowStep* step, this->StepToForwardAndBackwardStepMap.keys())
  415. {
  416. Q_ASSERT(step);
  417. if (QString::compare(step->id(), id, Qt::CaseInsensitive) == 0)
  418. {
  419. return step;
  420. }
  421. }
  422. return 0;
  423. }
  424. // --------------------------------------------------------------------------
  425. QList<QString> ctkWorkflowPrivate::forwardBranchIds(ctkWorkflowStep* step)const
  426. {
  427. Q_ASSERT(step);
  428. return this->StepToForwardAndBackwardStepMap.value(step)->forwardBranchIds();
  429. }
  430. // --------------------------------------------------------------------------
  431. QList<QString> ctkWorkflowPrivate::backwardBranchIds(ctkWorkflowStep* step)const
  432. {
  433. Q_ASSERT(step);
  434. return this->StepToForwardAndBackwardStepMap.value(step)->backwardBranchIds();
  435. }
  436. //---------------------------------------------------------------------------
  437. void ctkWorkflowPrivate::validateInternal(ctkWorkflowStep* step)
  438. {
  439. Q_ASSERT(step);
  440. logger.debug(QString("validateInternal - validating input from %1").arg(step->name()));
  441. if (step->hasValidateCommand())
  442. {
  443. step->invokeValidateCommand(this->DesiredBranchId);
  444. }
  445. else
  446. {
  447. step->validate(this->DesiredBranchId);
  448. }
  449. }
  450. // --------------------------------------------------------------------------
  451. void ctkWorkflowPrivate::onEntryInternal(
  452. ctkWorkflowStep* step,
  453. ctkWorkflowStep* comingFrom,
  454. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType)
  455. {
  456. Q_ASSERT(step);
  457. logger.debug(QString("onEntryInternal - entering input from %1").arg(step->name()));
  458. //Ensure we are transitioning between steps or starting the workflow
  459. Q_ASSERT(transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep
  460. || transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep
  461. || transitionType == ctkWorkflowInterstepTransition::StartingWorkflow
  462. || transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  463. if (step->hasOnEntryCommand())
  464. {
  465. step->invokeOnEntryCommand(comingFrom, transitionType);
  466. }
  467. else
  468. {
  469. step->onEntry(comingFrom, transitionType);
  470. }
  471. }
  472. // --------------------------------------------------------------------------
  473. void ctkWorkflowPrivate::onExitInternal(
  474. ctkWorkflowStep* step,
  475. ctkWorkflowStep* goingTo,
  476. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType)
  477. {
  478. Q_ASSERT(step);
  479. logger.debug(QString("onExitInternal - exiting %1").arg(step->name()));
  480. // Ensure we are transitioning between steps or starting the workflow
  481. Q_ASSERT (transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep ||
  482. transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep ||
  483. transitionType == ctkWorkflowInterstepTransition::StoppingWorkflow ||
  484. transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  485. if (step->hasOnExitCommand())
  486. {
  487. step->invokeOnExitCommand(goingTo, transitionType);
  488. }
  489. else
  490. {
  491. step->onExit(goingTo, transitionType);
  492. }
  493. }
  494. // --------------------------------------------------------------------------
  495. ctkWorkflowStep* ctkWorkflowPrivate::stepFromState(const QAbstractState* state)
  496. {
  497. if (state)
  498. {
  499. return this->StateToStepMap.value(state);
  500. }
  501. return 0;
  502. }
  503. // --------------------------------------------------------------------------
  504. int ctkWorkflowPrivate::numberOfForwardSteps(ctkWorkflowStep* step)
  505. {
  506. CTK_P(ctkWorkflow);
  507. return p->forwardSteps(step).length();
  508. }
  509. // --------------------------------------------------------------------------
  510. int ctkWorkflowPrivate::numberOfBackwardSteps(ctkWorkflowStep* step)
  511. {
  512. CTK_P(ctkWorkflow);
  513. return p->backwardSteps(step).length();
  514. }
  515. // --------------------------------------------------------------------------
  516. bool ctkWorkflowPrivate::pathExists(const QString& goalId, ctkWorkflowStep* origin)const
  517. {
  518. CTK_P(const ctkWorkflow);
  519. Q_ASSERT(!goalId.isEmpty());
  520. Q_ASSERT(this->CurrentStep);
  521. QString originId;
  522. if (origin)
  523. {
  524. originId = origin->id();
  525. }
  526. else
  527. {
  528. originId = this->CurrentStep->id();
  529. }
  530. // there exists a path from the origin to the goal if:
  531. // - there is a goal AND
  532. // - either:
  533. // - the origin is already the goal
  534. // - there is a path from at least one of the origin's successors to the goal
  535. return (p->hasStep(goalId)
  536. && ((QString::compare(goalId, originId, Qt::CaseInsensitive) == 0)
  537. || (p->canGoForward(origin)))); // <-- TODO insert logic here for looking at graph
  538. }
  539. // --------------------------------------------------------------------------
  540. bool ctkWorkflowPrivate::pathExistsFromNextStep(const QString& goalId, const QString& branchId)const
  541. {
  542. Q_ASSERT(!goalId.isEmpty());
  543. Q_ASSERT(!branchId.isEmpty());
  544. Q_ASSERT(this->CurrentStep);
  545. // return whether there exists a path from the the step that will be followed (given the branchId) to the goal
  546. ctkWorkflowStep* nextStep = this->StepToForwardAndBackwardStepMap.value(this->CurrentStep)->forwardStep(branchId);
  547. if (!nextStep)
  548. {
  549. return false;
  550. }
  551. else
  552. {
  553. return this->pathExists(goalId, nextStep);
  554. }
  555. }
  556. // --------------------------------------------------------------------------
  557. // ctkWorkflow methods
  558. // --------------------------------------------------------------------------
  559. ctkWorkflow::ctkWorkflow(QObject* _parent) : Superclass(_parent)
  560. {
  561. CTK_INIT_PRIVATE(ctkWorkflow);
  562. CTK_D(ctkWorkflow);
  563. d->StateMachine = new QStateMachine(this);
  564. }
  565. // --------------------------------------------------------------------------
  566. ctkWorkflow::~ctkWorkflow()
  567. {
  568. CTK_D(ctkWorkflow);
  569. if (d->StateMachine->isRunning())
  570. {
  571. d->StateMachine->stop();
  572. }
  573. }
  574. // --------------------------------------------------------------------------
  575. bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  576. const QString& branchId,
  577. const ctkWorkflow::TransitionDirectionality directionality)
  578. {
  579. CTK_D(ctkWorkflow);
  580. if (d->StateMachine->isRunning())
  581. {
  582. logger.warn("addTransition - Cannot add a transition while the workflow is started !");
  583. return false;
  584. }
  585. // cannot currently create a transition between two steps of the same id, which is equivalent to
  586. // adding a transition from a step to itself
  587. if (origin && destination && (QString::compare(origin->id(), destination->id(), Qt::CaseInsensitive) == 0))
  588. {
  589. logger.error("addTransition - Workflow does not currently support a transition"
  590. " from a step to itself. Use GoToStep instead !");
  591. return false;
  592. }
  593. // add the origin step if it doesn't exist in the workflow yet
  594. if (origin && !this->hasStep(origin->id()))
  595. {
  596. d->addStep(origin);
  597. }
  598. // add the destination step if it doesn't exist in the workflow yet
  599. if (destination && !this->hasStep(destination->id()))
  600. {
  601. d->addStep(destination);
  602. }
  603. if (origin && destination)
  604. {
  605. // ensure we haven't already added a transition with the same origin, destination and directionality
  606. if (this->hasTransition(origin, destination, branchId, directionality))
  607. {
  608. logger.warn("addTransition - Cannot create a transition that matches a "
  609. "previously created transtiion");
  610. return false;
  611. }
  612. // create the forward transition
  613. if (directionality == ctkWorkflow::Forward
  614. || directionality == ctkWorkflow::Bidirectional)
  615. {
  616. d->createTransitionToNextStep(origin, destination, branchId);
  617. }
  618. // create the backward transition
  619. if (directionality == ctkWorkflow::Backward
  620. || directionality == ctkWorkflow::Bidirectional)
  621. {
  622. d->createTransitionToPreviousStep(origin, destination, branchId);
  623. }
  624. }
  625. return true;
  626. }
  627. // --------------------------------------------------------------------------
  628. bool ctkWorkflow::hasTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  629. const QString& branchId,
  630. const ctkWorkflow::TransitionDirectionality directionality)
  631. {
  632. CTK_D(ctkWorkflow);
  633. // we have a bidirectional transition if we have both a forward and a backward transition
  634. if (directionality == ctkWorkflow::Bidirectional)
  635. {
  636. return this->hasTransition(origin, destination, branchId, ctkWorkflow::Forward)
  637. && this->hasTransition(origin, destination, branchId, ctkWorkflow::Backward);
  638. }
  639. else
  640. {
  641. if (branchId.isEmpty())
  642. {
  643. return d->hasDuplicateTransition(origin, destination, directionality);
  644. }
  645. else
  646. {
  647. return d->hasDuplicateTransition(origin, destination, directionality)
  648. || d->hasTransitionWithSameBranchId(origin, destination, branchId, directionality);
  649. }
  650. }
  651. }
  652. // --------------------------------------------------------------------------
  653. void ctkWorkflow::connectStep(ctkWorkflowStep* step)
  654. {
  655. Q_ASSERT(step);
  656. if (!step->hasValidateCommand())
  657. {
  658. QObject::connect(step->ctkWorkflowStepQObject(), SIGNAL(validationComplete(bool, const QString&)), this, SLOT(evaluateValidationResults(bool, const QString&)));
  659. }
  660. if (!step->hasOnEntryCommand())
  661. {
  662. QObject::connect(step->ctkWorkflowStepQObject(), SIGNAL(onEntryComplete()), this, SLOT(processingAfterOnEntry()));
  663. }
  664. if (!step->hasOnExitCommand())
  665. {
  666. QObject::connect(step->ctkWorkflowStepQObject(), SIGNAL(onExitComplete()), this, SLOT(processingAfterOnExit()));
  667. }
  668. }
  669. // --------------------------------------------------------------------------
  670. QList<ctkWorkflowStep*> ctkWorkflow::forwardSteps(ctkWorkflowStep* step)const
  671. {
  672. CTK_D(const ctkWorkflow);
  673. // use the given step if provided, otherwise use the workflow's current step
  674. if (step)
  675. {
  676. return d->StepToForwardAndBackwardStepMap.value(step)->forwardSteps();
  677. }
  678. else if (d->CurrentStep)
  679. {
  680. return d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->forwardSteps();
  681. }
  682. else
  683. {
  684. return QList<ctkWorkflowStep*>();
  685. }
  686. }
  687. // --------------------------------------------------------------------------
  688. QList<ctkWorkflowStep*> ctkWorkflow::backwardSteps(ctkWorkflowStep* step)const
  689. {
  690. CTK_D(const ctkWorkflow);
  691. // use the current step if provided, otherwise use the workflow's current step
  692. if (step)
  693. {
  694. return d->StepToForwardAndBackwardStepMap.value(step)->backwardSteps();
  695. }
  696. else if (d->CurrentStep)
  697. {
  698. return d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->backwardSteps();
  699. }
  700. else
  701. {
  702. return QList<ctkWorkflowStep*>();
  703. }
  704. }
  705. // --------------------------------------------------------------------------
  706. QList<ctkWorkflowStep*> ctkWorkflow::finishSteps()const
  707. {
  708. CTK_D(const ctkWorkflow);
  709. // iterate through our list of steps, and keep the steps that don't have anything following them
  710. QList<ctkWorkflowStep*> finishSteps;
  711. foreach (ctkWorkflowStep* step, d->StepToForwardAndBackwardStepMap.keys())
  712. {
  713. if (!this->canGoForward(step))
  714. {
  715. finishSteps.append(step);
  716. }
  717. }
  718. return finishSteps;
  719. }
  720. // --------------------------------------------------------------------------
  721. bool ctkWorkflow::canGoForward(ctkWorkflowStep* step)const
  722. {
  723. return (!this->forwardSteps(step).isEmpty());
  724. }
  725. // --------------------------------------------------------------------------
  726. bool ctkWorkflow::canGoBackward(ctkWorkflowStep* step)const
  727. {
  728. return (!this->backwardSteps(step).isEmpty());
  729. }
  730. // --------------------------------------------------------------------------
  731. bool ctkWorkflow::canGoToStep(const QString& targetId, ctkWorkflowStep* step)const
  732. {
  733. CTK_D(const ctkWorkflow);
  734. return d->pathExists(targetId, step);
  735. }
  736. // --------------------------------------------------------------------------
  737. bool ctkWorkflow::hasStep(const QString& id)const
  738. {
  739. CTK_D(const ctkWorkflow);
  740. return d->stepFromId(id);
  741. }
  742. // --------------------------------------------------------------------------
  743. CTK_GET_CXX(ctkWorkflow, ctkWorkflowStep*, initialStep, InitialStep);
  744. CTK_SET_CXX(ctkWorkflow, ctkWorkflowStep*, setInitialStep, InitialStep);
  745. CTK_GET_CXX(ctkWorkflow, ctkWorkflowStep*, currentStep, CurrentStep);
  746. // --------------------------------------------------------------------------
  747. void ctkWorkflow::start()
  748. {
  749. CTK_D(ctkWorkflow);
  750. if (!d->InitialStep)
  751. {
  752. logger.warn("start - Cannot start workflow without an initial step");
  753. return;
  754. }
  755. // Setup to do the entry processing for the initial setp
  756. d->StateMachine->setInitialState(d->InitialStep->processingState());
  757. d->OriginStep = 0;
  758. d->DestinationStep = d->InitialStep;
  759. d->TransitionType = ctkWorkflowInterstepTransition::StartingWorkflow;
  760. d->onEntryInternal(d->DestinationStep, d->OriginStep, d->TransitionType);
  761. d->StateMachine->start();
  762. }
  763. // --------------------------------------------------------------------------
  764. bool ctkWorkflow::isRunning()const
  765. {
  766. CTK_D(const ctkWorkflow);
  767. return d->StateMachine->isRunning();
  768. }
  769. // --------------------------------------------------------------------------
  770. void ctkWorkflow::stop()
  771. {
  772. CTK_D(ctkWorkflow);
  773. if (!d->StateMachine->isRunning())
  774. {
  775. return;
  776. }
  777. // Setup to do the exit processing for the current step
  778. if (d->CurrentStep)
  779. {
  780. d->OriginStep = d->CurrentStep;
  781. d->DestinationStep = 0;
  782. d->TransitionType = ctkWorkflowInterstepTransition::StoppingWorkflow;
  783. d->onExitInternal(d->OriginStep, d->DestinationStep, d->TransitionType);
  784. }
  785. d->StateMachine->stop();
  786. }
  787. // --------------------------------------------------------------------------
  788. void ctkWorkflow::goForward(const QString& desiredBranchId)
  789. {
  790. CTK_D(ctkWorkflow);
  791. if (!this->isRunning())
  792. {
  793. logger.warn("goForward - The workflow is not running !");
  794. return;
  795. }
  796. // if we're just going to the next step and not to a 'goTo' step, then check to make sure that
  797. // there exists a step following the current step
  798. if (!d->GoToStep)
  799. {
  800. if (!this->canGoForward())
  801. {
  802. logger.warn("goForward - Attempt to goForward from a finish step !");
  803. return;
  804. }
  805. }
  806. d->DesiredBranchId = desiredBranchId;
  807. logger.info("goForward - posting ValidationTransition");
  808. d->StateMachine->postEvent(
  809. new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationTransition));
  810. }
  811. // --------------------------------------------------------------------------
  812. void ctkWorkflow::goBackward(const QString& desiredBranchId)
  813. {
  814. CTK_D(ctkWorkflow);
  815. if (!this->isRunning())
  816. {
  817. logger.warn("goBackward - The workflow is not running !");
  818. return;
  819. }
  820. if (!this->canGoBackward())
  821. {
  822. logger.warn("goBackward - Attempt to goBackward from first step !");
  823. return;
  824. }
  825. ctkWorkflowStep* previousStep = d->StepToPreviousStepMap[d->CurrentStep];
  826. Q_ASSERT(previousStep);
  827. QString branchId = d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->backwardBranchId(previousStep);
  828. Q_ASSERT(!branchId.isEmpty());
  829. d->DesiredBranchId = desiredBranchId;
  830. logger.info("goBackward - posting TransitionToPreviousStep");
  831. d->StateMachine->postEvent(
  832. new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStep, branchId));
  833. }
  834. // --------------------------------------------------------------------------
  835. void ctkWorkflow::goToStep(const QString& targetId)
  836. {
  837. CTK_D(ctkWorkflow);
  838. if (!this->isRunning())
  839. {
  840. logger.warn("goToStep - The workflow is not running !");
  841. return;
  842. }
  843. // TODO currently returns true only if the workflow is running - need logic here
  844. if (!this->canGoToStep(targetId))
  845. {
  846. logger.warn(QString("goToStep - Cannot goToStep %1 ").arg(targetId));
  847. return;
  848. }
  849. ctkWorkflowStep* step = d->stepFromId(targetId);
  850. Q_ASSERT(step);
  851. logger.info(QString("goToStep - Attempting to go to finish step %1").arg(targetId));
  852. // if (step == d->CurrentStep)
  853. // {
  854. // qDebug() << "we are already in the desired finish step";
  855. // return;
  856. // }
  857. d->GoToStep = d->stepFromId(targetId);
  858. d->StartingStep = d->CurrentStep;
  859. this->goForward();
  860. }
  861. // --------------------------------------------------------------------------
  862. void ctkWorkflow::attemptToGoToNextStep()
  863. {
  864. logger.info("attemptToGoToNextStep - Attempting to go to the next step ");
  865. CTK_D(ctkWorkflow);
  866. Q_ASSERT(d->CurrentStep);
  867. //Q_ASSERT(this->canGoForward(d->CurrentStep));
  868. d->validateInternal(d->CurrentStep);
  869. }
  870. // --------------------------------------------------------------------------
  871. void ctkWorkflow::evaluateValidationResults(bool validationSucceeded, const QString& branchId)
  872. {
  873. if (validationSucceeded)
  874. {
  875. this->goToNextStepAfterSuccessfulValidation(branchId);
  876. }
  877. else
  878. {
  879. this->goToProcessingStateAfterValidationFailed();
  880. }
  881. }
  882. // --------------------------------------------------------------------------
  883. // if ctkWorkflowStep::validationComplete() did not provide a branchId, then:
  884. // - if there is one step following the current step, we will follow that transition
  885. // - if there are multiple steps following the current step, then we will follow the first
  886. // transition that was added
  887. // (either way this corresponds to following the first forwardBranchId we've recorded)
  888. // if ctkWorkflowStep::validationComplete() provided a branchId, then:
  889. // - if there is one transition following the current step that was not created using a branchId,
  890. // then we will follow it
  891. // - otherwise do a conditional branching based on the branchId provided by validationComplete()
  892. void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
  893. {
  894. CTK_D(ctkWorkflow);
  895. logger.debug("goToNextStepAfterSuccessfulValidation - Calidation succeeded");
  896. logger.info("goToNextStepAfterSuccessfulValidation - Posting TransitionToNextStep");
  897. // we may already be in the 'goTo' step - i.e. looping on a finish step
  898. if (d->GoToStep && d->CurrentStep == d->GoToStep)
  899. {
  900. this->goToStepSucceeded();
  901. return;
  902. }
  903. QString transitionBranchId;
  904. // these values are helpful for the logic below
  905. QString firstForwardBranchId = d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->firstForwardBranchId();
  906. int numberOfForwardSteps = d->numberOfForwardSteps(d->CurrentStep);
  907. Q_ASSERT(!firstForwardBranchId.isEmpty());
  908. Q_ASSERT(numberOfForwardSteps);
  909. // validationComplete() does not give us a branchId
  910. if (branchId.isEmpty())
  911. {
  912. transitionBranchId = firstForwardBranchId;
  913. if (numberOfForwardSteps > 1)
  914. {
  915. logger.warn("goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidatComplete() "
  916. "did not provide branchId at a branch in the workflow - will follow first "
  917. "transition that was created");
  918. }
  919. }
  920. // validationComplete() gives us a branchId
  921. else
  922. {
  923. if (numberOfForwardSteps == 1 && firstForwardBranchId.contains(d->ARTIFICIAL_BRANCH_ID_PREFIX))
  924. {
  925. transitionBranchId = firstForwardBranchId;
  926. logger.warn("goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidationComplete()"
  927. " returns a branchId, but was overridden by the workflow");
  928. }
  929. else
  930. {
  931. transitionBranchId = branchId;
  932. }
  933. }
  934. // if we are trying to go to a 'goTo' step, check that the selected branch will still take us along a path that leads to the 'goTo' step, and fail if not
  935. if (d->GoToStep && !d->pathExistsFromNextStep(d->GoToStep->id(), transitionBranchId))
  936. {
  937. this->goToProcessingStateAfterValidationFailed();
  938. return;
  939. }
  940. d->StateMachine->postEvent(new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToNextStep, transitionBranchId));
  941. }
  942. // --------------------------------------------------------------------------
  943. void ctkWorkflow::goToProcessingStateAfterValidationFailed()
  944. {
  945. logger.debug("goToNextStepAfterSuccessfulValidation - Validation failed");
  946. CTK_D(ctkWorkflow);
  947. // Validation failed in the process of attempting to go to the finish step
  948. if (d->GoToStep)
  949. {
  950. this->goToStepFailed();
  951. }
  952. logger.info("goToNextStepAfterSuccessfulValidation - Posting ValidationFailedTransition");
  953. d->StateMachine->postEvent(new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationFailedTransition));
  954. }
  955. // --------------------------------------------------------------------------
  956. void ctkWorkflow::performTransitionBetweenSteps()
  957. {
  958. CTK_D(ctkWorkflow);
  959. logger.debug("performTransitionBetweenSteps - Performing transition between steps");
  960. // Alternative: could find the origin and destination step based on
  961. // d->CurrentStep rather than QObject::sender(), but would require
  962. // keeping track of an origin step's destination step (and would be
  963. // tricky in an extension to branching workflows, unless we change
  964. // this method signature)
  965. ctkWorkflowInterstepTransition* transition = qobject_cast<ctkWorkflowInterstepTransition*>(QObject::sender());
  966. Q_ASSERT(transition);
  967. d->OriginStep = d->stepFromState(transition->sourceState());
  968. d->DestinationStep = d->stepFromState(transition->targetState());
  969. d->TransitionType = transition->transitionType();
  970. Q_ASSERT(d->TransitionType == ctkWorkflowInterstepTransition::TransitionToNextStep
  971. || d->TransitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep
  972. || d->TransitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  973. // update the map from the step to the previous step if we are going forward
  974. if (d->TransitionType == ctkWorkflowInterstepTransition::TransitionToNextStep)
  975. {
  976. d->StepToPreviousStepMap.insert(d->DestinationStep, d->OriginStep);
  977. }
  978. // exit the destination step
  979. d->onExitInternal(d->OriginStep, d->DestinationStep, d->TransitionType);
  980. }
  981. // --------------------------------------------------------------------------
  982. void ctkWorkflow::processingAfterOnExit()
  983. {
  984. CTK_D(ctkWorkflow);
  985. // enter the destination step if we have one
  986. if (d->DestinationStep)
  987. {
  988. d->onEntryInternal(d->DestinationStep, d->OriginStep, d->TransitionType);
  989. }
  990. // reset the pointers used internally for performing a transition if we're done
  991. else
  992. {
  993. d->OriginStep = 0;
  994. d->DestinationStep = 0;
  995. // we've exited the CurrentStep and haven't gone into another step, so we no longer have a
  996. // currentStep.
  997. d->CurrentStep = 0;
  998. }
  999. }
  1000. // --------------------------------------------------------------------------
  1001. void ctkWorkflow::processingAfterOnEntry()
  1002. {
  1003. logger.debug("processingAfterOnEntry");
  1004. CTK_D(ctkWorkflow);
  1005. if (!d->DestinationStep)
  1006. {
  1007. logger.error("processingAfterOnEntry - Called processingAfterOnEntry without "
  1008. "having set a destination step");
  1009. return;
  1010. }
  1011. // Update the currentStep and previous step
  1012. d->CurrentStep = d->DestinationStep;
  1013. // Reset the pointers used internally for performing a transition
  1014. d->OriginStep = 0;
  1015. d->DestinationStep = 0;
  1016. // // Reset the pointers used internally for performing a transition
  1017. // // back to the starting step
  1018. // if (d->TransitionToPreviousStartingStep)
  1019. // {
  1020. // std::cout << "TRANSITION TO PREVIOUS STARTING STEP EXISTS" << std::endl;
  1021. // //d->TransitionToPreviousStartingStep->sourceState()->removeTransition(d->TransitionToPreviousStartingStep);
  1022. // //std::cout << "removed" << std::endl;
  1023. // // d->TransitionToPreviousStartingStep = 0;
  1024. // //destination->processingState()->removeTransition(d->TransitionToPreviousStartingStep);
  1025. // //delete d->TransitionToPreviousStartingStep;
  1026. // // d->TransitionToPreviousStartingStep = 0;
  1027. // std::cout << "here" << std::endl;
  1028. // }
  1029. // If we are trying to get to the finish step, then check if we are
  1030. // finished.
  1031. if (d->GoToStep)
  1032. {
  1033. if (d->CurrentStep == d->GoToStep)
  1034. {
  1035. this->goToStepSucceeded();
  1036. }
  1037. // if we're not finished, continue transitioning to the next step
  1038. else
  1039. {
  1040. this->goForward();
  1041. }
  1042. }
  1043. else
  1044. {
  1045. emit this->currentStepChanged(d->CurrentStep);
  1046. }
  1047. }
  1048. // --------------------------------------------------------------------------
  1049. void ctkWorkflow::goToStepSucceeded()
  1050. {
  1051. CTK_D(ctkWorkflow);
  1052. logger.debug("goToStepSucceeded");
  1053. // after success, go back to the step at which we begin looking for
  1054. // the finish step (will exit the current step and enter the starting step)
  1055. d->createTransitionToPreviousStartingStep(d->StartingStep, d->CurrentStep);
  1056. d->GoToStep = 0;
  1057. d->StartingStep->setStatusText("Attempt to go to the finish step succeeded");
  1058. d->StartingStep = 0;
  1059. this->goFromGoToStepToStartingStep();
  1060. }
  1061. // --------------------------------------------------------------------------
  1062. void ctkWorkflow::goFromGoToStepToStartingStep()
  1063. {
  1064. CTK_D(ctkWorkflow);
  1065. logger.info("goFromGoToStepToStartingStep - Posting TransitionToPreviousStartingStep");
  1066. d->StateMachine->postEvent(new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep));
  1067. }
  1068. // --------------------------------------------------------------------------
  1069. void ctkWorkflow::goToStepFailed()
  1070. {
  1071. // Abort attempt to get to the finish step
  1072. CTK_D(ctkWorkflow);
  1073. d->GoToStep = 0;
  1074. d->StartingStep = 0;
  1075. // We don't need to transition between steps - leave the user at the
  1076. // point of failure, so that they can try to continue manually
  1077. // Emit the signal that we have changed the current step, since it wasn't emitted in the process
  1078. // of going to the 'goTo' step.
  1079. emit this->currentStepChanged(d->CurrentStep);
  1080. // if (failedOnBranch)
  1081. // {
  1082. // this->goToProcessingStateAfterValidationFailed();
  1083. // }
  1084. }