Salome HOME
Copyrights update
[modules/gui.git] / src / Qtx / QtxDblSpinBox.cxx
index 1a44050266e0c86aedcfc0ecc04563bd35e5c010..235218ff35964fd29dfca98349608a41dc124b9d 100755 (executable)
@@ -1,3 +1,21 @@
+// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
+// 
+// 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 
+// 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/
+//
 // File:      QtxDblSpinBox.cxx
 // Author:    Sergey TELKOV
 
@@ -83,7 +101,7 @@ myPrecision( 0 )
   rangeChange();
   updateDisplay();
 
-  //connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
+  connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
 }
 
 QtxDblSpinBox::QtxDblSpinBox( double min, double max, double step, QWidget* parent, const char* name )
@@ -100,7 +118,7 @@ myPrecision( 0 )
   rangeChange();
   updateDisplay();
 
-  //connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
+  connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
 }
 
 QtxDblSpinBox::~QtxDblSpinBox()
@@ -177,8 +195,8 @@ void QtxDblSpinBox::setLineStep( double step )
 
 double QtxDblSpinBox::value() const
 {
-  QtxDblSpinBox* _this = ( QtxDblSpinBox* )this;
-  _this->clearFocus();
+  QSpinBox::value();
+
   return myValue;
 }
 
@@ -273,7 +291,10 @@ void QtxDblSpinBox::updateDisplay()
 {
   if ( myBlocked )
     return;
-    
+
+  bool upd = editor()->isUpdatesEnabled();
+  editor()->setUpdatesEnabled( false );
+
   bool isBlock = myBlocked;
   myBlocked = true;
     
@@ -285,11 +306,20 @@ void QtxDblSpinBox::updateDisplay()
     QSpinBox::setValue( QSpinBox::minValue() );
   else
     QSpinBox::setValue( ( QSpinBox::minValue() + QSpinBox::maxValue() ) / 2 );
-    
+  
   QSpinBox::updateDisplay();
-    
+
+  editor()->setUpdatesEnabled( upd );
+
   editor()->setText( myCleared ? QString::null : txt );
-    
+  if ( !myCleared && editor()->hasFocus() )
+  {
+    if ( editor()->text() == specialValueText() )
+      editor()->selectAll();
+    else
+      editor()->setSelection( prefix().length(), editor()->text().length() - prefix().length() - suffix().length() );
+  }
+
   myBlocked = isBlock;
 }
 
@@ -320,7 +350,7 @@ void QtxDblSpinBox::interpretText()
 void QtxDblSpinBox::valueChange()
 {
   updateDisplay();
-  emit valueChanged( value() );
+  emit valueChanged( myValue );
   emit valueChanged( currentValueText() );
 }
 
@@ -359,15 +389,15 @@ QString QtxDblSpinBox::currentValueText()
 QString QtxDblSpinBox::mapValueToText( double v )
 {
        QString s;
-  s.setNum( v, myPrecision < 0 ? 'f' : 'g', myPrecision == 0 ? 6 : QABS( myPrecision ) );
-  return s;
+  s.setNum( v, myPrecision >= 0 ? 'f' : 'g', myPrecision == 0 ? 6 : QABS( myPrecision ) );
+  return removeTrailingZeroes( s );
 }
 
 QString QtxDblSpinBox::mapValueToText( int )
 {
   QString s;
-  s.setNum( myValue, myPrecision < 0 ? 'f' : 'g', myPrecision == 0 ? 6 : QABS( myPrecision ) );
-  return s;
+  s.setNum( myValue, myPrecision >= 0 ? 'f' : 'g', myPrecision == 0 ? 6 : QABS( myPrecision ) );
+  return removeTrailingZeroes( s );
 }
 
 double QtxDblSpinBox::mapTextToDoubleValue( bool* ok )
@@ -409,8 +439,27 @@ void QtxDblSpinBox::wheelEvent( QWheelEvent* e )
 
 void QtxDblSpinBox::onTextChanged( const QString& str )
 {
-  bool isBlock = myBlocked;
-  myBlocked = true;
-  interpretText();
-  myBlocked = isBlock;
+  if ( !myBlocked )
+    myCleared = false;
+}
+
+QString QtxDblSpinBox::removeTrailingZeroes( const QString& src ) const
+{
+  QString delim( "." );
+
+  int idx = src.findRev( delim );
+  if ( idx == -1 )
+    return src;
+
+  QString iPart = src.left( idx );
+  QString fPart = src.mid( idx + 1 );
+
+  while ( !fPart.isEmpty() && fPart.at( fPart.length() - 1 ) == '0' )
+    fPart.remove( fPart.length() - 1, 1 );
+
+  QString res = iPart;
+  if ( !fPart.isEmpty() )
+    res += delim + fPart;
+
+  return res;
 }