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
101 QComboBox* cb = comboBox();
102 QString cur = getString();
103 if ( cb && cb->count() > 0 && cb->currentItem() >= 0 )
104 cur = cb->text( cb->currentItem() );
106 if ( cb && cur == getString() )
107 return getId( cb->currentItem() );
109 return getId( getString() );
113 Set the current item acording to specified id.
115 void QDS_ComboBox::setIntegerValue( const int id )
117 if ( myValue.contains( id ) )
118 setString( myValue[id] );
124 Get the integer part of specified value and use it as new current identifier.
126 void QDS_ComboBox::setDoubleValue( const double val )
129 if ( myValue.contains( id ) )
130 setString( myValue[id] );
136 Returns visible state of identificator.
138 bool QDS_ComboBox::state( const int id ) const
141 if ( myState.contains( id ) )
147 Sets the visible state of identificator. If 'id' is -1 then specified
148 state will be set to all ids.
150 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
155 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
156 lst.append( it.key() );
161 setState( on, lst, append );
165 Sets the visible state of identificator from the specified list.
167 void QDS_ComboBox::setState( const bool on, const QValueList<int>& ids, const bool append )
169 if ( ids.isEmpty() && append )
172 bool changed = false;
175 for ( uint i = 0; i < ids.count(); i++ )
176 aMap.insert( *ids.at( i ), 0 );
178 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
180 if ( aMap.contains( it.key() ) )
182 if ( it.data() != on )
188 else if ( !append && it.data() == on )
199 Sets the user items into the combo box.
201 void QDS_ComboBox::setValues( const QValueList<int>& ids, const QStringList& names )
203 if ( ids.count() != names.count() )
211 This is an overloaded member function, provided for convenience.
212 It behaves essentially like the above function. It creates
213 QValueList (0, 1, 2 ... ) and call previous method
215 void QDS_ComboBox::setValues( const QStringList& names )
217 QValueList< int > ids;
218 for ( int i = 0, n = names.count(); i < n; i++ )
220 setValues( ids, names );
224 Sets the active item as item with default id. If default
225 not defined then first item will be used.
227 void QDS_ComboBox::reset()
230 QString aDefValue = defaultValue();
231 if ( !aDefValue.isEmpty() )
232 id = aDefValue.toInt();
237 setIntegerValue( id );
241 Returns identifier from given ComboBox string item.
243 int QDS_ComboBox::stringToValue( const QString& str ) const
249 Returns ComboBox string item from given identifier.
251 QString QDS_ComboBox::valueToString( const int val ) const
254 if ( myValue.contains( val ) )
260 Returns string from QLineEdit widget.
262 QString QDS_ComboBox::getString() const
265 QtxComboBox* cb = comboBox();
268 if ( !cb->editable() )
270 if ( !cb->isCleared() )
271 res = cb->currentText();
274 res = cb->lineEdit()->text();
280 Sets the string into QLineEdit widget.
282 void QDS_ComboBox::setString( const QString& txt )
284 QtxComboBox* cb = comboBox();
288 bool isClear = cb->isCleared();
291 for ( int i = 0; i < cb->count() && idx == -1; i++ )
292 if ( cb->text( i ) == txt )
295 int old = cb->currentItem();
297 cb->setCurrentItem( idx );
298 else if ( txt.isEmpty() )
300 if ( !cb->editable() )
301 cb->setCurrentText( txt );
303 cb->lineEdit()->setText( txt );
305 if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentItem() ) )
308 QString str = getString();
309 emit activated( integerValue() );
310 emit activated( str );
312 emit paramChanged( str );
317 Returns pointer to QtxComboBox widget.
319 QtxComboBox* QDS_ComboBox::comboBox() const
321 return ::qt_cast<QtxComboBox*>( controlWidget() );
325 Create QComboBox widget as control subwidget.
327 QWidget* QDS_ComboBox::createControl( QWidget* parent )
329 QtxComboBox* cb = new QtxComboBox( parent );
330 cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
331 connect( cb, SIGNAL( textChanged( const QString& ) ), this,
332 SLOT( onTextChanged( const QString& ) ) );
333 connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
337 void QDS_ComboBox::unitSystemChanged( const QString& system )
339 QDS_Datum::unitSystemChanged( system );
341 Handle(TColStd_HArray1OfInteger) anIds;
342 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
344 Handle(DDS_DicItem) aDicItem = dicItem();
345 if ( !aDicItem.IsNull() )
346 aDicItem->GetListOfValues( aValues, anIds, anIcons );
352 QMap<int, QString> userMap;
353 QIntList::iterator iIt = myUserIds.begin();
354 QStringList::iterator sIt = myUserNames.begin();
355 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
356 userMap.insert( *iIt, *sIt );
358 if ( !anIds.IsNull() && !aValues.IsNull() &&
359 anIds->Length() == aValues->Length() )
361 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
365 int id = anIds->Value( i );
366 if ( userMap.contains( id ) )
367 aValue = userMap[id];
370 aValue = toQString( aValues->Value( i ) );
371 if ( !anIcons.IsNull() && i <= anIcons->Upper() )
373 QString anIconId = toQString( anIcons->Value( i ) );
374 if ( anIconId != "" )
375 aPixmap = QPixmap( anIconId );
379 myDataIds.append( id );
380 myValue.insert( id, aValue );
381 myState.insert( id, true );
382 if ( !aPixmap.isNull() )
383 myIcons.insert( id, aPixmap );
387 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
390 if ( !myValue.contains( id ) )
392 myDataIds.append( id );
393 myValue.insert( id, userMap[id] );
398 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
399 if ( !myValue.contains( it1.key() ) )
400 del.append( it1.key() );
402 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
403 if ( !myState.contains( it2.key() ) )
404 add.append( it2.key() );
406 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
407 myState.remove( *iter1 );
409 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
410 myState.insert( *iter2, true );
416 Notify about text changing in line edit of ComboBox.
418 void QDS_ComboBox::onTextChanged( const QString& )
422 QString str = getString();
423 emit paramChanged( str );
427 Notify about activation new item.
429 void QDS_ComboBox::onActivated( int idx )
432 comboBox()->setCurrentItem( comboBox()->currentItem() );
434 int id = getId( idx );
438 QString str = getString();
439 emit activated( id );
440 emit activated( str );
442 emit paramChanged( str );
447 Updates ComboBox after have change of visible state or items have been inserted / removed.
449 void QDS_ComboBox::updateComboBox()
451 QtxComboBox* cb = comboBox();
455 bool isClear = false;
459 isClear = cb->isCleared();
461 curId = getId( cb->currentItem() );
468 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
471 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
474 myIndex.insert( id, idx++ );
477 if ( myIcons.contains( id ) )
478 cb->insertItem( myIcons[id], myValue[id] );
480 cb->insertItem( myValue[id] );
484 if ( cb && cb->count() )
486 cb->setFont( cb->font() );
487 cb->updateGeometry();
490 cb->setCurrentText( "" );
493 if ( getIndex( curId ) != -1 )
494 cb->setCurrentItem( getIndex( curId ) );
495 if ( curId != getId( cb->currentItem() ) )
496 onActivated( cb->currentItem() );
502 Returns index of ComboBox item according to id.
504 int QDS_ComboBox::getIndex( const int id ) const
507 if ( myIndex.contains( id ) )
513 Returns index of ComboBox item according to string.
515 int QDS_ComboBox::getIndex( const QString& str ) const
518 QComboBox* cb = comboBox();
521 for ( int i = 0; i < cb->count() && idx == -1; i++ )
522 if ( cb->text( i ) == str )
529 Returns id according to ComboBox item index.
531 int QDS_ComboBox::getId( const int idx ) const
534 IdIndexMap::ConstIterator it = myIndex.begin();
535 for (; it != myIndex.end() && id == -1; ++it )
536 if ( it.data() == idx )
542 Returns id according to ComboBox item string.
544 int QDS_ComboBox::getId( const QString& str ) const
548 IdValueMap::ConstIterator it = myValue.begin();
549 for (; it != myValue.end() && id == -1; ++it )
551 if ( it.data() == str )
553 if ( state( it.key() ) )
556 candidate = it.key();