ctkExpandableWidget.cpp 6.7 KB

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