ctkWorkflowAbstractPagedWidget.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <QMap>
  16. #include <QDebug>
  17. // CTK includes
  18. #include "ctkWorkflowAbstractPagedWidget.h"
  19. #include "ctkWorkflowStep.h"
  20. #include "ctkWorkflowWidgetStep.h"
  21. #include "ctkWorkflowWidget.h"
  22. #include "ctkWorkflowGroupBox.h"
  23. #include "ctkWorkflowButtonBoxWidget.h"
  24. // STD includes
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. class ctkWorkflowAbstractPagedWidgetPrivate
  28. {
  29. Q_DECLARE_PUBLIC(ctkWorkflowAbstractPagedWidget);
  30. protected:
  31. ctkWorkflowAbstractPagedWidget* const q_ptr;
  32. public:
  33. ctkWorkflowAbstractPagedWidgetPrivate(ctkWorkflowAbstractPagedWidget& object);
  34. ~ctkWorkflowAbstractPagedWidgetPrivate();
  35. // Maintain maps to associate:
  36. // - ctkWorkflowSteps with their page indices
  37. // - ctkWorkflowSteps with their labels
  38. // - page indices with their ctkWorkflowGroupBoxes
  39. QMap<ctkWorkflowStep*, int> StepToIndexMap;
  40. QMap<ctkWorkflowStep*, QString> StepToLabelMap;
  41. QMap<int, ctkWorkflowGroupBox*> IndexToGroupBoxMap;
  42. // Number of created pages
  43. int NumPages;
  44. ctkWorkflowGroupBox* GroupBoxShownPreviously;
  45. ctkWorkflowGroupBox* GroupBoxShownCurrently;
  46. // Create a new ctkWorkflowGroupBox based on the "recipe" provided by the superclass's
  47. // WorkflowGroupBox, and put it on a new page
  48. void createNewWorkflowGroupBox(int index);
  49. };
  50. // --------------------------------------------------------------------------
  51. // ctkWorkflowAbstractPagedWidgetPrivate methods
  52. //---------------------------------------------------------------------------
  53. ctkWorkflowAbstractPagedWidgetPrivate::ctkWorkflowAbstractPagedWidgetPrivate(ctkWorkflowAbstractPagedWidget& object)
  54. :q_ptr(&object)
  55. {
  56. this->GroupBoxShownPreviously = 0;
  57. this->GroupBoxShownCurrently = 0;
  58. }
  59. //---------------------------------------------------------------------------
  60. ctkWorkflowAbstractPagedWidgetPrivate::~ctkWorkflowAbstractPagedWidgetPrivate()
  61. {
  62. }
  63. //---------------------------------------------------------------------------
  64. void ctkWorkflowAbstractPagedWidgetPrivate::createNewWorkflowGroupBox(int index)
  65. {
  66. Q_Q(ctkWorkflowAbstractPagedWidget);
  67. ctkWorkflowGroupBox* recipe = q->workflowGroupBox();
  68. Q_ASSERT(recipe);
  69. ctkWorkflowGroupBox* newGroupBox = new ctkWorkflowGroupBox(q);
  70. newGroupBox->setPreText(recipe->preText());
  71. newGroupBox->setPostText(recipe->postText());
  72. newGroupBox->setHideWidgetsOfNonCurrentSteps(recipe->hideWidgetsOfNonCurrentSteps());
  73. q->createNewPage(newGroupBox);
  74. this->IndexToGroupBoxMap[index] = newGroupBox;
  75. }
  76. // --------------------------------------------------------------------------
  77. // ctkWorkflowAbstractPagedWidgetMethods
  78. // --------------------------------------------------------------------------
  79. ctkWorkflowAbstractPagedWidget::ctkWorkflowAbstractPagedWidget(QWidget* _parent) : Superclass(_parent)
  80. , d_ptr(new ctkWorkflowAbstractPagedWidgetPrivate(*this))
  81. {
  82. Q_D(ctkWorkflowAbstractPagedWidget);
  83. d->NumPages = 0;
  84. }
  85. // --------------------------------------------------------------------------
  86. ctkWorkflowAbstractPagedWidget::~ctkWorkflowAbstractPagedWidget()
  87. {
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkWorkflowAbstractPagedWidget::associateStepWithPage(ctkWorkflowStep* step, int index)
  91. {
  92. Q_D(ctkWorkflowAbstractPagedWidget);
  93. if (index < 0)
  94. {
  95. qWarning() << "Cannot associate step with a page of index -1";
  96. return;
  97. }
  98. if (step)
  99. {
  100. d->StepToIndexMap[step] = index;
  101. if (!d->IndexToGroupBoxMap.contains(index))
  102. {
  103. d->IndexToGroupBoxMap[index] = 0;
  104. }
  105. }
  106. }
  107. // --------------------------------------------------------------------------
  108. void ctkWorkflowAbstractPagedWidget::associateStepWithLabel(ctkWorkflowStep* step, QString label)
  109. {
  110. Q_D(ctkWorkflowAbstractPagedWidget);
  111. if (step)
  112. {
  113. d->StepToLabelMap[step] = label;
  114. }
  115. }
  116. // --------------------------------------------------------------------------
  117. void ctkWorkflowAbstractPagedWidget::associateStepWithPage(ctkWorkflowStep* step, int index, QString label)
  118. {
  119. this->associateStepWithPage(step, index);
  120. this->associateStepWithLabel(step, label);
  121. }
  122. // --------------------------------------------------------------------------
  123. ctkWorkflowGroupBox* ctkWorkflowAbstractPagedWidget::workflowGroupBox(ctkWorkflowStep* step)const
  124. {
  125. Q_D(const ctkWorkflowAbstractPagedWidget);
  126. if (d->StepToIndexMap.contains(step))
  127. {
  128. int index = d->StepToIndexMap[step];
  129. Q_ASSERT(d->IndexToGroupBoxMap.contains(index));
  130. return d->IndexToGroupBoxMap[index];
  131. }
  132. else
  133. {
  134. return 0;
  135. }
  136. }
  137. // --------------------------------------------------------------------------
  138. void ctkWorkflowAbstractPagedWidget::updateStepUI(ctkWorkflowStep* currentStep)
  139. {
  140. Q_D(ctkWorkflowAbstractPagedWidget);
  141. Q_ASSERT(currentStep);
  142. Q_ASSERT(this->workflowGroupBox());
  143. // Create layout and client area if this is our first time here
  144. if (!this->clientArea())
  145. {
  146. QVBoxLayout* layout = new QVBoxLayout();
  147. this->setLayout(layout);
  148. this->initClientArea();
  149. layout->addWidget(this->clientArea());
  150. if (this->showButtonBoxWidget())
  151. {
  152. layout->addWidget(this->buttonBoxWidget());
  153. }
  154. layout->setContentsMargins(0,0,0,0);
  155. // Use the superclass's groupBox as the first page, since we already have it
  156. d->IndexToGroupBoxMap[0] = this->workflowGroupBox();
  157. this->createNewPage(d->IndexToGroupBoxMap[0]);
  158. }
  159. // If the user hasn't previously associated the step with a page, then add a new page
  160. if (!d->StepToIndexMap.contains(currentStep))
  161. {
  162. this->associateStepWithPage(currentStep, d->NumPages++);
  163. }
  164. int index = d->StepToIndexMap[currentStep];
  165. // Create new page(s) if necessary
  166. // (i.e. if user specifies steps only for indices 3 and 4 with associateStepWithPage, we need to
  167. // create pages 0,1,2)
  168. if (!d->IndexToGroupBoxMap[index])
  169. {
  170. for (int i = 0; i <= index; i++)
  171. {
  172. if (!d->IndexToGroupBoxMap[i])
  173. {
  174. d->createNewWorkflowGroupBox(i);
  175. }
  176. }
  177. }
  178. // If the user hasn't previously associated the step with a label, then add a blank label
  179. if (!d->StepToLabelMap.contains(currentStep))
  180. {
  181. this->associateStepWithLabel(currentStep, "");
  182. }
  183. QString label = d->StepToLabelMap[currentStep];
  184. ctkWorkflowGroupBox* groupBox = d->IndexToGroupBoxMap[index];
  185. Q_ASSERT(groupBox);
  186. d->GroupBoxShownPreviously = d->GroupBoxShownCurrently;
  187. d->GroupBoxShownCurrently = groupBox;
  188. if (d->GroupBoxShownPreviously)
  189. {
  190. d->GroupBoxShownPreviously->updateGroupBox(0);
  191. }
  192. groupBox->updateGroupBox(currentStep);
  193. this->showPage(groupBox, label);
  194. }