ctkExampleWorkflowWidgetStepUsingSignalsAndSlots.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <QWidget>
  16. #include <QLabel>
  17. #include <QLineEdit>
  18. // CTK includes
  19. #include "ctkExampleWorkflowWidgetStepUsingSignalsAndSlots.h"
  20. #include "ctkWorkflowWidgetStep.h"
  21. #include "ctkWorkflowStep.h"
  22. #include "ctkWorkflowWidget.h"
  23. // STD includes
  24. #include <iostream>
  25. //-----------------------------------------------------------------------------
  26. class ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate
  27. {
  28. public:
  29. ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate();
  30. ~ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate(){}
  31. /// elements of this step's user interface
  32. QWidget* widget;
  33. QLabel* label;
  34. QLineEdit* lineEdit;
  35. int defaultLineEditValue;
  36. // counters of the number of times we have run the onEntry()
  37. // and onExit() functions
  38. int numberOfTimesRanOnEntry;
  39. int numberOfTimesRanOnExit;
  40. };
  41. //-----------------------------------------------------------------------------
  42. // ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate methods
  43. //-----------------------------------------------------------------------------
  44. ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate::ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate()
  45. {
  46. this->widget = 0;
  47. this->label = 0;
  48. this->lineEdit = 0;
  49. this->defaultLineEditValue = 10;
  50. this->numberOfTimesRanOnEntry = 0;
  51. this->numberOfTimesRanOnExit = 0;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // ctkExampleWorkflowWidgetStepUsingSignalsAndSlots methods
  55. //-----------------------------------------------------------------------------
  56. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::ctkExampleWorkflowWidgetStepUsingSignalsAndSlots(QObject* _parent) : Superclass(_parent)
  57. , d_ptr(new ctkExampleWorkflowWidgetStepUsingSignalsAndSlotsPrivate)
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::~ctkExampleWorkflowWidgetStepUsingSignalsAndSlots()
  62. {
  63. }
  64. //-----------------------------------------------------------------------------
  65. CTK_GET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QWidget*, widget, widget);
  66. CTK_SET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QWidget*, setWidget, widget);
  67. CTK_GET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QLabel*, label, label);
  68. CTK_SET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QLabel*, setLabel, label);
  69. CTK_GET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QLineEdit*, lineEdit, lineEdit);
  70. CTK_SET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, QLineEdit*, setLineEdit, lineEdit);
  71. CTK_GET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, int, numberOfTimesRanOnEntry, numberOfTimesRanOnEntry);
  72. CTK_GET_CPP(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots, int, numberOfTimesRanOnExit, numberOfTimesRanOnExit);
  73. //-----------------------------------------------------------------------------
  74. void ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::onEntry(
  75. const ctkWorkflowStep* comingFrom,
  76. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  77. {
  78. Q_UNUSED(comingFrom);
  79. Q_UNUSED(transitionType);
  80. // simply implements our counter of the number of times we have run
  81. // this function
  82. Q_D(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots);
  83. d->numberOfTimesRanOnEntry++;
  84. // signals that we are finished
  85. emit onEntryComplete();
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::onExit(
  89. const ctkWorkflowStep* goingTo,
  90. const ctkWorkflowInterstepTransition::InterstepTransitionType transitionType)
  91. {
  92. Q_UNUSED(goingTo);
  93. Q_UNUSED(transitionType);
  94. // simply implements our counter of the number of times we have run
  95. // this function
  96. Q_D(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots);
  97. d->numberOfTimesRanOnExit++;
  98. // signals that we are finished
  99. emit onExitComplete();
  100. }
  101. //-----------------------------------------------------------------------------
  102. void ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::createUserInterface()
  103. {
  104. Q_D(ctkExampleWorkflowWidgetStepUsingSignalsAndSlots);
  105. if (!d->widget)
  106. {
  107. return;
  108. }
  109. if (!d->widget->layout())
  110. {
  111. QVBoxLayout* layout = new QVBoxLayout();
  112. d->widget->setLayout(layout);
  113. }
  114. if (!d->label)
  115. {
  116. d->label = new QLabel();
  117. d->label->setText("enter a number greater than or equal to 10");
  118. d->widget->layout()->addWidget(d->label);
  119. }
  120. if (!d->lineEdit)
  121. {
  122. d->lineEdit = new QLineEdit();
  123. d->lineEdit->setInputMask("000");
  124. d->lineEdit->setText("10");
  125. d->widget->layout()->addWidget(d->lineEdit);
  126. }
  127. // signals that we are finished
  128. emit createUserInterfaceComplete();
  129. }
  130. //-----------------------------------------------------------------------------
  131. void ctkExampleWorkflowWidgetStepUsingSignalsAndSlots::validate(const QString& desiredBranchId)
  132. {
  133. Q_D(const ctkExampleWorkflowWidgetStepUsingSignalsAndSlots);
  134. bool retVal;
  135. int val;
  136. bool ok;
  137. if (d->lineEdit)
  138. {
  139. QString text = d->lineEdit->text();
  140. val = text.toInt(&ok);
  141. }
  142. // used when going to a finish step
  143. else
  144. {
  145. val = d->defaultLineEditValue;
  146. ok = true;
  147. }
  148. if (!ok)
  149. {
  150. //this->setStatusText("invalid (not an integer or empty)");
  151. retVal = false;
  152. }
  153. else if (val < 10)
  154. {
  155. //this->setStatusText("invalid (invalid number)");
  156. retVal = false;
  157. }
  158. else
  159. {
  160. //this->setStatusText("");
  161. retVal = true;
  162. }
  163. // return the validation results
  164. emit validationComplete(retVal, desiredBranchId);
  165. }