ctkCollapsibleGroupBox.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QDebug>
  12. #include <QChildEvent>
  13. #include <QMouseEvent>
  14. #include <QStylePainter>
  15. #include <QStyleOptionGroupBox>
  16. #include <QStyle>
  17. // CTK includes
  18. #include "ctkCollapsibleGroupBox.h"
  19. #if QT_VERSION >= 0x040600
  20. #include <QProxyStyle>
  21. //-----------------------------------------------------------------------------
  22. class ctkCollapsibleGroupBoxStyle:public QProxyStyle
  23. {
  24. public:
  25. virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
  26. {
  27. if (pe == QStyle::PE_IndicatorCheckBox)
  28. {
  29. const QGroupBox* groupBox= qobject_cast<const QGroupBox*>(widget);
  30. if (groupBox)
  31. {
  32. this->QProxyStyle::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, opt, p, widget);
  33. return;
  34. }
  35. }
  36. this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
  37. }
  38. };
  39. #else
  40. #endif
  41. //-----------------------------------------------------------------------------
  42. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
  43. :QGroupBox(_parent)
  44. {
  45. this->setCheckable(true);
  46. connect(this, SIGNAL(toggled(bool)), this, SLOT(expand(bool)));
  47. this->MaxHeight = this->maximumHeight();
  48. #if QT_VERSION >= 0x040600
  49. this->setStyle(new ctkCollapsibleGroupBoxStyle);
  50. #else
  51. this->setStyleSheet(
  52. "ctkCollapsibleGroupBox::indicator:checked{"
  53. "image: url(:/Icons/expand-up.png);}"
  54. "ctkCollapsibleGroupBox::indicator:unchecked{"
  55. "image: url(:/Icons/expand-down.png);}");
  56. #endif
  57. }
  58. //-----------------------------------------------------------------------------
  59. ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
  60. {
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkCollapsibleGroupBox::expand(bool _expand)
  64. {
  65. if (!_expand)
  66. {
  67. this->OldSize = this->size();
  68. }
  69. QObjectList childList = this->children();
  70. for (int i = 0; i < childList.size(); ++i)
  71. {
  72. QObject *o = childList.at(i);
  73. if (o && o->isWidgetType())
  74. {
  75. QWidget *w = static_cast<QWidget *>(o);
  76. if ( w )
  77. {
  78. w->setVisible(_expand);
  79. }
  80. }
  81. }
  82. if (_expand)
  83. {
  84. this->setMaximumHeight(this->MaxHeight);
  85. this->resize(this->OldSize);
  86. }
  87. else
  88. {
  89. this->MaxHeight = this->maximumHeight();
  90. this->setMaximumHeight(22);
  91. }
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
  95. {
  96. if(c && c->type() == QEvent::ChildAdded)
  97. {
  98. if (c->child() && c->child()->isWidgetType())
  99. {
  100. QWidget *w = static_cast<QWidget*>(c->child());
  101. w->setVisible(this->isChecked());
  102. }
  103. }
  104. QGroupBox::childEvent(c);
  105. }
  106. #if QT_VERSION < 0x040600
  107. //-----------------------------------------------------------------------------
  108. void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
  109. {
  110. this->QGroupBox::paintEvent(e);
  111. QStylePainter paint(this);
  112. QStyleOptionGroupBox option;
  113. initStyleOption(&option);
  114. option.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  115. paint.drawComplexControl(QStyle::CC_GroupBox, option);
  116. }
  117. //-----------------------------------------------------------------------------
  118. void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
  119. {
  120. if (event->button() != Qt::LeftButton) {
  121. event->ignore();
  122. return;
  123. }
  124. // no animation
  125. }
  126. //-----------------------------------------------------------------------------
  127. void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
  128. {
  129. if (event->button() != Qt::LeftButton) {
  130. event->ignore();
  131. return;
  132. }
  133. QStyleOptionGroupBox box;
  134. initStyleOption(&box);
  135. box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  136. QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
  137. event->pos(), this);
  138. bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
  139. || released == QStyle::SC_GroupBoxCheckBox);
  140. if (toggle)
  141. {
  142. this->setChecked(!this->isChecked());
  143. }
  144. }
  145. #endif
  146. //-----------------------------------------------------------------------------
  147. QSize ctkCollapsibleGroupBox::minimumSizeHint() const
  148. {
  149. //qDebug() << "ctkCollapsibleGroupBox::minimumSizeHint::" << this->QGroupBox::minimumSizeHint() ;
  150. return this->QGroupBox::minimumSizeHint();
  151. }
  152. //-----------------------------------------------------------------------------
  153. QSize ctkCollapsibleGroupBox::sizeHint() const
  154. {
  155. //qDebug() << "ctkCollapsibleGroupBox::sizeHint::" << this->QGroupBox::sizeHint() ;
  156. return this->QGroupBox::sizeHint();
  157. }
  158. //-----------------------------------------------------------------------------
  159. int ctkCollapsibleGroupBox::heightForWidth(int w) const
  160. {
  161. //qDebug() << "ctkCollapsibleGroupBox::heightForWidth::" << this->QGroupBox::heightForWidth(w) ;
  162. return this->QGroupBox::heightForWidth(w);
  163. }
  164. //-----------------------------------------------------------------------------
  165. void ctkCollapsibleGroupBox::resizeEvent ( QResizeEvent * _event )
  166. {
  167. //qDebug() << "ctkCollapsibleGroupBox::resizeEvent::" << _event->oldSize() << _event->size() ;
  168. return this->QGroupBox::resizeEvent(_event);
  169. }