ctkCollapsibleGroupBox.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, 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->setCheckable(true);
  50. connect(this, SIGNAL(toggled(bool)), this, SLOT(expand(bool)));
  51. this->MaxHeight = this->maximumHeight();
  52. #if QT_VERSION >= 0x040600
  53. this->setStyle(new ctkCollapsibleGroupBoxStyle);
  54. #else
  55. this->setStyleSheet(
  56. "ctkCollapsibleGroupBox::indicator:checked{"
  57. "image: url(:/Icons/expand-up.png);}"
  58. "ctkCollapsibleGroupBox::indicator:unchecked{"
  59. "image: url(:/Icons/expand-down.png);}");
  60. #endif
  61. }
  62. //-----------------------------------------------------------------------------
  63. ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
  64. {
  65. }
  66. //-----------------------------------------------------------------------------
  67. void ctkCollapsibleGroupBox::expand(bool _expand)
  68. {
  69. if (!_expand)
  70. {
  71. this->OldSize = this->size();
  72. }
  73. QObjectList childList = this->children();
  74. for (int i = 0; i < childList.size(); ++i)
  75. {
  76. QObject *o = childList.at(i);
  77. if (o && o->isWidgetType())
  78. {
  79. QWidget *w = static_cast<QWidget *>(o);
  80. if ( w )
  81. {
  82. w->setVisible(_expand);
  83. }
  84. }
  85. }
  86. if (_expand)
  87. {
  88. this->setMaximumHeight(this->MaxHeight);
  89. this->resize(this->OldSize);
  90. }
  91. else
  92. {
  93. this->MaxHeight = this->maximumHeight();
  94. this->setMaximumHeight(22);
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
  99. {
  100. if(c && c->type() == QEvent::ChildAdded)
  101. {
  102. if (c->child() && c->child()->isWidgetType())
  103. {
  104. QWidget *w = static_cast<QWidget*>(c->child());
  105. w->setVisible(this->isChecked());
  106. }
  107. }
  108. QGroupBox::childEvent(c);
  109. }
  110. #if QT_VERSION < 0x040600
  111. //-----------------------------------------------------------------------------
  112. void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
  113. {
  114. this->QGroupBox::paintEvent(e);
  115. QStylePainter paint(this);
  116. QStyleOptionGroupBox option;
  117. initStyleOption(&option);
  118. option.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  119. paint.drawComplexControl(QStyle::CC_GroupBox, option);
  120. }
  121. //-----------------------------------------------------------------------------
  122. void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
  123. {
  124. if (event->button() != Qt::LeftButton) {
  125. event->ignore();
  126. return;
  127. }
  128. // no animation
  129. }
  130. //-----------------------------------------------------------------------------
  131. void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
  132. {
  133. if (event->button() != Qt::LeftButton) {
  134. event->ignore();
  135. return;
  136. }
  137. QStyleOptionGroupBox box;
  138. initStyleOption(&box);
  139. box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  140. QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
  141. event->pos(), this);
  142. bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
  143. || released == QStyle::SC_GroupBoxCheckBox);
  144. if (toggle)
  145. {
  146. this->setChecked(!this->isChecked());
  147. }
  148. }
  149. #endif
  150. //-----------------------------------------------------------------------------
  151. QSize ctkCollapsibleGroupBox::minimumSizeHint() const
  152. {
  153. //qDebug() << "ctkCollapsibleGroupBox::minimumSizeHint::" << this->QGroupBox::minimumSizeHint() ;
  154. return this->QGroupBox::minimumSizeHint();
  155. }
  156. //-----------------------------------------------------------------------------
  157. QSize ctkCollapsibleGroupBox::sizeHint() const
  158. {
  159. //qDebug() << "ctkCollapsibleGroupBox::sizeHint::" << this->QGroupBox::sizeHint() ;
  160. return this->QGroupBox::sizeHint();
  161. }
  162. //-----------------------------------------------------------------------------
  163. int ctkCollapsibleGroupBox::heightForWidth(int w) const
  164. {
  165. //qDebug() << "ctkCollapsibleGroupBox::heightForWidth::" << this->QGroupBox::heightForWidth(w) ;
  166. return this->QGroupBox::heightForWidth(w);
  167. }
  168. //-----------------------------------------------------------------------------
  169. void ctkCollapsibleGroupBox::resizeEvent ( QResizeEvent * _event )
  170. {
  171. //qDebug() << "ctkCollapsibleGroupBox::resizeEvent::" << _event->oldSize() << _event->size() ;
  172. return this->QGroupBox::resizeEvent(_event);
  173. }