Back to index

salome-gui  6.5.0
QtxToolBar.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxToolBar.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxToolBar.h"
00027 
00028 #include <QAction>
00029 #include <QMainWindow>
00030 #include <QApplication>
00031 
00038 class QtxToolBar::Watcher : public QObject
00039 {
00040 public:
00041   Watcher( QtxToolBar* );
00042 
00043   void         shown( QtxToolBar* );
00044   void         hidden( QtxToolBar* );
00045 
00046   virtual bool eventFilter( QObject*, QEvent* );
00047 
00048   bool           isEmpty() const;
00049   bool           isVisible() const;
00050 
00051 protected:
00052   virtual void customEvent( QEvent* );
00053 
00054 private:
00055   enum { Install = QEvent::User, Update };
00056 
00057 private:
00058   void         installFilters();
00059 
00060   void         showContainer();
00061   void         hideContainer();
00062 
00063   void         updateVisibility();
00064 
00065   void         setEmpty( const bool );
00066   void         setVisible( const bool );
00067 
00068 private:
00069   QtxToolBar*  myCont;
00070   bool         myState;
00071   bool         myEmpty;
00072 };
00073 
00078 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
00079 : QObject( cont ),
00080   myCont( cont ),
00081   myState( true ),
00082   myEmpty( false )
00083 {
00084   setVisible( myCont->isVisibleTo( myCont->parentWidget() ) );
00085 
00086   myCont->installEventFilter( this );
00087 
00088   installFilters();
00089 }
00090 
00097 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
00098 {
00099   if ( o == myCont && e->type() == QEvent::ChildAdded )
00100     QApplication::postEvent( this, new QEvent( (QEvent::Type)Install ) );
00101 
00102   bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
00103                                    e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
00104                 ( o == myCont && ( e->type() == QEvent::ChildAdded   || e->type() == QEvent::ChildRemoved || e->type() == QEvent::Show ||
00105                                    e->type() == QEvent::ShowToParent || e->type() == QEvent::Hide ||
00106                                e->type() == QEvent::HideToParent || e->type() == QEvent::ActionAdded ||
00107                                    e->type() == QEvent::ActionRemoved ) );
00108 
00109   if ( updVis )
00110   {
00111     QtxToolBar* cont = myCont;
00112     myCont = 0;
00113     QApplication::sendPostedEvents( this, Update );
00114     myCont = cont;
00115     QApplication::postEvent( this, new QEvent( (QEvent::Type)Update ) );
00116   }
00117 
00118   return false;
00119 }
00120 
00125 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
00126 {
00127   if ( tb != myCont )
00128     return;
00129 
00130   setVisible( true );
00131 }
00132 
00137 void QtxToolBar::Watcher::hidden( QtxToolBar* tb )
00138 {
00139   if ( tb != myCont )
00140     return;
00141 
00142   setVisible( false );
00143 }
00144 
00145 bool QtxToolBar::Watcher::isEmpty() const
00146 {
00147   return myEmpty;
00148 }
00149 
00150 bool QtxToolBar::Watcher::isVisible() const
00151 {
00152   bool vis = false;
00153   if ( myCont && myCont->toggleViewAction() )
00154     vis = myCont->toggleViewAction()->isChecked();
00155   return vis;
00156 }
00157 
00158 void QtxToolBar::Watcher::setEmpty( const bool on )
00159 {
00160   myEmpty = on;
00161 }
00162 
00163 void QtxToolBar::Watcher::setVisible( const bool on )
00164 {
00165   if ( !myCont || !myCont->toggleViewAction() )
00166     return;
00167 
00168   bool block = myCont->toggleViewAction()->signalsBlocked();
00169   myCont->toggleViewAction()->blockSignals( true );
00170   myCont->toggleViewAction()->setChecked( on );
00171   myCont->toggleViewAction()->blockSignals( block );
00172 }
00173 
00177 void QtxToolBar::Watcher::showContainer()
00178 {
00179   if ( !myCont )
00180     return;
00181 
00182   bool vis = isVisible();
00183 
00184   QtxToolBar* cont = myCont;
00185   myCont = 0;
00186   cont->show();
00187   myCont = cont;
00188 
00189   setVisible( vis );
00190 }
00191 
00195 void QtxToolBar::Watcher::hideContainer()
00196 {
00197   if ( !myCont )
00198     return;
00199 
00200   bool vis = isVisible();
00201 
00202   QtxToolBar* cont = myCont;
00203   myCont = 0;
00204   cont->hide();
00205   myCont = cont;
00206 
00207   setVisible( vis );
00208 }
00209 
00214 void QtxToolBar::Watcher::customEvent( QEvent* e )
00215 {
00216   switch ( e->type() )
00217   {
00218   case Install:
00219     installFilters();
00220     break;
00221   case Update:
00222     updateVisibility();
00223     break;
00224   default:
00225     break;
00226   }
00227 }
00228 
00233 void QtxToolBar::Watcher::installFilters()
00234 {
00235   if ( !myCont )
00236     return;
00237 
00238   const QObjectList& objList = myCont->children();
00239   for ( QObjectList::const_iterator it = objList.begin(); it != objList.end(); ++it )
00240   {
00241     if ( (*it)->isWidgetType() && qstrcmp( "qt_dockwidget_internal", (*it)->objectName().toLatin1() ) )
00242       (*it)->installEventFilter( this );
00243   }
00244 }
00245 
00250 void QtxToolBar::Watcher::updateVisibility()
00251 {
00252   if ( !myCont )
00253     return;
00254 
00255   bool vis = false;
00256   QList<QAction*> actList = myCont->actions();
00257   for ( QList<QAction*>::const_iterator it = actList.begin(); it != actList.end() && !vis; ++it )
00258   {
00259     if ( (*it)->isSeparator() )
00260       continue;
00261 
00262     vis = (*it)->isVisible();
00263   }
00264 
00265   QMainWindow* mw = myCont->mainWindow();
00266   bool empty = isEmpty();
00267   if ( mw && empty == vis )
00268   {
00269     empty = !vis;
00270     setEmpty( empty );
00271     if ( !empty )
00272       myCont->toggleViewAction()->setVisible( myState );
00273     else
00274     {
00275       myState = myCont->toggleViewAction()->isVisible();
00276       myCont->toggleViewAction()->setVisible( false );
00277     }
00278   }
00279 
00280   vis = (!isEmpty() && isVisible());
00281   if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
00282     vis ? showContainer() : hideContainer();
00283 }
00284 
00297 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QWidget* parent )
00298 : QToolBar( label, parent ),
00299   myWatcher( 0 ),
00300   myStretch( false )
00301 {
00302   if ( watch )
00303     myWatcher = new Watcher( this );
00304 
00305   if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
00306     mw->addToolBar( this );
00307 }
00308 
00314 QtxToolBar::QtxToolBar( const QString& label, QWidget* parent )
00315 : QToolBar( label, parent ),
00316   myWatcher( 0 ),
00317   myStretch( false )
00318 {
00319   if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
00320     mw->addToolBar( this );
00321 }
00322 
00329 QtxToolBar::QtxToolBar( const bool watch, QWidget* parent )
00330 : QToolBar( parent ),
00331   myWatcher( 0 ),
00332   myStretch( false )
00333 {
00334   if ( watch )
00335     myWatcher = new Watcher( this );
00336 
00337   if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
00338     mw->addToolBar( this );
00339 }
00340 
00345 QtxToolBar::QtxToolBar( QWidget* parent )
00346 : QToolBar( parent ),
00347   myWatcher( 0 ),
00348   myStretch( false )
00349 {
00350   if ( QMainWindow* mw = ::qobject_cast<QMainWindow*>( parent ) )
00351     mw->addToolBar( this );
00352 }
00353 
00357 QtxToolBar::~QtxToolBar()
00358 {
00359 }
00360 
00365 void QtxToolBar::setVisible( bool visible )
00366 {
00367   if ( isVisible() == visible ) return;
00368   if ( myWatcher )
00369   {
00370     if ( visible )
00371       myWatcher->shown( this );
00372     else
00373       myWatcher->hidden( this );
00374   }
00375 
00376   QToolBar::setVisible( visible );
00377 }
00378 
00383 QMainWindow* QtxToolBar::mainWindow() const
00384 {
00385   QMainWindow* mw = 0;
00386   QWidget* wid = parentWidget();
00387   while ( !mw && wid )
00388   {
00389     mw = ::qobject_cast<QMainWindow*>( wid );
00390     wid = wid->parentWidget();
00391   }
00392   return mw;
00393 }
00394 
00395 bool QtxToolBar::event( QEvent* e )
00396 {
00397   if ( e->type() == QEvent::WindowTitleChange && objectName().isEmpty() )
00398     setObjectName( windowTitle() );
00399 
00400   return QToolBar::event( e );
00401 }