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: QtxGroupBox.cxx
23 // Author: Sergey TELKOV
25 #include "QtxGroupBox.h"
27 #include <QVBoxLayout>
28 #include <QHBoxLayout>
30 #include <QObjectList>
31 #include <QApplication>
35 \brief Enhanced group box widget.
37 The QtxGroupBox class allows inserting custom widgets in the
38 group box title. Use insertTitleWidget() method to add
39 custom widget to the title and removeTitleWidget() to remove it.
44 \param parent parent widget
46 QtxGroupBox::QtxGroupBox( QWidget* parent )
47 : QGroupBox( parent ),
55 \param title group box title text
56 \param parent parent widget
58 QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent )
59 : QGroupBox( title, parent ),
68 QtxGroupBox::~QtxGroupBox()
73 \brief Initialize the group box.
75 Creates horizontal box as container for title widgets.
77 void QtxGroupBox::initialize()
79 myContainer = new QWidget( this );
80 QHBoxLayout* base = new QHBoxLayout( myContainer );
82 base->setSpacing( 0 );
88 \brief Add widget to the group box title.
89 \param wid widget being added to the title
91 void QtxGroupBox::insertTitleWidget( QWidget* wid )
96 myContainer->layout()->addWidget( wid );
97 wid->installEventFilter( this );
103 \brief Remove widget from the group box title.
104 \param wid widget to be removed from the title
106 void QtxGroupBox::removeTitleWidget( QWidget* wid )
108 if ( !myContainer || wid->parentWidget() != myContainer )
111 myContainer->layout()->removeWidget( wid );
113 wid->removeEventFilter( this );
120 \brief Show/hide group box.
121 \param on if \c true, show group box, otherwise, hide it
123 void QtxGroupBox::setVisible( bool on )
128 QGroupBox::setVisible( on );
132 \brief Get recommended size for the widget.
133 \return recommended size for the widget
135 QSize QtxGroupBox::sizeHint() const
137 return expandTo( QGroupBox::sizeHint() );
141 \brief Get recommended minimum size for the widget.
142 \return recommended minimum size for the widget
144 QSize QtxGroupBox::minimumSizeHint() const
146 return expandTo( QGroupBox::minimumSizeHint() );
150 \brief Custom event filter.
151 \param obj event receiver
153 \return \c true if event processing should be stopped
155 bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e )
157 QEvent::Type type = e->type();
158 if ( myContainer && obj->parent() == myContainer &&
159 ( type == QEvent::Show || type == QEvent::ShowToParent ||
160 type == QEvent::Hide || type == QEvent::HideToParent ) )
161 QApplication::postEvent( this, new QEvent( QEvent::User ) );
163 return QGroupBox::eventFilter( obj, e );
166 \brief Get central widget (or first found one).
169 QWidget* QtxGroupBox::widget() const
175 for ( int i = 0; i < (int)layout()->count() && !w; i++ )
176 w = layout()->itemAt( i )->widget();
181 \brief Set central widget to the group box.
182 \param wid widget being added to the group box
184 void QtxGroupBox::setWidget( QWidget* wid )
186 QWidget* w = widget();
191 layout()->removeWidget( w );
195 else if ( !layout() )
197 QLayout* bl = new QVBoxLayout( this );
203 layout()->addWidget( wid );
206 wid->updateGeometry();
210 \brief Customize resize event.
211 \param e resize event
213 void QtxGroupBox::resizeEvent( QResizeEvent* e )
215 QGroupBox::resizeEvent( e );
221 \brief Customize child event.
224 void QtxGroupBox::childEvent( QChildEvent* e )
227 if ( e->type() == QEvent::ChildAdded && e->child() == myContainer )
230 QGroupBox::childEvent( e );
234 \brief Process custom events.
235 \param e custom event (not used)
237 void QtxGroupBox::customEvent( QEvent* /*e*/ )
243 \brief Get the group box title size.
246 QSize QtxGroupBox::titleSize() const
248 return QSize( fontMetrics().width( title() ), fontMetrics().height() );
252 \brief Update the group box title.
254 void QtxGroupBox::updateTitle()
259 int align = alignment();
261 if ( title().isEmpty() )
262 align = Qt::AlignRight;
264 QSize ts = titleSize();
268 int w = width() - ts.width();
269 if ( align == Qt::AlignCenter )
274 myContainer->resize( myContainer->minimumSizeHint() );
277 const QObjectList list = myContainer->children();
278 for ( QObjectList::const_iterator it = list.begin(); it != list.end() && !vis; ++it )
279 vis = (*it)->isWidgetType() && ((QWidget*)(*it))->isVisibleTo( myContainer );
286 if ( align == Qt::AlignRight )
287 x = rect().left() + m;
289 x = rect().right() - myContainer->width() - m;
291 int y = rect().top() - ( myContainer->height() - ts.height() ) / 2;
293 QPoint pos( x, qMax( 0, y ) );
294 myContainer->move( pos );
300 if ( myContainer && myContainer->isVisibleTo( this ) )
301 setInsideMargin( qMax( 0, myContainer->height() - ts.height() ) );
303 setInsideMargin( 0 );
310 \brief Expand group box to the specified size.
313 QSize QtxGroupBox::expandTo( const QSize& sz ) const
316 int sw = titleSize().width();
317 if ( myContainer && myContainer->isVisibleTo( (QWidget*)this ) )
319 if ( alignment() == Qt::AlignCenter )
320 sw += 2 * ( myContainer->width() + 5 );
322 sw += 1 * ( myContainer->width() + 5 );
324 sh = myContainer->height() + 5;
326 return QSize( qMax( sz.width(), sw ), qMax( sz.height(), sh ) );
330 \brief Set group box's inside margin size.
331 \param m new inside margin size
333 void QtxGroupBox::setInsideMargin( const int m )
335 QVBoxLayout* bl = ::qobject_cast<QVBoxLayout*>( layout() );
340 QSpacerItem* spacer = 0;
342 spacer = bl->itemAt( 0 )->spacerItem();
345 bl->insertSpacing( 0, m );
347 spacer->changeSize( 0, m );