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();
69 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
75 if ( myCont->mainWindow() )
76 myState = myCont->mainWindow()->appropriate( myCont );
78 myCont->installEventFilter( this );
79 myVisible = myCont->isVisibleTo( myCont->parentWidget() );
84 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
86 if ( o == myCont && e->type() == QEvent::ChildInserted )
87 QApplication::postEvent( this, new QCustomEvent( Install ) );
89 if ( o != myCont && e->type() == QEvent::IconChange )
92 if ( o != myCont && e->type() == QEvent::CaptionChange )
95 bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
96 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
97 ( o == myCont && ( e->type() == QEvent::ChildRemoved || e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) );
101 QtxToolBar* cont = myCont;
103 QApplication::sendPostedEvents( this, Update );
105 QApplication::postEvent( this, new QCustomEvent( Update ) );
111 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
119 void QtxToolBar::Watcher::hided( QtxToolBar* tb )
127 void QtxToolBar::Watcher::showContainer()
132 QtxToolBar* cont = myCont;
138 void QtxToolBar::Watcher::hideContainer()
143 QtxToolBar* cont = myCont;
149 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
162 void QtxToolBar::Watcher::installFilters()
167 const QObjectList* objList = myCont->children();
171 for ( QObjectListIt it( *objList ); it.current(); ++it )
173 if ( it.current()->isWidgetType() &&
174 qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
175 it.current()->installEventFilter( this );
179 void QtxToolBar::Watcher::updateVisibility()
186 const QObjectList* objList = myCont->children();
189 for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
191 if ( !it.current()->isWidgetType() ||
192 !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
195 QWidget* wid = (QWidget*)it.current();
196 vis = wid->isVisibleTo( wid->parentWidget() );
200 QMainWindow* mw = myCont->mainWindow();
201 if ( mw && myEmpty == vis )
205 mw->setAppropriate( myCont, myState );
208 myState = mw->appropriate( myCont );
209 mw->setAppropriate( myCont, false );
213 vis = !myEmpty && myVisible;
214 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
215 vis ? showContainer() : hideContainer();
218 void QtxToolBar::Watcher::updateIcon()
220 if ( !myCont || !myCont->widget() )
223 const QPixmap* ico = myCont->widget()->icon();
224 myCont->setIcon( ico ? *ico : QPixmap() );
227 void QtxToolBar::Watcher::updateCaption()
229 if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
230 myCont->setCaption( myCont->widget()->caption() );
234 Class: QtxToolBar [Public]
238 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
239 QWidget* parent, bool newLine, const char* name, WFlags f )
240 : QToolBar( label, main, parent, newLine, name, f ),
245 myWatcher = new Watcher( this );
248 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
249 QWidget* parent, bool newLine, const char* name, WFlags f )
250 : QToolBar( label, main, parent, newLine, name, f ),
256 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
257 : QToolBar( main, name ),
262 myWatcher = new Watcher( this );
265 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
266 : QToolBar( main, name ),
272 QtxToolBar::~QtxToolBar()
276 void QtxToolBar::setWidget( QWidget* wid )
279 wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
281 QToolBar::setWidget( wid );
286 for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
288 if ( it.current()->widget() == wid )
296 bool QtxToolBar::isStretchable() const
301 void QtxToolBar::setStretchable( const bool on )
303 if ( myStretch == on )
308 boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
310 if ( myStretch != isHorizontalStretchable() ||
311 myStretch != isVerticalStretchable() )
313 if ( orientation() == Horizontal )
314 setHorizontalStretchable( myStretch );
316 setVerticalStretchable( myStretch );
320 QSize QtxToolBar::sizeHint() const
322 QSize sz = QToolBar::sizeHint();
324 if ( place() == InDock && isStretchable() && area() )
326 if ( orientation() == Horizontal )
327 sz.setWidth( area()->width() );
329 sz.setHeight( area()->height() );
335 QSize QtxToolBar::minimumSizeHint() const
337 QSize sz = QToolBar::minimumSizeHint();
339 if ( place() == InDock && isStretchable() && area() )
341 if ( orientation() == Horizontal )
342 sz.setWidth( area()->width() );
344 sz.setHeight( area()->height() );
350 void QtxToolBar::show()
353 myWatcher->shown( this );
358 void QtxToolBar::hide()
361 myWatcher->hided( this );