Salome HOME
Set empty text if double value is set in editor
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
index cfcd5e6e0c59d0a42ea27f36287999c779520ae3..cce85ab35a9d451db0748d433ab27e9e4f678d1e 100644 (file)
@@ -4,6 +4,7 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Tools.h>
 
 #include <QKeyEvent>
 #include <QLineEdit>
@@ -11,6 +12,7 @@
 #include <QRegExp>
 
 #include <string>
+#include <iostream>
 
 /*!
  \class ModuleBase_ParamSpinBox
@@ -27,9 +29,7 @@
  */
 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
     : ModuleBase_DoubleSpinBox(theParent, thePrecision),
-      myAcceptVariables(true),
-      myHasVariables(false),
-      myDefaultValue(0.)
+      myAcceptVariables(true)
 {
   connectSignalsAndSlots();
 }
@@ -52,17 +52,7 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox()
  */
 void ModuleBase_ParamSpinBox::stepBy(int steps)
 {
-  QString str = text();
-  QString pref = prefix();
-  QString suff = suffix();
-
-  if (pref.length() && str.startsWith(pref))
-    str = str.right(str.length() - pref.length());
-  if (suff.length() && str.endsWith(suff))
-    str = str.left(str.length() - suff.length());
-
-  QRegExp varNameMask("([a-z]|[A-Z]|_).*");
-  if (varNameMask.exactMatch(str))
+  if (hasVariable())
     return;
 
   ModuleBase_DoubleSpinBox::stepBy(steps);
@@ -73,28 +63,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);
 }
 
 /*!
@@ -103,14 +73,6 @@ void ModuleBase_ParamSpinBox::onEditingFinished()
 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
 {
   myTextValue = text;
-
-  double value = 0;
-  if (isValid(text, value) == Acceptable) {
-    myCorrectValue = text;
-    myHasVariables = true;
-  } else {
-    myHasVariables = false;
-  }
 }
 
 /*!
@@ -121,24 +83,19 @@ void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
  */
 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
 {
-  double aValue = 0;
-  if (isValid(theText, aValue) == Acceptable)
-    return aValue;
-
-  return defaultValue();
+  if (!hasVariable(theText)) {
+    return ModuleBase_DoubleSpinBox::valueFromText(theText);
+  }
+  // small hack: return length of the string to iniiate valuesChanged signal
+  return theText.length();
 }
 
-/*!
- \brief This function is used by the spin box whenever it needs to display
- the given value.
-
- \param val spin box value
- \return text representation of the value
- \sa valueFromText()
- */
-QString ModuleBase_ParamSpinBox::textFromValue(double val) const
+QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
 {
-  return ModuleBase_DoubleSpinBox::textFromValue(val);
+  if (hasVariable(myTextValue)){
+    return myTextValue;
+  }
+  return ModuleBase_DoubleSpinBox::textFromValue(theValue);
 }
 
 /*!
@@ -149,41 +106,17 @@ QString ModuleBase_ParamSpinBox::textFromValue(double val) const
  */
 QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const
 {
-  QValidator::State res = QValidator::Invalid;
+  // Trying to interpret the current input text as a numeric value
+  if (!hasVariable(str))
+    return ModuleBase_DoubleSpinBox::validate(str, pos);
 
-  // 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
+  QValidator::State res = QValidator::Invalid;
   if (isAcceptVariables()) {
-    QRegExp varNameMask("(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*)|"
-                        "(_([a-z]|[A-Z]|[0-9])+([a-z]|[A-Z]|[0-9]|_)*)");
-    if (varNameMask.exactMatch(str))
-      res = QValidator::Acceptable;
-
-    if (res == QValidator::Invalid) {
-      varNameMask.setPattern("_");
-      if (varNameMask.exactMatch(str))
-        res = QValidator::Intermediate;
-    }
+    res = QValidator::Acceptable;
   }
-
-  // Trying to interpret the current input text as a numeric value
-  if (res == QValidator::Invalid)
-    res = ModuleBase_DoubleSpinBox::validate(str, pos);
-
   return res;
 }
 
-/*!
- \brief This function is used to set a default value for this spinbox.
- \param value default value
- */
-void ModuleBase_ParamSpinBox::setDefaultValue(const double value)
-{
-  myDefaultValue = value;
-}
-
 /*!
  \brief This function is used to set a current value for this spinbox.
  \param value current value
@@ -192,8 +125,7 @@ void ModuleBase_ParamSpinBox::setValue(const double value)
 {
   ModuleBase_DoubleSpinBox::setValue(value);
 
-  myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
-  myTextValue = myCorrectValue;
+  myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
 }
 
 /*!
@@ -223,9 +155,15 @@ bool ModuleBase_ParamSpinBox::isAcceptVariables() const
   return myAcceptVariables;
 }
 
-bool ModuleBase_ParamSpinBox::hasVariables() const
+bool ModuleBase_ParamSpinBox::hasVariable() const
 {
-  return myHasVariables;
+  return hasVariable(myTextValue);
+}
+
+bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
+{
+  QRegExp varNameMask("[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");
+  return !varNameMask.exactMatch(theText);
 }
 
 /*!
@@ -235,7 +173,7 @@ bool ModuleBase_ParamSpinBox::hasVariables() const
 ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText,
                                                                 double& theValue) const
 {
-  if (!findVariable(theText, theValue)) {
+  if (hasVariable() && !findVariable(theText, theValue)) {
     bool ok = false;
     theValue = locale().toDouble(theText, &ok);
     if (!ok) {
@@ -249,18 +187,6 @@ ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& t
   return Acceptable;
 }
 
-/*!
- \brief This function return a default acceptable value (commonly, 0.0).
- \return default acceptable value
- */
-double ModuleBase_ParamSpinBox::defaultValue() const
-{
-  if (minimum() > myDefaultValue || maximum() < myDefaultValue)
-    return minimum();
-
-  return myDefaultValue;
-}
-
 /*!
  \brief This function is used to check that string value lies within predefined range.
  \return check status
@@ -278,16 +204,7 @@ bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
                                            double& outValue) const
 {
 
-  SessionPtr aSession = ModelAPI_Session::get();
-  DocumentPtr aDocument = aSession->activeDocument();
-  ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
-                                                theName.toStdString());
-  ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
-  if(!aParam.get())
-    return false;
-  AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
-  outValue = aValueAttribute->value();
-  return true;
+  return ModelAPI_Tools::findVariable(theName.toStdString(), outValue);
 }
 
 /*!
@@ -305,7 +222,10 @@ void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e)
 /*!
  \brief This function is called when the spinbox recieves show event.
  */
-void ModuleBase_ParamSpinBox::showEvent(QShowEvent*)
+void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
 {
-  setText(myTextValue);
+  ModuleBase_DoubleSpinBox::showEvent(theEvent);
+  if (hasVariable(myTextValue)) {
+    setText(myTextValue);
+  }
 }