Back to index

salome-gui  6.5.0
QtxGroupBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxGroupBox.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxGroupBox.h"
00027 
00028 #include <QVBoxLayout>
00029 #include <QHBoxLayout>
00030 #include <QEvent>
00031 #include <QObjectList>
00032 #include <QApplication>
00033 
00047 QtxGroupBox::QtxGroupBox( QWidget* parent )
00048 : QGroupBox( parent ),
00049   myContainer( 0 )
00050 {
00051   initialize();
00052 }
00053 
00059 QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent )
00060 : QGroupBox( title, parent ),
00061   myContainer( 0 )
00062 {
00063   initialize();
00064 }
00065 
00069 QtxGroupBox::~QtxGroupBox()
00070 {
00071 }
00072 
00078 void QtxGroupBox::initialize()
00079 {
00080   myContainer = new QWidget( this );
00081   QHBoxLayout* base = new QHBoxLayout( myContainer );
00082   base->setMargin( 0 );
00083   base->setSpacing( 0 );
00084 
00085   updateTitle();
00086 }
00087 
00092 void QtxGroupBox::insertTitleWidget( QWidget* wid )
00093 {
00094   if ( !myContainer )
00095     return;
00096 
00097   myContainer->layout()->addWidget( wid );
00098   wid->installEventFilter( this );
00099 
00100   updateTitle();
00101 }
00102 
00107 void QtxGroupBox::removeTitleWidget( QWidget* wid )
00108 {
00109   if ( !myContainer || wid->parentWidget() != myContainer )
00110     return;
00111 
00112   myContainer->layout()->removeWidget( wid );
00113   wid->setParent( 0 );
00114   wid->removeEventFilter( this );
00115   wid->hide();
00116 
00117   updateTitle();
00118 }
00119 
00124 void QtxGroupBox::setVisible( bool on )
00125 {
00126   if ( on )
00127     updateTitle();
00128 
00129   QGroupBox::setVisible( on );
00130 }
00131 
00136 QSize QtxGroupBox::sizeHint() const
00137 {
00138   return expandTo( QGroupBox::sizeHint() );
00139 }
00140 
00145 QSize QtxGroupBox::minimumSizeHint() const
00146 {
00147   return expandTo( QGroupBox::minimumSizeHint() );
00148 }
00149 
00156 bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e )
00157 {
00158   QEvent::Type type = e->type();
00159   if ( myContainer && obj->parent() == myContainer &&
00160        ( type == QEvent::Show || type == QEvent::ShowToParent ||
00161          type == QEvent::Hide || type == QEvent::HideToParent ) )
00162     QApplication::postEvent( this, new QEvent( QEvent::User ) );
00163 
00164   return QGroupBox::eventFilter( obj, e );
00165 }
00170 QWidget* QtxGroupBox::widget() const
00171 {
00172   if ( !layout() )
00173     return 0;
00174 
00175   QWidget* w = 0;
00176   for ( int i = 0; i < (int)layout()->count() && !w; i++ )
00177     w = layout()->itemAt( i )->widget();
00178   return w;
00179 }
00180 
00185 void QtxGroupBox::setWidget( QWidget* wid )
00186 {
00187   QWidget* w = widget();
00188   if ( w == wid )
00189     return;
00190 
00191   if ( layout() )
00192     layout()->removeWidget( w );
00193 
00194   if ( !wid )
00195     delete layout();
00196   else if ( !layout() )
00197   {
00198     QLayout* bl = new QVBoxLayout( this );
00199     bl->setMargin( 0 );
00200     bl->setSpacing( 0 );
00201   }
00202 
00203   if ( layout() )
00204     layout()->addWidget( wid );
00205 
00206   if ( wid )
00207     wid->updateGeometry();
00208 }
00209 
00214 void QtxGroupBox::resizeEvent( QResizeEvent* e )
00215 {
00216   QGroupBox::resizeEvent( e );
00217 
00218   updateTitle();
00219 }
00220 
00225 void QtxGroupBox::childEvent( QChildEvent* e )
00226 {
00227 /*
00228   if ( e->type() == QEvent::ChildAdded && e->child() == myContainer )
00229     return;
00230 */
00231   QGroupBox::childEvent( e );
00232 }
00233 
00238 void QtxGroupBox::customEvent( QEvent* /*e*/ )
00239 {
00240   updateTitle();
00241 }
00242 
00247 QSize QtxGroupBox::titleSize() const
00248 {
00249   return QSize( fontMetrics().width( title() ), fontMetrics().height() );
00250 }
00251 
00255 void QtxGroupBox::updateTitle()
00256 {
00257   if ( !myContainer )
00258     return;
00259 
00260   int align = alignment();
00261 
00262   if ( title().isEmpty() )
00263     align = Qt::AlignRight;
00264 
00265   QSize ts = titleSize();
00266 
00267   int m = 5;
00268 
00269   int w = width() - ts.width();
00270   if ( align == Qt::AlignCenter )
00271     w = w / 2;
00272 
00273   w -= m;
00274 
00275   myContainer->resize( myContainer->minimumSizeHint() );
00276 
00277   bool vis = false;
00278   const QObjectList list = myContainer->children();
00279   for ( QObjectList::const_iterator it = list.begin(); it != list.end() && !vis; ++it )
00280     vis = (*it)->isWidgetType() && ((QWidget*)(*it))->isVisibleTo( myContainer );
00281 
00282   if ( !vis )
00283     myContainer->hide();
00284   else
00285   {
00286     int x = 0;
00287     if ( align == Qt::AlignRight )
00288       x = rect().left() + m;
00289     else
00290       x = rect().right() - myContainer->width() - m;
00291 
00292     int y = rect().top() - ( myContainer->height() - ts.height() ) / 2;
00293 
00294     QPoint pos( x, qMax( 0, y ) );
00295     myContainer->move( pos );
00296     myContainer->show();
00297   }
00298 
00299   if ( layout() )
00300   {
00301     if ( myContainer && myContainer->isVisibleTo( this ) )
00302       setInsideMargin( qMax( 0, myContainer->height() - ts.height() ) );
00303     else
00304       setInsideMargin( 0 );
00305   }
00306 
00307   updateGeometry();
00308 }
00309 
00314 QSize QtxGroupBox::expandTo( const QSize& sz ) const
00315 {
00316   int sh = 0;
00317   int sw = titleSize().width();
00318   if ( myContainer && myContainer->isVisibleTo( (QWidget*)this ) )
00319   {
00320     if ( alignment() == Qt::AlignCenter )
00321       sw += 2 * ( myContainer->width() + 5 );
00322     else
00323       sw += 1 * ( myContainer->width() + 5 );
00324     sw += 20;
00325     sh = myContainer->height() + 5;
00326   }
00327   return QSize( qMax( sz.width(), sw ), qMax( sz.height(), sh ) );
00328 }
00329 
00334 void QtxGroupBox::setInsideMargin( const int m )
00335 {
00336   QVBoxLayout* bl = ::qobject_cast<QVBoxLayout*>( layout() );
00337 
00338   if ( !bl )
00339     return;
00340 
00341   QSpacerItem* spacer = 0;
00342   if ( bl->count() )
00343     spacer = bl->itemAt( 0 )->spacerItem();
00344 
00345   if ( !spacer )
00346     bl->insertSpacing( 0, m );
00347   else
00348     spacer->changeSize( 0, m );
00349 }