Back to index

salome-gui  6.5.0
LightApp_ModuleAction.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   : LightApp_ModuleAction.cxx
00021 // Author : Sergey TELKOV, Vadim SANDLER
00022 //
00023 #include "LightApp_ModuleAction.h"
00024 
00025 #include <QtxComboBox.h>
00026 #include <QtxActionSet.h>
00027 #include <QVBoxLayout>
00028 #include <QApplication>
00029 #include <QEvent>
00030 
00037 class LightApp_ModuleAction::ActionSet : public QtxActionSet
00038 {
00039 public:
00040   ActionSet( QObject* );
00041   QAction* moduleAction( const QString& ) const;
00042   int      moduleId( const QString& ) const;
00043   int      moduleId( QAction* ) const;
00044   void     setVisible( bool );
00045 };
00046 
00052 LightApp_ModuleAction::ActionSet::ActionSet( QObject* parent )
00053 : QtxActionSet( parent ) 
00054 {
00055 }
00056 
00063 QAction* LightApp_ModuleAction::ActionSet::moduleAction( const QString& name ) const
00064 {
00065   QAction* a = 0;
00066 
00067   QList<QAction*> alist = actions();
00068   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
00069   {
00070     if ( (*it)->text() == name )
00071       a = *it;
00072   }
00073 
00074   return a;
00075 }
00076 
00083 int LightApp_ModuleAction::ActionSet::moduleId( const QString& name ) const
00084 {
00085   int id = -1;
00086 
00087   QList<QAction*> alist = actions();
00088   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && id == -1; ++it )
00089   {
00090     if ( (*it)->text() == name )
00091       id = actionId( *it );
00092   }
00093 
00094   return id;
00095 }
00096 
00103 int LightApp_ModuleAction::ActionSet::moduleId( QAction* a ) const
00104 {
00105   return actionId( a );
00106 }
00107 
00113 void LightApp_ModuleAction::ActionSet::setVisible( bool on )
00114 {
00115   QList<QAction*> alist = actions();
00116   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
00117     (*it)->setVisible( on );
00118 
00119   QtxActionSet::setVisible( on );
00120 }
00121 
00133 LightApp_ModuleAction::ComboAction::ComboAction( QObject* parent )
00134 : QtxAction( parent )
00135 {
00136 }
00137 
00143 QList<QtxComboBox*> LightApp_ModuleAction::ComboAction::widgets() const
00144 {
00145   QList<QtxComboBox*> lst;
00146 
00147   QList<QWidget*> wlist = createdWidgets();
00148   for ( QList<QWidget*>::const_iterator wit = wlist.begin(); wit != wlist.end(); ++wit )
00149     lst += qFindChildren<QtxComboBox *>(*wit);
00150 
00151   return lst;
00152 }
00153 
00160 QWidget* LightApp_ModuleAction::ComboAction::createWidget( QWidget* parent )
00161 {
00162   if ( !parent->inherits( "QToolBar" ) )
00163     return 0;
00164 
00165   QWidget* dumb = new QWidget( parent );
00166   QVBoxLayout* l = new QVBoxLayout( dumb );
00167   l->setSpacing( 0 ); l->setMargin( 0 );
00168   QtxComboBox* cb = new QtxComboBox( dumb );
00169   cb->setSizeAdjustPolicy( QComboBox::AdjustToContents );
00170   cb->setFocusPolicy( Qt::NoFocus );
00171   l->addWidget( cb );
00172   l->addSpacing( 3 );
00173 
00174   connect( cb, SIGNAL( activatedId( int ) ), this, SIGNAL( activatedId( int ) ) );
00175 
00176   return dumb;
00177 }
00178 
00191 class LightApp_ModuleAction::ActivateEvent : public QEvent
00192 {
00193 public:
00194   ActivateEvent( QEvent::Type type, int id ) : QEvent( type ), myId( id ) {};
00195   ~ActivateEvent() {};
00196 
00197   int     id() const { return myId; }
00198 
00199 private:
00200   int     myId;
00201 };
00202 
00243 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, QObject* parent )
00244 : QtxAction( parent )
00245 {
00246   setText( text );
00247   init();
00248 }
00249 
00259 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, const QIcon& ico, QObject* parent )
00260 : QtxAction( parent )
00261 {
00262   setText( text );
00263   setIcon( ico );
00264   init();
00265 }
00266 
00270 LightApp_ModuleAction::~LightApp_ModuleAction()
00271 {
00272 }
00273 
00278 QStringList LightApp_ModuleAction::modules() const
00279 {
00280   QStringList lst;
00281 
00282   QList<QAction*> alist = mySet->actions();
00283   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
00284     lst.append( (*it)->text() );
00285 
00286   return lst;
00287 }
00288 
00295 QIcon LightApp_ModuleAction::moduleIcon( const QString& name ) const
00296 {
00297   QAction* a = mySet->moduleAction( name );
00298   return a ? a->icon() : QIcon();
00299 }
00300 
00307 void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico )
00308 {
00309   QAction* a = mySet->moduleAction( name );
00310   if ( !a )
00311     return;
00312 
00313   a->setIcon( ico );
00314   update();
00315 }
00316 
00324 void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico,
00325                                           const int idx )
00326 {
00327   QtxAction* a = new QtxAction( name, ico, name, 0, this, true );
00328   a->setStatusTip( tr( "ACTIVATE_MODULE_TOP" ).arg( name ) );
00329 
00330   mySet->insertAction( a, -1, idx );
00331   update();
00332 }
00333 
00339 void LightApp_ModuleAction::removeModule( const QString& name )
00340 {
00341   int id = mySet->moduleId( name );
00342   if ( id == -1 )
00343     return;
00344 
00345   mySet->removeAction( id );
00346   update();
00347 }
00348 
00358 QString LightApp_ModuleAction::activeModule() const
00359 {
00360   QAction* a = active();
00361   return a ? a->text() : QString();
00362 }
00363 
00372 void LightApp_ModuleAction::setActiveModule( const QString& name )
00373 {
00374   if ( name == activeModule() )
00375     return;
00376 
00377   int id = mySet->moduleId( name );
00378   if ( name.isEmpty() || id != -1 )
00379     activate( id, false );
00380 }
00381 
00394 void LightApp_ModuleAction::setMode( const int mode )
00395 {
00396   myMode = mode;
00397   update();
00398 }
00399 
00405 int LightApp_ModuleAction::mode() const
00406 {
00407   return myMode;
00408 }
00409 
00414 void LightApp_ModuleAction::addedTo( QWidget* w )
00415 {
00416   if ( w->inherits( "QToolBar" ) )
00417     w->insertAction( this, myCombo );
00418   w->insertAction( this, mySet );
00419   update();
00420 }
00421 
00428 void LightApp_ModuleAction::removedFrom( QWidget* w )
00429 {
00430   if ( w->inherits( "QToolBar" ) )
00431     w->removeAction( myCombo );
00432   w->removeAction( mySet );
00433 }
00434 
00440 bool LightApp_ModuleAction::event( QEvent* e )
00441 {
00442   if ( e->type() == QEvent::MaxUser ) {
00443     activate( ((ActivateEvent*)e)->id(), false );
00444     return true;
00445   }
00446   return QtxAction::event( e );
00447 }
00448 
00459 void LightApp_ModuleAction::init()
00460 {
00461   setVisible( false );
00462 
00463   myMode = All;
00464   myCombo = new ComboAction( this );
00465   mySet = new ActionSet( this );
00466 
00467   connect( this,    SIGNAL( changed() ),          this, SLOT( onChanged() ) );
00468   connect( mySet,   SIGNAL( triggered( int ) ),   this, SLOT( onTriggered( int ) ) );
00469   connect( myCombo, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) );
00470 }
00471 
00476 void LightApp_ModuleAction::update()
00477 {
00478   QList<QtxComboBox*> lst = myCombo->widgets();
00479   for ( QList<QtxComboBox*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
00480     update( *it );
00481 
00482   myCombo->setVisible( myMode & ComboItem );
00483   mySet->setVisible( myMode & Buttons );
00484 }
00485 
00491 void LightApp_ModuleAction::update( QtxComboBox* cb )
00492 {
00493   if ( !cb )
00494     return;
00495 
00496   bool blocked = cb->blockSignals( true );
00497   int curId = mySet->moduleId( active() );
00498   QList<QAction*> alist = mySet->actions();
00499   cb->clear();
00500   
00501   cb->addItem( icon(), text() );
00502   cb->setId( 0, -1 );
00503 
00504   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
00505   {
00506     QAction* a = *it;
00507     int id = mySet->moduleId( a );
00508     cb->addItem( a->icon(), a->text() );
00509     cb->setId( cb->count() - 1, id );
00510   }
00511 
00512   cb->setCurrentId( curId );
00513   cb->blockSignals( blocked );
00514 }
00515 
00521 QAction* LightApp_ModuleAction::active() const
00522 {
00523   QAction* a = 0;
00524 
00525   QList<QAction*> alist = mySet->actions();
00526   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
00527   {
00528     if ( (*it)->isChecked() )
00529       a = *it;
00530   }
00531 
00532   return a;
00533 }
00534 
00541 void LightApp_ModuleAction::activate( int id, bool fromAction )
00542 {
00543   bool checked = false;
00544 
00545   QList<QAction*> alist = mySet->actions();
00546   for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
00547   {
00548     if ( mySet->moduleId( *it ) != id ) {
00549       (*it)->setChecked( false );
00550     }
00551     else {
00552       if ( !fromAction )
00553         (*it)->setChecked( true );
00554       checked = (*it)->isChecked();
00555     }
00556   }
00557 
00558   QList<QtxComboBox*> widgets = myCombo->widgets();
00559   for ( QList<QtxComboBox*>::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit )
00560   {
00561     QtxComboBox* cb = *wit;
00562     bool blocked = cb->signalsBlocked();
00563     cb->blockSignals( true );
00564     cb->setCurrentId( checked ? id : -1 );
00565     cb->blockSignals( blocked );
00566   }
00567 
00568   emit moduleActivated( activeModule() );
00569 }
00570 
00576 void LightApp_ModuleAction::onTriggered( int id )
00577 {
00578   activate( id );
00579 }
00580 
00587 void LightApp_ModuleAction::onChanged()
00588 {
00589   if ( !isVisible() )
00590     return;
00591 
00592   bool block = signalsBlocked();
00593   blockSignals( true );
00594   setVisible( false );
00595   blockSignals( block );
00596 }
00597 
00602 void LightApp_ModuleAction::onComboActivated( int id )
00603 {
00604   QApplication::postEvent( this, new ActivateEvent( QEvent::MaxUser, id ) );
00605 }