Back to index

salome-gui  6.5.0
QtxActionGroup.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:      QtxActionGroup.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxActionGroup.h"
00024 
00025 #include "QtxComboBox.h"
00026 
00027 #include <QMenu>
00028 #include <QMenuBar>
00029 #include <QActionGroup>
00030 
00059 QtxActionGroup::QtxActionGroup( QObject* parent )
00060 : QtxActionSet( parent ),
00061   myDropDown( false )
00062 {
00063   setMenu( new QMenu( 0 ) );
00064   myActionGroup = new QActionGroup( this );
00065 
00066   connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) );
00067 }
00068 
00075 QtxActionGroup::QtxActionGroup( QObject* parent, const bool exclusive )
00076 : QtxActionSet( parent ),
00077   myDropDown( false )
00078 {
00079   setMenu( new QMenu( 0 ) );
00080   myActionGroup = new QActionGroup( this );
00081   myActionGroup->setExclusive( exclusive );
00082 
00083   connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SIGNAL( selected( QAction* ) ) );
00084 }
00085 
00089 QtxActionGroup::~QtxActionGroup()
00090 {
00091 }
00092 
00098 bool QtxActionGroup::isExclusive() const
00099 {
00100   return myActionGroup->isExclusive();
00101 }
00102 
00108 void QtxActionGroup::setExclusive( const bool on )
00109 {
00110   if ( myActionGroup->isExclusive() == on )
00111     return;
00112 
00113   bool e = isEmptyAction();
00114 
00115   myActionGroup->setExclusive( on );
00116 
00117   if ( e != isEmptyAction() )
00118     updateType();
00119 }
00120 
00130 bool QtxActionGroup::usesDropDown() const
00131 {
00132   return myDropDown;
00133 }
00134 
00142 void QtxActionGroup::setUsesDropDown( const bool on )
00143 {
00144   if ( myDropDown == on )
00145     return;
00146 
00147   bool e = isEmptyAction();
00148 
00149   myDropDown = on;
00150 
00151   if ( e != isEmptyAction() )
00152     updateType();
00153 }
00154 
00159 void QtxActionGroup::add( QAction* a )
00160 {
00161   insertAction( a );
00162 }
00163 
00168 void QtxActionGroup::onActivated( int id )
00169 {
00170   const QObject* s = sender();
00171 
00172   QAction* a = action( id );
00173   if ( !a )
00174     return;
00175 
00176   if ( a->isChecked() )
00177     return;
00178 
00179   a->setChecked( true );
00180   a->trigger();
00181 
00182   QList<QWidget*> lst = createdWidgets();
00183   for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
00184   {
00185     QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
00186     if ( cb && cb != s )
00187       cb->setCurrentId( id );
00188   }
00189 }
00190 
00195 void QtxActionGroup::onTriggered( QAction* a )
00196 {
00197   int id = actionId( a );
00198   if ( id != -1 ) {
00199     QList<QWidget*> lst = createdWidgets();
00200     for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
00201     {
00202       QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
00203       if ( cb )
00204         cb->setCurrentId( id );
00205     }
00206   }
00207   
00208   emit selected( a );
00209 }
00210 
00215 void QtxActionGroup::updateAction( QWidget* w )
00216 {
00217   if ( !::qobject_cast<QMenu*>( w ) && !::qobject_cast<QMenuBar*>( w ) ) {
00218     QtxComboBox* cb = createdWidget( w );
00219     if ( !cb )
00220       QtxActionSet::updateAction( w );
00221     else
00222     {
00223       updateAction( cb );
00224       
00225       QList<QAction*> lst = actions();
00226       for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
00227         w->removeAction( *it );
00228     }
00229   }
00230   else
00231   {
00232     if ( !usesDropDown() ) {
00233       QtxActionSet::updateAction( w );
00234     }
00235     else {
00236       QList<QAction*> lst = actions();
00237       for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
00238         w->removeAction( *it );
00239     }
00240   }
00241 }
00242 
00247 void QtxActionGroup::updateAction( QtxComboBox* cb )
00248 {
00249   if ( !cb )
00250     return;
00251 
00252   cb->clear();
00253   cb->setCleared( false );
00254 
00255   QAction* cur = 0;
00256   QList<QAction*> lst = actions();
00257   for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
00258   {
00259     QAction* a = *it;
00260     cb->addItem( a->icon(), a->text() );
00261     cb->setId( cb->count() - 1, actionId( a ) );
00262     if ( a->isChecked() )
00263       cur = a;
00264   }
00265 
00266   if ( cur )
00267     cb->setCurrentId( actionId( cur ) );
00268   else
00269     cb->setCleared( true );
00270 }
00271 
00278 QWidget* QtxActionGroup::createWidget( QWidget* p )
00279 {
00280   if ( ::qobject_cast<QMenu*>( p ) || ::qobject_cast<QMenuBar*>( p ) )
00281     return 0;
00282 
00283   QtxComboBox* cb = !isEmptyAction() ? new QtxComboBox( p ) : 0;
00284   if ( cb )
00285     connect( cb, SIGNAL( activatedId( int ) ), this, SLOT( onActivated( int ) ) );
00286   return cb;
00287 }
00288 
00294 bool QtxActionGroup::isEmptyAction() const
00295 {
00296   return !isExclusive() || !usesDropDown();
00297 }
00298 
00303 void QtxActionGroup::actionAdded( QAction* a )
00304 {
00305   myActionGroup->addAction( a );
00306   if ( menu() )
00307     menu()->addAction( a );
00308 }
00309 
00314 void QtxActionGroup::actionRemoved( QAction* a )
00315 {
00316   myActionGroup->removeAction( a );
00317   if ( menu() )
00318     menu()->removeAction( a );
00319 }
00320 
00324 void QtxActionGroup::updateType()
00325 {
00326   QList<QWidget*> lst = associatedWidgets();
00327   for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
00328   {
00329     QWidget* w = *it;
00330     QList<QAction*> lst = w->actions();
00331 
00332     int i = lst.indexOf( this );
00333     w->removeAction( this );
00334 
00335     lst = w->actions();
00336     w->insertAction( i < lst.count() ? lst.at( i ) : 0, this );
00337   }
00338   setVisible( !isEmptyAction() );
00339 }
00340 
00346 QtxComboBox* QtxActionGroup::createdWidget( QWidget* p )
00347 {
00348   QtxComboBox* cb = 0;
00349   QList<QWidget*> lst = createdWidgets();
00350   for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end() && !cb; ++it )
00351   {
00352     if ( (*it)->parent() == p )
00353       cb = ::qobject_cast<QtxComboBox*>( *it );
00354   }
00355   return cb;
00356 }
00357