ctkExampleWorkflowWidgetStepUsingSignalsAndSlots.cpp 6.2 KB

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