Salome HOME
Simplification of the code: it removes focusNextPrevChild processing.
[modules/shaper.git] / src / ModuleBase / ModuleBase_DoubleSpinBox.cpp
index a8489408dc6bec240b76ccaf602e7552d6523ac5..081ca65b3fb12dad6800a131d18cda5e0c95a9c1 100644 (file)
@@ -8,6 +8,7 @@
 #include <QLineEdit>
 #include <QDoubleValidator>
 #include <QVariant>
+#include <QKeyEvent>
 
 #include <limits>
 
@@ -36,7 +37,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
@@ -57,10 +58,12 @@ const double PSEUDO_ZERO = 1.e-20;
  */
 ModuleBase_DoubleSpinBox::ModuleBase_DoubleSpinBox(QWidget* theParent, int thePrecision)
     : QDoubleSpinBox(theParent),
-      myCleared(false)
+      myCleared(false),
+      myIsModified(false),
+      myIsEmitKeyPressEvent(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);
@@ -71,10 +74,12 @@ ModuleBase_DoubleSpinBox::ModuleBase_DoubleSpinBox(QWidget* theParent, int thePr
   // 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);
+  setDecimals(qAbs(myPrecision));
 
   connect(lineEdit(), SIGNAL(textChanged( const QString& )), this,
           SLOT(onTextChanged( const QString& )));
+
+  connect(this, SIGNAL(valueChanged(const QString&)), this, SLOT(onValueChanged(const QString&)));
 }
 
 /*!
@@ -119,8 +124,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();
@@ -128,7 +133,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
@@ -196,6 +201,22 @@ QString ModuleBase_DoubleSpinBox::removeTrailingZeroes(const QString& src) const
   return res;
 }
 
+void ModuleBase_DoubleSpinBox::keyPressEvent(QKeyEvent *theEvent)
+{
+  switch (theEvent->key()) {
+    case Qt::Key_Enter:
+    case Qt::Key_Return: {
+      // do not react to the Enter key, the property panel processes it
+      if (!myIsEmitKeyPressEvent)
+        return;
+    }
+    break;
+    default:
+      break;
+  }
+  QDoubleSpinBox::keyPressEvent(theEvent);
+}
+
 /*!
  \brief Perform \a steps increment/decrement steps.
 
@@ -240,6 +261,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());
@@ -270,7 +292,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) {
@@ -305,4 +327,28 @@ QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) con
 void ModuleBase_DoubleSpinBox::onTextChanged(const QString& )
 {
   myCleared = false;
+  myIsModified = true;
+}
+
+void ModuleBase_DoubleSpinBox::onValueChanged(const QString& theValue)
+{
+  myIsModified = true;
+}
+
+bool ModuleBase_DoubleSpinBox::isModified() const
+{
+  return myIsModified;
+}
+
+void ModuleBase_DoubleSpinBox::clearModified()
+{
+  myIsModified = false;
+}
+
+bool ModuleBase_DoubleSpinBox::enableKeyPressEvent(const bool& theEnable)
+{
+  bool aPreviousValue = myIsEmitKeyPressEvent;
+  myIsEmitKeyPressEvent = theEnable;
+
+  return aPreviousValue;
 }