Back to index

salome-gui  6.5.0
QtxGridBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxGridBox.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxGridBox.h"
00024 
00025 #include <QGridLayout>
00026 #include <QChildEvent>
00027 
00034 class QtxGridBox::Space : public QWidget
00035 {
00036 public:
00037   Space( const int, QtxGridBox* );
00038   virtual ~Space();
00039 
00040   virtual QSize sizeHint() const;
00041   virtual QSize minimumSizeHint() const;
00042 
00043 private:
00044   int           mySize;
00045   QtxGridBox*   myGrid;
00046 };
00047 
00053 QtxGridBox::Space::Space( const int sz, QtxGridBox* gb )
00054 : QWidget( gb ),
00055   mySize( sz ),
00056   myGrid( gb )
00057 {
00058   setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00059 }
00060 
00064 QtxGridBox::Space::~Space()
00065 {
00066 }
00067 
00072 QSize QtxGridBox::Space::sizeHint() const
00073 {
00074   return minimumSizeHint();
00075 }
00076 
00081 QSize QtxGridBox::Space::minimumSizeHint() const
00082 {
00083   QSize sz( 0, 0 );
00084   if ( myGrid && myGrid->orientation() == Qt::Horizontal )
00085     sz.setWidth( mySize );
00086   else
00087     sz.setHeight( mySize );
00088   return sz;
00089 }
00090 
00103 QtxGridBox::QtxGridBox( QWidget* parent, const int m, const int s )
00104 : QWidget( parent ),
00105   myCols( 1 ),
00106   mySkip( false ),
00107   myOrient( Qt::Vertical ),
00108   myCol( 0 ),
00109   myRow( 0 )
00110 {
00111   myLayout = new QGridLayout( this );
00112   myLayout->setMargin( m );
00113   myLayout->setSpacing( s );
00114 }
00115 
00124 QtxGridBox::QtxGridBox( const int cols, Qt::Orientation o, QWidget* parent, const int m, const int s )
00125 : QWidget( parent ),
00126   myCols( cols ),
00127   mySkip( false ),
00128   myOrient( o ),
00129   myLayout( 0 ),
00130   myCol( 0 ),
00131   myRow( 0 )
00132 {
00133   myLayout = new QGridLayout( this );
00134   myLayout->setMargin( m );
00135   myLayout->setSpacing( s );
00136 }
00137 
00141 QtxGridBox::~QtxGridBox()
00142 {
00143 }
00144 
00149 int QtxGridBox::columns() const
00150 {
00151   return myCols;
00152 }
00153 
00158 Qt::Orientation QtxGridBox::orientation() const
00159 {
00160   return myOrient;
00161 }
00162 
00167 void QtxGridBox::setColumns( const int cols )
00168 {
00169   setLayout( cols, orientation() );
00170 }
00171 
00176 void QtxGridBox::setOrientation( Qt::Orientation o )
00177 {
00178   setLayout( columns(), o );
00179 }
00180 
00186 void QtxGridBox::setLayout( const int cols, Qt::Orientation o )
00187 {
00188   if ( myCols == cols && myOrient == o )
00189     return;
00190 
00191   myCols = cols;
00192   myOrient = o;
00193 
00194   arrangeWidgets();
00195 }
00196 
00201 bool QtxGridBox::skipInvisible() const
00202 {
00203   return mySkip;
00204 }
00205 
00214 void QtxGridBox::setSkipInvisible( const bool on )
00215 {
00216   if ( mySkip == on )
00217     return;
00218 
00219   mySkip = on;
00220   arrangeWidgets();
00221 }
00222 
00227 void QtxGridBox::addSpace( const int sp )
00228 {
00229   new Space( sp, this );
00230 }
00231 
00236 int QtxGridBox::insideMargin() const
00237 {
00238   return myLayout->margin();
00239 }
00240 
00245 int QtxGridBox::insideSpacing() const
00246 {
00247   return myLayout->spacing();
00248 }
00249 
00254 void QtxGridBox::setInsideMargin( const int m )
00255 {
00256   myLayout->setMargin( m );
00257 }
00258 
00263 void QtxGridBox::setInsideSpacing( const int s )
00264 {
00265   myLayout->setSpacing( s );
00266 }
00267 
00274 bool QtxGridBox::eventFilter( QObject* o, QEvent* e )
00275 {
00276   if ( skipInvisible() && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
00277                             e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
00278     arrangeWidgets();
00279 
00280   return QWidget::eventFilter( o, e );
00281 }
00282 
00287 void QtxGridBox::childEvent( QChildEvent* e )
00288 {
00289   if ( e->child()->isWidgetType() )
00290   {
00291     QWidget* wid = (QWidget*)e->child();
00292     if ( e->type() == QEvent::ChildAdded )
00293     {
00294       placeWidget( wid );
00295       wid->installEventFilter( this );
00296     }
00297     else if ( e->type() == QEvent::ChildRemoved )
00298       wid->removeEventFilter( this );
00299   }
00300   QWidget::childEvent( e );
00301 }
00302 
00306 void QtxGridBox::skip()
00307 {
00308   if ( orientation() == Qt::Horizontal )
00309   {
00310     myCol++;
00311     if ( myCol >= columns() )
00312     {
00313       myRow++;
00314       myCol = 0;
00315     }
00316   }
00317   else
00318   {
00319     myRow++;
00320     if ( myRow >= columns() )
00321     {
00322       myCol++;
00323       myRow = 0;
00324     }
00325   }
00326 }
00327 
00331 void QtxGridBox::arrangeWidgets()
00332 {
00333   myRow = myCol = 0;
00334   int m = myLayout ? myLayout->margin() : 0;
00335   int s = myLayout ? myLayout->spacing() : 0;
00336   delete myLayout;
00337   myLayout = new QGridLayout( this );
00338   myLayout->setMargin( m );
00339   myLayout->setSpacing( s );
00340 
00341   QObjectList list = children();
00342   for ( QObjectList::iterator it = list.begin(); it != list.end(); ++it )
00343   {
00344     if ( !(*it)->isWidgetType() )
00345       continue;
00346 
00347     QWidget* wid = (QWidget*)(*it);
00348     if ( !skipInvisible() || wid->isVisibleTo( this ) )
00349       placeWidget( wid );
00350   }
00351   updateGeometry();
00352 }
00353 
00358 void QtxGridBox::placeWidget( QWidget* wid )
00359 {
00360   myLayout->addWidget( wid, myRow, myCol );
00361 
00362   skip();
00363 }