Salome HOME
Define Rename as an operation
[modules/shaper.git] / src / ModuleBase / ModuleBase_DoubleSpinBox.cpp
index 171e1e849f64cc2c89407c3e7121526436f0f0dd..080138f5483b0314722ba314cb4c0ed73a4b7a93 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:      ModuleBase_DoubleSpinBox.cxx
 // Author:    Sergey TELKOV
 //
@@ -34,7 +36,7 @@ const double PSEUDO_ZERO = 1.e-20;
  \endcode
 
  Another useful feature is possibility to use scientific notation (e.g. 1.234e+18)
- for the widegt text. To enable this, negative precision should be specified either
+ for the widget text. To enable this, negative precision should be specified either
  through a constructor or using setPrecision() method.
 
  Note that "decimals" property of QDoubleSpinBox is almost completely substituted
@@ -50,23 +52,26 @@ const double PSEUDO_ZERO = 1.e-20;
  a step value of 1.0 and a precision of 2 decimal places.
  The value is initially set to 0.00.
 
- \param parent parent object
+ \param theParent parent object
+ \param thePrecision precision of values input
  */
-ModuleBase_DoubleSpinBox::ModuleBase_DoubleSpinBox(QWidget* parent, int thePrecision)
-    : QDoubleSpinBox(parent),
+ModuleBase_DoubleSpinBox::ModuleBase_DoubleSpinBox(QWidget* theParent, int thePrecision)
+    : QDoubleSpinBox(theParent),
       myCleared(false)
 {
   // VSR 01/07/2010: Disable thousands separator for spin box
-  // (to avoid incosistency of double-2-string and string-2-double conversion)
+  // (to avoid inconsistency of double-2-string and string-2-double conversion)
   QLocale loc;
   loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
   setLocale(loc);
 
+  // MPV 15/09/2014: this must be set before setDecimals; otherwise in release mode setDecimals may crash
+  myPrecision = thePrecision;
+
   // Use precision equal to default Qt decimals
   // it's necessary to set decimals before the range setting,
   // by default Qt rounds boundaries to 2 decimals at setRange
-  setDecimals(thePrecision);
-  myPrecision = thePrecision;
+  setDecimals(qAbs(myPrecision));
 
   connect(lineEdit(), SIGNAL(textChanged( const QString& )), this,
           SLOT(onTextChanged( const QString& )));
@@ -114,8 +119,8 @@ void ModuleBase_DoubleSpinBox::setCleared(const bool on)
  */
 void ModuleBase_DoubleSpinBox::setPrecision(const int prec)
 {
-  int newPrec = qMax(prec, 0);
-  int oldPrec = qMax(myPrecision, 0);
+  int newPrec = qAbs(prec);
+  int oldPrec = qAbs(myPrecision);
   myPrecision = prec;
   if (newPrec != oldPrec)
     update();
@@ -123,7 +128,7 @@ void ModuleBase_DoubleSpinBox::setPrecision(const int prec)
 
 /*!
  \brief Get precision value of the spin box
- \return current prevision value
+ \return current precision value
  \sa setPrecision()
  */
 int ModuleBase_DoubleSpinBox::getPrecision() const
@@ -161,7 +166,7 @@ QString ModuleBase_DoubleSpinBox::textFromValue(double val) const
 
 /*!
  \brief Return source string with removed leading and trailing zeros.
- \param str source string
+ \param src source string
  \return resulting string
  */
 QString ModuleBase_DoubleSpinBox::removeTrailingZeroes(const QString& src) const
@@ -235,6 +240,7 @@ QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) con
   // Otherwise, expect myPrecision digits after the decimal point.
   int decs = myPrecision < 0 ? qAbs(myPrecision) - 1 : myPrecision;
 
+  v.setLocale(this->locale());
   v.setDecimals(decs);
   v.setBottom(minimum());
   v.setTop(maximum());
@@ -244,7 +250,7 @@ QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) con
   if (overhead == 0)
     state = v.validate(str, pos);
   else {
-    if (str.length() >= overhead && str.startsWith(pref) && str.right(suff.length()) == suff) {
+    if ((uint)(str.length()) >= overhead && str.startsWith(pref) && str.right(suff.length()) == suff) {
       QString core = str.mid(pref.length(), str.length() - overhead);
       int corePos = pos - pref.length();
       state = v.validate(core, corePos);
@@ -265,7 +271,7 @@ QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) con
     }
   }
 
-  // Treat values ouside (min; max) range as Invalid
+  // Treat values outside (min; max) range as Invalid
   // This check is enabled by assigning "strict_validity_check" dynamic property
   // with value "true" to the spin box instance.
   if (state == QValidator::Intermediate) {
@@ -296,9 +302,8 @@ QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) con
 
 /*!
  \brief Called when user enters the text in the spin box.
- \param txt current spin box text (not used)
  */
-void ModuleBase_DoubleSpinBox::onTextChanged(const QString& /*txt*/)
+void ModuleBase_DoubleSpinBox::onTextChanged(const QString& )
 {
   myCleared = false;
 }