1 // Copyright (C) 2007-2012 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
23 #include "QDS_ComboBox.h"
25 #include <QtxComboBox.h>
27 #include <TColStd_HArray1OfInteger.hxx>
28 #include <TColStd_HArray1OfExtendedString.hxx>
35 \brief Datum with control corresponding to the combo box.
37 This control is used for datum with enumerable values.
38 It can be used for datum which has type of value 'List'.
40 Each item of the combobox is defined by two properties: integer identifier
41 and string name. All operations on items are performed via identifier.
47 Create combobox datum object with datum identifier \a id
48 and parent widget \a parent.
50 Parameter \a flags defines behaviour of datum and set of created
51 subwidgets. Default value of this parameter is QDS::All.
53 Parameter \a comp specifies the component name which will be used
54 when searching the dictionary item.
56 \param id datum identifier
57 \param parent parent widget
58 \param flags datum flags
61 QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
62 : QDS_Datum( id, parent, flags, comp )
69 QDS_ComboBox::~QDS_ComboBox()
74 \brief Check if combo box allows text editing.
75 \return \c true if combo box is editable
77 bool QDS_ComboBox::editable() const
79 if ( comboBox() && comboBox()->lineEdit() )
80 return !comboBox()->lineEdit()->isReadOnly();
86 \brief Enable/disable text editing.
87 \param on if \c true, combo box is made editable
89 void QDS_ComboBox::setEditable( const bool on )
91 QComboBox* aCombo = comboBox();
93 aCombo->setEditable( on );
94 if ( aCombo && aCombo->lineEdit() )
96 aCombo->lineEdit()->setReadOnly( !on );
97 aCombo->setValidator(0);
99 aCombo->setValidator( validator() );
104 \brief Get number of items in the combo box.
106 \param total if \c false, only visible items are taken into account,
107 otherwise get total number of items
108 \return number of items
110 int QDS_ComboBox::count( bool total ) const
113 return myValue.count();
114 else if ( comboBox() )
115 return comboBox()->count();
121 \brief Get items identifiers.
123 \param ids returned list of items IDs
124 \param total if \c false, only visible items are taken into account,
125 otherwise get total number of items
127 void QDS_ComboBox::values( QList<int>& ids, bool total ) const
130 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
131 if ( total || ( myState.contains( *it ) && myState[*it] ) )
136 \brief Get the current item ID as integer value.
137 \return current item ID converted to integer
139 int QDS_ComboBox::integerValue() const
141 QComboBox* cb = comboBox();
142 QString cur = getString();
143 if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
144 cur = cb->itemText( cb->currentIndex() );
146 if ( cb && cur == getString() )
147 return getId( cb->currentIndex() );
149 return getId( getString() );
153 \brief Get the current item ID as double value.
154 \return current item ID converted to double
156 double QDS_ComboBox::doubleValue() const
160 QComboBox* cb = comboBox();
161 QString cur = getString();
162 if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 )
163 cur = cb->itemText( cb->currentIndex() );
165 if ( cb && cur == getString() )
166 return getId( cb->currentIndex() );
168 return getId( getString() );
172 \brief Set the current item acording to the specified \a id.
175 void QDS_ComboBox::setIntegerValue( const int id )
179 if ( myValue.contains( id ) )
180 setString( myValue[id] );
186 \brief Set the current item acording to the specified \a id.
189 Integer part of \a val is used as new current ID.
193 void QDS_ComboBox::setDoubleValue( const double val )
198 if ( myValue.contains( id ) )
199 setString( myValue[id] );
205 \brief Get visibility state of the item specified by \a id.
207 \return item visibility state
209 bool QDS_ComboBox::state( const int id ) const
212 if ( myState.contains( id ) )
218 \brief Set the visibility state of the item specified by \a id.
220 If \a id is -1 then specified state will be set to all items.
222 If \a append is set to \c true, keep current status for other items,
223 otherwise status of other items is cleared.
225 \param on new visibility state
227 \param append if \c true, keep original status for other items
229 void QDS_ComboBox::setState( const bool on, const int id, const bool append )
234 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
235 lst.append( it.key() );
240 setState( on, lst, append );
244 \brief Set the visibility state of items specified by \a ids.
246 If \a append is set to \c true, keep current status for other items,
247 otherwise status of other items is cleared.
249 \param on new visibility state
250 \param ids items IDs list
251 \param append if \c true, keep original status for other items
253 void QDS_ComboBox::setState( const bool on, const QList<int>& ids, const bool append )
257 if ( ids.isEmpty() && append )
260 bool changed = false;
263 for ( uint i = 0; i < ids.count(); i++ )
264 aMap.insert( ids.at( i ), 0 );
266 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
268 if ( aMap.contains( it.key() ) )
270 if ( it.value() != on )
276 else if ( !append && it.value() == on )
287 \brief Set the custom user items into the combo box.
289 User items like standard dictionary list items will be added
290 into the combobox. This function allows user to override
294 \param names items names
296 void QDS_ComboBox::setValues( const QList<int>& ids, const QStringList& names )
300 if ( ids.count() != names.count() )
306 //unitSystemChanged( "" );
310 \brief Set the custom user items into the combo box.
313 User items like standard dictionary list items will be added
314 into the combobox. This function allows user to override
317 Uses (0, 1, 2 ... ) as items IDs.
319 \param names items names
321 void QDS_ComboBox::setValues( const QStringList& names )
326 for ( int i = 0, n = names.count(); i < n; i++ )
328 setValues( ids, names );
332 \brief Reset the datum.
334 Set the active item as item with default ID. If default ID is not defined
335 then the first item is used.
337 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->setCleared( true );
415 cb->lineEdit()->setText( txt );
417 if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentIndex() ) || isClear != cb->isCleared() )
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( editTextChanged( 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 )
557 int ind = comboBox()->currentIndex();
558 comboBox()->setCurrentIndex( -1 );
559 comboBox()->setCurrentIndex( ind );
562 int id = getId( idx );
566 QString str = getString();
567 emit activated( id );
568 emit activated( str );
570 emit paramChanged( str );
575 \brief Update combo box.
577 void QDS_ComboBox::updateComboBox()
579 QtxComboBox* cb = comboBox();
583 bool isClear = false;
587 isClear = cb->isCleared();
589 curId = getId( cb->currentIndex() );
596 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
599 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
602 myIndex.insert( id, idx++ );
605 if ( myIcons.contains( id ) )
606 cb->addItem( QIcon(myIcons[id]), myValue[id] );
608 cb->addItem( myValue[id] );
612 if ( cb && cb->count() )
614 cb->setFont( cb->font() );
615 cb->updateGeometry();
618 cb->setItemText( cb->currentIndex(), "" );
621 if ( getIndex( curId ) != -1 )
622 cb->setCurrentIndex( getIndex( curId ) );
623 if ( curId != getId( cb->currentIndex() ) )
624 onActivated( cb->currentIndex() );
630 \brief Get index of the combo box item according to its identifier.
632 \return item index or -1 if not found
634 int QDS_ComboBox::getIndex( const int id ) const
637 if ( myIndex.contains( id ) )
643 \brief Get index of the combo box item.
644 \param str combo box item
645 \return item index or -1 if not found
647 int QDS_ComboBox::getIndex( const QString& str ) const
650 QComboBox* cb = comboBox();
653 for ( int i = 0; i < cb->count() && idx == -1; i++ )
654 if ( cb->itemText( i ) == str )
661 \brief Get item identifier according to the specified index.
662 \param idx item index
663 \return item ID or -1 if index is out of range
665 int QDS_ComboBox::getId( const int idx ) const
668 IdIndexMap::ConstIterator it = myIndex.begin();
669 for (; it != myIndex.end() && id == -1; ++it )
670 if ( it.value() == idx )
676 \brief Get item identifier.
677 \param str combo box item
678 \return item ID or -1 if not found
680 int QDS_ComboBox::getId( const QString& str ) const
684 IdValueMap::ConstIterator it = myValue.begin();
685 for (; it != myValue.end() && id == -1; ++it )
687 if ( it.value() == str )
689 if ( state( it.key() ) )
692 candidate = it.key();
702 \fn void QDS_ComboBox::activated( int id );
703 \brief Emitted when the current item in the combo box is changed.
704 \param id current item ID
708 \fn void QDS_ComboBox::activated( const QString& txt );
709 \brief Emitted when the current item in the combo box is changed.
710 \param txt current item text