ctkCollapsibleGroupBox.cpp 6.3 KB

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