1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "QDS_ComboBox.h"
24 #include <QtxComboBox.h>
26 #include <TColStd_HArray1OfInteger.hxx>
27 #include <TColStd_HArray1OfExtendedString.hxx>
34 \brief Datum with control corresponding to the combo box.
36 This control is used for datum with enumerable values.
37 It can be used for datum which has type of value 'List'.
39 Each item of the combobox is defined by two properties: integer identifier
40 and string name. All operations on items are performed via identifier.
46 Create combobox datum object with datum identifier \a id
47 and parent widget \a parent.
49 Parameter \a flags defines behaviour of datum and set of created
50 subwidgets. Default value of this parameter is QDS::All.
52 Parameter \a comp specifies the component name which will be used
53 when searching the dictionary item.
55 \param id datum identifier
56 \param parent parent widget
57 \param flags datum flags
60 QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
61 : QDS_Datum( id, parent, flags, comp )
68 QDS_ComboBox::~QDS_ComboBox()
73 \brief Check if combo box allows text editing.
74 \return \c true if combo box is editable
76 bool QDS_ComboBox::editable() const
78 if ( comboBox() && comboBox()->lineEdit() )
79 return !comboBox()->lineEdit()->isReadOnly();
85 \brief Enable/disable text editing.
86 \param on if \c true, combo box is made editable
88 void QDS_ComboBox::setEditable( const bool on )
90 QComboBox* aCombo = comboBox();
92 aCombo->setEditable( on );
93 if ( aCombo && aCombo->lineEdit() )
95 aCombo->lineEdit()->setReadOnly( !on );
96 aCombo->setValidator(0);
98 aCombo->setValidator( validator() );
103 \brief Get number of items in the combo box.
105 \param total if \c false, only visible items are taken into account,
106 otherwise get total number of items
107 \return number of items
109 int QDS_ComboBox::count( bool total ) const
112 return myValue.count();
113 else if ( comboBox() )
114 return comboBox()->count();
120 \brief Get items identifiers.
122 \param ids returned list of items IDs
123 \param total if \c false, only visible items are taken into account,
124 otherwise get total number of items
126 void QDS_ComboBox::values( QList<int>& ids, bool total ) const
129 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
130 if ( total || ( myState.contains( *it ) && myState[*it] ) )
135 \brief Get the current item ID as integer value.
136 \return current item ID converted to integer
138 int QDS_ComboBox::integerValue() const
140 QComboBox* cb = comboBox();
141 QString cur = getString();
142 if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
143 cur = cb->itemText( cb->currentIndex() );
145 if ( cb && cur == getString() )
146 return getId( cb->currentIndex() );
148 return getId( getString() );
152 \brief Get the current item ID as double value.
153 \return current item ID converted to double
155 double QDS_ComboBox::doubleValue() const
159 QComboBox* cb = comboBox();
160 QString cur = getString();
161 if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
162 cur = cb->itemText( cb->currentIndex() );
164 if ( cb && cur == getString() )
165 return getId( cb->currentIndex() );
167 return getId( getString() );
171 \brief Set the current item acording to the specified \a id.
174 void QDS_ComboBox::setIntegerValue( const int id )
178 if ( myValue.contains( id ) )
179 setString( myValue[id] );
185 \brief Set the current item acording to the specified \a id.
188 Integer part of \a val is used as new current ID.
192 void QDS_ComboBox::setDoubleValue( const double val )
197 if ( myValue.contains( id ) )
198 setString( myValue[id] );
204 \brief Get visibility state of the item specified by \a id.
206 \return item visibility state
208 bool QDS_ComboBox::state( const int id ) const
211 if ( myState.contains( id ) )
217 \brief Set the visibility state of the item specified by \a id.
219 If \a id is -1 then specified state will be set to all items.
221 If \a append is set to \c true, keep current status for other items,
222 otherwise status of other items is cleared.
224 \param on new visibility state
226 \param append if \c true, keep original status for other items
228 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
233 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
234 lst.append( it.key() );
239 setState( on, lst, append );
243 \brief Set the visibility state of items specified by \a ids.
245 If \a append is set to \c true, keep current status for other items,
246 otherwise status of other items is cleared.
248 \param on new visibility state
249 \param ids items IDs list
250 \param append if \c true, keep original status for other items
252 void QDS_ComboBox::setState( const bool on, const QList<int>& ids, const bool append )
256 if ( ids.isEmpty() && append )
259 bool changed = false;
262 for ( uint i = 0; i < ids.count(); i++ )
263 aMap.insert( ids.at( i ), 0 );
265 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
267 if ( aMap.contains( it.key() ) )
269 if ( it.value() != on )
275 else if ( !append && it.value() == on )
286 \brief Set the custom user items into the combo box.
288 User items like standard dictionary list items will be added
289 into the combobox. This function allows user to override
293 \param names items names
295 void QDS_ComboBox::setValues( const QList<int>& ids, const QStringList& names )
299 if ( ids.count() != names.count() )
305 unitSystemChanged( "" );
309 \brief Set the custom user items into the combo box.
312 User items like standard dictionary list items will be added
313 into the combobox. This function allows user to override
316 Uses (0, 1, 2 ... ) as items IDs.
318 \param names items names
320 void QDS_ComboBox::setValues( const QStringList& names )
325 for ( int i = 0, n = names.count(); i < n; i++ )
327 setValues( ids, names );
331 \brief Reset the datum.
333 Set the active item as item with default ID. If default ID is not defined
334 then the first item is used.
336 void QDS_ComboBox::reset()
339 QString aDefValue = defaultValue();
340 if ( !aDefValue.isEmpty() )
341 setString( aDefValue );
343 setIntegerValue( getId( 0 ) );
347 \brief Convert string to integer value.
349 \return item ID or -1 if not found
351 int QDS_ComboBox::stringToValue( const QString& str ) const
357 \brief Convert integer to string value.
359 \return item value or empty string if \c val is invalid
361 QString QDS_ComboBox::valueToString( const int val ) const
364 if ( myValue.contains( val ) )
370 \brief Get string from the combo box.
373 QString QDS_ComboBox::getString() const
376 QtxComboBox* cb = comboBox();
379 if ( !cb->isEditable() )
381 if ( !cb->isCleared() )
382 res = cb->currentText();
385 res = cb->lineEdit()->text();
391 \brief Set the string value to the combo box widget.
392 \param txt string value
394 void QDS_ComboBox::setString( const QString& txt )
396 QtxComboBox* cb = comboBox();
400 bool isClear = cb->isCleared();
403 for ( int i = 0; i < cb->count() && idx == -1; i++ )
404 if ( cb->itemText( i ) == txt )
407 int old = cb->currentIndex();
409 cb->setCurrentIndex( idx );
410 else if ( txt.isEmpty() )
412 if ( !cb->isEditable() )
413 cb->setItemText( cb->currentIndex(), txt );
415 cb->lineEdit()->setText( txt );
417 if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentIndex() ) )
420 QString str = getString();
421 emit activated( integerValue() );
422 emit activated( str );
424 emit paramChanged( str );
429 \brief Get combo box widget.
430 \return internal combo box widget
432 QtxComboBox* QDS_ComboBox::comboBox() const
434 return ::qobject_cast<QtxComboBox*>( controlWidget() );
438 \brief Create internal combo box as control widget.
439 \param parent parent widget
440 \return created combo box widget
442 QWidget* QDS_ComboBox::createControl( QWidget* parent )
444 QtxComboBox* cb = new QtxComboBox( parent );
445 cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
446 connect( cb, SIGNAL( textChanged( const QString& ) ), this,
447 SLOT( onTextChanged( const QString& ) ) );
448 connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
453 \brief Process notification about active units system changing.
455 Update combobox contents.
457 \param system new active units system
459 void QDS_ComboBox::unitSystemChanged( const QString& system )
461 QDS_Datum::unitSystemChanged( system );
463 Handle(TColStd_HArray1OfInteger) anIds;
464 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
466 Handle(DDS_DicItem) aDicItem = dicItem();
467 if ( !aDicItem.IsNull() )
468 aDicItem->GetListOfValues( aValues, anIds, anIcons );
474 QMap<int, QString> userMap;
475 QIntList::iterator iIt = myUserIds.begin();
476 QStringList::iterator sIt = myUserNames.begin();
477 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
478 userMap.insert( *iIt, *sIt );
480 if ( !anIds.IsNull() && !aValues.IsNull() &&
481 anIds->Length() == aValues->Length() )
483 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
487 int id = anIds->Value( i );
488 if ( userMap.contains( id ) )
489 aValue = userMap[id];
492 aValue = toQString( aValues->Value( i ) );
493 if ( !anIcons.IsNull() && i <= anIcons->Upper() )
495 QString anIconId = toQString( anIcons->Value( i ) );
496 if ( anIconId != "" )
497 aPixmap = QPixmap( anIconId );
501 myDataIds.append( id );
502 myValue.insert( id, aValue );
503 myState.insert( id, true );
504 if ( !aPixmap.isNull() )
505 myIcons.insert( id, aPixmap );
509 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
512 if ( !myValue.contains( id ) )
514 myDataIds.append( id );
515 myValue.insert( id, userMap[id] );
520 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
521 if ( !myValue.contains( it1.key() ) )
522 del.append( it1.key() );
524 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
525 if ( !myState.contains( it2.key() ) )
526 add.append( it2.key() );
528 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
529 myState.remove( *iter1 );
531 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
532 myState.insert( *iter2, true );
538 \brief Called when text in the combo box (editable) is modified by the user.
539 \param txt current text in the combo box editor (not used)
541 void QDS_ComboBox::onTextChanged( const QString& /*txt*/ )
545 QString str = getString();
546 emit paramChanged( str );
550 \brief Called when combo box item is activated.
551 \param idx index of teh item being activated
553 void QDS_ComboBox::onActivated( int idx )
556 comboBox()->setCurrentIndex( comboBox()->currentIndex() );
558 int id = getId( idx );
562 QString str = getString();
563 emit activated( id );
564 emit activated( str );
566 emit paramChanged( str );
571 \brief Update combo box.
573 void QDS_ComboBox::updateComboBox()
575 QtxComboBox* cb = comboBox();
579 bool isClear = false;
583 isClear = cb->isCleared();
585 curId = getId( cb->currentIndex() );
592 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
595 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
598 myIndex.insert( id, idx++ );
601 if ( myIcons.contains( id ) )
602 cb->addItem( QIcon(myIcons[id]), myValue[id] );
604 cb->addItem( myValue[id] );
608 if ( cb && cb->count() )
610 cb->setFont( cb->font() );
611 cb->updateGeometry();
614 cb->setItemText( cb->currentIndex(), "" );
617 if ( getIndex( curId ) != -1 )
618 cb->setCurrentIndex( getIndex( curId ) );
619 if ( curId != getId( cb->currentIndex() ) )
620 onActivated( cb->currentIndex() );
626 \brief Get index of the combo box item according to its identifier.
628 \return item index or -1 if not found
630 int QDS_ComboBox::getIndex( const int id ) const
633 if ( myIndex.contains( id ) )
639 \brief Get index of the combo box item.
640 \param str combo box item
641 \return item index or -1 if not found
643 int QDS_ComboBox::getIndex( const QString& str ) const
646 QComboBox* cb = comboBox();
649 for ( int i = 0; i < cb->count() && idx == -1; i++ )
650 if ( cb->itemText( i ) == str )
657 \brief Get item identifier according to the specified index.
658 \param idx item index
659 \return item ID or -1 if index is out of range
661 int QDS_ComboBox::getId( const int idx ) const
664 IdIndexMap::ConstIterator it = myIndex.begin();
665 for (; it != myIndex.end() && id == -1; ++it )
666 if ( it.value() == idx )
672 \brief Get item identifier.
673 \param str combo box item
674 \return item ID or -1 if not found
676 int QDS_ComboBox::getId( const QString& str ) const
680 IdValueMap::ConstIterator it = myValue.begin();
681 for (; it != myValue.end() && id == -1; ++it )
683 if ( it.value() == str )
685 if ( state( it.key() ) )
688 candidate = it.key();
698 \fn void QDS_ComboBox::activated( int id );
699 \brief Emitted when the current item in the combo box is changed.
700 \param id current item ID
704 \fn void QDS_ComboBox::activated( const QString& txt );
705 \brief Emitted when the current item in the combo box is changed.
706 \param txt current item text