ctkExampleDerivedWorkflowWidgetStep.cpp 5.7 KB

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