Salome HOME
Update merging two Coincidence constraints (issue #955)
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
index 925b1cc94c8508545041d6b63677ae962331aa76..febd29badbf419cdbf187edbbcf8533742bf04e6 100644 (file)
@@ -8,10 +8,13 @@
 
 #include <QKeyEvent>
 #include <QLineEdit>
-#include <QToolTip>
+#include <QLocale>
 #include <QRegExp>
+#include <QToolTip>
+#include <QApplication>
 
 #include <string>
+#include <iostream>
 
 /*!
  \class ModuleBase_ParamSpinBox
@@ -51,7 +54,7 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
  */
 void ModuleBase_ParamSpinBox::stepBy(int steps)
 {
-  if (hasVariable())
+  if ((!myTextValue.isEmpty()) && hasVariable())
     return;
 
   ModuleBase_DoubleSpinBox::stepBy(steps);
@@ -62,28 +65,8 @@ void ModuleBase_ParamSpinBox::stepBy(int steps)
  */
 void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
 {
-  connect(this, SIGNAL(editingFinished()),
-          this, SLOT(onEditingFinished()));
-
   connect(this, SIGNAL(valueChanged(const QString&)),
           this, SLOT(onTextChanged(const QString&)));
-
-  //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
-  //        this,       SLOT(onTextChanged(const QString&)));
-
-  //connect(lineEdit(), SIGNAL(textChanged(const QString&)),
-  //        this,       SIGNAL(textChanged(const QString&)));
-}
-
-/*!
- \brief This function is called when editing is finished.
- */
-void ModuleBase_ParamSpinBox::onEditingFinished()
-{
-  if (myTextValue.isNull())
-    myTextValue = text();
-
-  setText(myTextValue);
 }
 
 /*!
@@ -92,11 +75,6 @@ void ModuleBase_ParamSpinBox::onEditingFinished()
 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
 {
   myTextValue = text;
-
-  double value = 0;
-  if (isValid(text, value) == Acceptable) {
-    myCorrectValue = text;
-  }
 }
 
 /*!
@@ -110,9 +88,16 @@ double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
   if (!hasVariable(theText))
     return ModuleBase_DoubleSpinBox::valueFromText(theText);
 
-  double aValue = 0;
-  findVariable(theText, aValue);
-  return aValue;
+  // small hack: return length of the string to initiate valuesChanged signal
+  return qHash(theText);
+}
+
+QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
+{
+  if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
+    return myTextValue;
+  }
+  return ModuleBase_DoubleSpinBox::textFromValue(theValue);
 }
 
 /*!
@@ -128,21 +113,8 @@ QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) cons
     return ModuleBase_DoubleSpinBox::validate(str, pos);
 
   QValidator::State res = QValidator::Invalid;
-
-  // Considering the input text as a variable name
-  // Applying Python identifier syntax:
-  // either a string starting with a letter, or a string starting with
-  // an underscore followed by at least one alphanumeric character
   if (isAcceptVariables()) {
-    QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
-    if (varNameMask.exactMatch(str))
-      res = QValidator::Acceptable;
-
-    if (res == QValidator::Invalid) {
-      varNameMask.setPattern("_");
-      if (varNameMask.exactMatch(str))
-        res = QValidator::Intermediate;
-    }
+    res = QValidator::Acceptable;
   }
   return res;
 }
@@ -153,10 +125,8 @@ QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) cons
  */
 void ModuleBase_ParamSpinBox::setValue(const double value)
 {
+  myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
   ModuleBase_DoubleSpinBox::setValue(value);
-
-  myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
-  myTextValue = myCorrectValue;
 }
 
 /*!
@@ -165,6 +135,7 @@ void ModuleBase_ParamSpinBox::setValue(const double value)
  */
 void ModuleBase_ParamSpinBox::setText(const QString& value)
 {
+  myTextValue = value;
   lineEdit()->setText(value);
 }
 
@@ -188,13 +159,38 @@ bool ModuleBase_ParamSpinBox::isAcceptVariables() const
 
 bool ModuleBase_ParamSpinBox::hasVariable() const
 {
-  return hasVariable(text());
+  if (myTextValue.isEmpty())
+    return false;
+  return hasVariable(myTextValue);
 }
 
 bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
 {
-  QRegExp varNameMask("([a-z]|[A-Z]|_).*");
-  return varNameMask.exactMatch(theText);
+  //const QString aDigitPattern = QString("[-+]?[0-9]*[%1]?[0-9]*([eE][-+]?[0-9]+)?");
+
+  //bool aHasDigit = false;
+  //{
+  //  QRegExp varNameMask(aDigitPattern.arg("."));
+  //  aHasDigit = varNameMask.exactMatch(theText);
+  //}
+  //if (!aHasDigit)
+  //{
+  //  QRegExp varNameMask(aDigitPattern.arg(","));
+  //  aHasDigit = varNameMask.exactMatch(theText);
+  //}
+  bool isDouble = false;
+  QLocale::c().toDouble(theText, &isDouble);
+
+//  theText.toDouble(&isDouble);
+//  if (isDouble) {
+//    QLocale aLoc; // create default locale
+//    QChar aDecPnt = aLoc.decimalPoint();
+//    if (aDecPnt == '.')
+//      isDouble = theText.contains(aDecPnt) || (!theText.contains(','));
+//    else if (aDecPnt == ',')
+//      isDouble = theText.contains(aDecPnt) || (!theText.contains('.'));
+//  }
+  return !isDouble;
 }
 
 /*!
@@ -234,21 +230,21 @@ bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
 bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
                                            double& outValue) const
 {
-
-  return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
+  ResultParameterPtr aParam;
+  return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, aParam);
 }
 
 /*!
  \brief This function is called when the spinbox recieves key press event.
  */
-void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
-{
-  if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
-    QWidget::keyPressEvent(e);
-  } else {
-    ModuleBase_DoubleSpinBox::keyPressEvent(e);
-  }
-}
+//void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
+//{
+//  if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
+//    QWidget::keyPressEvent(e);
+//  } else {
+//    ModuleBase_DoubleSpinBox::keyPressEvent(e);
+//  }
+//}
 
 /*!
  \brief This function is called when the spinbox recieves show event.
@@ -256,7 +252,7 @@ void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
 void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
 {
   ModuleBase_DoubleSpinBox::showEvent(theEvent);
-  if (hasVariable(myTextValue)) {
+  if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
     setText(myTextValue);
   }
 }