1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "QDS_ComboBox.h"
21 #include <DDS_Dictionary.h>
23 #include <TCollection_AsciiString.hxx>
24 #include <TColStd_HArray1OfInteger.hxx>
25 #include <TColStd_HArray1OfExtendedString.hxx>
27 #include <qlineedit.h>
32 QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
33 : QDS_Datum( id, parent, flags, comp )
40 QDS_ComboBox::~QDS_ComboBox()
45 Returns true if ComboBox allow to edit current Text.
47 bool QDS_ComboBox::editable() const
49 if ( comboBox() && comboBox()->lineEdit() )
50 return !comboBox()->lineEdit()->isReadOnly();
56 Sets the possibily of current text editing.
58 void QDS_ComboBox::setEditable( const bool on )
60 QComboBox* aCombo = comboBox();
62 aCombo->setEditable( on );
63 if ( aCombo && aCombo->lineEdit() )
65 aCombo->lineEdit()->setReadOnly( !on );
66 aCombo->clearValidator();
68 aCombo->setValidator( validator() );
73 Returns number of items in ComboBox. If total is 'false' then only
74 visible items are taken into account otherwise all items.
76 int QDS_ComboBox::count( bool total ) const
79 return myValue.count();
80 else if ( comboBox() )
81 return comboBox()->count();
87 Returns list of ids. If total is 'false' then only visible items
88 are taken into account otherwise all items.
90 void QDS_ComboBox::values( QValueList<int>& ids, bool total ) const
93 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
94 if ( total || ( myState.contains( *it ) && myState[*it] ) )
99 Returns the current id as integer.
101 int QDS_ComboBox::integerValue() const
103 QComboBox* cb = comboBox();
104 QString cur = getString();
105 if ( cb && cb->count() > 0 && cb->currentItem() >= 0 )
106 cur = cb->text( cb->currentItem() );
108 if ( cb && cur == getString() )
109 return getId( cb->currentItem() );
111 return getId( getString() );
115 Returns the current id as double.
117 double QDS_ComboBox::doubleValue() const
121 QComboBox* cb = comboBox();
122 QString cur = getString();
123 if ( cb && cb->count() > 0 && cb->currentItem() >= 0 )
124 cur = cb->text( cb->currentItem() );
126 if ( cb && cur == getString() )
127 return getId( cb->currentItem() );
129 return getId( getString() );
133 Set the current item acording to specified id.
135 void QDS_ComboBox::setIntegerValue( const int id )
139 if ( myValue.contains( id ) )
140 setString( myValue[id] );
146 Get the integer part of specified value and use it as new current identifier.
148 void QDS_ComboBox::setDoubleValue( const double val )
153 if ( myValue.contains( id ) )
154 setString( myValue[id] );
160 Returns visible state of identificator.
162 bool QDS_ComboBox::state( const int id ) const
165 if ( myState.contains( id ) )
171 Sets the visible state of identificator. If 'id' is -1 then specified
172 state will be set to all ids.
174 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
179 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
180 lst.append( it.key() );
185 setState( on, lst, append );
189 Sets the visible state of identificator from the specified list.
191 void QDS_ComboBox::setState( const bool on, const QValueList<int>& ids, const bool append )
195 if ( ids.isEmpty() && append )
198 bool changed = false;
201 for ( uint i = 0; i < ids.count(); i++ )
202 aMap.insert( *ids.at( i ), 0 );
204 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
206 if ( aMap.contains( it.key() ) )
208 if ( it.data() != on )
214 else if ( !append && it.data() == on )
225 Sets the user items into the combo box.
227 void QDS_ComboBox::setValues( const QValueList<int>& ids, const QStringList& names )
231 if ( ids.count() != names.count() )
239 This is an overloaded member function, provided for convenience.
240 It behaves essentially like the above function. It creates
241 QValueList (0, 1, 2 ... ) and call previous method
243 void QDS_ComboBox::setValues( const QStringList& names )
247 QValueList< int > ids;
248 for ( int i = 0, n = names.count(); i < n; i++ )
250 setValues( ids, names );
254 Sets the active item as item with default id. If default
255 not defined then first item will be used.
257 void QDS_ComboBox::reset()
260 QString aDefValue = defaultValue();
261 if ( !aDefValue.isEmpty() )
262 id = aDefValue.toInt();
267 setIntegerValue( id );
271 Returns identifier from given ComboBox string item.
273 int QDS_ComboBox::stringToValue( const QString& str ) const
279 Returns ComboBox string item from given identifier.
281 QString QDS_ComboBox::valueToString( const int val ) const
284 if ( myValue.contains( val ) )
290 Returns string from QLineEdit widget.
292 QString QDS_ComboBox::getString() const
295 QtxComboBox* cb = comboBox();
298 if ( !cb->editable() )
300 if ( !cb->isCleared() )
301 res = cb->currentText();
304 res = cb->lineEdit()->text();
310 Sets the string into QLineEdit widget.
312 void QDS_ComboBox::setString( const QString& txt )
314 QtxComboBox* cb = comboBox();
318 bool isClear = cb->isCleared();
321 for ( int i = 0; i < cb->count() && idx == -1; i++ )
322 if ( cb->text( i ) == txt )
325 int old = cb->currentItem();
327 cb->setCurrentItem( idx );
328 else if ( txt.isEmpty() )
330 if ( !cb->editable() )
331 cb->setCurrentText( txt );
333 cb->lineEdit()->setText( txt );
335 if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentItem() ) )
338 QString str = getString();
339 emit activated( integerValue() );
340 emit activated( str );
342 emit paramChanged( str );
347 Returns pointer to QtxComboBox widget.
349 QtxComboBox* QDS_ComboBox::comboBox() const
351 return ::qt_cast<QtxComboBox*>( controlWidget() );
355 Create QComboBox widget as control subwidget.
357 QWidget* QDS_ComboBox::createControl( QWidget* parent )
359 QtxComboBox* cb = new QtxComboBox( parent );
360 cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
361 connect( cb, SIGNAL( textChanged( const QString& ) ), this,
362 SLOT( onTextChanged( const QString& ) ) );
363 connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
367 void QDS_ComboBox::unitSystemChanged( const QString& system )
369 QDS_Datum::unitSystemChanged( system );
371 Handle(TColStd_HArray1OfInteger) anIds;
372 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
374 Handle(DDS_DicItem) aDicItem = dicItem();
375 if ( !aDicItem.IsNull() )
376 aDicItem->GetListOfValues( aValues, anIds, anIcons );
382 QMap<int, QString> userMap;
383 QIntList::iterator iIt = myUserIds.begin();
384 QStringList::iterator sIt = myUserNames.begin();
385 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
386 userMap.insert( *iIt, *sIt );
388 if ( !anIds.IsNull() && !aValues.IsNull() &&
389 anIds->Length() == aValues->Length() )
391 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
395 int id = anIds->Value( i );
396 if ( userMap.contains( id ) )
397 aValue = userMap[id];
400 aValue = toQString( aValues->Value( i ) );
401 if ( !anIcons.IsNull() && i <= anIcons->Upper() )
403 QString anIconId = toQString( anIcons->Value( i ) );
404 if ( anIconId != "" )
405 aPixmap = QPixmap( anIconId );
409 myDataIds.append( id );
410 myValue.insert( id, aValue );
411 myState.insert( id, true );
412 if ( !aPixmap.isNull() )
413 myIcons.insert( id, aPixmap );
417 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
420 if ( !myValue.contains( id ) )
422 myDataIds.append( id );
423 myValue.insert( id, userMap[id] );
428 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
429 if ( !myValue.contains( it1.key() ) )
430 del.append( it1.key() );
432 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
433 if ( !myState.contains( it2.key() ) )
434 add.append( it2.key() );
436 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
437 myState.remove( *iter1 );
439 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
440 myState.insert( *iter2, true );
446 Notify about text changing in line edit of ComboBox.
448 void QDS_ComboBox::onTextChanged( const QString& )
452 QString str = getString();
453 emit paramChanged( str );
457 Notify about activation new item.
459 void QDS_ComboBox::onActivated( int idx )
462 comboBox()->setCurrentItem( comboBox()->currentItem() );
464 int id = getId( idx );
468 QString str = getString();
469 emit activated( id );
470 emit activated( str );
472 emit paramChanged( str );
477 Updates ComboBox after have change of visible state or items have been inserted / removed.
479 void QDS_ComboBox::updateComboBox()
481 QtxComboBox* cb = comboBox();
485 bool isClear = false;
489 isClear = cb->isCleared();
491 curId = getId( cb->currentItem() );
498 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
501 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
504 myIndex.insert( id, idx++ );
507 if ( myIcons.contains( id ) )
508 cb->insertItem( myIcons[id], myValue[id] );
510 cb->insertItem( myValue[id] );
514 if ( cb && cb->count() )
516 cb->setFont( cb->font() );
517 cb->updateGeometry();
520 cb->setCurrentText( "" );
523 if ( getIndex( curId ) != -1 )
524 cb->setCurrentItem( getIndex( curId ) );
525 if ( curId != getId( cb->currentItem() ) )
526 onActivated( cb->currentItem() );
532 Returns index of ComboBox item according to id.
534 int QDS_ComboBox::getIndex( const int id ) const
537 if ( myIndex.contains( id ) )
543 Returns index of ComboBox item according to string.
545 int QDS_ComboBox::getIndex( const QString& str ) const
548 QComboBox* cb = comboBox();
551 for ( int i = 0; i < cb->count() && idx == -1; i++ )
552 if ( cb->text( i ) == str )
559 Returns id according to ComboBox item index.
561 int QDS_ComboBox::getId( const int idx ) const
564 IdIndexMap::ConstIterator it = myIndex.begin();
565 for (; it != myIndex.end() && id == -1; ++it )
566 if ( it.data() == idx )
572 Returns id according to ComboBox item string.
574 int QDS_ComboBox::getId( const QString& str ) const
578 IdValueMap::ConstIterator it = myValue.begin();
579 for (; it != myValue.end() && id == -1; ++it )
581 if ( it.data() == str )
583 if ( state( it.key() ) )
586 candidate = it.key();