ctkWorkflow.cpp 43 KB

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