ctkExpandableWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <QDebug>
  16. #include <QEvent>
  17. #include <QLayout>
  18. // CTK includes
  19. #include "ctkExpandableWidget.h"
  20. #include "ctkSizeGrip.h"
  21. //-----------------------------------------------------------------------------
  22. class ctkExpandableWidgetPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkExpandableWidget);
  25. protected:
  26. ctkExpandableWidget* const q_ptr;
  27. public:
  28. ctkExpandableWidgetPrivate(ctkExpandableWidget& object);
  29. ~ctkExpandableWidgetPrivate();
  30. void init();
  31. void positionSizeGrip();
  32. ctkSizeGrip* SizeGrip;
  33. QSize SizeGripMargins;
  34. bool SizeGripInside;
  35. };
  36. //-----------------------------------------------------------------------------
  37. ctkExpandableWidgetPrivate::ctkExpandableWidgetPrivate(ctkExpandableWidget& object)
  38. : q_ptr(&object)
  39. , SizeGrip(0)
  40. , SizeGripInside(true)
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. ctkExpandableWidgetPrivate::~ctkExpandableWidgetPrivate()
  45. {
  46. this->SizeGrip = 0; // will be deleted automatically
  47. }
  48. //-----------------------------------------------------------------------------
  49. void ctkExpandableWidgetPrivate::init()
  50. {
  51. Q_Q(ctkExpandableWidget);
  52. this->SizeGrip = new ctkSizeGrip(q, q);
  53. this->SizeGrip->setResizeWidget(false);
  54. q->connect(this->SizeGrip, SIGNAL(widgetSizeHintChanged(QSize)),
  55. q, SLOT(updateSizeHint()));
  56. this->positionSizeGrip();
  57. }
  58. //-----------------------------------------------------------------------------
  59. void ctkExpandableWidgetPrivate::positionSizeGrip()
  60. {
  61. Q_Q(ctkExpandableWidget);
  62. if (!this->SizeGrip)
  63. {
  64. return;
  65. }
  66. if (this->SizeGripInside)
  67. {
  68. q->setContentsMargins(0, 0, 0, 0);
  69. }
  70. else
  71. {
  72. int rightMargin = q->orientations() == Qt::Horizontal ?
  73. this->SizeGrip->sizeHint().width() : 0;
  74. int bottomMargin = q->orientations() == Qt::Vertical ?
  75. this->SizeGrip->sizeHint().height() : 0;
  76. q->setContentsMargins(0, 0, rightMargin, bottomMargin);
  77. }
  78. int w = q->orientations() == Qt::Vertical ? q->width() : this->SizeGrip->sizeHint().width();
  79. int h = q->orientations() == Qt::Horizontal ? q->height() : this->SizeGrip->sizeHint().height();
  80. int x = q->width() - w - this->SizeGripMargins.width();
  81. int y = q->height() - h - this->SizeGripMargins.height();
  82. this->SizeGrip->setGeometry(x, y, w, h);
  83. }
  84. //-----------------------------------------------------------------------------
  85. ctkExpandableWidget::ctkExpandableWidget(QWidget *parentWidget)
  86. : Superclass(parentWidget)
  87. , d_ptr(new ctkExpandableWidgetPrivate(*this))
  88. {
  89. Q_D(ctkExpandableWidget);
  90. d->init();
  91. }
  92. //-----------------------------------------------------------------------------
  93. ctkExpandableWidget::~ctkExpandableWidget()
  94. {
  95. }
  96. //------------------------------------------------------------------------------
  97. void ctkExpandableWidget::setOrientations(Qt::Orientations orientations)
  98. {
  99. Q_D(ctkExpandableWidget);
  100. d->SizeGrip->setOrientations(orientations);
  101. d->positionSizeGrip();
  102. this->updateGeometry();
  103. }
  104. //------------------------------------------------------------------------------
  105. Qt::Orientations ctkExpandableWidget::orientations()const
  106. {
  107. Q_D(const ctkExpandableWidget);
  108. return d->SizeGrip->orientations();
  109. }
  110. //------------------------------------------------------------------------------
  111. void ctkExpandableWidget::setSizeGripInside(bool inside)
  112. {
  113. Q_D(ctkExpandableWidget);
  114. d->SizeGripInside = inside;
  115. d->positionSizeGrip();
  116. this->updateGeometry();
  117. }
  118. //------------------------------------------------------------------------------
  119. bool ctkExpandableWidget::isSizeGripInside()const
  120. {
  121. Q_D(const ctkExpandableWidget);
  122. return d->SizeGripInside;
  123. }
  124. //------------------------------------------------------------------------------
  125. void ctkExpandableWidget::setSizeGripMargins(QSize margins)
  126. {
  127. Q_D(ctkExpandableWidget);
  128. d->SizeGripMargins = margins;
  129. d->positionSizeGrip();
  130. this->updateGeometry();
  131. }
  132. //------------------------------------------------------------------------------
  133. QSize ctkExpandableWidget::sizeGripMargins()const
  134. {
  135. Q_D(const ctkExpandableWidget);
  136. return d->SizeGripMargins;
  137. }
  138. /*
  139. //------------------------------------------------------------------------------
  140. QSize ctkExpandableWidget::minimumSizeHint()const
  141. {
  142. Q_D(const ctkExpandableWidget);
  143. return d->recomputeSizeHint(d->MinimumSizeHint);
  144. }
  145. */
  146. //------------------------------------------------------------------------------
  147. QSize ctkExpandableWidget::sizeHint()const
  148. {
  149. Q_D(const ctkExpandableWidget);
  150. QSize sizeHint = this->Superclass::sizeHint();
  151. if (d->SizeGrip->widgetSizeHint().width())
  152. {
  153. sizeHint.setWidth(d->SizeGrip->widgetSizeHint().width());
  154. }
  155. if (d->SizeGrip->widgetSizeHint().height())
  156. {
  157. sizeHint.setHeight(d->SizeGrip->widgetSizeHint().height());
  158. }
  159. return sizeHint;
  160. }
  161. //------------------------------------------------------------------------------
  162. void ctkExpandableWidget::resizeEvent(QResizeEvent* event)
  163. {
  164. Q_D(ctkExpandableWidget);
  165. this->Superclass::resizeEvent(event);
  166. d->positionSizeGrip();
  167. }
  168. //------------------------------------------------------------------------------
  169. bool ctkExpandableWidget::event(QEvent* event)
  170. {
  171. Q_D(ctkExpandableWidget);
  172. bool res = this->Superclass::event(event);
  173. if (event->type() == QEvent::ChildAdded &&
  174. d->SizeGrip && d->SizeGripInside)
  175. {
  176. d->SizeGrip->raise();
  177. }
  178. return res;
  179. }
  180. //------------------------------------------------------------------------------
  181. void ctkExpandableWidget::updateSizeHint()
  182. {
  183. this->updateGeometry();
  184. }