Back to index

salome-gui  6.5.0
QDS_ComboBox.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_ComboBox.h"
00024 
00025 #include <QtxComboBox.h>
00026 
00027 #include <TColStd_HArray1OfInteger.hxx>
00028 #include <TColStd_HArray1OfExtendedString.hxx>
00029 
00030 #include <QLineEdit>
00031 
00032 /*
00033   \class QDS_ComboBox
00034   
00035   \brief Datum with control corresponding to the combo box. 
00036 
00037   This control is used for datum with enumerable values.
00038   It can be used for datum which has type of value 'List'. 
00039 
00040   Each item of the combobox is defined by two properties: integer identifier 
00041   and string name. All operations on items are performed via identifier.
00042 */
00043 
00061 QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
00062 : QDS_Datum( id, parent, flags, comp )
00063 {
00064 }
00065 
00069 QDS_ComboBox::~QDS_ComboBox()
00070 {
00071 }
00072 
00077 bool QDS_ComboBox::editable() const
00078 {
00079   if ( comboBox() && comboBox()->lineEdit() )
00080     return !comboBox()->lineEdit()->isReadOnly();
00081   else
00082     return false;
00083 }
00084 
00089 void QDS_ComboBox::setEditable( const bool on )
00090 {
00091   QComboBox* aCombo = comboBox();
00092   if ( aCombo )
00093     aCombo->setEditable( on );
00094   if ( aCombo && aCombo->lineEdit() )
00095   {
00096     aCombo->lineEdit()->setReadOnly( !on );
00097     aCombo->setValidator(0);
00098     if ( on )
00099       aCombo->setValidator( validator() );
00100   }
00101 }
00102 
00110 int QDS_ComboBox::count( bool total ) const
00111 {
00112   if ( total )
00113     return myValue.count();
00114   else if ( comboBox() )
00115     return comboBox()->count();
00116   else
00117     return 0;
00118 }
00119 
00127 void QDS_ComboBox::values( QList<int>& ids, bool total ) const
00128 {
00129   ids.clear();
00130   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
00131     if ( total || ( myState.contains( *it ) && myState[*it] ) )
00132       ids.append( *it );
00133 }
00134 
00139 int QDS_ComboBox::integerValue() const
00140 {
00141   QComboBox* cb = comboBox();
00142   QString cur = getString();
00143   if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
00144     cur = cb->itemText( cb->currentIndex() );
00145 
00146   if ( cb && cur == getString() )
00147     return getId( cb->currentIndex() );
00148   else
00149     return getId( getString() );
00150 }
00151 
00156 double QDS_ComboBox::doubleValue() const
00157 {
00158   initDatum();
00159 
00160   QComboBox* cb = comboBox();
00161   QString cur = getString();
00162   if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
00163     cur = cb->itemText( cb->currentIndex() );
00164 
00165   if ( cb && cur == getString() )
00166     return getId( cb->currentIndex() );
00167   else
00168     return getId( getString() );
00169 }
00170 
00175 void QDS_ComboBox::setIntegerValue( const int id )
00176 {
00177   initDatum();
00178 
00179   if ( myValue.contains( id ) )
00180     setString( myValue[id] );
00181   else 
00182     setString( "" );
00183 }
00184 
00193 void QDS_ComboBox::setDoubleValue( const double val )
00194 {
00195   initDatum();
00196 
00197   int id = (int)val;
00198   if ( myValue.contains( id ) )
00199     setString( myValue[id] );
00200   else if ( id == -1 )
00201     setString( "" );
00202 }
00203 
00209 bool QDS_ComboBox::state( const int id ) const
00210 {
00211   bool state = false;
00212   if ( myState.contains( id ) )
00213     state = myState[id];
00214   return state;
00215 }
00216 
00229 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
00230 {
00231   QList<int> lst;
00232   if ( id < 0 )
00233   {
00234     for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
00235       lst.append( it.key() );
00236   }
00237   else
00238     lst.append( id );
00239 
00240   setState( on, lst, append );
00241 }
00242 
00253 void QDS_ComboBox::setState( const bool on, const QList<int>& ids, const bool append )
00254 {
00255   initDatum();
00256 
00257   if ( ids.isEmpty() && append )
00258     return;
00259 
00260   bool changed = false;
00261 
00262   QMap<int, int> aMap;
00263   for ( uint i = 0; i < ids.count(); i++ )
00264     aMap.insert( ids.at( i ), 0 );
00265 
00266   for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
00267   {
00268     if ( aMap.contains( it.key() ) )
00269     {
00270       if ( it.value() != on )
00271       {
00272         it.value() = on;
00273         changed = true;
00274       }
00275     }
00276     else if ( !append && it.value() == on )
00277     {
00278       it.value() = !on;
00279       changed = true;
00280     }
00281   }
00282   if ( changed )
00283     updateComboBox();
00284 }
00285 
00296 void QDS_ComboBox::setValues( const QList<int>& ids, const QStringList& names )
00297 {
00298   initDatum();
00299 
00300   if ( ids.count() != names.count() )
00301     return;
00302 
00303   myUserIds = ids;
00304   myUserNames = names;
00305 
00306   //unitSystemChanged( "" );
00307 }
00308 
00321 void QDS_ComboBox::setValues( const QStringList& names )
00322 {
00323   initDatum();
00324 
00325   QList< int > ids;
00326   for ( int i = 0, n = names.count(); i < n; i++ )
00327     ids.append( i );
00328   setValues( ids, names );
00329 }
00330 
00337 void QDS_ComboBox::reset()
00338 {
00339   QString aDefValue = defaultValue();
00340   if ( !aDefValue.isEmpty() )
00341     setString( aDefValue );
00342   else
00343     setIntegerValue( getId( 0 ) );
00344 }
00345 
00351 int QDS_ComboBox::stringToValue( const QString& str ) const
00352 {
00353   return getId( str );
00354 }
00355 
00361 QString QDS_ComboBox::valueToString( const int val ) const
00362 {
00363   QString str;
00364   if ( myValue.contains( val ) )
00365     str = myValue[val];
00366   return str;
00367 }
00368 
00373 QString QDS_ComboBox::getString() const
00374 {
00375   QString res;
00376   QtxComboBox* cb = comboBox();
00377   if ( cb )
00378   {
00379     if ( !cb->isEditable() )
00380     {
00381       if ( !cb->isCleared() )
00382         res = cb->currentText(); 
00383     }
00384     else
00385       res = cb->lineEdit()->text();
00386   }
00387   return res;
00388 }
00389 
00394 void QDS_ComboBox::setString( const QString& txt )
00395 {
00396   QtxComboBox* cb = comboBox();
00397   if ( !cb )
00398     return;
00399 
00400   bool isClear = cb->isCleared();
00401   
00402   int idx = -1;
00403   for ( int i = 0; i < cb->count() && idx == -1; i++ )
00404     if ( cb->itemText( i ) == txt )
00405       idx = i;
00406 
00407   int old = cb->currentIndex();
00408   if ( idx != -1 )
00409     cb->setCurrentIndex( idx );
00410   else if ( txt.isEmpty() )
00411   {
00412     if ( !cb->isEditable() )
00413       cb->setCleared( true );
00414     else
00415       cb->lineEdit()->setText( txt );
00416   }
00417   if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentIndex() ) || isClear != cb->isCleared() )
00418   {
00419     onParamChanged();
00420     QString str = getString();
00421     emit activated( integerValue() );
00422     emit activated( str );
00423     emit paramChanged();
00424     emit paramChanged( str );
00425   }
00426 }
00427 
00432 QtxComboBox* QDS_ComboBox::comboBox() const
00433 {
00434   return ::qobject_cast<QtxComboBox*>( controlWidget() );
00435 }
00436 
00442 QWidget* QDS_ComboBox::createControl( QWidget* parent )
00443 {
00444   QtxComboBox* cb = new QtxComboBox( parent );
00445   cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
00446   connect( cb, SIGNAL( textChanged( const QString& ) ), this,
00447            SLOT( onTextChanged( const QString& ) ) );
00448   connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
00449   return cb;
00450 }
00451 
00459 void QDS_ComboBox::unitSystemChanged( const QString& system )
00460 {
00461   QDS_Datum::unitSystemChanged( system );
00462 
00463   Handle(TColStd_HArray1OfInteger) anIds;
00464   Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
00465 
00466   Handle(DDS_DicItem) aDicItem = dicItem();
00467   if ( !aDicItem.IsNull() )
00468     aDicItem->GetListOfValues( aValues, anIds, anIcons );
00469 
00470   myValue.clear();
00471   myIcons.clear();
00472   myDataIds.clear();
00473 
00474   QMap<int, QString> userMap;
00475   QIntList::iterator iIt = myUserIds.begin();
00476   QStringList::iterator sIt = myUserNames.begin();
00477   for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
00478     userMap.insert( *iIt, *sIt );
00479 
00480   if ( !anIds.IsNull() && !aValues.IsNull() &&
00481        anIds->Length() == aValues->Length() )
00482   {
00483     for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
00484     {
00485       QString aValue;
00486       QPixmap aPixmap;
00487       int id = anIds->Value( i );
00488       if ( userMap.contains( id  ) )
00489         aValue = userMap[id];
00490       else
00491       {
00492         aValue = toQString( aValues->Value( i ) );
00493         if ( !anIcons.IsNull() && i <= anIcons->Upper() )
00494         {
00495           QString anIconId = toQString( anIcons->Value( i ) );
00496           if ( anIconId != "" )
00497             aPixmap = QPixmap( anIconId );
00498         }
00499       }
00500 
00501       myDataIds.append( id );
00502       myValue.insert( id, aValue );
00503       myState.insert( id, true );
00504       if ( !aPixmap.isNull() )
00505         myIcons.insert( id, aPixmap );
00506     }
00507   }
00508 
00509   for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
00510   {
00511     int id = *iIt;
00512     if ( !myValue.contains( id  ) )
00513     {
00514       myDataIds.append( id );
00515       myValue.insert( id, userMap[id] );
00516     }
00517   }
00518 
00519   QIntList del, add;
00520   for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
00521     if ( !myValue.contains( it1.key() ) )
00522       del.append( it1.key() );
00523 
00524   for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
00525     if ( !myState.contains( it2.key() ) )
00526       add.append( it2.key() );
00527 
00528   for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
00529     myState.remove( *iter1 );
00530 
00531   for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
00532     myState.insert( *iter2, true );
00533 
00534   updateComboBox();
00535 }
00536 
00541 void QDS_ComboBox::onTextChanged( const QString& /*txt*/ )
00542 {
00543   onParamChanged();
00544   emit paramChanged();
00545   QString str = getString();
00546   emit paramChanged( str );
00547 }
00548 
00553 void QDS_ComboBox::onActivated( int idx )
00554 {
00555   if ( comboBox() )
00556   {
00557     int ind = comboBox()->currentIndex();
00558     comboBox()->setCurrentIndex( -1 );
00559     comboBox()->setCurrentIndex( ind );
00560   }
00561 
00562   int id = getId( idx );
00563   if ( id != -1 )
00564   {
00565     onParamChanged();
00566     QString str = getString();
00567     emit activated( id );
00568     emit activated( str );
00569     emit paramChanged();
00570     emit paramChanged( str );
00571   }
00572 }
00573 
00577 void QDS_ComboBox::updateComboBox()
00578 {
00579   QtxComboBox* cb = comboBox();
00580 
00581   int curId = -1;
00582 
00583   bool isClear = false;
00584 
00585   if ( cb )
00586   {
00587     isClear = cb->isCleared();
00588 
00589     curId = getId( cb->currentIndex() );
00590     cb->clear();
00591   }
00592 
00593   myIndex.clear();
00594 
00595   int idx = 0;
00596   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
00597   {
00598     int id = *it;
00599     if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
00600       continue;
00601 
00602     myIndex.insert( id, idx++ );
00603     if ( cb )
00604     {
00605       if ( myIcons.contains( id ) )
00606         cb->addItem( QIcon(myIcons[id]), myValue[id] );
00607       else
00608         cb->addItem( myValue[id] );
00609     }
00610   }
00611 
00612   if ( cb && cb->count() )
00613   {
00614     cb->setFont( cb->font() );
00615     cb->updateGeometry();
00616 
00617     if ( isClear )
00618       cb->setItemText( cb->currentIndex(), "" );
00619     else
00620     {
00621       if ( getIndex( curId ) != -1 )
00622         cb->setCurrentIndex( getIndex( curId ) );
00623       if ( curId != getId( cb->currentIndex() ) )
00624         onActivated( cb->currentIndex() );
00625     }
00626   }
00627 }
00628 
00634 int QDS_ComboBox::getIndex( const int id ) const
00635 {
00636   int idx = -1;
00637   if ( myIndex.contains( id ) )
00638     idx = myIndex[id];
00639   return idx;
00640 }
00641 
00647 int QDS_ComboBox::getIndex( const QString& str ) const
00648 {
00649   int idx = -1;
00650   QComboBox* cb = comboBox();
00651   if ( cb )
00652   {
00653     for ( int i = 0; i < cb->count() && idx == -1; i++ )
00654       if ( cb->itemText( i ) == str )
00655         idx = i;
00656   }
00657   return idx;
00658 }
00659 
00665 int QDS_ComboBox::getId( const int idx ) const
00666 {
00667   int id = -1;
00668   IdIndexMap::ConstIterator it = myIndex.begin();
00669   for (; it != myIndex.end() && id == -1; ++it )
00670     if ( it.value() == idx )
00671       id = it.key();
00672   return id;
00673 }
00674 
00680 int QDS_ComboBox::getId( const QString& str ) const
00681 {
00682   int id = -1;
00683   int candidate = -1;
00684   IdValueMap::ConstIterator it = myValue.begin();
00685   for (; it != myValue.end() && id == -1; ++it )
00686   {
00687     if ( it.value() == str )
00688     {
00689       if ( state( it.key() ) )
00690         id = it.key();
00691       else
00692         candidate = it.key();
00693     }
00694   }
00695   if ( id == -1 )
00696     id = candidate;
00697 
00698   return id;
00699 }
00700