ctkExampleUseOfWorkflowWidgetUsingSignalsAndSlots.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // 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 "ctkWorkflowGroupBox.h"
  22. #include "ctkExampleWorkflowWidgetStepUsingSignalsAndSlots.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. ///
  27. /// Simple of example of how to setup a workflow using custom steps
  28. /// that were created by implementing the step's functions within a
  29. /// class deriving from QObject, and that communicate with the
  30. /// workflow using signals and slots.
  31. /// See: ctkExampleWorkflowWidgetStepUsingSignalsAndSlots for an
  32. /// example of how to setup the custom steps.
  33. //-----------------------------------------------------------------------------
  34. int ctkExampleUseOfWorkflowWidgetUsingSignalsAndSlots(int argc, char * argv [] )
  35. {
  36. QApplication app(argc, argv);
  37. // this boolean is used in setHideWidgetsOfNonCurrentSteps() below
  38. // false: when a widget does not belong to the current step, it is
  39. // hidden
  40. // true: when a widget does not belong to the current step, it is
  41. // shown, but disabled
  42. bool hideWidgets = false;
  43. // create the workflow
  44. ctkWorkflow* workflow = new ctkWorkflow;
  45. // create the workflow's UI component
  46. ctkWorkflowTabWidget* workflowWidget = new ctkWorkflowTabWidget;
  47. workflowWidget->setWorkflow(workflow);
  48. ctkWorkflowGroupBox* groupBox = workflowWidget->workflowGroupBox();
  49. groupBox->setPreText("I am some pre-text");
  50. groupBox->setPostText("I am some post-text");
  51. groupBox->setHideWidgetsOfNonCurrentSteps(hideWidgets);
  52. // create and add the first workflow step
  53. ctkWorkflowWidgetStep* testStep1 = new ctkWorkflowWidgetStep(workflow, "Step 1");
  54. testStep1->setName("Step 1");
  55. testStep1->setDescription("I am in step 1");
  56. // can specify the name of the tab
  57. workflowWidget->associateStepWithLabel(testStep1, "name 1");
  58. // create and add the second workflow step
  59. ctkWorkflowWidgetStep* testStep2 = new ctkWorkflowWidgetStep(workflow, "Step 2");
  60. testStep2->setName("Step 2");
  61. testStep2->setDescription("I am in step 2");
  62. // a new tab is automatically created
  63. workflowWidget->associateStepWithLabel(testStep2, "name 2");
  64. // create and add a third workflow step
  65. ctkWorkflowWidgetStep* testStep3 = new ctkWorkflowWidgetStep(workflow, "Step 3");
  66. testStep3->setName("Step 3");
  67. testStep3->setDescription("I am in step 3");
  68. // can place a step on a tab that was previously created by
  69. // specifying its index
  70. workflowWidget->associateStepWithPage(testStep3, 1, "name 3");
  71. // add the steps to the workflow
  72. workflow->addTransition(testStep1, testStep2);
  73. workflow->addTransition(testStep2, testStep3);
  74. // create the qObjects that implement the required functions for
  75. // each step, and communicate with the workflow using signals and slots
  76. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject1 =
  77. new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots(testStep1);
  78. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject2 =
  79. new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots(testStep2);
  80. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots* qObject3 =
  81. new ctkExampleWorkflowWidgetStepUsingSignalsAndSlots(testStep3);
  82. // set the widget for each qObject
  83. qObject1->setWidget(testStep1->stepArea());
  84. qObject2->setWidget(testStep2->stepArea());
  85. qObject3->setWidget(testStep3->stepArea());
  86. // use the qObjects for validation
  87. QObject::connect(testStep1->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject1, SLOT(validate(const QString&)));
  88. QObject::connect(testStep2->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject2, SLOT(validate(const QString&)));
  89. QObject::connect(testStep3->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject3, SLOT(validate(const QString&)));
  90. // use the qObjects for entry processing
  91. QObject::connect(testStep1->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject1, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  92. QObject::connect(testStep2->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject2, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  93. QObject::connect(testStep3->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject3, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  94. // use the qObjects for exit processing
  95. QObject::connect(testStep1->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject1, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  96. QObject::connect(testStep2->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject2, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  97. QObject::connect(testStep3->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject3, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)));
  98. // use the qObjects for populating the stepWidgetsList
  99. QObject::connect(testStep1->ctkWorkflowStepQObject(), SIGNAL(invokeCreateUserInterfaceCommand()), qObject1, SLOT(createUserInterface()));
  100. QObject::connect(qObject1, SIGNAL(createUserInterfaceComplete()), testStep1->ctkWorkflowStepQObject(), SIGNAL(showUserInterfaceComplete()));
  101. QObject::connect(testStep2->ctkWorkflowStepQObject(), SIGNAL(invokeCreateUserInterfaceCommand()), qObject2, SLOT(createUserInterface()));
  102. QObject::connect(qObject2, SIGNAL(createUserInterfaceComplete()), testStep2->ctkWorkflowStepQObject(), SIGNAL(showUserInterfaceComplete()));
  103. QObject::connect(testStep3->ctkWorkflowStepQObject(), SIGNAL(invokeCreateUserInterfaceCommand()), qObject3, SLOT(createUserInterface()));
  104. QObject::connect(qObject3, SIGNAL(createUserInterfaceComplete()), testStep3->ctkWorkflowStepQObject(), SIGNAL(showUserInterfaceComplete()));
  105. testStep1->setHasValidateCommand(1);
  106. testStep1->setHasOnEntryCommand(1);
  107. testStep1->setHasOnExitCommand(1);
  108. testStep1->setHasCreateUserInterfaceCommand(1);
  109. testStep2->setHasValidateCommand(1);
  110. testStep2->setHasOnEntryCommand(1);
  111. testStep2->setHasOnExitCommand(1);
  112. testStep2->setHasCreateUserInterfaceCommand(1);
  113. testStep3->setHasValidateCommand(1);
  114. testStep3->setHasOnEntryCommand(1);
  115. testStep3->setHasOnExitCommand(1);
  116. testStep3->setHasCreateUserInterfaceCommand(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. }