1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: QtxGridBox.cxx
21 // Author: Sergey TELKOV
23 #include "QtxGridBox.h"
25 #include <QGridLayout>
26 #include <QChildEvent>
29 \class QtxGridBox::Space
31 \brief Represents a space in the grid box.
34 class QtxGridBox::Space : public QWidget
37 Space( const int, QtxGridBox* );
40 virtual QSize sizeHint() const;
41 virtual QSize minimumSizeHint() const;
51 \param gb parent grid box
53 QtxGridBox::Space::Space( const int sz, QtxGridBox* gb )
58 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
64 QtxGridBox::Space::~Space()
69 \brief Get recommended size for the widget.
70 \return recommended size for the widget
72 QSize QtxGridBox::Space::sizeHint() const
74 return minimumSizeHint();
78 \brief Get recommended minimum size for the widget.
79 \return recommended minimum size for the widget
81 QSize QtxGridBox::Space::minimumSizeHint() const
84 if ( myGrid && myGrid->orientation() == Qt::Horizontal )
85 sz.setWidth( mySize );
87 sz.setHeight( mySize );
93 \brief A container widget with possibility to automatically layout
99 \param parent parent widget
100 \param m grid box margin
101 \param s grid box spacing
103 QtxGridBox::QtxGridBox( QWidget* parent, const int m, const int s )
107 myOrient( Qt::Vertical ),
111 myLayout = new QGridLayout( this );
112 myLayout->setMargin( m );
113 myLayout->setSpacing( s );
118 \param cols number of grid box columns or rows (depending on the orientation)
119 \param o grid box orientation
120 \param parent parent widget
121 \param m grid box margin
122 \param s grid box spacing
124 QtxGridBox::QtxGridBox( const int cols, Qt::Orientation o, QWidget* parent, const int m, const int s )
133 myLayout = new QGridLayout( this );
134 myLayout->setMargin( m );
135 myLayout->setSpacing( s );
141 QtxGridBox::~QtxGridBox()
146 \brief Get number of grid box columns/rows (depending on the orientation).
147 \return number of columns (rows)
149 int QtxGridBox::columns() const
155 \brief Get the grid box orientation.
158 Qt::Orientation QtxGridBox::orientation() const
164 \brief Set number of grid box columns/rows (depending on the orientation).
165 \param cols number of columns (rows)
167 void QtxGridBox::setColumns( const int cols )
169 setLayout( cols, orientation() );
173 \brief Set the grid box orientation.
176 void QtxGridBox::setOrientation( Qt::Orientation o )
178 setLayout( columns(), o );
182 \brief Initialize internal layout.
183 \param cols number of columns (rows)
186 void QtxGridBox::setLayout( const int cols, Qt::Orientation o )
188 if ( myCols == cols && myOrient == o )
198 \brief Get "skip invisible widgets" flags.
199 \return current flag state
201 bool QtxGridBox::skipInvisible() const
207 \brief Set "skip invisible widgets" flags.
209 If this flag is set to \c false, invisible widgets
210 are not taken into account when layouting widgets.
212 \param on new flag state
214 void QtxGridBox::setSkipInvisible( const bool on )
224 \brief Add space (empty cell) to the grid box.
225 \param sp requied space size
227 void QtxGridBox::addSpace( const int sp )
229 new Space( sp, this );
233 \brief Get grid box's inside margin size.
234 \return inside margin size
236 int QtxGridBox::insideMargin() const
238 return myLayout->margin();
242 \brief Get grid box's inside spacing size.
243 \return inside spacing size
245 int QtxGridBox::insideSpacing() const
247 return myLayout->spacing();
251 \brief Set grid box's inside margin size.
252 \param m new inside margin size
254 void QtxGridBox::setInsideMargin( const int m )
256 myLayout->setMargin( m );
260 \brief Set grid box's inside spacing size.
261 \param s new inside spacing size
263 void QtxGridBox::setInsideSpacing( const int s )
265 myLayout->setSpacing( s );
269 \brief Custom event filter.
270 \param o event receiver object.
272 \return \c true if the event processing should be stopped
274 bool QtxGridBox::eventFilter( QObject* o, QEvent* e )
276 if ( skipInvisible() && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
277 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
280 return QWidget::eventFilter( o, e );
284 \brief Customize child event.
287 void QtxGridBox::childEvent( QChildEvent* e )
289 if ( e->child()->isWidgetType() )
291 QWidget* wid = (QWidget*)e->child();
292 if ( e->type() == QEvent::ChildAdded )
295 wid->installEventFilter( this );
297 else if ( e->type() == QEvent::ChildRemoved )
298 wid->removeEventFilter( this );
300 QWidget::childEvent( e );
304 \brief Increment the grid box current cell.
306 void QtxGridBox::skip()
308 if ( orientation() == Qt::Horizontal )
311 if ( myCol >= columns() )
320 if ( myRow >= columns() )
329 \brief Arrange child widgets.
331 void QtxGridBox::arrangeWidgets()
334 int m = myLayout ? myLayout->margin() : 0;
335 int s = myLayout ? myLayout->spacing() : 0;
337 myLayout = new QGridLayout( this );
338 myLayout->setMargin( m );
339 myLayout->setSpacing( s );
341 QObjectList list = children();
342 for ( QObjectList::iterator it = list.begin(); it != list.end(); ++it )
344 if ( !(*it)->isWidgetType() )
347 QWidget* wid = (QWidget*)(*it);
348 if ( !skipInvisible() || wid->isVisibleTo( this ) )
355 \brief Place new widget to the current grid box cell.
356 \param wid widget being inserted
358 void QtxGridBox::placeWidget( QWidget* wid )
360 myLayout->addWidget( wid, myRow, myCol );