ctkExampleUseOfWorkflowWidgetUsingSignalsAndSlots.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <QApplication>
  16. #include <QTimer>
  17. // CTK includes
  18. #include "ctkWorkflow.h"
  19. #include "ctkWorkflowTabWidget.h"
  20. #include "ctkWorkflowWidgetStep.h"
  21. #include "ctkExampleWorkflowWidgetStepUsingSignalsAndSlots.h"
  22. // STD includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. ///
  26. /// Simple of example of how to setup a workflow using custom steps
  27. /// that were created by implementing the step's functions within a
  28. /// class deriving from QObject, and that communicate with the
  29. /// workflow using signals and slots.
  30. /// See: ctkExampleWorkflowWidgetStepUsingSignalsAndSlots for an
  31. /// example of how to setup the custom steps.
  32. //-----------------------------------------------------------------------------
  33. int ctkExampleUseOfWorkflowWidgetUsingSignalsAndSlots(int argc, char * argv [] )
  34. {
  35. QApplication app(argc, argv);
  36. // this boolean is used in setHideWidgetsOfNonCurrentSteps() below
  37. // false: when a widget does not belong to the current step, it is
  38. // hidden
  39. // true: when a widget does not belong to the current step, it is
  40. // shown, but disabled
  41. bool hideWidgets = false;
  42. // create the workflow
  43. ctkWorkflow* workflow = new ctkWorkflow;
  44. // create the workflow's UI component
  45. ctkWorkflowTabWidget* workflowWidget = new ctkWorkflowTabWidget;
  46. workflowWidget->setWorkflow(workflow);
  47. workflowWidget->setPreText("I am some pre-text");
  48. workflowWidget->setPostText("I am some post-text");
  49. workflowWidget->setHideWidgetsOfNonCurrentSteps(hideWidgets);
  50. // create and add the first workflow step
  51. ctkWorkflowWidgetStep* testStep1 = new ctkWorkflowWidgetStep(workflow, "Step 1");
  52. testStep1->setName("Step 1");
  53. testStep1->setDescription("I am in step 1");
  54. // can specify the name of the tab
  55. workflowWidget->addStepArea(testStep1, "tab 1");
  56. // create and add the second workflow step
  57. ctkWorkflowWidgetStep* testStep2 = new ctkWorkflowWidgetStep(workflow, "Step 2");
  58. testStep2->setName("Step 2");
  59. testStep2->setDescription("I am in step 2");
  60. // a new tab is automatically created
  61. workflowWidget->addStepArea(testStep2, "tab 2");
  62. // create and add a third workflow step
  63. ctkWorkflowWidgetStep* testStep3 = new ctkWorkflowWidgetStep(workflow, "Step 3");
  64. testStep3->setName("Step 3");
  65. testStep3->setDescription("I am in step 3");
  66. // can place a step on a tab that was previously created by
  67. // specifying its index
  68. workflowWidget->addStepArea(testStep3, 1);
  69. // add the steps to the workflow
  70. workflow->addTransition(testStep1, testStep2);
  71. workflow->addTransition(testStep2, testStep3);
  72. // create the qObjects that implement the required functions for
  73. // each step, and communicate with the workflow using signals and slots
  74. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject1 = new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots;
  75. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject2 = new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots;
  76. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject3 = new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots;
  77. // use the qObjects for validation
  78. QObject::connect(testStep1, SIGNAL(invokeValidateCommand(const QString&)), qObject1, SLOT(validate(const QString&)));
  79. QObject::connect(qObject1, SIGNAL(validationComplete(int)), workflow, SLOT(evaluateValidationResults(int)));
  80. QObject::connect(testStep2, SIGNAL(invokeValidateCommand(const QString&)), qObject2, SLOT(validate(const QString&)));
  81. QObject::connect(qObject2, SIGNAL(validationComplete(int)), workflow, SLOT(evaluateValidationResults(int)));
  82. QObject::connect(testStep3, SIGNAL(invokeValidateCommand(const QString&)), qObject3, SLOT(validate(const QString&)));
  83. QObject::connect(qObject3, SIGNAL(validationComplete(int)), workflow, SLOT(evaluateValidationResults(int)));
  84. // use the qObjects for entry processing
  85. QObject::connect(testStep1, SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject1, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  86. QObject::connect(qObject1, SIGNAL(onEntryComplete()), workflow, SLOT(evaluateOnEntryResults()));
  87. QObject::connect(testStep2, SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject2, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  88. QObject::connect(qObject2, SIGNAL(onEntryComplete()), workflow, SLOT(evaluateOnEntryResults()));
  89. QObject::connect(testStep3, SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject3, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  90. QObject::connect(qObject3, SIGNAL(onEntryComplete()), workflow, SLOT(evaluateOnEntryResults()));
  91. // use the qObjects for exit processing
  92. QObject::connect(testStep1, SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject1, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  93. QObject::connect(qObject1, SIGNAL(onExitComplete()), workflow, SLOT(evaluateOnExitResults()));
  94. QObject::connect(testStep2, SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject2, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  95. QObject::connect(qObject2, SIGNAL(onExitComplete()), workflow, SLOT(evaluateOnExitResults()));
  96. QObject::connect(testStep3, SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)), qObject3, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowTransition::WorkflowTransitionType)));
  97. QObject::connect(qObject3, SIGNAL(onExitComplete()), workflow, SLOT(evaluateOnExitResults()));
  98. // use the qObjects for populating the stepWidgetsList
  99. QObject::connect(testStep1, SIGNAL(invokePopulateStepWidgetsListCommand(QList<QWidget*>&)), qObject1, SLOT(populateStepWidgetsList(QList<QWidget*>&)));
  100. QObject::connect(qObject1, SIGNAL(populateStepWidgetsListComplete()), testStep1, SLOT(evaluatePopulateStepWidgetsListResults()));
  101. QObject::connect(testStep2, SIGNAL(invokePopulateStepWidgetsListCommand(QList<QWidget*>&)), qObject2, SLOT(populateStepWidgetsList(QList<QWidget*>&)));
  102. QObject::connect(qObject2, SIGNAL(populateStepWidgetsListComplete()), testStep2, SLOT(evaluatePopulateStepWidgetsListResults()));
  103. QObject::connect(testStep3, SIGNAL(invokePopulateStepWidgetsListCommand(QList<QWidget*>&)), qObject3, SLOT(populateStepWidgetsList(QList<QWidget*>&)));
  104. QObject::connect(qObject3, SIGNAL(populateStepWidgetsListComplete()), testStep3, SLOT(evaluatePopulateStepWidgetsListResults()));
  105. testStep1->setHasValidateCommand(1);
  106. testStep1->setHasOnEntryCommand(1);
  107. testStep1->setHasOnExitCommand(1);
  108. testStep1->setHasPopulateStepWidgetsListCommand(1);
  109. testStep2->setHasValidateCommand(1);
  110. testStep2->setHasOnEntryCommand(1);
  111. testStep2->setHasOnExitCommand(1);
  112. testStep2->setHasPopulateStepWidgetsListCommand(1);
  113. testStep3->setHasValidateCommand(1);
  114. testStep3->setHasOnEntryCommand(1);
  115. testStep3->setHasOnExitCommand(1);
  116. testStep3->setHasPopulateStepWidgetsListCommand(1);
  117. // testStep1 is the initial step
  118. workflow->setInitialStep(testStep1);
  119. // testStep3 will be a finish step
  120. // - will perform the processing associated with entering and
  121. // leaving each step, using the default values supplied
  122. // - if successful: brings you back to the step where you requested
  123. // to go to the finish step, so that you can begin customization
  124. // using user inputs if desired
  125. // - if unsuccessful: leaves you in the step of failure, so that you
  126. // can attempt to recify things from there; prints an error message
  127. // at the bottom of the widget. To see this behavior:
  128. // 1) "Next" to step 2
  129. // 2) change step 2's value to something invalid (ex. 0)
  130. // 3) "Back" to step 1
  131. // 4) "finish" - attempts to go to step 3, but leaves you in step 2
  132. // start the workflow
  133. workflow->start();
  134. workflowWidget->show();
  135. // change this value (500) to increase the time that the widget is
  136. // shown
  137. QTimer::singleShot(500, &app, SLOT(quit()));
  138. app.exec();
  139. // stop the workflow
  140. workflow->stop();
  141. QTimer::singleShot(100, &app, SLOT(quit()));
  142. app.exec();
  143. // handles deletion of the workflowWidget, workflow, steps, states
  144. // and transitions
  145. delete workflowWidget;
  146. return EXIT_SUCCESS;
  147. }