Back to index

salome-gui  6.5.0
QtxActionSet.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxActionSet.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxActionSet.h"
00024 
00025 #include <QApplication>
00026 
00045 QtxActionSet::QtxActionSet( QObject* parent )
00046 : QtxAction( parent )
00047 {
00048   connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
00049 
00050   setVisible( false );
00051 }
00052 
00056 QtxActionSet::~QtxActionSet()
00057 {
00058 }
00059 
00064 QList<QAction*> QtxActionSet::actions() const
00065 {
00066   return mySet;
00067 }
00068 
00073 void QtxActionSet::setActions( const QList<QAction*>& lst )
00074 {
00075   for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
00076   {
00077     if ( !lst.contains( *it ) )
00078       delete *it;
00079   }
00080 
00081   mySet.clear();
00082 
00083   insertActions( lst );
00084 }
00085 
00091 void QtxActionSet::insertActions( const QList<QAction*>& lst, const int index )
00092 {
00093   int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
00094 
00095   for ( QList<QAction*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
00096   {
00097     QAction* a = *it;
00098     int ident = generateId();
00099 
00100     a->setParent( this );
00101     mySet.insert( idx++, a );
00102     a->setData( ident );
00103 
00104     connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
00105   }
00106 
00107   updateAction();
00108 }
00109 
00120 int QtxActionSet::insertAction( QAction* a, const int id, const int index )
00121 {
00122   if ( !a )
00123     return -1;
00124 
00125   int ident = id < 0 ? generateId() : id;
00126   int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
00127 
00128   a->setParent( this );
00129   mySet.insert( idx, a );
00130   a->setData( ident );
00131 
00132   connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
00133 
00134   actionAdded( a );
00135 
00136   updateAction();
00137 
00138   return ident;
00139 }
00140 
00151 int QtxActionSet::insertAction( const QString& txt, const int id, const int index )
00152 {
00153   return insertAction( new QtxAction( txt, txt, 0, this ), id, index );
00154 }
00155 
00167 int QtxActionSet::insertAction( const QString& txt, const QIcon& icon, const int id, const int index )
00168 {
00169   return insertAction( new QtxAction( txt, icon, txt, 0, this ), id, index );
00170 }
00171 
00179 void QtxActionSet::removeAction( QAction* a )
00180 {
00181   if ( !mySet.contains( a ) )
00182     return;
00183 
00184   mySet.removeAll( a );
00185   actionRemoved( a );
00186   delete a;
00187 }
00188 
00196 void QtxActionSet::removeAction( const int id )
00197 {
00198   removeAction( action( id ) );
00199 }
00200 
00206 void QtxActionSet::clear()
00207 {
00208   qDeleteAll( mySet );
00209   mySet.clear();
00210 
00211   updateAction();
00212 }
00213 
00219 void QtxActionSet::onChanged()
00220 {
00221   QList<QWidget*> lst = createdWidgets();
00222   for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
00223     (*it)->setEnabled( isEnabled() );
00224 
00225   if ( !isVisible() || !isEmptyAction() )
00226     return;
00227 
00228   bool block = signalsBlocked();
00229   blockSignals( true );
00230   setVisible( false );
00231   blockSignals( block );
00232 }
00233 
00238 void QtxActionSet::onActionTriggered( bool /*on*/ )
00239 {
00240   QAction* a = ::qobject_cast<QAction*>( sender() );
00241   if ( !a )
00242     return;
00243 
00244   int id = actionId( a );
00245   if ( id != -1 )
00246     emit triggered( id );
00247   emit triggered( a );
00248 }
00249 
00254 void QtxActionSet::addedTo( QWidget* w )
00255 {
00256   QtxAction::addedTo( w );
00257 
00258   updateAction( w );
00259 }
00260 
00265 void QtxActionSet::removedFrom( QWidget* w )
00266 {
00267   QtxAction::removedFrom( w );
00268 
00269   updateAction( w );
00270 }
00271 
00277 QAction* QtxActionSet::action( int id ) const
00278 {
00279   QAction* a = 0;
00280   for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end() && !a; ++it )
00281   {
00282     if ( actionId( *it ) == id )
00283       a = *it;
00284   }
00285   return a;
00286 }
00287 
00293 int QtxActionSet::actionId( QAction* a ) const
00294 {
00295   int id = -1;
00296   if ( a && a->data().canConvert( QVariant::Int ) )
00297     id = a->data().toInt();
00298   return id;
00299 }
00300 
00306 void QtxActionSet::setActionId( QAction* a, const int id )
00307 {
00308   if ( !a || id == -1 )
00309     return;
00310 
00311   a->setData( id );
00312 }
00313 
00317 void QtxActionSet::actionAdded( QAction* )
00318 {
00319 }
00320 
00324 void QtxActionSet::actionRemoved( QAction* )
00325 {
00326 }
00327 
00332 int QtxActionSet::generateId() const
00333 {
00334   QMap<int, int> map;
00335   for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end(); ++it )
00336     map.insert( (*it)->data().toInt(), 0 );
00337 
00338   int id = -2;
00339   while ( map.contains( id ) )
00340     id--;
00341 
00342   return id;
00343 }
00344 
00348 void QtxActionSet::updateAction()
00349 {
00350   QList<QWidget*> lst = associatedWidgets();
00351   for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
00352     updateAction( *it );
00353 }
00354 
00359 void QtxActionSet::updateAction( QWidget* w )
00360 {
00361   if ( !w )
00362     return;
00363 
00364   for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
00365     w->removeAction( *it );
00366 
00367   if ( !w->actions().contains( this ) )
00368     return;
00369 
00370   QAction* first = 0;
00371   for ( int i = 0; i < mySet.count(); i++ )
00372   {
00373     QAction* a = mySet.at( i );
00374     if ( !first )
00375       first = a;
00376     w->insertAction( this, a );
00377   }
00378   if ( first )
00379   {
00380     QApplication::instance()->removeEventFilter( this );
00381 
00382     w->insertAction( first, this );
00383 
00384     QApplication::instance()->installEventFilter( this );
00385   }
00386 }
00387 
00393 bool QtxActionSet::isEmptyAction() const
00394 {
00395   return true;
00396 }
00397