ctkExampleDerivedWorkflowWidgetStep.cpp 5.7 KB

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