1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
23 // File: QtxGroupBox.cxx
24 // Author: Sergey TELKOV
26 #include "QtxGroupBox.h"
28 #include <QVBoxLayout>
29 #include <QHBoxLayout>
31 #include <QObjectList>
32 #include <QApplication>
36 \brief Enhanced group box widget.
38 The QtxGroupBox class allows inserting custom widgets in the
39 group box title. Use insertTitleWidget() method to add
40 custom widget to the title and removeTitleWidget() to remove it.
45 \param parent parent widget
47 QtxGroupBox::QtxGroupBox( QWidget* parent )
48 : QGroupBox( parent ),
56 \param title group box title text
57 \param parent parent widget
59 QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent )
60 : QGroupBox( title, parent ),
69 QtxGroupBox::~QtxGroupBox()
74 \brief Initialize the group box.
76 Creates horizontal box as container for title widgets.
78 void QtxGroupBox::initialize()
80 myContainer = new QWidget( this );
81 QHBoxLayout* base = new QHBoxLayout( myContainer );
83 base->setSpacing( 0 );
89 \brief Add widget to the group box title.
90 \param wid widget being added to the title
92 void QtxGroupBox::insertTitleWidget( QWidget* wid )
97 myContainer->layout()->addWidget( wid );
98 wid->installEventFilter( this );
104 \brief Remove widget from the group box title.
105 \param wid widget to be removed from the title
107 void QtxGroupBox::removeTitleWidget( QWidget* wid )
109 if ( !myContainer || wid->parentWidget() != myContainer )
112 myContainer->layout()->removeWidget( wid );
114 wid->removeEventFilter( this );
121 \brief Show/hide group box.
122 \param on if \c true, show group box, otherwise, hide it
124 void QtxGroupBox::setVisible( bool on )
129 QGroupBox::setVisible( on );
133 \brief Get recommended size for the widget.
134 \return recommended size for the widget
136 QSize QtxGroupBox::sizeHint() const
138 return expandTo( QGroupBox::sizeHint() );
142 \brief Get recommended minimum size for the widget.
143 \return recommended minimum size for the widget
145 QSize QtxGroupBox::minimumSizeHint() const
147 return expandTo( QGroupBox::minimumSizeHint() );
151 \brief Custom event filter.
152 \param obj event receiver
154 \return \c true if event processing should be stopped
156 bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e )
158 QEvent::Type type = e->type();
159 if ( myContainer && obj->parent() == myContainer &&
160 ( type == QEvent::Show || type == QEvent::ShowToParent ||
161 type == QEvent::Hide || type == QEvent::HideToParent ) )
162 QApplication::postEvent( this, new QEvent( QEvent::User ) );
164 return QGroupBox::eventFilter( obj, e );
167 \brief Get central widget (or first found one).
170 QWidget* QtxGroupBox::widget() const
176 for ( int i = 0; i < (int)layout()->count() && !w; i++ )
177 w = layout()->itemAt( i )->widget();
182 \brief Set central widget to the group box.
183 \param wid widget being added to the group box
185 void QtxGroupBox::setWidget( QWidget* wid )
187 QWidget* w = widget();
192 layout()->removeWidget( w );
196 else if ( !layout() )
198 QLayout* bl = new QVBoxLayout( this );
204 layout()->addWidget( wid );
207 wid->updateGeometry();
211 \brief Customize resize event.
212 \param e resize event
214 void QtxGroupBox::resizeEvent( QResizeEvent* e )
216 QGroupBox::resizeEvent( e );
222 \brief Customize child event.
225 void QtxGroupBox::childEvent( QChildEvent* e )
228 if ( e->type() == QEvent::ChildAdded && e->child() == myContainer )
231 QGroupBox::childEvent( e );
235 \brief Process custom events.
236 \param e custom event (not used)
238 void QtxGroupBox::customEvent( QEvent* /*e*/ )
244 \brief Get the group box title size.
247 QSize QtxGroupBox::titleSize() const
249 return QSize( fontMetrics().width( title() ), fontMetrics().height() );
253 \brief Update the group box title.
255 void QtxGroupBox::updateTitle()
260 int align = alignment();
262 if ( title().isEmpty() )
263 align = Qt::AlignRight;
265 QSize ts = titleSize();
269 int w = width() - ts.width();
270 if ( align == Qt::AlignCenter )
275 myContainer->resize( myContainer->minimumSizeHint() );
278 const QObjectList list = myContainer->children();
279 for ( QObjectList::const_iterator it = list.begin(); it != list.end() && !vis; ++it )
280 vis = (*it)->isWidgetType() && ((QWidget*)(*it))->isVisibleTo( myContainer );
287 if ( align == Qt::AlignRight )
288 x = rect().left() + m;
290 x = rect().right() - myContainer->width() - m;
292 int y = rect().top() - ( myContainer->height() - ts.height() ) / 2;
294 QPoint pos( x, qMax( 0, y ) );
295 myContainer->move( pos );
301 if ( myContainer && myContainer->isVisibleTo( this ) )
302 setInsideMargin( qMax( 0, myContainer->height() - ts.height() ) );
304 setInsideMargin( 0 );
311 \brief Expand group box to the specified size.
314 QSize QtxGroupBox::expandTo( const QSize& sz ) const
317 int sw = titleSize().width();
318 if ( myContainer && myContainer->isVisibleTo( (QWidget*)this ) )
320 if ( alignment() == Qt::AlignCenter )
321 sw += 2 * ( myContainer->width() + 5 );
323 sw += 1 * ( myContainer->width() + 5 );
325 sh = myContainer->height() + 5;
327 return QSize( qMax( sz.width(), sw ), qMax( sz.height(), sh ) );
331 \brief Set group box's inside margin size.
332 \param m new inside margin size
334 void QtxGroupBox::setInsideMargin( const int m )
336 QVBoxLayout* bl = ::qobject_cast<QVBoxLayout*>( layout() );
341 QSpacerItem* spacer = 0;
343 spacer = bl->itemAt( 0 )->spacerItem();
346 bl->insertSpacing( 0, m );
348 spacer->changeSize( 0, m );