ctkWorkflowAbstractPagedWidget.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <QBoxLayout>
  16. #include <QWidget>
  17. #include <QMap>
  18. // CTK includes
  19. #include "ctkWorkflowAbstractPagedWidget.h"
  20. #include "ctkWorkflowWidgetStep.h"
  21. // STD includes
  22. #include <iostream>
  23. //-----------------------------------------------------------------------------
  24. class ctkWorkflowAbstractPagedWidgetPrivate: public ctkPrivate<ctkWorkflowAbstractPagedWidget>
  25. {
  26. public:
  27. CTK_DECLARE_PUBLIC(ctkWorkflowAbstractPagedWidget);
  28. ctkWorkflowAbstractPagedWidgetPrivate();
  29. ~ctkWorkflowAbstractPagedWidgetPrivate();
  30. // maintain a map of <widget, pageArea> pairs to find the
  31. // page that a widget belongs to
  32. typedef QMap<QWidget*, QWidget*> widgetToPagedWidgetMapType;
  33. typedef QMap<QWidget*, QWidget*>::iterator widgetToPagedWidgetMapIterator;
  34. widgetToPagedWidgetMapType widgetToPagedWidgetMap;
  35. };
  36. // --------------------------------------------------------------------------
  37. // ctkWorkflowAbstractPagedWidgetPrivate methods
  38. //---------------------------------------------------------------------------
  39. ctkWorkflowAbstractPagedWidgetPrivate::ctkWorkflowAbstractPagedWidgetPrivate()
  40. {
  41. }
  42. //---------------------------------------------------------------------------
  43. ctkWorkflowAbstractPagedWidgetPrivate::~ctkWorkflowAbstractPagedWidgetPrivate()
  44. {
  45. }
  46. // --------------------------------------------------------------------------
  47. // ctkWorkflowAbstractPagedWidgetMethods
  48. // --------------------------------------------------------------------------
  49. ctkWorkflowAbstractPagedWidget::ctkWorkflowAbstractPagedWidget(QWidget* _parent) : Superclass(_parent)
  50. {
  51. CTK_INIT_PRIVATE(ctkWorkflowAbstractPagedWidget);
  52. }
  53. // --------------------------------------------------------------------------
  54. ctkWorkflowAbstractPagedWidget::~ctkWorkflowAbstractPagedWidget()
  55. {
  56. }
  57. // --------------------------------------------------------------------------
  58. void ctkWorkflowAbstractPagedWidget::setClientAreaDirection(const QBoxLayout::Direction& direction, int index)
  59. {
  60. CTK_D(ctkWorkflowAbstractPagedWidget);
  61. // change the direction for all pages if not given an index
  62. if (index == -1)
  63. {
  64. ctkWorkflowAbstractPagedWidgetPrivate::widgetToPagedWidgetMapIterator it = d->widgetToPagedWidgetMap.begin();
  65. ctkWorkflowAbstractPagedWidgetPrivate::widgetToPagedWidgetMapIterator end = d->widgetToPagedWidgetMap.end();
  66. for (; it != end; ++it)
  67. {
  68. this->changeWidgetDirection(it.value(), direction);
  69. }
  70. }
  71. // change the direction for a specific index
  72. else
  73. {
  74. QWidget* pageArea = this->getWidgetFromIndex(index);
  75. if (pageArea)
  76. {
  77. this->changeWidgetDirection(pageArea, direction);
  78. }
  79. }
  80. this->setPrivateClientAreaDirection(direction);
  81. }
  82. // --------------------------------------------------------------------------
  83. void ctkWorkflowAbstractPagedWidget::addStepArea(ctkWorkflowWidgetStep* step, int index)
  84. {
  85. this->addStepArea(step, index, "");
  86. }
  87. // --------------------------------------------------------------------------
  88. void ctkWorkflowAbstractPagedWidget::addStepArea(ctkWorkflowWidgetStep* step, const QString& label)
  89. {
  90. this->addStepArea(step, -1, label);
  91. }
  92. // --------------------------------------------------------------------------
  93. void ctkWorkflowAbstractPagedWidget::addStepArea(ctkWorkflowWidgetStep* step, int index, const QString& label)
  94. {
  95. if (step)
  96. {
  97. this->addWidget(step->stepArea(), index, label);
  98. }
  99. }
  100. // --------------------------------------------------------------------------
  101. void ctkWorkflowAbstractPagedWidget::addWidget(QWidget* widget)
  102. {
  103. this->addWidget(widget, -1, "");
  104. }
  105. // --------------------------------------------------------------------------
  106. void ctkWorkflowAbstractPagedWidget::addWidget(QWidget* widget, int index)
  107. {
  108. this->addWidget(widget, index, "");
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkWorkflowAbstractPagedWidget::addWidget(QWidget* widget, const QString& label)
  112. {
  113. this->addWidget(widget, -1, label);
  114. }
  115. // --------------------------------------------------------------------------
  116. void ctkWorkflowAbstractPagedWidget::addWidget(QWidget* widget, int index, const QString& label)
  117. {
  118. if (widget)
  119. {
  120. CTK_D(ctkWorkflowAbstractPagedWidget);
  121. QWidget* pageArea;
  122. // adding a new page
  123. if (index == -1)
  124. {
  125. pageArea = new QWidget();
  126. pageArea->setLayout(new QBoxLayout(this->clientAreaDirection()));
  127. }
  128. // if adding to a previously created page, try to get the parent
  129. // widget for the page with the specified index
  130. else
  131. {
  132. pageArea = this->getWidgetFromIndex(index);
  133. }
  134. // add the widget to the page
  135. if (pageArea)
  136. {
  137. pageArea->layout()->addWidget(widget);
  138. d->widgetToPagedWidgetMap[widget] = pageArea;
  139. // if we are creating a new page, then add it to the client area
  140. if (index == -1)
  141. {
  142. this->addWidgetToClientArea(pageArea, label);
  143. }
  144. }
  145. // if an invalid page index was given, create a new page
  146. else
  147. {
  148. this->addWidget(widget, -1, label);
  149. }
  150. }
  151. }
  152. // --------------------------------------------------------------------------
  153. void ctkWorkflowAbstractPagedWidget::showWidget(QWidget* widget)
  154. {
  155. if (widget)
  156. {
  157. CTK_D(ctkWorkflowAbstractPagedWidget);
  158. // try to get the page widget for the page corresponding to the
  159. // given widget
  160. QWidget* pageArea = d->widgetToPagedWidgetMap[widget];
  161. if (!pageArea)
  162. {
  163. std::cerr << "cannot show widget without corresponding page" << std::endl;
  164. return;
  165. }
  166. this->setCurrentWidget(pageArea);
  167. widget->show();
  168. }
  169. }