ctkCollapsibleGroupBox.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. class ctkCollapsibleGroupBoxStyle:public QProxyStyle
  22. {
  23. public:
  24. virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
  25. {
  26. if (pe == QStyle::PE_IndicatorCheckBox)
  27. {
  28. const QGroupBox* groupBox= qobject_cast<const QGroupBox*>(widget);
  29. if (groupBox)
  30. {
  31. this->QProxyStyle::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, opt, p, widget);
  32. return;
  33. }
  34. }
  35. this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
  36. }
  37. };
  38. #else
  39. #endif
  40. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
  41. :QGroupBox(_parent)
  42. {
  43. this->setCheckable(true);
  44. connect(this, SIGNAL(toggled(bool)), this, SLOT(expand(bool)));
  45. this->MaxHeight = this->maximumHeight();
  46. #if QT_VERSION >= 0x040600
  47. this->setStyle(new ctkCollapsibleGroupBoxStyle);
  48. #else
  49. this->setStyleSheet(
  50. "ctkCollapsibleGroupBox::indicator:checked{"
  51. "image: url(:/Icons/expand-up.png);}"
  52. "ctkCollapsibleGroupBox::indicator:unchecked{"
  53. "image: url(:/Icons/expand-down.png);}");
  54. #endif
  55. }
  56. ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
  57. {
  58. }
  59. void ctkCollapsibleGroupBox::expand(bool _expand)
  60. {
  61. if (!_expand)
  62. {
  63. this->OldSize = this->size();
  64. }
  65. QObjectList childList = this->children();
  66. for (int i = 0; i < childList.size(); ++i)
  67. {
  68. QObject *o = childList.at(i);
  69. if (o && o->isWidgetType())
  70. {
  71. QWidget *w = static_cast<QWidget *>(o);
  72. if ( w )
  73. {
  74. w->setVisible(_expand);
  75. }
  76. }
  77. }
  78. if (_expand)
  79. {
  80. this->setMaximumHeight(this->MaxHeight);
  81. this->resize(this->OldSize);
  82. }
  83. else
  84. {
  85. this->MaxHeight = this->maximumHeight();
  86. this->setMaximumHeight(22);
  87. }
  88. }
  89. void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
  90. {
  91. if(c && c->type() == QEvent::ChildAdded)
  92. {
  93. if (c->child() && c->child()->isWidgetType())
  94. {
  95. QWidget *w = static_cast<QWidget*>(c->child());
  96. w->setVisible(this->isChecked());
  97. }
  98. }
  99. QGroupBox::childEvent(c);
  100. }
  101. #if QT_VERSION < 0x040600
  102. void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
  103. {
  104. this->QGroupBox::paintEvent(e);
  105. QStylePainter paint(this);
  106. QStyleOptionGroupBox option;
  107. initStyleOption(&option);
  108. option.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  109. paint.drawComplexControl(QStyle::CC_GroupBox, option);
  110. }
  111. void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
  112. {
  113. if (event->button() != Qt::LeftButton) {
  114. event->ignore();
  115. return;
  116. }
  117. // no animation
  118. }
  119. void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
  120. {
  121. if (event->button() != Qt::LeftButton) {
  122. event->ignore();
  123. return;
  124. }
  125. QStyleOptionGroupBox box;
  126. initStyleOption(&box);
  127. box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  128. QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
  129. event->pos(), this);
  130. bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
  131. || released == QStyle::SC_GroupBoxCheckBox);
  132. if (toggle)
  133. {
  134. this->setChecked(!this->isChecked());
  135. }
  136. }
  137. #endif
  138. QSize ctkCollapsibleGroupBox::minimumSizeHint() const
  139. {
  140. //qDebug() << "ctkCollapsibleGroupBox::minimumSizeHint::" << this->QGroupBox::minimumSizeHint() ;
  141. return this->QGroupBox::minimumSizeHint();
  142. }
  143. QSize ctkCollapsibleGroupBox::sizeHint() const
  144. {
  145. //qDebug() << "ctkCollapsibleGroupBox::sizeHint::" << this->QGroupBox::sizeHint() ;
  146. return this->QGroupBox::sizeHint();
  147. }
  148. int ctkCollapsibleGroupBox::heightForWidth(int w) const
  149. {
  150. //qDebug() << "ctkCollapsibleGroupBox::heightForWidth::" << this->QGroupBox::heightForWidth(w) ;
  151. return this->QGroupBox::heightForWidth(w);
  152. }
  153. void ctkCollapsibleGroupBox::resizeEvent ( QResizeEvent * _event )
  154. {
  155. //qDebug() << "ctkCollapsibleGroupBox::resizeEvent::" << _event->oldSize() << _event->size() ;
  156. return this->QGroupBox::resizeEvent(_event);
  157. }