Back to index

salome-gui  6.5.0
QtxActionToolMgr.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:      QtxActionToolMgr.cxx
00024 // Author:    Alexander SOLOVYOV, Sergey TELKOV
00025 //
00026 #include "QtxActionToolMgr.h"
00027 
00028 #include "QtxAction.h"
00029 #include "QtxToolBar.h"
00030 
00031 #include <QApplication>
00032 #include <QHideEvent>
00033 #include <QMainWindow>
00034 
00071 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
00072 : QtxActionMgr( p ),
00073   myMainWindow( p )
00074 {
00075 }
00076 
00080 QtxActionToolMgr::~QtxActionToolMgr()
00081 {
00082 }
00083 
00088 QMainWindow* QtxActionToolMgr::mainWindow() const
00089 {
00090   return myMainWindow;
00091 }
00092 
00105 int QtxActionToolMgr::createToolBar( const QString& title, const int tid, QMainWindow* mw, bool vis )
00106 {
00107   return createToolBar( title, true, Qt::AllToolBarAreas, tid, mw, vis );
00108 }
00109 
00124 int QtxActionToolMgr::createToolBar( const QString& title, bool floatable, Qt::ToolBarAreas dockAreas, 
00125                                      int tid, QMainWindow* mw, bool vis )
00126 {
00127   static int _toolBarId = -1;
00128 
00129   int tbId = -1;
00130   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
00131   {
00132     if( it.value().toolBar->windowTitle().toLower() == title.toLower() &&
00133         ( !mw || it.value().toolBar->parent()==mw ) )
00134       tbId = it.key();
00135   }
00136 
00137   if ( tbId != -1 )
00138     return tbId;
00139 
00140   QMainWindow* tbw = mw ? mw : mainWindow();
00141   QToolBar* tb = find( title, tbw );
00142 
00143   tbId = tid < 0 ? --_toolBarId : tid;
00144 
00145   myToolBars.insert( tbId, ToolBarInfo() );
00146   ToolBarInfo& tInfo = myToolBars[tbId];
00147 
00148   if ( !tb )
00149   {
00150     tb = new QtxToolBar( true, tbw );
00151     //tb->setVisible( false );  // VSR: create toolbar visible initially
00152     tb->setFloatable( floatable );
00153     tb->setAllowedAreas( dockAreas );
00154     tb->setMovable( dockAreas & Qt::AllToolBarAreas );
00155     //mainWindow()->addToolBar( tb );
00156     tb->setWindowTitle( title );
00157     tb->setObjectName( title );
00158     tb->setToolTip( title );
00159     if ( !vis )
00160       QApplication::postEvent( tb, new QHideEvent());
00161    }
00162 
00163   tInfo.toolBar = tb;
00164   connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
00165 
00166   return tbId;
00167 }
00168 
00175 QToolBar* QtxActionToolMgr::find( const QString& title, QMainWindow* mw ) const
00176 {
00177   if ( !mw )
00178     return 0;
00179 
00180   QString pattern = title.toLower();
00181 
00182   QToolBar* res = 0;
00183   QList<QToolBar*> toolbars = qFindChildren<QToolBar*>( mw );
00184   for ( QList<QToolBar*>::iterator it = toolbars.begin(); it != toolbars.end() && !res; ++it )
00185   {
00186     if ( (*it)->windowTitle().toLower() == pattern )
00187       res = *it;
00188   }
00189   return res;
00190 }
00191 
00196 void QtxActionToolMgr::removeToolBar( const int tid )
00197 {
00198   if ( !myToolBars.contains( tid ) )
00199     return;
00200 
00201   delete myToolBars[tid].toolBar;
00202   myToolBars.remove( tid );
00203 }
00204 
00209 void QtxActionToolMgr::removeToolBar( const QString& title )
00210 {
00211   removeToolBar( find( title ) );
00212 }
00213 
00221 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
00222 {
00223   if ( !contains( id ) || !hasToolBar( tid ) )
00224     return -1;
00225 /*
00226   if ( containsAction( id, tid ) )
00227     remove( id, tid );
00228 */
00229   ToolNode node( id );
00230 
00231   NodeList& list = myToolBars[tid].nodes;
00232   int index = idx < 0 ? list.count() : qMin( idx, (int)list.count() );
00233   list.insert( index, node );
00234   triggerUpdate( tid );
00235 
00236   return id;
00237 }
00238 
00246 int QtxActionToolMgr::insert( QAction* a, const int tid, const int idx )
00247 {
00248   return insert( registerAction( a ), tid, idx );
00249 }
00250 
00258 int QtxActionToolMgr::insert( const int id, const QString& title, const int idx )
00259 {
00260   return insert( id, createToolBar( title ), idx );
00261 }
00262 
00270 int QtxActionToolMgr::insert( QAction* a, const QString& title, const int idx )
00271 {
00272   return insert( registerAction( a ), createToolBar( title ), idx );
00273 }
00274 
00281 int QtxActionToolMgr::append( const int id, const int tid )
00282 {
00283   return insert( id, tid );
00284 }
00285 
00292 int QtxActionToolMgr::append( QAction* a, const int tid )
00293 {
00294   return insert( a, tid );
00295 }
00296 
00303 int QtxActionToolMgr::append( const int id, const QString& title )
00304 {
00305   return insert( id, title );
00306 }
00307 
00314 int QtxActionToolMgr::append( QAction* a, const QString& title )
00315 {
00316   return insert( a, title );
00317 }
00318 
00325 int QtxActionToolMgr::prepend( const int id, const int tid )
00326 {
00327   return insert( id, tid, 0 );
00328 }
00329 
00336 int QtxActionToolMgr::prepend( QAction* a, const int tid )
00337 {
00338   return insert( a, tid, 0 );
00339 }
00340 
00347 int QtxActionToolMgr::prepend( const int id, const QString& title )
00348 {
00349   return insert( id, title, 0 );
00350 }
00351 
00358 int QtxActionToolMgr::prepend( QAction* a, const QString& title )
00359 {
00360   return insert( a, title, 0 );
00361 }
00362 
00368 void QtxActionToolMgr::remove( const int id, const int tid )
00369 {
00370   if ( !myToolBars.contains( tid ) )
00371     return;
00372 
00373   NodeList newList;
00374   const NodeList& nodes = myToolBars[tid].nodes;
00375   for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
00376   {
00377     if ( (*it).id != id )
00378       newList.append( *it );
00379   }
00380 
00381   myToolBars[tid].nodes = newList;
00382 
00383   triggerUpdate( tid );
00384 }
00385 
00391 void QtxActionToolMgr::remove( const int id, const QString& title )
00392 {
00393   remove( id, find( title ) );
00394 }
00395 
00401 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
00402 {
00403   QToolBar* tb = 0;
00404   if ( myToolBars.contains( tid ) )
00405     tb = myToolBars[tid].toolBar;
00406   return tb;
00407 }
00408 
00414 QToolBar* QtxActionToolMgr::toolBar( const QString& title ) const
00415 {
00416   return toolBar( find( title ) );
00417 }
00418 
00423 QIntList QtxActionToolMgr::toolBarsIds() const
00424 {
00425   return myToolBars.keys();
00426 }
00427 
00433 bool QtxActionToolMgr::hasToolBar( const int tid ) const
00434 {
00435   return myToolBars.contains( tid );
00436 }
00437 
00443 bool QtxActionToolMgr::hasToolBar( const QString& title ) const
00444 {
00445   return find( title ) != -1;
00446 }
00447 
00454 bool QtxActionToolMgr::containsAction( const int id, const int tid ) const
00455 {
00456   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
00457   {
00458     if ( tid == -1 || it.key() == tid )
00459     {
00460       const NodeList& list = it.value().nodes;
00461       for ( NodeList::const_iterator nit = list.begin(); nit != list.end(); ++nit )
00462         if ( (*nit).id == id )
00463           return true;
00464     }
00465   }
00466   return false;
00467 }
00468 
00475 int QtxActionToolMgr::index( const int id, const int tid ) const
00476 {
00477   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
00478   {
00479     if ( it.key() == tid )
00480     {
00481       const NodeList& list = it.value().nodes;
00482       int idx = 0;
00483       for ( NodeList::const_iterator nit = list.begin(); nit != list.end(); ++nit, ++idx )
00484         if ( (*nit).id == id ) return idx;
00485     }
00486   }
00487   return -1;
00488 }
00489 
00495 void QtxActionToolMgr::onToolBarDestroyed()
00496 {
00497   myToolBars.remove( find( (QToolBar*)sender() ) );
00498 }
00499 
00505 int QtxActionToolMgr::find( const QString& title ) const
00506 {
00507   int id = -1;
00508   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
00509   {
00510     if ( it.value().toolBar->windowTitle() == title )
00511       id = it.key();
00512   }
00513   return id;
00514 }
00515 
00521 int QtxActionToolMgr::find( QToolBar* tb ) const
00522 {
00523   int id = -1;
00524   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
00525   {
00526     if ( it.value().toolBar == tb )
00527       id = it.key();
00528   }
00529   return id;
00530 }
00531 
00536 void QtxActionToolMgr::updateToolBar( const int tid )
00537 {
00538   if ( !isUpdatesEnabled() )
00539     return;
00540 
00541   if ( !myToolBars.contains( tid ) )
00542     return;
00543 
00544   QToolBar* tb = myToolBars[tid].toolBar;
00545   const NodeList& list = myToolBars[tid].nodes;
00546 
00547   for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
00548   {
00549     QAction* a = action( (*it).id );
00550     tb->removeAction( a );
00551 //    if ( a )
00552 //      a->removeFrom( tb );
00553   }
00554 
00555   tb->clear();
00556 
00557   for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
00558   {
00559     if ( !isVisible( (*itr).id, tid ) )
00560       continue;
00561 
00562     QAction* a = action( (*itr).id );
00563     tb->addAction( a );
00564 //    if ( a )
00565 //      a->addTo( tb );
00566   }
00567 
00568   simplifySeparators( tb );
00569   
00570   // fix of 19921 -->
00571   if ( !tb->isVisible() )
00572     tb->adjustSize();
00573   // fix of 19921 <--
00574 }
00575 
00579 void QtxActionToolMgr::internalUpdate()
00580 {
00581   if ( !isUpdatesEnabled() )
00582     return;
00583 
00584   for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
00585     updateToolBar( it1.key() );
00586 
00587   myUpdateIds.clear();
00588 }
00589 
00594 void QtxActionToolMgr::simplifySeparators( QToolBar* tb )
00595 {
00596   Qtx::simplifySeparators( tb );
00597 }
00598 
00603 void QtxActionToolMgr::show( const int id )
00604 {
00605   setShown( id, true );
00606 }
00607 
00612 void QtxActionToolMgr::hide( const int id )
00613 {
00614   setShown( id, false );
00615 }
00616 
00622 void QtxActionToolMgr::setShown( const int id, const bool on )
00623 {
00624   for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
00625     setVisible( id, it.key(), on );
00626 }
00627 
00633 bool QtxActionToolMgr::isShown( const int id ) const
00634 {
00635   QList<const ToolNode*> nodes;
00636   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
00637   {
00638     const NodeList& nl = it.value().nodes;
00639     for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
00640     {
00641       const ToolNode& node = *itr;
00642       if ( node.id == id )
00643         nodes.append( &node );
00644     }
00645   }
00646 
00647   if ( nodes.isEmpty() )
00648     return false;
00649 
00650   bool vis = true;
00651   for ( QList<const ToolNode*>::iterator itr = nodes.begin(); itr != nodes.end() && vis; ++itr )
00652     vis = (*itr)->visible;
00653 
00654   return vis;
00655 }
00656 
00663 bool QtxActionToolMgr::isVisible( const int id, const int tid ) const
00664 {
00665   if ( !myToolBars.contains( tid ) )
00666     return false;
00667 
00668   bool vis = false;
00669   const ToolBarInfo& inf = myToolBars[tid];
00670   for ( NodeList::const_iterator it = inf.nodes.begin(); it != inf.nodes.end() && !vis; ++it )
00671   {
00672     const ToolNode& node = *it;
00673     if ( node.id == id )
00674 
00675       vis = node.visible;
00676   }
00677   return vis;
00678 }
00679 
00686 void QtxActionToolMgr::setVisible( const int id, const int tid, const bool on )
00687 {
00688   if ( !myToolBars.contains( tid ) )
00689     return;
00690 
00691   bool changed = false;
00692   NodeList& lst = myToolBars[tid].nodes;
00693   for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
00694   {
00695     ToolNode& node = *it;
00696     if ( node.id == id )
00697     {
00698       changed = changed || node.visible != on;
00699       node.visible = on;
00700     }
00701   }
00702 
00703   if ( changed )
00704     triggerUpdate( tid );
00705 }
00706 
00713 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
00714 {
00715   ToolCreator cr( &r, this );
00716   return r.read( fname, cr );
00717 }
00718 
00724 void QtxActionToolMgr::updateContent()
00725 {
00726   if ( !isUpdatesEnabled() )
00727     return;
00728 
00729   for ( QMap<int,int>::const_iterator it = myUpdateIds.constBegin(); it != myUpdateIds.constEnd(); ++it )
00730     updateToolBar( it.key() );
00731   myUpdateIds.clear();
00732 }
00733 
00738 void QtxActionToolMgr::triggerUpdate( const int tid )
00739 {
00740   myUpdateIds.insert( tid, 0 );
00741   QtxActionMgr::triggerUpdate();
00742 }
00743 
00744 
00758 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
00759                                             QtxActionToolMgr* mgr )
00760 : QtxActionMgr::Creator( r ),
00761   myMgr( mgr )
00762 {
00763 }
00764 
00768 QtxActionToolMgr::ToolCreator::~ToolCreator()
00769 {
00770 }
00771 
00780 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool /*subMenu*/,
00781                                            const ItemAttributes& attr, const int tid )
00782 {  
00783   if( !myMgr || !reader() )
00784     return -1;
00785 
00786   QString label   = reader()->option( "label",     "label"     ),
00787           id      = reader()->option( "id",        "id"        ),
00788           pos     = reader()->option( "pos",       "pos"       ),
00789           group   = reader()->option( "group",     "group"     ),
00790           tooltip = reader()->option( "tooltip",   "tooltip"   ),
00791           sep     = reader()->option( "separator", "separator" ),
00792           accel   = reader()->option( "accel",     "accel"     ),
00793           icon    = reader()->option( "icon",      "icon"      ),
00794           toggle  = reader()->option( "toggle",    "toggle"    );
00795 
00796   int res = -1, actId = intValue( attr, id, -1 );
00797   if( tid==-1 )
00798     res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
00799   else if( tag==sep )
00800     res = myMgr->insert( separator(), tid, intValue( attr, pos, -1 ) );
00801   else
00802   {
00803     QIcon set;
00804     QPixmap pix;
00805     QString name = strValue( attr, icon );
00806     if( !name.isEmpty() && loadPixmap( name, pix ) )
00807       set = QIcon( pix );
00808 
00809     QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set, strValue( attr, label ),
00810                                        QKeySequence( strValue( attr, accel ) ), myMgr );
00811     QString toggleact = strValue( attr, toggle );
00812     newAct->setCheckable( !toggleact.isEmpty() );
00813     newAct->setChecked( toggleact.toLower() == "true" );
00814         
00815     connect( newAct );
00816     int aid = myMgr->registerAction( newAct, actId );
00817     res = myMgr->insert( aid, tid, intValue( attr, pos, -1 ) );
00818   }
00819 
00820   return res;
00821 }