ctkCollapsibleGroupBox.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDebug>
00023 #include <QChildEvent>
00024 #include <QMouseEvent>
00025 #include <QStylePainter>
00026 #include <QStyleOptionGroupBox>
00027 #include <QStyle>
00028 
00029 // CTK includes
00030 #include "ctkCollapsibleGroupBox.h"
00031 
00032 #if QT_VERSION >= 0x040600
00033 #include <QProxyStyle>
00034 
00035 //-----------------------------------------------------------------------------
00036 class ctkCollapsibleGroupBoxStyle:public QProxyStyle
00037 {
00038   public:
00039 
00040   virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
00041   {
00042     if (pe == QStyle::PE_IndicatorCheckBox)
00043       {
00044       const QGroupBox* groupBox= qobject_cast<const QGroupBox*>(widget);
00045       if (groupBox)
00046         {
00047         this->QProxyStyle::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, opt, p, widget);
00048         return;
00049         }
00050       }
00051     this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
00052   }
00053 };
00054 #else
00055   
00056 #endif
00057 
00058 //-----------------------------------------------------------------------------
00059 ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
00060   :QGroupBox(_parent)
00061 {
00062   this->setCheckable(true);
00063   connect(this, SIGNAL(toggled(bool)), this, SLOT(expand(bool)));
00064 
00065   this->MaxHeight = this->maximumHeight();
00066 #if QT_VERSION >= 0x040600
00067   this->setStyle(new ctkCollapsibleGroupBoxStyle);
00068 #else
00069   this->setStyleSheet(
00070     "ctkCollapsibleGroupBox::indicator:checked{"
00071     "image: url(:/Icons/expand-up.png);}"
00072     "ctkCollapsibleGroupBox::indicator:unchecked{"
00073     "image: url(:/Icons/expand-down.png);}");
00074 #endif
00075 }
00076 
00077 //-----------------------------------------------------------------------------
00078 ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
00079 {
00080 
00081 }
00082 
00083 //-----------------------------------------------------------------------------
00084 void ctkCollapsibleGroupBox::expand(bool _expand)
00085 {
00086   if (!_expand)
00087     {
00088     this->OldSize = this->size();
00089     }
00090 
00091   QObjectList childList = this->children();
00092   for (int i = 0; i < childList.size(); ++i) 
00093     {
00094     QObject *o = childList.at(i);
00095     if (o && o->isWidgetType()) 
00096       {
00097       QWidget *w = static_cast<QWidget *>(o);
00098       if ( w )
00099         {
00100         w->setVisible(_expand);
00101         }
00102       }
00103     }
00104   
00105   if (_expand)
00106     {
00107     this->setMaximumHeight(this->MaxHeight);
00108     this->resize(this->OldSize);
00109     }
00110   else
00111     {
00112     this->MaxHeight = this->maximumHeight();
00113     this->setMaximumHeight(22);
00114     }
00115 }
00116 
00117 //-----------------------------------------------------------------------------
00118 void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
00119 {
00120   if(c && c->type() == QEvent::ChildAdded)
00121     {
00122     if (c->child() && c->child()->isWidgetType())
00123       {
00124       QWidget *w = static_cast<QWidget*>(c->child());
00125       w->setVisible(this->isChecked());
00126       }
00127     }
00128   QGroupBox::childEvent(c);
00129 }
00130 
00131 #if QT_VERSION < 0x040600
00132 //-----------------------------------------------------------------------------
00133 void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
00134 {
00135   this->QGroupBox::paintEvent(e);
00136   
00137   QStylePainter paint(this);
00138   QStyleOptionGroupBox option;
00139   initStyleOption(&option);
00140   option.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
00141   paint.drawComplexControl(QStyle::CC_GroupBox, option);
00142   
00143 }
00144 
00145 //-----------------------------------------------------------------------------
00146 void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
00147 {
00148     if (event->button() != Qt::LeftButton) {
00149         event->ignore();
00150         return;
00151     }
00152     // no animation
00153 }
00154 
00155 //-----------------------------------------------------------------------------
00156 void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
00157 {
00158     if (event->button() != Qt::LeftButton) {
00159         event->ignore();
00160         return;
00161     }
00162 
00163     QStyleOptionGroupBox box;
00164     initStyleOption(&box);
00165     box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
00166     QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
00167                                                                  event->pos(), this);
00168     bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
00169                                    || released == QStyle::SC_GroupBoxCheckBox);
00170     if (toggle)
00171       {
00172       this->setChecked(!this->isChecked());
00173       }
00174 }
00175 
00176 #endif
00177 
00178 //-----------------------------------------------------------------------------
00179 QSize ctkCollapsibleGroupBox::minimumSizeHint() const
00180 {
00181   //qDebug() << "ctkCollapsibleGroupBox::minimumSizeHint::" << this->QGroupBox::minimumSizeHint() ;
00182   return this->QGroupBox::minimumSizeHint();
00183 }
00184 
00185 //-----------------------------------------------------------------------------
00186 QSize ctkCollapsibleGroupBox::sizeHint() const
00187 {
00188   //qDebug() << "ctkCollapsibleGroupBox::sizeHint::" << this->QGroupBox::sizeHint() ;
00189   return this->QGroupBox::sizeHint();
00190 }
00191 
00192 //-----------------------------------------------------------------------------
00193 int ctkCollapsibleGroupBox::heightForWidth(int w) const
00194 {
00195   //qDebug() << "ctkCollapsibleGroupBox::heightForWidth::" << this->QGroupBox::heightForWidth(w) ;
00196   return this->QGroupBox::heightForWidth(w);
00197 }
00198 
00199 //-----------------------------------------------------------------------------
00200 void ctkCollapsibleGroupBox::resizeEvent ( QResizeEvent * _event )
00201 {
00202   //qDebug() << "ctkCollapsibleGroupBox::resizeEvent::" << _event->oldSize() << _event->size() ;
00203   return this->QGroupBox::resizeEvent(_event);
00204 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1