1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxToolBar.cxx
23 // Author: Sergey TELKOV
25 #include "QtxToolBar.h"
28 #include <QMainWindow>
29 #include <QApplication>
32 \class QtxToolBar::Watcher
34 \brief Internal class which goal is to watch parent toolbar state changing.
37 class QtxToolBar::Watcher : public QObject
40 Watcher( QtxToolBar* );
42 void shown( QtxToolBar* );
43 void hidden( QtxToolBar* );
45 virtual bool eventFilter( QObject*, QEvent* );
48 bool isVisible() const;
51 virtual void customEvent( QEvent* );
54 enum { Install = QEvent::User, Update };
57 void installFilters();
62 void updateVisibility();
64 void setEmpty( const bool );
65 void setVisible( const bool );
75 \param cont toolbar to be watched
77 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
83 setVisible( myCont->isVisibleTo( myCont->parentWidget() ) );
85 myCont->installEventFilter( this );
91 \brief Custom event filter.
92 \param o event receiver object
93 \param e event sent to object
94 \return \c true if further event processing should be stopped
96 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
98 if ( o == myCont && e->type() == QEvent::ChildAdded )
99 QApplication::postEvent( this, new QEvent( (QEvent::Type)Install ) );
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 ||
104 e->type() == QEvent::ShowToParent || e->type() == QEvent::ActionAdded ||
105 e->type() == QEvent::ActionRemoved ) );
109 QtxToolBar* cont = myCont;
111 QApplication::sendPostedEvents( this, Update );
113 QApplication::postEvent( this, new QEvent( (QEvent::Type)Update ) );
120 \brief Set internal status to "shown"
123 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
132 \brief Set internal status to "hidden"
135 void QtxToolBar::Watcher::hidden( QtxToolBar* tb )
143 bool QtxToolBar::Watcher::isEmpty() const
148 bool QtxToolBar::Watcher::isVisible() const
151 if ( myCont && myCont->toggleViewAction() )
152 vis = myCont->toggleViewAction()->isChecked();
156 void QtxToolBar::Watcher::setEmpty( const bool on )
161 void QtxToolBar::Watcher::setVisible( const bool on )
163 if ( !myCont || !myCont->toggleViewAction() )
166 bool block = myCont->toggleViewAction()->signalsBlocked();
167 myCont->toggleViewAction()->blockSignals( true );
168 myCont->toggleViewAction()->setChecked( on );
169 myCont->toggleViewAction()->blockSignals( block );
173 \brief Show the toolbar being watched
175 void QtxToolBar::Watcher::showContainer()
180 bool vis = isVisible();
182 QtxToolBar* cont = myCont;
191 \brief Hide the toolbar being watched
193 void QtxToolBar::Watcher::hideContainer()
198 bool vis = isVisible();
200 QtxToolBar* cont = myCont;
209 \brief Proces custom events.
210 \param e custom event
212 void QtxToolBar::Watcher::customEvent( QEvent* e )
228 \brief Install this object as event dilter to all children widgets
229 of the toolbar being watched.
231 void QtxToolBar::Watcher::installFilters()
236 const QObjectList& objList = myCont->children();
237 for ( QObjectList::const_iterator it = objList.begin(); it != objList.end(); ++it )
239 if ( (*it)->isWidgetType() && qstrcmp( "qt_dockwidget_internal", (*it)->objectName().toLatin1() ) )
240 (*it)->installEventFilter( this );
245 \brief Update visibility state of all children widgets of the toolbar
248 void QtxToolBar::Watcher::updateVisibility()
254 QList<QAction*> actList = myCont->actions();
255 for ( QList<QAction*>::const_iterator it = actList.begin(); it != actList.end() && !vis; ++it )
257 if ( (*it)->isSeparator() )
260 vis = (*it)->isVisible();
263 QMainWindow* mw = myCont->mainWindow();
264 bool empty = isEmpty();
265 if ( mw && empty == vis )
270 myCont->toggleViewAction()->setVisible( myState );
273 myState = myCont->toggleViewAction()->isVisible();
274 myCont->toggleViewAction()->setVisible( false );
278 vis = !empty && isVisible();
279 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
280 vis ? showContainer() : hideContainer();
285 \brief Enhanced toolbar class.
290 \param watch if \c true the event filter is installed to watch toolbar state changes
291 to update it properly
292 \param label toolbar title
293 \param parent parent widget
295 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QWidget* parent )
296 : QToolBar( label, parent ),
301 myWatcher = new Watcher( this );
303 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
304 mw->addToolBar( this );
309 \param label toolbar title
310 \param parent parent widget
312 QtxToolBar::QtxToolBar( const QString& label, QWidget* parent )
313 : QToolBar( label, parent ),
317 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
318 mw->addToolBar( this );
323 \param watch if \c true the event filter is installed to watch toolbar state changes
324 to update it properly
325 \param parent parent widget
327 QtxToolBar::QtxToolBar( const bool watch, QWidget* parent )
328 : QToolBar( parent ),
333 myWatcher = new Watcher( this );
335 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
336 mw->addToolBar( this );
341 \param parent parent widget
343 QtxToolBar::QtxToolBar( QWidget* parent )
344 : QToolBar( parent ),
348 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
349 mw->addToolBar( this );
355 QtxToolBar::~QtxToolBar()
360 \brief Show/hide the toolbar.
361 \param on new visibility state
363 void QtxToolBar::setVisible( bool visible )
368 myWatcher->shown( this );
370 myWatcher->hidden( this );
373 QToolBar::setVisible( visible );
377 \brief Get parent main window.
378 \return main window pointer
380 QMainWindow* QtxToolBar::mainWindow() const
383 QWidget* wid = parentWidget();
386 mw = ::qobject_cast<QMainWindow*>( wid );
387 wid = wid->parentWidget();
392 bool QtxToolBar::event( QEvent* e )
394 if ( e->type() == QEvent::WindowTitleChange && objectName().isEmpty() )
395 setObjectName( windowTitle() );
397 return QToolBar::event( e );