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 void shown( QtxToolBar* );
24 void hided( QtxToolBar* );
26 virtual bool eventFilter( QObject*, QEvent* );
29 virtual void customEvent( QCustomEvent* );
32 enum { Install = QEvent::User, Update };
35 void installFilters();
42 void updateVisibility();
51 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
57 if ( myCont->mainWindow() )
58 myState = myCont->mainWindow()->appropriate( myCont );
60 myCont->installEventFilter( this );
61 myVisible = myCont->isVisibleTo( myCont->parentWidget() );
66 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
68 if ( o == myCont && e->type() == QEvent::ChildInserted )
69 QApplication::postEvent( this, new QCustomEvent( Install ) );
71 if ( o != myCont && e->type() == QEvent::IconChange )
74 if ( o != myCont && e->type() == QEvent::CaptionChange )
77 bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
78 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
79 ( o == myCont && e->type() == QEvent::ChildRemoved );
83 QtxToolBar* cont = myCont;
85 QApplication::sendPostedEvents( this, Update );
87 QApplication::postEvent( this, new QCustomEvent( Update ) );
93 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
101 void QtxToolBar::Watcher::hided( QtxToolBar* tb )
109 void QtxToolBar::Watcher::showContainer()
114 QtxToolBar* cont = myCont;
120 void QtxToolBar::Watcher::hideContainer()
125 QtxToolBar* cont = myCont;
131 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
144 void QtxToolBar::Watcher::installFilters()
149 const QObjectList* objList = myCont->children();
153 for ( QObjectListIt it( *objList ); it.current(); ++it )
155 if ( it.current()->isWidgetType() &&
156 qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
157 it.current()->installEventFilter( this );
161 void QtxToolBar::Watcher::updateVisibility()
168 const QObjectList* objList = myCont->children();
171 for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
173 if ( !it.current()->isWidgetType() ||
174 !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
177 QWidget* wid = (QWidget*)it.current();
178 vis = wid->isVisibleTo( wid->parentWidget() );
182 QMainWindow* mw = myCont->mainWindow();
183 if ( mw && myEmpty != vis )
187 mw->setAppropriate( myCont, myState );
190 myState = mw->appropriate( myCont );
191 mw->setAppropriate( myCont, false );
195 vis = myEmpty && myVisible;
196 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
197 vis ? showContainer() : hideContainer();
200 void QtxToolBar::Watcher::updateIcon()
202 if ( !myCont || !myCont->widget() )
205 const QPixmap* ico = myCont->widget()->icon();
206 myCont->setIcon( ico ? *ico : QPixmap() );
209 void QtxToolBar::Watcher::updateCaption()
211 if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
212 myCont->setCaption( myCont->widget()->caption() );
216 Class: QtxToolBar [Public]
220 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
221 QWidget* parent, bool newLine, const char* name, WFlags f )
222 : QToolBar( label, main, parent, newLine, name, f ),
227 myWatcher = new Watcher( this );
230 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
231 QWidget* parent, bool newLine, const char* name, WFlags f )
232 : QToolBar( label, main, parent, newLine, name, f ),
238 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
239 : QToolBar( main, name ),
244 myWatcher = new Watcher( this );
247 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
248 : QToolBar( main, name ),
254 QtxToolBar::~QtxToolBar()
258 void QtxToolBar::setWidget( QWidget* wid )
261 wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
263 QToolBar::setWidget( wid );
268 for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
270 if ( it.current()->widget() == wid )
278 bool QtxToolBar::isStretchable() const
283 void QtxToolBar::setStretchable( const bool on )
285 if ( myStretch == on )
290 boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
292 if ( myStretch != isHorizontalStretchable() ||
293 myStretch != isVerticalStretchable() )
295 if ( orientation() == Horizontal )
296 setHorizontalStretchable( myStretch );
298 setVerticalStretchable( myStretch );
302 QSize QtxToolBar::sizeHint() const
304 QSize sz = QToolBar::sizeHint();
306 if ( place() == InDock && isStretchable() && area() )
308 if ( orientation() == Horizontal )
309 sz.setWidth( area()->width() );
311 sz.setHeight( area()->height() );
317 QSize QtxToolBar::minimumSizeHint() const
319 QSize sz = QToolBar::minimumSizeHint();
321 if ( place() == InDock && isStretchable() && area() )
323 if ( orientation() == Horizontal )
324 sz.setWidth( area()->width() );
326 sz.setHeight( area()->height() );
332 void QtxToolBar::show()
335 myWatcher->shown( this );
340 void QtxToolBar::hide()
343 myWatcher->hided( this );