ctkWorkflowAbstractPagedWidget.cpp 7.3 KB

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