X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FQDS%2FQDS_ComboBox.cxx;h=7ebf29dfd6095b6e6add77cfe3844110a993d8dc;hb=8d623c5450c440d287993e3650ec4d1e4d44ca58;hp=a3e7a3af34d2e1930300dffa8eeaa48e0d0d6b31;hpb=aa05f2a1b23a54321ea7248ae9ac3463652c734e;p=modules%2Fgui.git diff --git a/src/QDS/QDS_ComboBox.cxx b/src/QDS/QDS_ComboBox.cxx index a3e7a3af3..7ebf29dfd 100644 --- a/src/QDS/QDS_ComboBox.cxx +++ b/src/QDS/QDS_ComboBox.cxx @@ -1,11 +1,14 @@ -// Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D +// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License. // -// This library is distributed in the hope that it will be useful +// This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. @@ -14,20 +17,46 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // -// See http://www.salome-platform.org/ +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // + #include "QDS_ComboBox.h" -#include +#include -#include #include #include -#include +#include + +/* + \class QDS_ComboBox + + \brief Datum with control corresponding to the combo box. + + This control is used for datum with enumerable values. + It can be used for datum which has type of value 'List'. + + Each item of the combobox is defined by two properties: integer identifier + and string name. All operations on items are performed via identifier. +*/ /*! - Constructor. + \brief Constructor. + + Create combobox datum object with datum identifier \a id + and parent widget \a parent. + + Parameter \a flags defines behaviour of datum and set of created + subwidgets. Default value of this parameter is QDS::All. + + Parameter \a comp specifies the component name which will be used + when searching the dictionary item. + + \param id datum identifier + \param parent parent widget + \param flags datum flags + \param comp component */ QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, const QString& comp ) : QDS_Datum( id, parent, flags, comp ) @@ -35,14 +64,15 @@ QDS_ComboBox::QDS_ComboBox( const QString& id, QWidget* parent, const int flags, } /*! - Destructor. + \brief Destructor. */ QDS_ComboBox::~QDS_ComboBox() { } /*! - Returns true if ComboBox allow to edit current Text. + \brief Check if combo box allows text editing. + \return \c true if combo box is editable */ bool QDS_ComboBox::editable() const { @@ -53,7 +83,8 @@ bool QDS_ComboBox::editable() const } /*! - Sets the possibily of current text editing. + \brief Enable/disable text editing. + \param on if \c true, combo box is made editable */ void QDS_ComboBox::setEditable( const bool on ) { @@ -63,15 +94,18 @@ void QDS_ComboBox::setEditable( const bool on ) if ( aCombo && aCombo->lineEdit() ) { aCombo->lineEdit()->setReadOnly( !on ); - aCombo->clearValidator(); + aCombo->setValidator(0); if ( on ) aCombo->setValidator( validator() ); } } /*! - Returns number of items in ComboBox. If total is 'false' then only - visible items are taken into account otherwise all items. + \brief Get number of items in the combo box. + + \param total if \c false, only visible items are taken into account, + otherwise get total number of items + \return number of items */ int QDS_ComboBox::count( bool total ) const { @@ -84,10 +118,13 @@ int QDS_ComboBox::count( bool total ) const } /*! - Returns list of ids. If total is 'false' then only visible items - are taken into account otherwise all items. + \brief Get items identifiers. + + \param ids returned list of items IDs + \param total if \c false, only visible items are taken into account, + otherwise get total number of items */ -void QDS_ComboBox::values( QValueList& ids, bool total ) const +void QDS_ComboBox::values( QList& ids, bool total ) const { ids.clear(); for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it ) @@ -96,23 +133,25 @@ void QDS_ComboBox::values( QValueList& ids, bool total ) const } /*! - Returns the current id as integer. + \brief Get the current item ID as integer value. + \return current item ID converted to integer */ int QDS_ComboBox::integerValue() const { QComboBox* cb = comboBox(); QString cur = getString(); - if ( cb && cb->count() > 0 && cb->currentItem() >= 0 ) - cur = cb->text( cb->currentItem() ); + if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 ) + cur = cb->itemText( cb->currentIndex() ); if ( cb && cur == getString() ) - return getId( cb->currentItem() ); + return getId( cb->currentIndex() ); else return getId( getString() ); } /*! - Returns the current id as double. + \brief Get the current item ID as double value. + \return current item ID converted to double */ double QDS_ComboBox::doubleValue() const { @@ -120,17 +159,18 @@ double QDS_ComboBox::doubleValue() const QComboBox* cb = comboBox(); QString cur = getString(); - if ( cb && cb->count() > 0 && cb->currentItem() >= 0 ) - cur = cb->text( cb->currentItem() ); + if ( cb && cb->count() > 0 && cb->currentIndex() >= 0 ) + cur = cb->itemText( cb->currentIndex() ); if ( cb && cur == getString() ) - return getId( cb->currentItem() ); + return getId( cb->currentIndex() ); else return getId( getString() ); } /*! - Set the current item acording to specified id. + \brief Set the current item acording to the specified \a id. + \param id item ID */ void QDS_ComboBox::setIntegerValue( const int id ) { @@ -143,7 +183,12 @@ void QDS_ComboBox::setIntegerValue( const int id ) } /*! - Get the integer part of specified value and use it as new current identifier. + \brief Set the current item acording to the specified \a id. + \overload + + Integer part of \a val is used as new current ID. + + \param val item ID */ void QDS_ComboBox::setDoubleValue( const double val ) { @@ -157,7 +202,9 @@ void QDS_ComboBox::setDoubleValue( const double val ) } /*! - Returns visible state of identificator. + \brief Get visibility state of the item specified by \a id. + \param id item ID + \return item visibility state */ bool QDS_ComboBox::state( const int id ) const { @@ -168,12 +215,20 @@ bool QDS_ComboBox::state( const int id ) const } /*! - Sets the visible state of identificator. If 'id' is -1 then specified - state will be set to all ids. + \brief Set the visibility state of the item specified by \a id. + + If \a id is -1 then specified state will be set to all items. + + If \a append is set to \c true, keep current status for other items, + otherwise status of other items is cleared. + + \param on new visibility state + \param id item ID + \param append if \c true, keep original status for other items */ void QDS_ComboBox::setState( const bool on, const int id, const bool append ) { - QValueList lst; + QList lst; if ( id < 0 ) { for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it ) @@ -186,9 +241,16 @@ void QDS_ComboBox::setState( const bool on, const int id, const bool append ) } /*! - Sets the visible state of identificator from the specified list. + \brief Set the visibility state of items specified by \a ids. + + If \a append is set to \c true, keep current status for other items, + otherwise status of other items is cleared. + + \param on new visibility state + \param ids items IDs list + \param append if \c true, keep original status for other items */ -void QDS_ComboBox::setState( const bool on, const QValueList& ids, const bool append ) +void QDS_ComboBox::setState( const bool on, const QList& ids, const bool append ) { initDatum(); @@ -199,21 +261,21 @@ void QDS_ComboBox::setState( const bool on, const QValueList& ids, const bo QMap aMap; for ( uint i = 0; i < ids.count(); i++ ) - aMap.insert( *ids.at( i ), 0 ); + aMap.insert( ids.at( i ), 0 ); for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it ) { if ( aMap.contains( it.key() ) ) { - if ( it.data() != on ) + if ( it.value() != on ) { - it.data() = on; + it.value() = on; changed = true; } } - else if ( !append && it.data() == on ) + else if ( !append && it.value() == on ) { - it.data() = !on; + it.value() = !on; changed = true; } } @@ -222,9 +284,16 @@ void QDS_ComboBox::setState( const bool on, const QValueList& ids, const bo } /*! - Sets the user items into the combo box. + \brief Set the custom user items into the combo box. + + User items like standard dictionary list items will be added + into the combobox. This function allows user to override + items. + + \param ids items IDs + \param names items names */ -void QDS_ComboBox::setValues( const QValueList& ids, const QStringList& names ) +void QDS_ComboBox::setValues( const QList& ids, const QStringList& names ) { initDatum(); @@ -233,42 +302,51 @@ void QDS_ComboBox::setValues( const QValueList& ids, const QStringList& nam myUserIds = ids; myUserNames = names; + + //unitSystemChanged( "" ); } /*! - This is an overloaded member function, provided for convenience. - It behaves essentially like the above function. It creates - QValueList (0, 1, 2 ... ) and call previous method + \brief Set the custom user items into the combo box. + \overload + + User items like standard dictionary list items will be added + into the combobox. This function allows user to override + items. + + Uses (0, 1, 2 ... ) as items IDs. + + \param names items names */ void QDS_ComboBox::setValues( const QStringList& names ) { initDatum(); - QValueList< int > ids; + QList< int > ids; for ( int i = 0, n = names.count(); i < n; i++ ) ids.append( i ); setValues( ids, names ); } /*! - Sets the active item as item with default id. If default - not defined then first item will be used. + \brief Reset the datum. + + Set the active item as item with default ID. If default ID is not defined + then the first item is used. */ void QDS_ComboBox::reset() { - int id = -1; QString aDefValue = defaultValue(); if ( !aDefValue.isEmpty() ) - id = aDefValue.toInt(); - - if ( id == -1 ) - id = getId( 0 ); - - setIntegerValue( id ); + setString( aDefValue ); + else + setIntegerValue( getId( 0 ) ); } /*! - Returns identifier from given ComboBox string item. + \brief Convert string to integer value. + \param str item + \return item ID or -1 if not found */ int QDS_ComboBox::stringToValue( const QString& str ) const { @@ -276,7 +354,9 @@ int QDS_ComboBox::stringToValue( const QString& str ) const } /*! - Returns ComboBox string item from given identifier. + \brief Convert integer to string value. + \param val item ID + \return item value or empty string if \c val is invalid */ QString QDS_ComboBox::valueToString( const int val ) const { @@ -287,7 +367,8 @@ QString QDS_ComboBox::valueToString( const int val ) const } /*! - Returns string from QLineEdit widget. + \brief Get string from the combo box. + \return string value */ QString QDS_ComboBox::getString() const { @@ -295,7 +376,7 @@ QString QDS_ComboBox::getString() const QtxComboBox* cb = comboBox(); if ( cb ) { - if ( !cb->editable() ) + if ( !cb->isEditable() ) { if ( !cb->isCleared() ) res = cb->currentText(); @@ -307,7 +388,8 @@ QString QDS_ComboBox::getString() const } /*! - Sets the string into QLineEdit widget. + \brief Set the string value to the combo box widget. + \param txt string value */ void QDS_ComboBox::setString( const QString& txt ) { @@ -319,20 +401,20 @@ void QDS_ComboBox::setString( const QString& txt ) int idx = -1; for ( int i = 0; i < cb->count() && idx == -1; i++ ) - if ( cb->text( i ) == txt ) + if ( cb->itemText( i ) == txt ) idx = i; - int old = cb->currentItem(); + int old = cb->currentIndex(); if ( idx != -1 ) - cb->setCurrentItem( idx ); + cb->setCurrentIndex( idx ); else if ( txt.isEmpty() ) { - if ( !cb->editable() ) - cb->setCurrentText( txt ); + if ( !cb->isEditable() ) + cb->setCleared( true ); else cb->lineEdit()->setText( txt ); } - if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentItem() ) ) + if ( isClear != txt.isEmpty() || ( !isClear && old != cb->currentIndex() ) || isClear != cb->isCleared() ) { onParamChanged(); QString str = getString(); @@ -344,26 +426,36 @@ void QDS_ComboBox::setString( const QString& txt ) } /*! - Returns pointer to QtxComboBox widget. + \brief Get combo box widget. + \return internal combo box widget */ QtxComboBox* QDS_ComboBox::comboBox() const { - return ::qt_cast( controlWidget() ); + return ::qobject_cast( controlWidget() ); } /*! - Create QComboBox widget as control subwidget. + \brief Create internal combo box as control widget. + \param parent parent widget + \return created combo box widget */ QWidget* QDS_ComboBox::createControl( QWidget* parent ) { QtxComboBox* cb = new QtxComboBox( parent ); cb->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) ); - connect( cb, SIGNAL( textChanged( const QString& ) ), this, + connect( cb, SIGNAL( editTextChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) ); connect( cb, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) ); return cb; } +/*! + \brief Process notification about active units system changing. + + Update combobox contents. + + \param system new active units system +*/ void QDS_ComboBox::unitSystemChanged( const QString& system ) { QDS_Datum::unitSystemChanged( system ); @@ -443,9 +535,10 @@ void QDS_ComboBox::unitSystemChanged( const QString& system ) } /*! - Notify about text changing in line edit of ComboBox. + \brief Called when text in the combo box (editable) is modified by the user. + \param txt current text in the combo box editor (not used) */ -void QDS_ComboBox::onTextChanged( const QString& ) +void QDS_ComboBox::onTextChanged( const QString& /*txt*/ ) { onParamChanged(); emit paramChanged(); @@ -454,12 +547,17 @@ void QDS_ComboBox::onTextChanged( const QString& ) } /*! - Notify about activation new item. + \brief Called when combo box item is activated. + \param idx index of teh item being activated */ void QDS_ComboBox::onActivated( int idx ) { if ( comboBox() ) - comboBox()->setCurrentItem( comboBox()->currentItem() ); + { + int ind = comboBox()->currentIndex(); + comboBox()->setCurrentIndex( -1 ); + comboBox()->setCurrentIndex( ind ); + } int id = getId( idx ); if ( id != -1 ) @@ -474,7 +572,7 @@ void QDS_ComboBox::onActivated( int idx ) } /*! - Updates ComboBox after have change of visible state or items have been inserted / removed. + \brief Update combo box. */ void QDS_ComboBox::updateComboBox() { @@ -488,7 +586,7 @@ void QDS_ComboBox::updateComboBox() { isClear = cb->isCleared(); - curId = getId( cb->currentItem() ); + curId = getId( cb->currentIndex() ); cb->clear(); } @@ -505,9 +603,9 @@ void QDS_ComboBox::updateComboBox() if ( cb ) { if ( myIcons.contains( id ) ) - cb->insertItem( myIcons[id], myValue[id] ); + cb->addItem( QIcon(myIcons[id]), myValue[id] ); else - cb->insertItem( myValue[id] ); + cb->addItem( myValue[id] ); } } @@ -517,19 +615,21 @@ void QDS_ComboBox::updateComboBox() cb->updateGeometry(); if ( isClear ) - cb->setCurrentText( "" ); + cb->setItemText( cb->currentIndex(), "" ); else { if ( getIndex( curId ) != -1 ) - cb->setCurrentItem( getIndex( curId ) ); - if ( curId != getId( cb->currentItem() ) ) - onActivated( cb->currentItem() ); + cb->setCurrentIndex( getIndex( curId ) ); + if ( curId != getId( cb->currentIndex() ) ) + onActivated( cb->currentIndex() ); } } } /*! - Returns index of ComboBox item according to id. + \brief Get index of the combo box item according to its identifier. + \param id item ID + \return item index or -1 if not found */ int QDS_ComboBox::getIndex( const int id ) const { @@ -540,7 +640,9 @@ int QDS_ComboBox::getIndex( const int id ) const } /*! - Returns index of ComboBox item according to string. + \brief Get index of the combo box item. + \param str combo box item + \return item index or -1 if not found */ int QDS_ComboBox::getIndex( const QString& str ) const { @@ -549,27 +651,31 @@ int QDS_ComboBox::getIndex( const QString& str ) const if ( cb ) { for ( int i = 0; i < cb->count() && idx == -1; i++ ) - if ( cb->text( i ) == str ) + if ( cb->itemText( i ) == str ) idx = i; } return idx; } /*! - Returns id according to ComboBox item index. + \brief Get item identifier according to the specified index. + \param idx item index + \return item ID or -1 if index is out of range */ int QDS_ComboBox::getId( const int idx ) const { int id = -1; IdIndexMap::ConstIterator it = myIndex.begin(); for (; it != myIndex.end() && id == -1; ++it ) - if ( it.data() == idx ) + if ( it.value() == idx ) id = it.key(); return id; } /*! - Returns id according to ComboBox item string. + \brief Get item identifier. + \param str combo box item + \return item ID or -1 if not found */ int QDS_ComboBox::getId( const QString& str ) const { @@ -578,7 +684,7 @@ int QDS_ComboBox::getId( const QString& str ) const IdValueMap::ConstIterator it = myValue.begin(); for (; it != myValue.end() && id == -1; ++it ) { - if ( it.data() == str ) + if ( it.value() == str ) { if ( state( it.key() ) ) id = it.key(); @@ -591,3 +697,15 @@ int QDS_ComboBox::getId( const QString& str ) const return id; } + +/*! + \fn void QDS_ComboBox::activated( int id ); + \brief Emitted when the current item in the combo box is changed. + \param id current item ID +*/ + +/*! + \fn void QDS_ComboBox::activated( const QString& txt ); + \brief Emitted when the current item in the combo box is changed. + \param txt current item text +*/