ctkWorkflowAbstractPagedWidget.cpp 5.6 KB

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