ctkWorkflowAbstractPagedWidget.cpp 7.5 KB

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