1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxGridBox.cxx
23 // Author: Sergey TELKOV
25 #include "QtxGridBox.h"
27 #include <QGridLayout>
28 #include <QChildEvent>
31 \class QtxGridBox::Space
33 \brief Represents a space in the grid box.
36 class QtxGridBox::Space : public QWidget
39 Space( const int, QtxGridBox* );
42 virtual QSize sizeHint() const;
43 virtual QSize minimumSizeHint() const;
53 \param gb parent grid box
55 QtxGridBox::Space::Space( const int sz, QtxGridBox* gb )
60 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
66 QtxGridBox::Space::~Space()
71 \brief Get recommended size for the widget.
72 \return recommended size for the widget
74 QSize QtxGridBox::Space::sizeHint() const
76 return minimumSizeHint();
80 \brief Get recommended minimum size for the widget.
81 \return recommended minimum size for the widget
83 QSize QtxGridBox::Space::minimumSizeHint() const
86 if ( myGrid && myGrid->orientation() == Qt::Horizontal )
87 sz.setWidth( mySize );
89 sz.setHeight( mySize );
95 \brief A container widget with possibility to automatically layout
101 \param parent parent widget
102 \param m grid box margin
103 \param s grid box spacing
105 QtxGridBox::QtxGridBox( QWidget* parent, const int m, const int s )
109 myOrient( Qt::Vertical ),
113 myLayout = new QGridLayout( this );
114 myLayout->setMargin( m );
115 myLayout->setSpacing( s );
120 \param cols number of grid box columns or rows (depending on the orientation)
121 \param o grid box orientation
122 \param parent parent widget
123 \param m grid box margin
124 \param s grid box spacing
126 QtxGridBox::QtxGridBox( const int cols, Qt::Orientation o, QWidget* parent, const int m, const int s )
135 myLayout = new QGridLayout( this );
136 myLayout->setMargin( m );
137 myLayout->setSpacing( s );
143 QtxGridBox::~QtxGridBox()
148 \brief Get number of grid box columns/rows (depending on the orientation).
149 \return number of columns (rows)
151 int QtxGridBox::columns() const
157 \brief Get the grid box orientation.
160 Qt::Orientation QtxGridBox::orientation() const
166 \brief Set number of grid box columns/rows (depending on the orientation).
167 \param cols number of columns (rows)
169 void QtxGridBox::setColumns( const int cols )
171 setLayout( cols, orientation() );
175 \brief Set the grid box orientation.
178 void QtxGridBox::setOrientation( Qt::Orientation o )
180 setLayout( columns(), o );
184 \brief Initialize internal layout.
185 \param cols number of columns (rows)
188 void QtxGridBox::setLayout( const int cols, Qt::Orientation o )
190 if ( myCols == cols && myOrient == o )
200 \brief Get "skip invisible widgets" flags.
201 \return current flag state
203 bool QtxGridBox::skipInvisible() const
209 \brief Set "skip invisible widgets" flags.
211 If this flag is set to \c false, invisible widgets
212 are not taken into account when layouting widgets.
214 \param on new flag state
216 void QtxGridBox::setSkipInvisible( const bool on )
226 \brief Add space (empty cell) to the grid box.
227 \param sp requied space size
229 void QtxGridBox::addSpace( const int sp )
231 new Space( sp, this );
235 \brief Get grid box's inside margin size.
236 \return inside margin size
238 int QtxGridBox::insideMargin() const
240 return myLayout->margin();
244 \brief Get grid box's inside spacing size.
245 \return inside spacing size
247 int QtxGridBox::insideSpacing() const
249 return myLayout->spacing();
253 \brief Set grid box's inside margin size.
254 \param m new inside margin size
256 void QtxGridBox::setInsideMargin( const int m )
258 myLayout->setMargin( m );
262 \brief Set grid box's inside spacing size.
263 \param s new inside spacing size
265 void QtxGridBox::setInsideSpacing( const int s )
267 myLayout->setSpacing( s );
271 \brief Custom event filter.
272 \param o event receiver object.
274 \return \c true if the event processing should be stopped
276 bool QtxGridBox::eventFilter( QObject* o, QEvent* e )
278 if ( skipInvisible() && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
279 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
282 return QWidget::eventFilter( o, e );
286 \brief Customize child event.
289 void QtxGridBox::childEvent( QChildEvent* e )
291 if ( e->child()->isWidgetType() )
293 QWidget* wid = (QWidget*)e->child();
294 if ( e->type() == QEvent::ChildAdded )
297 wid->installEventFilter( this );
299 else if ( e->type() == QEvent::ChildRemoved )
300 wid->removeEventFilter( this );
302 QWidget::childEvent( e );
306 \brief Increment the grid box current cell.
308 void QtxGridBox::skip()
310 if ( orientation() == Qt::Horizontal )
313 if ( myCol >= columns() )
322 if ( myRow >= columns() )
331 \brief Arrange child widgets.
333 void QtxGridBox::arrangeWidgets()
336 int m = myLayout ? myLayout->margin() : 0;
337 int s = myLayout ? myLayout->spacing() : 0;
339 myLayout = new QGridLayout( this );
340 myLayout->setMargin( m );
341 myLayout->setSpacing( s );
343 QObjectList list = children();
344 for ( QObjectList::iterator it = list.begin(); it != list.end(); ++it )
346 if ( !(*it)->isWidgetType() )
349 QWidget* wid = (QWidget*)(*it);
350 if ( !skipInvisible() || wid->isVisibleTo( this ) )
357 \brief Place new widget to the current grid box cell.
358 \param wid widget being inserted
360 void QtxGridBox::placeWidget( QWidget* wid )
362 myLayout->addWidget( wid, myRow, myCol );