Salome HOME
updated copyright message
[modules/gui.git] / src / QDS / QDS_ComboBox.cxx
index f5e9ef80bd3568f37068f0dc096caafddbd650cf..8bf1ba93e0c74e9896f582abf2b0a241b05789ce 100644 (file)
@@ -1,15 +1,62 @@
+// Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
+//
+// 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.
+//
+// You should have received a copy of the GNU Lesser General Public
+// 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/ or email : webmaster.salome@opencascade.com
+//
+
 #include "QDS_ComboBox.h"
 
-#include <DDS_Dictionary.h>
+#include <QtxComboBox.h>
 
-#include <TCollection_AsciiString.hxx>
 #include <TColStd_HArray1OfInteger.hxx>
 #include <TColStd_HArray1OfExtendedString.hxx>
 
-#include <qlineedit.h>
+#include <QLineEdit>
+
+/*
+  \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 )
@@ -17,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
 {
@@ -35,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 )
 {
@@ -45,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
 {
@@ -66,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<int>& ids, bool total ) const
+void QDS_ComboBox::values( QList<int>& ids, bool total ) const
 {
   ids.clear();
   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
@@ -78,42 +133,49 @@ void QDS_ComboBox::values( QValueList<int>& 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
 {
+  initDatum();
+
   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 )
 {
+  initDatum();
+
   if ( myValue.contains( id ) )
     setString( myValue[id] );
   else 
@@ -121,10 +183,17 @@ 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 )
 {
+  initDatum();
+
   int id = (int)val;
   if ( myValue.contains( id ) )
     setString( myValue[id] );
@@ -133,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
 {
@@ -144,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<int> lst;
+  QList<int> lst;
   if ( id < 0 )
   {
     for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
@@ -162,32 +241,41 @@ 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<int>& ids, const bool append )
+void QDS_ComboBox::setState( const bool on, const QList<int>& ids, const bool append )
 {
+  initDatum();
+
   if ( ids.isEmpty() && append )
     return;
 
   bool changed = false;
 
   QMap<int, int> aMap;
-  for ( uint i = 0; i < ids.count(); i++ )
-    aMap.insert( *ids.at( i ), 0 );
+  for ( int i = 0; i < ids.count(); i++ )
+    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;
     }
   }
@@ -196,49 +284,69 @@ void QDS_ComboBox::setState( const bool on, const QValueList<int>& 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<int>& ids, const QStringList& names )
+void QDS_ComboBox::setValues( const QList<int>& ids, const QStringList& names )
 {
+  initDatum();
+
   if ( ids.count() != names.count() )
     return;
 
   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 )
 {
-  QValueList< int > ids;
+  initDatum();
+
+  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
 {
@@ -246,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
 {
@@ -257,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
 {
@@ -265,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(); 
@@ -277,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 )
 {
@@ -289,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();
@@ -314,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<QtxComboBox*>( controlWidget() );
+  return ::qobject_cast<QtxComboBox*>( 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 );
@@ -413,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();
@@ -424,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 )
@@ -444,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()
 {
@@ -458,7 +586,7 @@ void QDS_ComboBox::updateComboBox()
   {
     isClear = cb->isCleared();
 
-    curId = getId( cb->currentItem() );
+    curId = getId( cb->currentIndex() );
     cb->clear();
   }
 
@@ -475,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] );
     }
   }
 
@@ -487,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
 {
@@ -510,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
 {
@@ -519,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
 {
@@ -548,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();
@@ -561,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
+*/