#include "QtxGroupBox.h"
-#include <qhbox.h>
-#include <qlayout.h>
-#include <qtoolbutton.h>
-#include <qapplication.h>
-#include <qobjectlist.h>
+#include <QLayout>
+#include <QObjectList>
+#include <QToolButton>
+#include <QApplication>
/*!
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();
/*!
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();
*/
QtxGroupBox::~QtxGroupBox()
{
- delete myContainer;
}
/*!
*/
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();
}
if ( !myContainer )
return;
- wid->reparent( myContainer, QPoint( 0, 0 ), true );
+ myContainer->layout()->addWidget( wid );
wid->installEventFilter( this );
updateTitle();
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();
}
/*!
*/
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();
}
*/
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() );
}
/*!
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<QVBoxLayout*>( 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 );
+}