/*========================================================================= Library: CTK Copyright (c) Kitware Inc. All rights reserved. Distributed under a BSD License. See LICENSE.txt file. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ // Qt includes #include #include #include #include #include #include // CTK includes #include "ctkCollapsibleGroupBox.h" #if QT_VERSION >= 0x040600 #include //----------------------------------------------------------------------------- class ctkCollapsibleGroupBoxStyle:public QProxyStyle { public: virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const { if (pe == QStyle::PE_IndicatorCheckBox) { const QGroupBox* groupBox= qobject_cast(widget); if (groupBox) { this->QProxyStyle::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, opt, p, widget); return; } } this->QProxyStyle::drawPrimitive(pe, opt, p, widget); } }; #else #endif //----------------------------------------------------------------------------- ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent) :QGroupBox(_parent) { this->setCheckable(true); connect(this, SIGNAL(toggled(bool)), this, SLOT(expand(bool))); this->MaxHeight = this->maximumHeight(); #if QT_VERSION >= 0x040600 this->setStyle(new ctkCollapsibleGroupBoxStyle); #else this->setStyleSheet( "ctkCollapsibleGroupBox::indicator:checked{" "image: url(:/Icons/expand-up.png);}" "ctkCollapsibleGroupBox::indicator:unchecked{" "image: url(:/Icons/expand-down.png);}"); #endif } //----------------------------------------------------------------------------- ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox() { } //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::expand(bool _expand) { if (!_expand) { this->OldSize = this->size(); } QObjectList childList = this->children(); for (int i = 0; i < childList.size(); ++i) { QObject *o = childList.at(i); if (o && o->isWidgetType()) { QWidget *w = static_cast(o); if ( w ) { w->setVisible(_expand); } } } if (_expand) { this->setMaximumHeight(this->MaxHeight); this->resize(this->OldSize); } else { this->MaxHeight = this->maximumHeight(); this->setMaximumHeight(22); } } //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::childEvent(QChildEvent* c) { if(c && c->type() == QEvent::ChildAdded) { if (c->child() && c->child()->isWidgetType()) { QWidget *w = static_cast(c->child()); w->setVisible(this->isChecked()); } } QGroupBox::childEvent(c); } #if QT_VERSION < 0x040600 //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e) { this->QGroupBox::paintEvent(e); QStylePainter paint(this); QStyleOptionGroupBox option; initStyleOption(&option); option.activeSubControls &= !QStyle::SC_GroupBoxCheckBox; paint.drawComplexControl(QStyle::CC_GroupBox, option); } //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event) { if (event->button() != Qt::LeftButton) { event->ignore(); return; } // no animation } //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event) { if (event->button() != Qt::LeftButton) { event->ignore(); return; } QStyleOptionGroupBox box; initStyleOption(&box); box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox; QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box, event->pos(), this); bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel || released == QStyle::SC_GroupBoxCheckBox); if (toggle) { this->setChecked(!this->isChecked()); } } #endif //----------------------------------------------------------------------------- QSize ctkCollapsibleGroupBox::minimumSizeHint() const { //qDebug() << "ctkCollapsibleGroupBox::minimumSizeHint::" << this->QGroupBox::minimumSizeHint() ; return this->QGroupBox::minimumSizeHint(); } //----------------------------------------------------------------------------- QSize ctkCollapsibleGroupBox::sizeHint() const { //qDebug() << "ctkCollapsibleGroupBox::sizeHint::" << this->QGroupBox::sizeHint() ; return this->QGroupBox::sizeHint(); } //----------------------------------------------------------------------------- int ctkCollapsibleGroupBox::heightForWidth(int w) const { //qDebug() << "ctkCollapsibleGroupBox::heightForWidth::" << this->QGroupBox::heightForWidth(w) ; return this->QGroupBox::heightForWidth(w); } //----------------------------------------------------------------------------- void ctkCollapsibleGroupBox::resizeEvent ( QResizeEvent * _event ) { //qDebug() << "ctkCollapsibleGroupBox::resizeEvent::" << _event->oldSize() << _event->size() ; return this->QGroupBox::resizeEvent(_event); }