ctkExampleUseOfWorkflowWidgetUsingDerivedSteps.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.apache.org/licenses/LICENSE-2.0.txt
  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 "ctkWorkflowGroupBox.h"
  21. #include "ctkExampleDerivedWorkflowWidgetStep.h"
  22. // STD includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. ///
  26. /// Simple example of how to setup a workflow using custom steps that
  27. /// were created by deriving ctkWorkflowWidgetStep.
  28. /// See: ctkExampleDerivedWorkflowWidgetStep for an example of how to
  29. /// setup the custom steps.
  30. //-----------------------------------------------------------------------------
  31. int ctkExampleUseOfWorkflowWidgetUsingDerivedSteps ( int argc, char * argv [] )
  32. {
  33. QApplication app(argc, argv);
  34. // this boolean is used in setHideWidgetsOfNonCurrentSteps() below
  35. // false: when a widget does not belong to the current step, it is
  36. // hidden
  37. // true: when a widget does not belong to the current step, it is
  38. // shown, but disabled
  39. bool hideWidgets = false;
  40. // create the workflow
  41. ctkWorkflow* workflow = new ctkWorkflow;
  42. // create the workflow's UI component
  43. ctkWorkflowTabWidget* workflowWidget = new ctkWorkflowTabWidget;
  44. workflowWidget->setWorkflow(workflow);
  45. ctkWorkflowGroupBox* groupBox = workflowWidget->workflowGroupBox();
  46. groupBox->setPreText("I am some pre-text");
  47. groupBox->setPostText("I am some post-text");
  48. groupBox->setHideWidgetsOfNonCurrentSteps(hideWidgets);
  49. // create and add the first workflow step
  50. ctkExampleDerivedWorkflowWidgetStep* testStep1 = new ctkExampleDerivedWorkflowWidgetStep(workflow, "Step 1");
  51. testStep1->setName("Step 1");
  52. testStep1->setDescription("I am in step 1");
  53. // can specify the name of the tab
  54. workflowWidget->associateStepWithLabel(testStep1, "name 1");
  55. // create and add the second workflow step
  56. ctkExampleDerivedWorkflowWidgetStep* testStep2 = new ctkExampleDerivedWorkflowWidgetStep(workflow, "Step 2");
  57. testStep2->setName("Step 2");
  58. testStep2->setDescription("I am in step 2");
  59. // a new tab is automatically created
  60. workflowWidget->associateStepWithLabel(testStep2, "name 2");
  61. // create and add a third workflow step
  62. ctkExampleDerivedWorkflowWidgetStep* testStep3 = new ctkExampleDerivedWorkflowWidgetStep(workflow, "Step 3");
  63. testStep3->setName("Step 3");
  64. testStep3->setDescription("I am in step 3");
  65. // can place a step on a tab that was previously created by
  66. // specifying its index
  67. workflowWidget->associateStepWithPage(testStep3, 1, "name 3");
  68. // add the steps to the workflow
  69. workflow->addTransition(testStep1, testStep2);
  70. workflow->addTransition(testStep2, testStep3);
  71. // testStep1 is the initial step
  72. workflow->setInitialStep(testStep1);
  73. // testStep3 will be a finish step, since it is the last step in the workflow
  74. // - will perform the processing associated with entering and
  75. // leaving each step, using the default values supplied
  76. // - if successful: brings you back to the step where you requested
  77. // to go to the finish step, so that you can begin customization
  78. // using user inputs if desired
  79. // - if unsuccessful: leaves you in the step of failure, so that you
  80. // can attempt to recify things from there; prints an error message
  81. // at the bottom of the widget. To see this behavior:
  82. // 1) "Next" to step 2
  83. // 2) change step 2's value to something invalid (ex. 0)
  84. // 3) "Back" to step 1
  85. // 4) "finish" - attempts to go to step 3, but leaves you in step 2
  86. // start the workflow
  87. workflow->start();
  88. workflowWidget->show();
  89. // change this value (500) to increase the time that the widget is
  90. // shown
  91. QTimer::singleShot(500, &app, SLOT(quit()));
  92. app.exec();
  93. // stop the workflow
  94. workflow->stop();
  95. QTimer::singleShot(100, &app, SLOT(quit()));
  96. app.exec();
  97. // handles deletion of the workflowWidget, workflow, steps, states
  98. // and transitions
  99. delete workflowWidget;
  100. return EXIT_SUCCESS;
  101. }