Back to index

salome-gui  6.5.0
LightApp_Dialog.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:      LightApp_Dialog.cxx
00024 // Author:    Alexander SOLOVYOV
00025 //
00026 #include "LightApp_Dialog.h"
00027 #include <SUIT_Session.h>
00028 #include <SUIT_ResourceMgr.h>
00029 
00030 #include <QAbstractButton>
00031 #include <QToolButton>
00032 #include <QLineEdit>
00033 #include <QLabel>
00034 
00038 LightApp_Dialog::LightApp_Dialog( QWidget* parent, const char* name, bool modal,
00039                                   bool allowResize, const int f, Qt::WindowFlags wf )
00040 : QtxDialog( parent, modal, allowResize, f, wf ),
00041   myIsExclusive( true ),
00042   myIsBusy( false )
00043 {
00044   setObjectName( name );
00045   setObjectPixmap( "LightApp", tr( "ICON_SELECT" ) );
00046 }
00047 
00051 LightApp_Dialog::~LightApp_Dialog()
00052 {
00053 }
00054 
00058 void LightApp_Dialog::show()
00059 {
00060   QtxDialog::show();
00061 }
00062 
00066 bool LightApp_Dialog::isExclusive() const
00067 {
00068   return myIsExclusive;
00069 }
00070 
00075 void LightApp_Dialog::updateButtons( const int _id )
00076 {
00077   if( !myIsExclusive )
00078     return;
00079 
00080   int id = _id;
00081 
00082   ObjectMap::const_iterator anIt = myObjects.begin(),
00083                             aLast = myObjects.end();
00084   for( ; anIt!=aLast; anIt++ )
00085   {
00086     QToolButton* but = (QToolButton*)anIt.value().myBtn;
00087     if( but && but->isChecked() )
00088     {
00089       if( id==-1 )
00090         id = anIt.key();
00091 
00092       if( anIt.key()!=id )
00093         but->setChecked( false );
00094     }
00095   }
00096 }
00097 
00102 void LightApp_Dialog::setExclusive( const bool ex )
00103 {
00104   myIsExclusive = ex;
00105   updateButtons();
00106 }
00107 
00112 void LightApp_Dialog::showObject( const int id )
00113 {
00114   setObjectShown( id, true );
00115 }
00116 
00121 void LightApp_Dialog::hideObject( const int id )
00122 {
00123   setObjectShown( id, false );
00124 }
00125 
00131 void LightApp_Dialog::setObjectShown( const int id, const bool shown )
00132 {
00133   if( myObjects.contains( id ) && isObjectShown( id )!=shown )
00134   {
00135     Object& obj = myObjects[ id ];
00136     obj.myEdit->setShown( shown );
00137     obj.myBtn->setShown( shown );
00138     obj.myLabel->setShown( shown );
00139     if( !shown )
00140       ( ( QToolButton* )obj.myBtn )->setChecked( false );
00141   }
00142 }
00143 
00148 bool LightApp_Dialog::isObjectShown( const int id ) const
00149 {
00150   return myObjects.contains( id ) &&
00151          ( myObjects[ id ].myEdit->isVisible() ||
00152            myObjects[ id ].myEdit->isVisibleTo( myObjects[ id ].myEdit->parentWidget() ) );
00153 }
00154 
00160 void LightApp_Dialog::setObjectEnabled( const int id, const bool en )
00161 {
00162   if( myObjects.contains( id ) && isObjectEnabled( id )!=en )
00163   {
00164     Object& obj = myObjects[ id ];
00165     obj.myEdit->setEnabled( en );
00166     obj.myBtn->setEnabled( en );
00167 //    obj.myLabel->setEnabled( en );
00168     if( !en )
00169       ( ( QToolButton* )obj.myBtn )->setChecked( false );
00170   } 
00171 }
00172 
00177 bool LightApp_Dialog::isObjectEnabled( const int id ) const
00178 {
00179   return myObjects.contains( id ) && myObjects[ id ].myEdit->isEnabled();
00180 }
00181 
00189 void LightApp_Dialog::selectObject( const QString& name, const int type, const QString& id, const bool update )
00190 {
00191   QStringList names;   names.append( name );
00192   TypesList types;     types.append( type );
00193   QStringList ids;     ids.append( id );
00194   selectObject( names, types, ids, update );
00195 }
00196 
00204 void LightApp_Dialog::selectObject( const QStringList& _names,
00205                                      const TypesList& _types,
00206                                      const QStringList& _ids,
00207                                      const bool update )
00208 {
00209   ObjectMap::iterator anIt = myObjects.begin(),
00210                       aLast = myObjects.end();
00211   for( ; anIt!=aLast; anIt++ )
00212     if( anIt.value().myBtn->isChecked() )
00213       selectObject( anIt.key(), _names, _types, _ids, update );
00214 }
00215 
00220 bool LightApp_Dialog::hasSelection( const int id ) const
00221 {
00222   return myObjects.contains( id ) && !myObjects[ id ].myIds.isEmpty();
00223 }
00224 
00229 void LightApp_Dialog::clearSelection( const int id )
00230 {
00231   if( id==-1 )
00232   {
00233     ObjectMap::const_iterator anIt = myObjects.begin(),
00234                               aLast = myObjects.end();
00235     for( ; anIt!=aLast; anIt++ )
00236       clearSelection( anIt.key() );
00237   }
00238   
00239   else if( myObjects.contains( id ) )
00240   {
00241     myObjects[ id ].myIds.clear();
00242     myObjects[ id ].myTypes.clear();
00243     myObjects[ id ].myNames.clear();
00244     
00245     myObjects[ id ].myEdit->setText( QString() );
00246     emit selectionChanged( id );
00247   }
00248 }
00249 
00255 QWidget* LightApp_Dialog::objectWg( const int theId, const int theWgId ) const
00256 {
00257   QWidget* aResWg = 0;
00258   if( myObjects.contains( theId ) )
00259   {
00260     if ( theWgId == Label )
00261       aResWg = myObjects[ theId ].myLabel;
00262     else if ( theWgId == Btn )
00263       aResWg = myObjects[ theId ].myBtn;
00264     else if ( theWgId == Control )
00265       aResWg = myObjects[ theId ].myEdit;
00266   }
00267   return aResWg;
00268 }
00269 
00274 QString LightApp_Dialog::objectText( const int theId ) const
00275 {
00276   return myObjects.contains( theId ) ? myObjects[ theId ].myEdit->text() : "";
00277 }
00278 
00284 void LightApp_Dialog::setObjectText( const int theId, const QString& theText )
00285 {
00286   if ( myObjects.contains( theId ) )
00287     myObjects[ theId ].myEdit->setText( theText );
00288 }
00289 
00295 void LightApp_Dialog::selectedObject( const int id, QStringList& list ) const
00296 {
00297   if( myObjects.contains( id ) )
00298     list = myObjects[ id ].myIds;
00299 }
00300 
00305 QString LightApp_Dialog::selectedObject( const int id ) const
00306 {
00307   if ( myObjects.contains( id ) && myObjects[ id ].myIds.count() > 0 )
00308     return myObjects[ id ].myIds.first();
00309   else
00310     return "";
00311 }
00312 
00317 void LightApp_Dialog::objectSelection( SelectedObjects& objs ) const
00318 {
00319   //objs.clear();
00320   ObjectMap::const_iterator anIt = myObjects.begin(),
00321                             aLast = myObjects.end();
00322   for( ; anIt!=aLast; anIt++ )
00323   {
00324     QStringList ids;
00325     selectedObject( anIt.key(), ids );
00326     if( !ids.isEmpty() )
00327       objs.insert( anIt.key(), ids );
00328   }
00329 }
00330 
00338 int LightApp_Dialog::createObject( const QString& label, QWidget* parent, const int id )
00339 {  
00340   int nid = id;
00341   if( nid<0 )
00342     for( nid=0; myObjects.contains( nid ); nid++ );
00343   
00344   if( !myObjects.contains( nid ) )
00345   {
00346     QLabel* lab = new QLabel( label, parent );
00347     myObjects[ nid ].myLabel = lab;
00348     
00349     QToolButton* but = new QToolButton( parent );
00350     but->setIcon( QIcon( myPixmap ) );
00351     but->setCheckable( true );
00352     but->setMaximumWidth( but->height() );
00353     but->setMinimumWidth( but->height() );    
00354     connect( but, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
00355     myObjects[ nid ].myBtn = but;
00356 
00357     QLineEdit* ne = new QLineEdit( parent );
00358     ne->setReadOnly( true );
00359     ne->setMinimumWidth( 150 );
00360     connect( ne, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
00361     myObjects[ nid ].myEdit = ne;
00362 
00363     myObjects[ nid ].myNI = OneNameOrCount;
00364   }
00365   return nid;
00366 }
00367 
00373 void LightApp_Dialog::renameObject( const int id, const QString& label )
00374 {
00375   if( myObjects.contains( id ) )
00376     myObjects[ id ].myLabel->setText( label );
00377 }
00378 
00384 void LightApp_Dialog::setObjectType( const int id, const int type1, ... )
00385 {
00386   TypesList types;
00387   
00388   const int* tt = &type1;
00389   while( *tt>=0 )
00390   {
00391     types.append( *tt );
00392     tt++;
00393   }
00394 
00395   setObjectType( id, types );
00396 }
00397 
00403 void LightApp_Dialog::setObjectType( const int id, const TypesList& list )
00404 {
00405   if( !myObjects.contains( id ) )
00406     return;
00407 
00408   TypesList& internal = myObjects[ id ].myPossibleTypes;
00409     
00410   QMap<int,int> types;
00411   TypesList::const_iterator anIt = list.begin(),
00412                             aLast = list.end();
00413   for( ; anIt!=aLast; anIt++ )
00414     types.insert( *anIt, 0 );
00415 
00416 
00417   internal.clear();
00418   QMap<int,int>::const_iterator aMIt = types.begin(),
00419                                 aMLast = types.end();
00420   for( ; aMIt!=aMLast; aMIt++ )
00421     internal.append( aMIt.key() );
00422 
00423   updateObject( id );
00424 }
00425 
00431 void LightApp_Dialog::addObjectType( const int id, const int type1, const int, ... )
00432 {
00433   TypesList types; objectTypes( id, types );
00434 
00435   const int* tt = &type1;
00436   while( *tt>=0 )
00437   {
00438     types.append( *tt );
00439     tt++;
00440   }
00441 
00442   setObjectType( id, types );  
00443 }
00444 
00450 void LightApp_Dialog::addObjectType( const int id, const TypesList& list )
00451 {
00452   TypesList types = list; objectTypes( id, types );
00453   setObjectType( id, types );
00454 }
00455 
00461 void LightApp_Dialog::addObjectType( const int id, const int type )
00462 {
00463   TypesList types; objectTypes( id, types );
00464   types.append( type );
00465   setObjectType( id, types );
00466 }
00467 
00472 void LightApp_Dialog::removeObjectType( const int id )
00473 {
00474   TypesList types;
00475   setObjectType( id, types );
00476 }
00477 
00483 void LightApp_Dialog::removeObjectType( const int id, const TypesList& list )
00484 {
00485   if( !myObjects.contains( id ) )
00486     return;
00487 
00488   TypesList& internal = myObjects[ id ].myPossibleTypes;
00489 
00490   QMap<int,int> types;
00491   TypesList::const_iterator anIt = internal.begin(),
00492                             aLast = internal.end();
00493   for( ; anIt!=aLast; anIt++ )
00494     types.insert( *anIt, 0 );
00495   anIt = list.begin(); aLast = list.end();
00496   for( ; anIt!=aLast; anIt++ )
00497     types.remove( *anIt );
00498 
00499 
00500   internal.clear();
00501   QMap<int,int>::const_iterator aMIt = types.begin(),
00502                                 aMLast = types.end();
00503   for( ; aMIt!=aMLast; aMIt++ )
00504     internal.append( aMIt.key() );
00505 
00506   updateObject( id );
00507 }
00508 
00514 void LightApp_Dialog::removeObjectType( const int id, const int type )
00515 {
00516   TypesList list; list.append( type );
00517   removeObjectType( id, list );
00518 }
00519 
00525 bool LightApp_Dialog::hasObjectType( const int id, const int type ) const
00526 {
00527   if( myObjects.contains( id ) )
00528     return myObjects[ id ].myPossibleTypes.contains( type );
00529   else
00530     return false;
00531 }
00532 
00538 void LightApp_Dialog::objectTypes( const int id, TypesList& list ) const
00539 {
00540   if( myObjects.contains( id ) )
00541   {
00542     TypesList::const_iterator anIt = myObjects[ id ].myPossibleTypes.begin(),
00543                               aLast = myObjects[ id ].myPossibleTypes.end();
00544     for( ; anIt!=aLast; anIt++ )
00545       list.append( *anIt );
00546   }  
00547 }
00548 
00552 void LightApp_Dialog::onToggled( bool on )
00553 {
00554   QAbstractButton* but = ( QAbstractButton* )sender();
00555   int id = -1;
00556 
00557   if( !but )
00558     return;
00559     
00560   ObjectMap::const_iterator anIt = myObjects.begin(),
00561                             aLast = myObjects.end();
00562   for( ; anIt!=aLast && id==-1; anIt++ )
00563     if( anIt.value().myBtn==but )
00564       id = anIt.key();
00565 
00566   if( id!=-1 )
00567   {
00568     if( on )
00569     {
00570       updateButtons( id );
00571       emit objectActivated( id );
00572     }
00573     else
00574       emit objectDeactivated( id );
00575   }
00576 }
00577 
00583 void LightApp_Dialog::updateObject( const int id, bool emit_signal )
00584 {
00585   if( hasSelection( id ) )
00586   {
00587     Object& obj = myObjects[ id ];
00588     filterTypes( id, obj.myNames, obj.myTypes, obj.myIds );
00589     obj.myEdit->setText( selectionDescription( obj.myNames, obj.myTypes, obj.myNI ) );
00590     if( emit_signal )
00591       emit selectionChanged( id );
00592   }
00593 }
00594 
00602 void LightApp_Dialog::filterTypes( const int id, QStringList& names, TypesList& types, QStringList& ids ) const
00603 {
00604   if( !myObjects.contains( id ) )
00605     return;
00606 
00607   const Object& obj = myObjects[ id ];
00608   if( obj.myPossibleTypes.isEmpty() )
00609     return;
00610 
00611   QStringList new_names, new_ids;
00612   TypesList new_types;
00613   
00614   TypesList::const_iterator anIt1 = types.begin(),
00615                             aLast = types.end();
00616   QStringList::const_iterator anIt2 = names.begin(),
00617                               anIt3 = ids.begin();
00618   for( ; anIt1!=aLast; anIt1++, anIt2++, anIt3++ )
00619     if( obj.myPossibleTypes.contains( *anIt1 ) )
00620     {
00621       if( new_types.count()==1 && !multipleSelection( id ) )
00622         break;
00623         
00624       new_names.append( *anIt2 );
00625       new_types.append( *anIt1 );
00626       new_ids.append( *anIt3 );       
00627     }
00628   names = new_names;
00629   types = new_types;
00630   ids = new_ids;
00631 }
00632 
00636 SUIT_ResourceMgr* LightApp_Dialog::resMgr() const
00637 {
00638   return SUIT_Session::session()->resourceMgr();
00639 }
00640 
00645 void LightApp_Dialog::setObjectPixmap( const QPixmap& p )
00646 {
00647   myPixmap = p;
00648   ObjectMap::const_iterator anIt = myObjects.begin(),
00649                             aLast = myObjects.end();
00650   for( ; anIt!=aLast; anIt++ )
00651     ( ( QToolButton* )anIt.value().myBtn )->setIcon( p );
00652 }                        
00653 
00659 void LightApp_Dialog::setObjectPixmap( const QString& section, const QString& file )
00660 {
00661   SUIT_ResourceMgr* mgr = resMgr();
00662   if( mgr )
00663     setObjectPixmap( mgr->loadPixmap( section, file ) );
00664 }
00665 
00670 bool LightApp_Dialog::multipleSelection( const int id ) const
00671 {
00672   return nameIndication( id )!=OneName;  
00673 }
00674 
00679 LightApp_Dialog::NameIndication LightApp_Dialog::nameIndication( const int id ) const
00680 {
00681   if( myObjects.contains( id ) )
00682     return myObjects[ id ].myNI;
00683   else
00684     return OneNameOrCount;
00685 }
00686 
00692 void LightApp_Dialog::setNameIndication( const int id, const NameIndication ni )
00693 {
00694   if( id==-1 )
00695   {
00696     ObjectMap::iterator anIt = myObjects.begin(),
00697                         aNext,
00698                         aLast = myObjects.end();
00699     for( ; anIt!=aLast; anIt++ )
00700     {
00701       anIt.value().myNI = ni;
00702       setReadOnly( anIt.key(), isReadOnly( anIt.key() ) );
00703       aNext = anIt; aNext++;
00704       updateObject( anIt.key(), aNext==aLast );
00705     }
00706   }
00707   else if( myObjects.contains( id ) )
00708   {
00709     myObjects[ id ].myNI = ni;
00710     setReadOnly( id, isReadOnly( id ) );
00711     updateObject( id, true );
00712   }
00713 }
00714 
00721 QString LightApp_Dialog::selectionDescription( const QStringList& names, const TypesList& types, const NameIndication ni ) const
00722 {
00723   if( names.count()!=types.count() )
00724     return "LightApp_Dialog::selectionDescription(): Error!!!";
00725     
00726   if( names.isEmpty() )
00727     return QString();
00728     
00729   switch( ni )
00730   {
00731     case OneName:
00732       return names.first();
00733       break;
00734       
00735     case OneNameOrCount:
00736       if( names.count()==1 )
00737         return names.first();
00738       else
00739         return countOfTypes( types );
00740       break;
00741       
00742     case ListOfNames:
00743       return names.join( " " );
00744       break;
00745       
00746     case Count:
00747       return countOfTypes( types );
00748       break;
00749   };
00750   return QString();
00751 }
00752 
00757 QString LightApp_Dialog::countOfTypes( const TypesList& types ) const
00758 {
00759   QMap<int, int> typesCount;
00760   QStringList typeCount;
00761   
00762   TypesList::const_iterator anIt = types.begin(),
00763                             aLast = types.end();
00764   for( ; anIt!=aLast; anIt++ )
00765     if( typesCount.contains( *anIt ) )
00766       typesCount[ *anIt ]++;
00767     else
00768       typesCount[ *anIt ] = 1;
00769 
00770   QMap<int,int>::const_iterator aMIt = typesCount.begin(),
00771                                 aMLast = typesCount.end();
00772   for( ; aMIt!=aMLast; aMIt++ )
00773     typeCount.append( QString( "%1 %2" ).arg( aMIt.value() ).arg( typeName( aMIt.key() ) ) );
00774 
00775   return typeCount.join( ", " );
00776 }
00777 
00782 QString& LightApp_Dialog::typeName( const int type )
00783 {
00784   return myTypeNames[ type ];
00785 }
00786 
00791 const QString LightApp_Dialog::typeName( const int type ) const
00792 {
00793   return myTypeNames[ type ];
00794 }
00795 
00796 
00801 void LightApp_Dialog::activateObject( const int theId )
00802 {
00803   if ( myObjects.contains( theId ) && !myObjects[ theId ].myBtn->isChecked() )
00804     myObjects[ theId ].myBtn->toggle();
00805 }
00806 
00810 void LightApp_Dialog::deactivateAll()
00811 {
00812   ObjectMap::iterator anIt = myObjects.begin(),
00813                       aLast = myObjects.end();
00814   for( ; anIt!=aLast; anIt++ )
00815   {
00816     QToolButton* btn = ( QToolButton* )anIt.value().myBtn;
00817     btn->setChecked( false );
00818   }
00819 }
00820 
00829 void LightApp_Dialog::selectObject( const int id, const QString& name, const int type, const QString& selid, const bool update )
00830 {
00831   QStringList names;   names.append( name );
00832   TypesList types;     types.append( type );
00833   QStringList ids;     ids.append( selid );
00834   selectObject( id, names, types, ids, update );
00835 }
00836 
00845 void LightApp_Dialog::selectObject( const int id, const QStringList& _names, const TypesList& _types,
00846                                      const QStringList& _ids, const bool update )
00847 {
00848   if( !myObjects.contains( id ) )
00849     return;
00850     
00851   QStringList names = _names, ids = _ids;
00852   TypesList types = _types;
00853 
00854   filterTypes( id, names, types, ids );
00855 
00856   Object& obj = myObjects[ id ];
00857   if( update )
00858     obj.myEdit->setText( selectionDescription( names, types, obj.myNI ) );
00859   obj.myTypes = types;
00860   obj.myIds = ids;
00861   obj.myNames = names;
00862 
00863   emit selectionChanged( id );
00864 }
00865 
00871 void LightApp_Dialog::setReadOnly( const int id, const bool ro )
00872 {
00873   if( myObjects.contains( id ) )
00874     myObjects[ id ].myEdit->setReadOnly( nameIndication( id )==ListOfNames || nameIndication( id )==OneName ? ro : true );
00875 }
00876 
00881 bool LightApp_Dialog::isReadOnly( const int id ) const
00882 {
00883   if( myObjects.contains( id ) )
00884     return myObjects[ id ].myEdit->isReadOnly();
00885   else
00886     return true;
00887 }
00888 
00892 void LightApp_Dialog::onTextChanged( const QString& text )
00893 {
00894   if( myIsBusy )
00895     return;
00896 
00897   myIsBusy = true;
00898 
00899   if( sender() && sender()->inherits( "QLineEdit" ) )
00900   {
00901     QLineEdit* edit = ( QLineEdit* )sender();
00902     int id = -1;
00903     ObjectMap::const_iterator anIt = myObjects.begin(),
00904                               aLast = myObjects.end();
00905     for( ; anIt!=aLast; anIt++ )
00906       if( anIt.value().myEdit == edit )
00907         id = anIt.key();
00908 
00909     if( id>=0 && !isReadOnly( id ) )
00910     {
00911       QStringList list = text.split( " ", QString::SkipEmptyParts );
00912       emit objectChanged( id, list );
00913     }
00914   }
00915 
00916   myIsBusy = false;
00917 }