Back to index

salome-gui  6.5.0
QDS_RadioBox.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 #include "QDS_RadioBox.h"
00024 
00025 #include <TColStd_HArray1OfInteger.hxx>
00026 #include <TColStd_HArray1OfExtendedString.hxx>
00027 
00028 #include <QButtonGroup>
00029 #include <QGroupBox>
00030 #include <QVBoxLayout>
00031 #include <QRadioButton>
00032 
00033 /*
00034   \class QDS_RadioBox
00035   \brief Datum with control corresponding to button group with set of exclusive radio buttons.
00036 
00037   This control used for datum with enumerable values. It can be used for datum which has
00038   type of value 'List'. Each radio button of group box is defined by two properties:
00039   integer identifier and string name. All operations on radio buttons are performed via identifier.
00040 
00041   If datum label text is specified, then it is displayed in the group box title.
00042 */
00043 
00060 QDS_RadioBox::QDS_RadioBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
00061 : QDS_Datum( id, parent, flags & ~( Label | Units ), comp ),
00062   myButtonGroup( 0 )
00063 {
00064 }
00065 
00069 QDS_RadioBox::~QDS_RadioBox()
00070 {
00071 }
00072 
00082 int QDS_RadioBox::count( bool total ) const
00083 {
00084   if ( total )
00085     return myValue.count();
00086   else
00087   {
00088     QList<QRadioButton*> bList;
00089     buttons( bList );
00090     return bList.count();
00091   }
00092 }
00093 
00099 void QDS_RadioBox::values( QList<int>& ids, bool total ) const
00100 {
00101   ids.clear();
00102   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
00103     if ( total || ( myState.contains( *it ) && myState[*it] ) )
00104       ids.append( *it );
00105 }
00106 
00112 bool QDS_RadioBox::state( const int id ) const
00113 {
00114   bool state = false;
00115   if ( myState.contains( id ) )
00116     state = myState[id];
00117   return state;
00118 }
00119 
00132 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
00133 {
00134   QList<int> lst;
00135   if ( id < 0 )
00136   {
00137     for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
00138       lst.append( it.key() );
00139   }
00140   else
00141     lst.append( id );
00142 
00143   setState( on, lst, append );
00144 }
00145 
00156 void QDS_RadioBox::setState( const bool on, const QList<int>& ids, const bool append )
00157 {
00158   if ( ids.isEmpty() && append )
00159     return;
00160 
00161   bool changed = false;
00162 
00163   QMap<int, int> aMap;
00164   for ( uint i = 0; i < ids.count(); i++ )
00165     aMap.insert( ids.at( i ), 0 );
00166 
00167   for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
00168   {
00169     if ( aMap.contains( it.key() ) )
00170     {
00171       if ( it.value() != on )
00172       {
00173         it.value() = on;
00174         changed = true;
00175       }
00176     }
00177     else if ( !append && it.value() == on )
00178     {
00179       it.value() = !on;
00180       changed = true;
00181     }
00182   }
00183   if ( changed )
00184     updateRadioBox();
00185 }
00186 
00197 void QDS_RadioBox::setValues( const QList<int>& ids, const QStringList& names )
00198 {
00199   if ( ids.count() != names.count() )
00200     return;
00201 
00202   myUserIds = ids;
00203   myUserNames = names;
00204 }
00205 
00218 void QDS_RadioBox::setValues( const QStringList& names )
00219 {
00220   QList< int > ids;
00221   for ( int i = 0, n = names.count(); i < n; i++ )
00222     ids.append( i );
00223   setValues( ids, names );
00224 }
00225 
00232 QString QDS_RadioBox::getString() const
00233 {
00234   QString res;
00235   QButtonGroup* bg = buttonGroup();
00236   if ( bg )
00237   {
00238     int id = bg->checkedId();
00239     if ( id != -1 )
00240       res = QString::number( id );
00241   }
00242   return res;
00243 }
00244 
00252 void QDS_RadioBox::setString( const QString& txt )
00253 {
00254   QButtonGroup* bg = buttonGroup();
00255   if ( !bg )
00256     return;
00257 
00258   int oldId = bg->checkedId();
00259 
00260   if ( txt.isEmpty() )
00261   {
00262     QList<QRadioButton*> bList;
00263     buttons( bList );
00264     QListIterator<QRadioButton*> it( bList );
00265     while ( it.hasNext() )
00266       it.next()->setChecked( false );
00267   }
00268   else
00269   {
00270     bool ok;
00271     int id = txt.toInt( &ok );
00272     if ( !ok )
00273       id = -1;
00274 
00275     bool block = signalsBlocked();
00276     blockSignals( true );
00277     bg->button(id)->setChecked(true);
00278     blockSignals( block );
00279   }
00280 
00281   int newId = bg->checkedId();
00282 
00283   if ( oldId != newId )
00284   {
00285     onParamChanged();
00286     QString str = getString();
00287     emit activated( newId );
00288     emit paramChanged();
00289     emit paramChanged( str );
00290   }
00291 }
00292 
00297 QButtonGroup* QDS_RadioBox::buttonGroup() const
00298 {
00299   return myButtonGroup;
00300 }
00301 
00306 QGroupBox* QDS_RadioBox::groupBox() const
00307 {
00308   return ::qobject_cast<QGroupBox*>( controlWidget() );
00309 }
00310 
00315 QWidget* QDS_RadioBox::createControl( QWidget* parent )
00316 {
00317   myButtonGroup = new QButtonGroup( parent );
00318   myButtonGroup->setExclusive( true );
00319 
00320   QGroupBox *gb = new QGroupBox( "", parent );
00321   QVBoxLayout *vbox = new QVBoxLayout;
00322   vbox->addStretch(1);
00323   gb->setLayout(vbox);
00324   return gb;
00325 }
00326 
00334 void QDS_RadioBox::unitSystemChanged( const QString& system )
00335 {
00336   QDS_Datum::unitSystemChanged( system );
00337 
00338   Handle(TColStd_HArray1OfInteger) anIds;
00339   Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
00340 
00341   Handle(DDS_DicItem) aDicItem = dicItem();
00342   if ( !aDicItem.IsNull() )
00343     aDicItem->GetListOfValues( aValues, anIds, anIcons );
00344 
00345   myValue.clear();
00346   myDataIds.clear();
00347 
00348   QMap<int, QString> userMap;
00349   QIntList::iterator iIt = myUserIds.begin();
00350   QStringList::iterator sIt = myUserNames.begin();
00351   for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
00352     userMap.insert( *iIt, *sIt );
00353 
00354   if ( !anIds.IsNull() && !aValues.IsNull() &&
00355        anIds->Length() == aValues->Length() )
00356   {
00357     for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
00358     {
00359       QString aValue;
00360       int id = anIds->Value( i );
00361       if ( userMap.contains( id  ) )
00362         aValue = userMap[id];
00363       else
00364         aValue = toQString( aValues->Value( i ) );
00365 
00366       myDataIds.append( id );
00367       myValue.insert( id, aValue );
00368       myState.insert( id, true );
00369     }
00370   }
00371 
00372   for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
00373   {
00374     int id = *iIt;
00375     if ( !myValue.contains( id  ) )
00376     {
00377       myDataIds.append( id );
00378       myValue.insert( id, userMap[id] );
00379     }
00380   }
00381 
00382   QIntList del, add;
00383   for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
00384     if ( !myValue.contains( it1.key() ) )
00385       del.append( it1.key() );
00386 
00387   for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
00388     if ( !myState.contains( it2.key() ) )
00389       add.append( it2.key() );
00390 
00391   for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
00392     myState.remove( *iter1 );
00393 
00394   for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
00395     myState.insert( *iter2, true );
00396 
00397   QGroupBox* gb = groupBox();
00398   if ( gb ) gb->setTitle( label() );
00399 
00400   updateRadioBox();
00401 }
00402 
00407 void QDS_RadioBox::onToggled( bool on )
00408 {
00409   if ( !on )
00410     return;
00411 
00412   onParamChanged();
00413   emit paramChanged();
00414   QString str = getString();
00415   emit paramChanged( str );
00416 }
00417 
00421 void QDS_RadioBox::updateRadioBox()
00422 {
00423   QButtonGroup* bg = buttonGroup();
00424   if ( !bg )
00425     return;
00426 
00427   QGroupBox* gb = groupBox();
00428   if ( !gb )
00429     return;
00430 
00431   int curId = bg->checkedId();
00432 
00433   QList<QRadioButton*> bList;
00434   buttons( bList );
00435   QListIterator<QRadioButton*> itr( bList );
00436   while ( itr.hasNext() ) {
00437     QRadioButton* aButton = itr.next();
00438     if ( gb->layout() ) gb->layout()->removeWidget(aButton);
00439     delete aButton;
00440   }
00441 
00442   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
00443   {
00444     int id = *it;
00445     if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
00446       continue;
00447 
00448     QRadioButton* rb = new QRadioButton( myValue[id] );
00449     ((QObject*)rb)->setParent( bg );
00450     bg->addButton( rb, id );
00451     if ( gb->layout() ) gb->layout()->addWidget(rb);
00452 
00453     connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
00454   }
00455 
00456   if ( curId != -1 )
00457   {
00458     int id = curId;
00459     if ( !bg->button( id ) )
00460     {
00461       QList<QRadioButton*> bList;
00462       buttons( bList );
00463       if ( !bList.isEmpty() )
00464         id = bg->id( bList.empty() ? 0 : bList.first() );
00465     }
00466 
00467     bool block = signalsBlocked();
00468     blockSignals( true );
00469     bg->button(id)->setChecked(true);
00470     blockSignals( block );
00471   }
00472 
00473   if ( curId != bg->checkedId() )
00474   {
00475     onParamChanged();
00476     QString str = getString();
00477     emit paramChanged();
00478     emit paramChanged( str );
00479   }
00480 }
00481 
00486 void QDS_RadioBox::buttons( QList<QRadioButton*>& lst ) const
00487 {
00488   lst.clear();
00489 
00490   QButtonGroup* bg = buttonGroup();
00491   if ( !bg )
00492     return;
00493 
00494   QList<QRadioButton*> objs = bg->findChildren<QRadioButton*>();
00495   QListIterator<QRadioButton*> it( objs );
00496   while ( it.hasNext() )
00497     lst.append( it.next() );
00498 }
00499