1 #include "QDS_ComboBox.h"
3 #include <DDS_Dictionary.h>
5 #include <TCollection_AsciiString.hxx>
6 #include <TColStd_HArray1OfInteger.hxx>
7 #include <TColStd_HArray1OfExtendedString.hxx>
14 QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
15 : QDS_Datum( id, parent, flags, comp )
22 QDS_ComboBox::~QDS_ComboBox()
27 Returns true if ComboBox allow to edit current Text.
29 bool QDS_ComboBox::editable() const
31 if ( comboBox() && comboBox()->lineEdit() )
32 return !comboBox()->lineEdit()->isReadOnly();
38 Sets the possibily of current text editing.
40 void QDS_ComboBox::setEditable( const bool on )
42 QComboBox* aCombo = comboBox();
44 aCombo->setEditable( on );
45 if ( aCombo && aCombo->lineEdit() )
47 aCombo->lineEdit()->setReadOnly( !on );
48 aCombo->clearValidator();
50 aCombo->setValidator( validator() );
55 Returns number of items in ComboBox. If total is 'false' then only
56 visible items are taken into account otherwise all items.
58 int QDS_ComboBox::count( bool total ) const
61 return myValue.count();
62 else if ( comboBox() )
63 return comboBox()->count();
69 Returns list of ids. If total is 'false' then only visible items
70 are taken into account otherwise all items.
72 void QDS_ComboBox::values( QValueList<int>& ids, bool total ) const
75 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
76 if ( total || ( myState.contains( *it ) && myState[*it] ) )
81 Returns the current id as integer.
83 int QDS_ComboBox::integerValue() const
85 QComboBox* cb = comboBox();
86 QString cur = getString();
87 if ( cb && cb->count() > 0 && cb->currentItem() >= 0 )
88 cur = cb->text( cb->currentItem() );
90 if ( cb && cur == getString() )
91 return getId( cb->currentItem() );
93 return getId( getString() );
97 Returns the current id as double.
99 double QDS_ComboBox::doubleValue() 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 Set the current item acording to specified id.
117 void QDS_ComboBox::setIntegerValue( const int id )
121 if ( myValue.contains( id ) )
122 setString( myValue[id] );
128 Get the integer part of specified value and use it as new current identifier.
130 void QDS_ComboBox::setDoubleValue( const double val )
135 if ( myValue.contains( id ) )
136 setString( myValue[id] );
142 Returns visible state of identificator.
144 bool QDS_ComboBox::state( const int id ) const
147 if ( myState.contains( id ) )
153 Sets the visible state of identificator. If 'id' is -1 then specified
154 state will be set to all ids.
156 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
161 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
162 lst.append( it.key() );
167 setState( on, lst, append );
171 Sets the visible state of identificator from the specified list.
173 void QDS_ComboBox::setState( const bool on, const QValueList<int>& ids, const bool append )
177 if ( ids.isEmpty() && append )
180 bool changed = false;
183 for ( uint i = 0; i < ids.count(); i++ )
184 aMap.insert( *ids.at( i ), 0 );
186 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
188 if ( aMap.contains( it.key() ) )
190 if ( it.data() != on )
196 else if ( !append && it.data() == on )
207 Sets the user items into the combo box.
209 void QDS_ComboBox::setValues( const QValueList<int>& ids, const QStringList& names )
213 if ( ids.count() != names.count() )
221 This is an overloaded member function, provided for convenience.
222 It behaves essentially like the above function. It creates
223 QValueList (0, 1, 2 ... ) and call previous method
225 void QDS_ComboBox::setValues( const QStringList& names )
229 QValueList< int > ids;
230 for ( int i = 0, n = names.count(); i < n; i++ )
232 setValues( ids, names );
236 Sets the active item as item with default id. If default
237 not defined then first item will be used.
239 void QDS_ComboBox::reset()
242 QString aDefValue = defaultValue();
243 if ( !aDefValue.isEmpty() )
244 id = aDefValue.toInt();
249 setIntegerValue( id );
253 Returns identifier from given ComboBox string item.
255 int QDS_ComboBox::stringToValue( const QString& str ) const
261 Returns ComboBox string item from given identifier.
263 QString QDS_ComboBox::valueToString( const int val ) const
266 if ( myValue.contains( val ) )
272 Returns string from QLineEdit widget.
274 QString QDS_ComboBox::getString() const
277 QtxComboBox* cb = comboBox();
280 if ( !cb->editable() )
282 if ( !cb->isCleared() )
283 res = cb->currentText();
286 res = cb->lineEdit()->text();
292 Sets the string into QLineEdit widget.
294 void QDS_ComboBox::setString( const QString& txt )
296 QtxComboBox* cb = comboBox();
300 bool isClear = cb->isCleared();
303 for ( int i = 0; i < cb->count() && idx == -1; i++ )
304 if ( cb->text( i ) == txt )
307 int old = cb->currentItem();
309 cb->setCurrentItem( idx );
310 else if ( txt.isEmpty() )
312 if ( !cb->editable() )
313 cb->setCurrentText( txt );
315 cb->lineEdit()->setText( txt );
317 if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentItem() ) )
320 QString str = getString();
321 emit activated( integerValue() );
322 emit activated( str );
324 emit paramChanged( str );
329 Returns pointer to QtxComboBox widget.
331 QtxComboBox* QDS_ComboBox::comboBox() const
333 return ::qt_cast<QtxComboBox*>( controlWidget() );
337 Create QComboBox widget as control subwidget.
339 QWidget* QDS_ComboBox::createControl( QWidget* parent )
341 QtxComboBox* cb = new QtxComboBox( parent );
342 cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
343 connect( cb, SIGNAL( textChanged( const QString& ) ), this,
344 SLOT( onTextChanged( const QString& ) ) );
345 connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
349 void QDS_ComboBox::unitSystemChanged( const QString& system )
351 QDS_Datum::unitSystemChanged( system );
353 Handle(TColStd_HArray1OfInteger) anIds;
354 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
356 Handle(DDS_DicItem) aDicItem = dicItem();
357 if ( !aDicItem.IsNull() )
358 aDicItem->GetListOfValues( aValues, anIds, anIcons );
364 QMap<int, QString> userMap;
365 QIntList::iterator iIt = myUserIds.begin();
366 QStringList::iterator sIt = myUserNames.begin();
367 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
368 userMap.insert( *iIt, *sIt );
370 if ( !anIds.IsNull() && !aValues.IsNull() &&
371 anIds->Length() == aValues->Length() )
373 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
377 int id = anIds->Value( i );
378 if ( userMap.contains( id ) )
379 aValue = userMap[id];
382 aValue = toQString( aValues->Value( i ) );
383 if ( !anIcons.IsNull() && i <= anIcons->Upper() )
385 QString anIconId = toQString( anIcons->Value( i ) );
386 if ( anIconId != "" )
387 aPixmap = QPixmap( anIconId );
391 myDataIds.append( id );
392 myValue.insert( id, aValue );
393 myState.insert( id, true );
394 if ( !aPixmap.isNull() )
395 myIcons.insert( id, aPixmap );
399 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
402 if ( !myValue.contains( id ) )
404 myDataIds.append( id );
405 myValue.insert( id, userMap[id] );
410 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
411 if ( !myValue.contains( it1.key() ) )
412 del.append( it1.key() );
414 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
415 if ( !myState.contains( it2.key() ) )
416 add.append( it2.key() );
418 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
419 myState.remove( *iter1 );
421 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
422 myState.insert( *iter2, true );
428 Notify about text changing in line edit of ComboBox.
430 void QDS_ComboBox::onTextChanged( const QString& )
434 QString str = getString();
435 emit paramChanged( str );
439 Notify about activation new item.
441 void QDS_ComboBox::onActivated( int idx )
444 comboBox()->setCurrentItem( comboBox()->currentItem() );
446 int id = getId( idx );
450 QString str = getString();
451 emit activated( id );
452 emit activated( str );
454 emit paramChanged( str );
459 Updates ComboBox after have change of visible state or items have been inserted / removed.
461 void QDS_ComboBox::updateComboBox()
463 QtxComboBox* cb = comboBox();
467 bool isClear = false;
471 isClear = cb->isCleared();
473 curId = getId( cb->currentItem() );
480 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
483 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
486 myIndex.insert( id, idx++ );
489 if ( myIcons.contains( id ) )
490 cb->insertItem( myIcons[id], myValue[id] );
492 cb->insertItem( myValue[id] );
496 if ( cb && cb->count() )
498 cb->setFont( cb->font() );
499 cb->updateGeometry();
502 cb->setCurrentText( "" );
505 if ( getIndex( curId ) != -1 )
506 cb->setCurrentItem( getIndex( curId ) );
507 if ( curId != getId( cb->currentItem() ) )
508 onActivated( cb->currentItem() );
514 Returns index of ComboBox item according to id.
516 int QDS_ComboBox::getIndex( const int id ) const
519 if ( myIndex.contains( id ) )
525 Returns index of ComboBox item according to string.
527 int QDS_ComboBox::getIndex( const QString& str ) const
530 QComboBox* cb = comboBox();
533 for ( int i = 0; i < cb->count() && idx == -1; i++ )
534 if ( cb->text( i ) == str )
541 Returns id according to ComboBox item index.
543 int QDS_ComboBox::getId( const int idx ) const
546 IdIndexMap::ConstIterator it = myIndex.begin();
547 for (; it != myIndex.end() && id == -1; ++it )
548 if ( it.data() == idx )
554 Returns id according to ComboBox item string.
556 int QDS_ComboBox::getId( const QString& str ) const
560 IdValueMap::ConstIterator it = myValue.begin();
561 for (; it != myValue.end() && id == -1; ++it )
563 if ( it.data() == str )
565 if ( state( it.key() ) )
568 candidate = it.key();