1 // File: QtxToolBar.cxx
2 // Author: Sergey TELKOV
4 #include "QtxToolBar.h"
9 #include <qobjectlist.h>
10 #include <qmainwindow.h>
11 #include <qapplication.h>
14 Class: QtxToolBar::Watcher [Internal]
15 Descr: Internal object with event filter.
18 class QtxToolBar::Watcher : public QObject
21 Watcher( QtxToolBar* );
23 virtual bool eventFilter( QObject*, QEvent* );
26 virtual void customEvent( QCustomEvent* );
29 enum { Install = QEvent::User, Update };
32 void installFilters();
36 void updateVisibility();
43 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
48 if ( myCont->mainWindow() )
49 myVis = myCont->mainWindow()->appropriate( myCont );
51 myCont->installEventFilter( this );
56 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
58 if ( o == myCont && e->type() == QEvent::ChildInserted )
59 QApplication::postEvent( this, new QCustomEvent( Install ) );
61 if ( o != myCont && e->type() == QEvent::IconChange )
64 if ( o != myCont && e->type() == QEvent::CaptionChange )
67 bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
68 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
69 ( o == myCont && e->type() == QEvent::ChildRemoved );
73 QtxToolBar* cont = myCont;
75 QApplication::sendPostedEvents( this, Update );
77 QApplication::postEvent( this, new QCustomEvent( Update ) );
83 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
96 void QtxToolBar::Watcher::installFilters()
101 const QObjectList* objList = myCont->children();
105 for ( QObjectListIt it( *objList ); it.current(); ++it )
107 if ( it.current()->isWidgetType() &&
108 qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
109 it.current()->installEventFilter( this );
113 void QtxToolBar::Watcher::updateVisibility()
120 const QObjectList* objList = myCont->children();
123 for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
125 if ( !it.current()->isWidgetType() ||
126 !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
129 QWidget* wid = (QWidget*)it.current();
130 vis = wid->isVisibleTo( wid->parentWidget() );
134 QMainWindow* mw = myCont->mainWindow();
138 mw->setAppropriate( myCont, myVis );
141 myVis = mw->appropriate( myCont );
142 mw->setAppropriate( myCont, false );
146 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
147 vis ? myCont->show() : myCont->hide();
150 void QtxToolBar::Watcher::updateIcon()
152 if ( !myCont || !myCont->widget() )
155 const QPixmap* ico = myCont->widget()->icon();
156 myCont->setIcon( ico ? *ico : QPixmap() );
159 void QtxToolBar::Watcher::updateCaption()
161 if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
162 myCont->setCaption( myCont->widget()->caption() );
166 Class: QtxToolBar [Public]
170 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
171 QWidget* parent, bool newLine, const char* name, WFlags f )
172 : QToolBar( label, main, parent, newLine, name, f ),
177 myWatcher = new Watcher( this );
180 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
181 QWidget* parent, bool newLine, const char* name, WFlags f )
182 : QToolBar( label, main, parent, newLine, name, f ),
188 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
189 : QToolBar( main, name ),
194 myWatcher = new Watcher( this );
197 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
198 : QToolBar( main, name ),
204 QtxToolBar::~QtxToolBar()
208 void QtxToolBar::setWidget( QWidget* wid )
211 wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
213 QToolBar::setWidget( wid );
218 for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
220 if ( it.current()->widget() == wid )
228 bool QtxToolBar::isStretchable() const
233 void QtxToolBar::setStretchable( const bool on )
235 if ( myStretch == on )
240 boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
242 if ( myStretch != isHorizontalStretchable() ||
243 myStretch != isVerticalStretchable() )
245 if ( orientation() == Horizontal )
246 setHorizontalStretchable( myStretch );
248 setVerticalStretchable( myStretch );
252 QSize QtxToolBar::sizeHint() const
254 QSize sz = QToolBar::sizeHint();
256 if ( place() == InDock && isStretchable() && area() )
258 if ( orientation() == Horizontal )
259 sz.setWidth( area()->width() );
261 sz.setHeight( area()->height() );
267 QSize QtxToolBar::minimumSizeHint() const
269 QSize sz = QToolBar::minimumSizeHint();
271 if ( place() == InDock && isStretchable() && area() )
273 if ( orientation() == Horizontal )
274 sz.setWidth( area()->width() );
276 sz.setHeight( area()->height() );