1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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
23 // File: QtxToolBar.cxx
24 // Author: Sergey TELKOV
26 #include "QtxToolBar.h"
29 #include <QMainWindow>
30 #include <QApplication>
33 \class QtxToolBar::Watcher
35 \brief Internal class which goal is to watch parent toolbar state changing.
38 class QtxToolBar::Watcher : public QObject
41 Watcher( QtxToolBar* );
43 void shown( QtxToolBar* );
44 void hidden( QtxToolBar* );
46 virtual bool eventFilter( QObject*, QEvent* );
49 bool isVisible() const;
52 virtual void customEvent( QEvent* );
55 enum { Install = QEvent::User, Update };
58 void installFilters();
63 void updateVisibility();
65 void setEmpty( const bool );
66 void setVisible( const bool );
76 \param cont toolbar to be watched
78 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
84 setVisible( myCont->isVisibleTo( myCont->parentWidget() ) );
86 myCont->installEventFilter( this );
92 \brief Custom event filter.
93 \param o event receiver object
94 \param e event sent to object
95 \return \c true if further event processing should be stopped
97 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
99 if ( o == myCont && e->type() == QEvent::ChildAdded )
100 QApplication::postEvent( this, new QEvent( (QEvent::Type)Install ) );
102 bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
103 e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
104 ( o == myCont && ( e->type() == QEvent::ChildAdded || e->type() == QEvent::ChildRemoved || e->type() == QEvent::Show ||
105 e->type() == QEvent::ShowToParent || e->type() == QEvent::Hide ||
106 e->type() == QEvent::HideToParent || e->type() == QEvent::ActionAdded ||
107 e->type() == QEvent::ActionRemoved ) );
111 QtxToolBar* cont = myCont;
113 QApplication::sendPostedEvents( this, Update );
115 QApplication::postEvent( this, new QEvent( (QEvent::Type)Update ) );
122 \brief Set internal status to "shown"
125 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
134 \brief Set internal status to "hidden"
137 void QtxToolBar::Watcher::hidden( QtxToolBar* tb )
145 bool QtxToolBar::Watcher::isEmpty() const
150 bool QtxToolBar::Watcher::isVisible() const
153 if ( myCont && myCont->toggleViewAction() )
154 vis = myCont->toggleViewAction()->isChecked();
158 void QtxToolBar::Watcher::setEmpty( const bool on )
163 void QtxToolBar::Watcher::setVisible( const bool on )
165 if ( !myCont || !myCont->toggleViewAction() )
168 bool block = myCont->toggleViewAction()->signalsBlocked();
169 myCont->toggleViewAction()->blockSignals( true );
170 myCont->toggleViewAction()->setChecked( on );
171 myCont->toggleViewAction()->blockSignals( block );
175 \brief Show the toolbar being watched
177 void QtxToolBar::Watcher::showContainer()
182 bool vis = isVisible();
184 QtxToolBar* cont = myCont;
193 \brief Hide the toolbar being watched
195 void QtxToolBar::Watcher::hideContainer()
200 bool vis = isVisible();
202 QtxToolBar* cont = myCont;
211 \brief Proces custom events.
212 \param e custom event
214 void QtxToolBar::Watcher::customEvent( QEvent* e )
216 switch ( (int)e->type() )
230 \brief Install this object as event dilter to all children widgets
231 of the toolbar being watched.
233 void QtxToolBar::Watcher::installFilters()
238 const QObjectList& objList = myCont->children();
239 for ( QObjectList::const_iterator it = objList.begin(); it != objList.end(); ++it )
241 if ( (*it)->isWidgetType() && qstrcmp( "qt_dockwidget_internal", (*it)->objectName().toUtf8() ) )
242 (*it)->installEventFilter( this );
247 \brief Update visibility state of all children widgets of the toolbar
250 void QtxToolBar::Watcher::updateVisibility()
256 QList<QAction*> actList = myCont->actions();
257 for ( QList<QAction*>::const_iterator it = actList.begin(); it != actList.end() && !vis; ++it )
259 if ( (*it)->isSeparator() )
262 vis = (*it)->isVisible();
265 QMainWindow* mw = myCont->mainWindow();
266 bool empty = isEmpty();
267 if ( mw && empty == vis )
272 myCont->toggleViewAction()->setVisible( myState );
275 myState = myCont->toggleViewAction()->isVisible();
276 myCont->toggleViewAction()->setVisible( false );
280 vis = (!isEmpty() && isVisible());
281 if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
282 vis ? showContainer() : hideContainer();
287 \brief Enhanced toolbar class.
292 \param watch if \c true the event filter is installed to watch toolbar state changes
293 to update it properly
294 \param label toolbar title
295 \param parent parent widget
297 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QWidget* parent )
298 : QToolBar( label, parent ),
303 myWatcher = new Watcher( this );
305 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
306 mw->addToolBar( this );
311 \param label toolbar title
312 \param parent parent widget
314 QtxToolBar::QtxToolBar( const QString& label, QWidget* parent )
315 : QToolBar( label, parent ),
319 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
320 mw->addToolBar( this );
325 \param watch if \c true the event filter is installed to watch toolbar state changes
326 to update it properly
327 \param parent parent widget
329 QtxToolBar::QtxToolBar( const bool watch, QWidget* parent )
330 : QToolBar( parent ),
335 myWatcher = new Watcher( this );
337 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
338 mw->addToolBar( this );
343 \param parent parent widget
345 QtxToolBar::QtxToolBar( QWidget* parent )
346 : QToolBar( parent ),
350 if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
351 mw->addToolBar( this );
357 QtxToolBar::~QtxToolBar()
362 \brief Show/hide the toolbar.
363 \param on new visibility state
365 void QtxToolBar::setVisible( bool visible )
367 if ( isVisible() != visible ) {
371 myWatcher->shown( this );
373 myWatcher->hidden( this );
377 QToolBar::setVisible( visible );
381 \brief Get parent main window.
382 \return main window pointer
384 QMainWindow* QtxToolBar::mainWindow() const
387 QWidget* wid = parentWidget();
390 mw = ::qobject_cast<QMainWindow*>( wid );
391 wid = wid->parentWidget();
396 bool QtxToolBar::event( QEvent* e )
398 if ( e->type() == QEvent::WindowTitleChange && objectName().isEmpty() )
399 setObjectName( windowTitle() );
401 return QToolBar::event( e );