From: stv Date: Wed, 30 May 2007 08:09:38 +0000 (+0000) Subject: no message X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8b2eb24ef58ae228059e20f9f76e1320e0e02194;p=modules%2Fgui.git no message --- diff --git a/src/Qtx/QtxGroupBox.cxx b/src/Qtx/QtxGroupBox.cxx index b2062e38e..1ed871168 100644 --- a/src/Qtx/QtxGroupBox.cxx +++ b/src/Qtx/QtxGroupBox.cxx @@ -21,36 +21,16 @@ #include "QtxGroupBox.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include /*! Constructor */ -QtxGroupBox::QtxGroupBox( QWidget* parent, const char* name ) -: QGroupBox( parent, name ), -myContainer( 0 ) -{ -} - -/*! - Constructor -*/ -QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent, const char* name ) -: QGroupBox( title, parent, name ), -myContainer( 0 ) -{ - initialize(); -} - -/*! - Constructor -*/ -QtxGroupBox::QtxGroupBox( int strips, Orientation o, QWidget* parent, const char* name ) -: QGroupBox( strips, o, parent, name ), +QtxGroupBox::QtxGroupBox( QWidget* parent ) +: QGroupBox( parent ), myContainer( 0 ) { initialize(); @@ -59,9 +39,8 @@ myContainer( 0 ) /*! Constructor */ -QtxGroupBox::QtxGroupBox( int strips, Orientation o, const QString& title, - QWidget* parent, const char* name ) -: QGroupBox( strips, o, title, parent, name ), +QtxGroupBox::QtxGroupBox( const QString& title, QWidget* parent ) +: QGroupBox( title, parent ), myContainer( 0 ) { initialize(); @@ -72,7 +51,6 @@ myContainer( 0 ) */ QtxGroupBox::~QtxGroupBox() { - delete myContainer; } /*! @@ -80,7 +58,10 @@ QtxGroupBox::~QtxGroupBox() */ void QtxGroupBox::initialize() { - myContainer = new QHBox( this, 0, WStyle_Customize | WStyle_NoBorderEx | WStyle_Tool ); + myContainer = new QWidget( this ); + QHBoxLayout* base = new QHBoxLayout( myContainer ); + base->setMargin( 0 ); + base->setSpacing( 0 ); updateTitle(); } @@ -94,7 +75,7 @@ void QtxGroupBox::insertTitleWidget( QWidget* wid ) if ( !myContainer ) return; - wid->reparent( myContainer, QPoint( 0, 0 ), true ); + myContainer->layout()->addWidget( wid ); wid->installEventFilter( this ); updateTitle(); @@ -109,138 +90,89 @@ void QtxGroupBox::removeTitleWidget( QWidget* wid ) if ( !myContainer || wid->parentWidget() != myContainer ) return; - wid->reparent( 0, QPoint( 0, 0 ), false ); + myContainer->layout()->removeWidget( wid ); + wid->setParent( 0 ); wid->removeEventFilter( this ); + wid->hide(); updateTitle(); } /*! - Calculates margin + Shows group box */ -void QtxGroupBox::adjustInsideMargin() +void QtxGroupBox::setVisible( bool on ) { - QApplication::sendPostedEvents( myContainer, QEvent::ChildInserted ); + if ( on ) + updateTitle(); - myContainer->resize( myContainer->minimumSizeHint() ); - - setInsideMargin( myContainer->height() ); + QGroupBox::setVisible( on ); } /*! - Sets the alignment of the group box title + \return the recommended size for the widget */ -void QtxGroupBox::setAlignment( int align ) +QSize QtxGroupBox::sizeHint() const { - QGroupBox::setAlignment( align ); - - updateTitle(); + return expandTo( QGroupBox::sizeHint() ); } /*! - Sets title of groop box + \return the recommended minimum size for the widget */ -void QtxGroupBox::setTitle( const QString& title ) +QSize QtxGroupBox::minimumSizeHint() const { - QGroupBox::setTitle( title ); - - updateTitle(); + return expandTo( QGroupBox::minimumSizeHint() ); } /*! - Changes the layout of the group box - \param strips - number of column/rows - \param o - orientation -*/ -void QtxGroupBox::setColumnLayout( int strips, Orientation o ) -{ - if ( myContainer ) - myContainer->reparent( 0, QPoint( 0, 0 ), false ); - - QGroupBox::setColumnLayout( strips, o ); - - if ( myContainer ) - myContainer->reparent( this, QPoint( 0, 0 ), false ); - - updateTitle(); -} - -/*! - Shows group box + Custom event filter */ -void QtxGroupBox::show() +bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e ) { - QGroupBox::show(); + QEvent::Type type = e->type(); + if ( myContainer && obj->parent() == myContainer && + ( type == QEvent::Show || type == QEvent::ShowToParent || + type == QEvent::Hide || type == QEvent::HideToParent ) ) + QApplication::postEvent( this, new QEvent( QEvent::User ) ); - updateTitle(); + return QGroupBox::eventFilter( obj, e ); } -/*! - Updates group box -*/ -void QtxGroupBox::update() +QWidget* QtxGroupBox::widget() const { - QGroupBox::update(); + if ( !layout() ) + return 0; - updateTitle(); + QWidget* w = 0; + for ( int i = 0; i < (int)layout()->count() && !w; i++ ) + w = layout()->itemAt( i )->widget(); + return w; } -/*! - \return the recommended size for the widget -*/ -QSize QtxGroupBox::sizeHint() const +void QtxGroupBox::setWidget( QWidget* wid ) { - QSize sz = QGroupBox::sizeHint(); - - int sw = titleSize().width(); - - if ( myContainer ) - { - if ( alignment() == AlignCenter ) - sw += 2 * ( myContainer->width() + 5 ); - else - sw += 1 * ( myContainer->width() + 5 ); - } - - sw += frameRect().left(); - - return QSize( QMAX( sz.width(), sw ), sz.height() ); -} - -/*! - \return the recommended minimum size for the widget -*/ -QSize QtxGroupBox::minimumSizeHint() const -{ - QSize sz = QGroupBox::minimumSizeHint(); + QWidget* w = widget(); + if ( w == wid ) + return; - int sw = titleSize().width() + myContainer ? myContainer->width() + 5 : 0; + if ( layout() ) + layout()->removeWidget( w ); - if ( myContainer ) + if ( !wid ) + delete layout(); + else if ( !layout() ) { - if ( alignment() == AlignCenter ) - sw += 2 * ( myContainer->width() + 5 ); - else - sw += 1 * ( myContainer->width() + 5 ); + QLayout* bl = new QVBoxLayout( this ); + bl->setMargin( 0 ); + bl->setSpacing( 0 ); } - sw += frameRect().left(); - - return QSize( QMAX( sz.width(), sw ), sz.height() ); -} - -/*! - Custom event filter -*/ -bool QtxGroupBox::eventFilter( QObject* obj, QEvent* e ) -{ - QEvent::Type type = e->type(); - if ( myContainer && obj->parent() == myContainer && - ( type == QEvent::Show || type == QEvent::ShowToParent || - type == QEvent::Hide || type == QEvent::HideToParent ) ) - QApplication::postEvent( this, new QCustomEvent( QEvent::User ) ); + if ( layout() ) + layout()->addWidget( wid ); - return QGroupBox::eventFilter( obj, e ); + if ( wid ) + wid->updateGeometry(); } /*! @@ -258,24 +190,17 @@ void QtxGroupBox::resizeEvent( QResizeEvent* e ) */ void QtxGroupBox::childEvent( QChildEvent* e ) { - if ( e->type() == QEvent::ChildInserted && e->child() == myContainer ) +/* + if ( e->type() == QEvent::ChildAdded && e->child() == myContainer ) return; - +*/ QGroupBox::childEvent( e ); } /*! Event filter of custom items */ -void QtxGroupBox::customEvent( QCustomEvent* ) -{ - updateTitle(); -} - -/*! - On frame changed -*/ -void QtxGroupBox::frameChanged() +void QtxGroupBox::customEvent( QEvent* ) { updateTitle(); } @@ -285,21 +210,7 @@ void QtxGroupBox::frameChanged() */ QSize QtxGroupBox::titleSize() const { - QSize sz( 0, 0 ); - - if ( layout() ) - { - QSpacerItem* si = 0; - for ( QLayoutIterator it = layout()->iterator(); it.current() && !si; ++it ) - si = it.current()->spacerItem(); - if ( si ) - sz = si->sizeHint(); - } - - int w = sz.width(); - int h = sz.height() + insideMargin(); - - return QSize( w, h ); + return QSize( fontMetrics().width( title() ), fontMetrics().height() ); } /*! @@ -311,53 +222,84 @@ void QtxGroupBox::updateTitle() return; int align = alignment(); - if ( align == AlignAuto ) - align = QApplication::reverseLayout() ? AlignRight : AlignLeft; if ( title().isEmpty() ) - align = AlignRight; + align = Qt::AlignRight; QSize ts = titleSize(); int m = 5; - int w = frameRect().width() - ts.width(); - if ( align == AlignCenter ) + int w = width() - ts.width(); + if ( align == Qt::AlignCenter ) w = w / 2; w -= m; - QApplication::sendPostedEvents( myContainer, QEvent::ChildInserted ); myContainer->resize( myContainer->minimumSizeHint() ); bool vis = false; - const QObjectList* list = myContainer->children(); - if ( list ) - { - for ( QObjectListIt it( *list ); it.current() && !vis; ++it ) - vis = it.current()->isWidgetType() && - ((QWidget*)it.current())->isVisibleTo( myContainer ); - } + const QObjectList list = myContainer->children(); + for ( QObjectList::const_iterator it = list.begin(); it != list.end() && !vis; ++it ) + vis = (*it)->isWidgetType() && ((QWidget*)(*it))->isVisibleTo( myContainer ); - if ( myContainer->height() > ts.height() || myContainer->width() > w || !vis ) + if ( !vis ) myContainer->hide(); else { int x = 0; - if ( align == AlignRight ) - x = frameRect().left() + m; + if ( align == Qt::AlignRight ) + x = rect().left() + m; else - x = frameRect().right() - myContainer->width() - m; + x = rect().right() - myContainer->width() - m; - int y = frameRect().top() - ( myContainer->height() - frameWidth() ) / 2; + int y = rect().top() - ( myContainer->height() - ts.height() ) / 2; - QPoint pos( x, QMAX( 0, y ) ); - pos = mapToGlobal( pos ); - if ( myContainer->parentWidget() ) - pos = myContainer->parentWidget()->mapFromGlobal( pos ); + QPoint pos( x, qMax( 0, y ) ); myContainer->move( pos ); myContainer->show(); } + if ( layout() ) + { + if ( myContainer && myContainer->isVisibleTo( this ) ) + setInsideMargin( qMax( 0, myContainer->height() - ts.height() ) ); + else + setInsideMargin( 0 ); + } + updateGeometry(); } + +QSize QtxGroupBox::expandTo( const QSize& sz ) const +{ + int sh = 0; + int sw = titleSize().width(); + if ( myContainer && myContainer->isVisibleTo( (QWidget*)this ) ) + { + if ( alignment() == Qt::AlignCenter ) + sw += 2 * ( myContainer->width() + 5 ); + else + sw += 1 * ( myContainer->width() + 5 ); + sw += 20; + sh = myContainer->height() + 5; + } + return QSize( qMax( sz.width(), sw ), qMax( sz.height(), sh ) ); +} + +void QtxGroupBox::setInsideMargin( const int m ) +{ + QVBoxLayout* bl = ::qobject_cast( layout() ); + + if ( !bl ) + return; + + QSpacerItem* spacer = 0; + if ( bl->count() ) + spacer = bl->itemAt( 0 )->spacerItem(); + + if ( !spacer ) + bl->insertSpacing( 0, m ); + else + spacer->changeSize( 0, m ); +} diff --git a/src/Qtx/QtxGroupBox.h b/src/Qtx/QtxGroupBox.h index 20c26fcc7..a2d0715d1 100644 --- a/src/Qtx/QtxGroupBox.h +++ b/src/Qtx/QtxGroupBox.h @@ -24,47 +24,43 @@ #include "Qtx.h" -#include -#include +#include +#include class QTX_EXPORT QtxGroupBox : public QGroupBox { Q_OBJECT public: - QtxGroupBox( QWidget* = 0, const char* = 0 ); - QtxGroupBox( const QString&, QWidget* = 0, const char* = 0 ); - QtxGroupBox( int, Orientation, QWidget* = 0, const char* = 0 ); - QtxGroupBox( int, Orientation, const QString&, QWidget* = 0, const char* = 0 ); + QtxGroupBox( QWidget* = 0 ); + QtxGroupBox( const QString&, QWidget* = 0 ); virtual ~QtxGroupBox(); - virtual void setAlignment( int ); - virtual void setTitle( const QString& ); - virtual void setColumnLayout( int, Orientation ); - virtual void insertTitleWidget( QWidget* ); virtual void removeTitleWidget( QWidget* ); - virtual void show(); - virtual void update(); - - void adjustInsideMargin(); - virtual QSize sizeHint() const; virtual QSize minimumSizeHint() const; virtual bool eventFilter( QObject*, QEvent* ); + QWidget* widget() const; + void setWidget( QWidget* ); + +public slots: + virtual void setVisible( bool ); + protected: - virtual void frameChanged(); virtual void childEvent( QChildEvent* ); virtual void resizeEvent( QResizeEvent* ); - virtual void customEvent( QCustomEvent* ); + virtual void customEvent( QEvent* ); private: void initialize(); void updateTitle(); QSize titleSize() const; + void setInsideMargin( const int ); + QSize expandTo( const QSize& ) const; private: QWidget* myContainer;