1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 // File: QtxToolBar.cxx
20 // Author: Sergey TELKOV
22 #include "QtxToolBar.h"
26 #include <qdockarea.h>
27 #include <qobjectlist.h>
28 #include <qmainwindow.h>
29 #include <qapplication.h>
32 Class: QtxToolBar::Watcher [Internal]
33 Descr: Internal object with event filter.
36 class QtxToolBar::Watcher : public QObject
39 Watcher( QtxToolBar* );
41 void shown( QtxToolBar* );
42 void hided( QtxToolBar* );
44 virtual bool eventFilter( QObject*, QEvent* );
47 virtual void customEvent( QCustomEvent* );
50 enum { Install = QEvent::User, Update };
53 void installFilters();
60 void updateVisibility();
72 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
78 if ( myCont->mainWindow() )
79 myState = myCont->mainWindow()->appropriate( myCont );
81 myCont->installEventFilter( this );
82 myVisible = myCont->isVisibleTo( myCont->parentWidget() );
90 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
92 if ( o == myCont && e->type() == QEvent::ChildInserted )
93 QApplication::postEvent( this, new QCustomEvent( Install ) );
95 if ( o != myCont && e->type() == QEvent::IconChange )
98 if ( o != myCont && e->type() == QEvent::CaptionChange )
101 bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
102 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
103 ( o == myCont && ( e->type() == QEvent::ChildRemoved || e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) );
107 QtxToolBar* cont = myCont;
109 QApplication::sendPostedEvents( this, Update );
111 QApplication::postEvent( this, new QCustomEvent( Update ) );
118 Sets internal visibility state to true
120 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
129 Sets internal visibility state to false
131 void QtxToolBar::Watcher::hided( QtxToolBar* tb )
140 Shows corresponding QtxToolBar
142 void QtxToolBar::Watcher::showContainer()
147 QtxToolBar* cont = myCont;
154 Hides corresponding QtxToolBar
156 void QtxToolBar::Watcher::hideContainer()
161 QtxToolBar* cont = myCont;
168 Event handler of custom events
170 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
184 Installs event filters
186 void QtxToolBar::Watcher::installFilters()
191 const QObjectList* objList = myCont->children();
195 for ( QObjectListIt it( *objList ); it.current(); ++it )
197 if ( it.current()->isWidgetType() &&
198 qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
199 it.current()->installEventFilter( this );
204 Update visibility state
206 void QtxToolBar::Watcher::updateVisibility()
213 const QObjectList* objList = myCont->children();
216 for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
218 if ( !it.current()->isWidgetType() ||
219 !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
222 QWidget* wid = (QWidget*)it.current();
223 vis = wid->isVisibleTo( wid->parentWidget() );
227 QMainWindow* mw = myCont->mainWindow();
228 if ( mw && myEmpty == vis )
232 mw->setAppropriate( myCont, myState );
235 myState = mw->appropriate( myCont );
236 mw->setAppropriate( myCont, false );
240 vis = !myEmpty && myVisible;
241 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
242 vis ? showContainer() : hideContainer();
248 void QtxToolBar::Watcher::updateIcon()
250 if ( !myCont || !myCont->widget() )
253 const QPixmap* ico = myCont->widget()->icon();
254 myCont->setIcon( ico ? *ico : QPixmap() );
260 void QtxToolBar::Watcher::updateCaption()
262 if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
263 myCont->setCaption( myCont->widget()->caption() );
269 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
270 QWidget* parent, bool newLine, const char* name, WFlags f )
271 : QToolBar( label, main, parent, newLine, name, f ),
276 myWatcher = new Watcher( this );
282 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
283 QWidget* parent, bool newLine, const char* name, WFlags f )
284 : QToolBar( label, main, parent, newLine, name, f ),
293 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
294 : QToolBar( main, name ),
299 myWatcher = new Watcher( this );
305 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
306 : QToolBar( main, name ),
315 QtxToolBar::~QtxToolBar()
320 Change the toolbar's main widget
321 \param wid - new main widget
323 void QtxToolBar::setWidget( QWidget* wid )
326 wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
328 QToolBar::setWidget( wid );
333 for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
335 if ( it.current()->widget() == wid )
344 \return true if toolbar is stretchable
346 bool QtxToolBar::isStretchable() const
352 Sets stretchable state of toolbar
353 \param on - new state
355 void QtxToolBar::setStretchable( const bool on )
357 if ( myStretch == on )
362 boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
364 if ( myStretch != isHorizontalStretchable() ||
365 myStretch != isVerticalStretchable() )
367 if ( orientation() == Horizontal )
368 setHorizontalStretchable( myStretch );
370 setVerticalStretchable( myStretch );
375 \return the recommended size for the widget
377 QSize QtxToolBar::sizeHint() const
379 QSize sz = QToolBar::sizeHint();
381 if ( place() == InDock && isStretchable() && area() )
383 if ( orientation() == Horizontal )
384 sz.setWidth( area()->width() );
386 sz.setHeight( area()->height() );
393 \return the recommended minimum size for the widget
395 QSize QtxToolBar::minimumSizeHint() const
397 QSize sz = QToolBar::minimumSizeHint();
399 if ( place() == InDock && isStretchable() && area() )
401 if ( orientation() == Horizontal )
402 sz.setWidth( area()->width() );
404 sz.setHeight( area()->height() );
413 void QtxToolBar::show()
416 myWatcher->shown( this );
424 void QtxToolBar::hide()
427 myWatcher->hided( this );