Salome HOME
Updated copyright comment
[modules/gui.git] / src / Qtx / QtxIntSpinBox.cxx
old mode 100755 (executable)
new mode 100644 (file)
index e002377..7d45444
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2024  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.
-// 
-// 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 
+// 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 
+// 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
 //
+
 // File:      QtxIntSpinBox.cxx
 // Author:    Sergey TELKOV
-
+//
 #include "QtxIntSpinBox.h"
 
-#include <qlineedit.h>
-#include <qapplication.h>
+#include <QLineEdit>
+/*!
+  \class QtxIntSpinBox
+  \brief Enhanced version of the Qt's spin box.
+
+  The QtxIntSpinBox class represents the widget for entering the
+  integer values. In addition to the functionality provided by
+  QSpinBox, this class supports "cleared" state - this is the
+  state corresponding to "None" (or empty) entered value.
+
+  To set "cleared" state use setCleared() method. To check if the spin
+  box stores "cleared" state, use isCleared() method.
+  For example:
+  \code
+  if (mySpinBox->isCleared()) {
+    ... // process "None" state
+  }
+  else {
+    int value = mySpinBox->value();
+    ... // process entered value
+  }
+  \endcode
+*/
 
 /*!
-  Constructor
+  \brief Constructor.
+
+  Constructs a spin box with 0 as minimum value and 99 as maximum value, 
+  a step value of 1. The value is initially set to 0.
+
+  \param parent parent object
 */
-QtxIntSpinBox::QtxIntSpinBox( QWidget* parent, const char* name )
-: QSpinBox( parent, name ),
-myCleared( false ),
-myBlocked( false )
+QtxIntSpinBox::QtxIntSpinBox( QWidget* parent )
+: QSpinBox( parent ),
+  myCleared( false )
 {
-  connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
+#if !defined(GLOBAL_DOUBLE_CONVERSION)
+  // VSR 01/07/2010: Disable thousands separator for spin box
+  // (to avoid incosistency of double-2-string and string-2-double conversion)
+  // see issue 14540 (old id 21219)
+  QLocale loc;
+  loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
+  setLocale(loc);
+#endif
+
+  setCorrectionMode( QSpinBox::CorrectToNearestValue );
+  connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
+           this, SLOT( onTextChanged( const QString& ) ) );
 }
 
 /*!
-  Constructor
+  \brief Constructor.
+
+  Constructs a spin box with specified minimum, maximum and step value.
+  The value is initially set to the minimum value.
+
+  \param min spin box minimum possible value
+  \param max spin box maximum possible value
+  \param step spin box increment/decrement value
+  \param parent parent object
 */
-QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent, const char* name )
-: QSpinBox( min, max, step, parent, name ),
-myCleared( false ),
-myBlocked( false )
+QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent )
+: QSpinBox( parent ),
+  myCleared( false )
 {
-  connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
+#if !defined(GLOBAL_DOUBLE_CONVERSION)
+  // VSR 01/07/2010: Disable thousands separator for spin box
+  // (to avoid incosistency of double-2-string and string-2-double conversion)
+  // see issue 14540 (old id 21219)
+  QLocale loc;
+  loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
+  setLocale(loc);
+#endif
+
+  setMinimum( min );
+  setMaximum( max );
+  setSingleStep( step );
+  setCorrectionMode( QSpinBox::CorrectToNearestValue );
+
+  connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
+           this, SLOT( onTextChanged( const QString& ) ) );
 }
 
 /*!
-  Destructor
+  \brief Destructor.
 */
 QtxIntSpinBox::~QtxIntSpinBox()
 {
 }
 
 /*!
-  \return true if spin box is cleared
+  \brief Check if spin box is in the "cleared" state.
+  \return \c true if spin box is cleared
 */
 bool QtxIntSpinBox::isCleared() const
 {
@@ -62,112 +125,49 @@ bool QtxIntSpinBox::isCleared() const
 }
 
 /*!
-  Changes cleared status of spin box
-  \param on - new status
+  \brief Change "cleared" status of the spin box.
+  \param on new "cleared" status
 */
 void QtxIntSpinBox::setCleared( const bool on )
 {
   if ( myCleared == on )
     return;
-    
+  
   myCleared = on;
-  updateDisplay();
+  setSpecialValueText( specialValueText() );
 }
 
 /*!
-  Changes value of spin box
-  \param val - new value of spin box
+  \brief Convert value to the text.
+  \param val value being converted
+  \return string containing the converted value
 */
-void QtxIntSpinBox::setValue( int value )
+QString QtxIntSpinBox::textFromValue( int val ) const
 {
-  myCleared = false;
-
-  QSpinBox::setValue( value );
+  return myCleared ? QString() : QSpinBox::textFromValue( val );
 }
 
 /*!
-  Custom event filter
-*/
-bool QtxIntSpinBox::eventFilter( QObject* o, QEvent* e )
-{
-  if ( !myCleared || o != editor() || !editor()->text().stripWhiteSpace().isEmpty() )
-    return QSpinBox::eventFilter( o, e );
+  \brief Perform \a steps increment/decrement steps.
+  
+  The \a steps value can be any integer number. If it is > 0,
+  the value incrementing is done, otherwise value is decremented
+  \a steps times.  
 
-  if ( e->type() == QEvent::FocusOut || e->type() == QEvent::Leave || e->type() == QEvent::Hide )
-    return false;
-
-  if ( e->type() == QEvent::KeyPress &&
-            ( ((QKeyEvent*)e)->key() == Key_Tab || ((QKeyEvent*)e)->key() == Key_BackTab ) )
-  {
-    QApplication::sendEvent( this, e );
-    return true;
-  }
-
-  return QSpinBox::eventFilter( o, e );
-}
-
-/*!
-  Sets integer value by text in editor
+  \param steps number of increment/decrement steps
 */
-void QtxIntSpinBox::interpretText()
+void QtxIntSpinBox::stepBy( int steps )
 {
   myCleared = false;
 
-  QSpinBox::interpretText();
+  QSpinBox::stepBy( steps );
 }
 
 /*!
-  Updates text of editor
-*/
-void QtxIntSpinBox::updateDisplay()
-{
-  if ( myBlocked )
-    return;
-
-  bool block = myBlocked;
-  myBlocked = true;
-
-  QSpinBox::updateDisplay();
-
-  if ( myCleared )
-    editor()->clear();
-  else if ( editor()->hasFocus() )
-  {
-    if ( editor()->text() == specialValueText() )
-      editor()->selectAll();
-    else
-      editor()->setSelection( prefix().length(), editor()->text().length() - prefix().length() - suffix().length() );
-  }
-
-  myBlocked = block;
-}
-
-/*!
-  Custom handler for leave event
-*/
-void QtxIntSpinBox::leaveEvent( QEvent* e )
-{
-  if ( !myCleared )
-    QSpinBox::leaveEvent( e );
-}
-
-/*!
-  Custom handler for wheel event
-*/
-void QtxIntSpinBox::wheelEvent( QWheelEvent* e )
-{
-  if ( !isEnabled() )
-    return;
-
-  QSpinBox::wheelEvent( e );
-  updateDisplay();
-}
-
-/*!
-  SLOT: called if text is changed
+  \brief Called when user enters the text in the spin box.
+  \param txt current spin box text (not used)
 */
 void QtxIntSpinBox::onTextChanged( const QString& )
 {
-  if ( !myBlocked )
-    myCleared = false;
+  myCleared = false;
 }