Salome HOME
Issue #2079 : Select parent feature for default Constructions. Disable popup menu...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
index 5ad7c1dcf3b88203317d95d6c4b9372e72dd5c29..c42e163b84f8c9331a28347fc890ee8f5b4067fc 100644 (file)
@@ -1,39 +1,60 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
 #include "ModuleBase_ParamSpinBox.h"
 
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
+#include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Tools.h>
 
 #include <QKeyEvent>
 #include <QLineEdit>
-#include <QToolTip>
+#include <QLocale>
 #include <QRegExp>
+#include <QToolTip>
+#include <QApplication>
 
-#include <string>
+#include <QStringListModel>
+#include <QCompleter>
+#include <QShortcut>
 
-/*!
- \class ModuleBase_ParamSpinBox
- */
-
-/*!
- \brief Constructor.
+#include <string>
+#include <iostream>
 
- Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
- a step value of 1.0 and a precision of 2 decimal places.
- The value is initially set to 0.00.
+//#define DEBUG_COMPLETE_WITH_PARAMETERS
 
- \param parent parent object
- */
 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
     : ModuleBase_DoubleSpinBox(theParent, thePrecision),
-      myAcceptVariables(true),
-      myHasVariables(false),
-      myDefaultValue(0.)
+      myAcceptVariables(true)
 {
+#ifdef DEBUG_COMPLETE_WITH_PARAMETERS
+  myCompleter = new QCompleter(this);
+  myCompleter->setWidget(this);
+  myCompleter->setCompletionMode(QCompleter::PopupCompletion);
+
+  myCompleterModel = new QStringListModel(this);
+  myCompleter->setModel(myCompleterModel);
+  // Use sorted model to accelerate completion (QCompleter will use binary search)
+  myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
+  myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
+
+  lineEdit()->setCompleter(myCompleter);
+#endif
+
   connectSignalsAndSlots();
 }
 
+void ModuleBase_ParamSpinBox::setCompletionList(QStringList& theList)
+{
+#ifdef DEBUG_COMPLETE_WITH_PARAMETERS
+  theList.sort();
+  theList.removeDuplicates();
+  myCompleterModel->setStringList(theList);
+#endif
+}
+
 /*!
  \brief Destructor.
  */
@@ -52,17 +73,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 ((!myTextValue.isEmpty()) && hasVariable())
     return;
 
   ModuleBase_DoubleSpinBox::stepBy(steps);
@@ -73,72 +84,31 @@ 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);
 }
 
-/*!
- \brief This function is called when value is changed.
- */
 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
 {
   myTextValue = text;
-
-  double value = 0;
-  if (isValid(text, value) == Acceptable) {
-    myCorrectValue = text;
-    myHasVariables = true;
-  } else {
-    myHasVariables = false;
-  }
+  emit textChanged(text);
 }
 
-/*!
- \brief Interpret text entered by the user as a value.
- \param text text entered by the user
- \return mapped value
- \sa textFromValue()
- */
 double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const
 {
-  double aValue = 0;
-  if (isValid(theText, aValue) == Acceptable)
-    return aValue;
+  if (!hasVariable(theText))
+    return ModuleBase_DoubleSpinBox::valueFromText(theText);
 
-  return defaultValue();
+  // small hack: return hash of the string to initiate valuesChanged signal
+  return qHash(theText);
 }
 
-/*!
- \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 ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){
+    return myTextValue;
+  }
+  return ModuleBase_DoubleSpinBox::textFromValue(theValue);
 }
 
 /*!
@@ -149,50 +119,30 @@ 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-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;
   }
-
-  // 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
+
+ The new value is ignored if the spinbox has a variable.
  */
 void ModuleBase_ParamSpinBox::setValue(const double value)
 {
-  ModuleBase_DoubleSpinBox::setValue(value);
+  if (hasVariable())
+    return;
 
-  myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
-  myTextValue = myCorrectValue;
+  myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
+  ModuleBase_DoubleSpinBox::setValue(value);
 }
 
 /*!
@@ -201,6 +151,7 @@ void ModuleBase_ParamSpinBox::setValue(const double value)
  */
 void ModuleBase_ParamSpinBox::setText(const QString& value)
 {
+  myTextValue = value;
   lineEdit()->setText(value);
 }
 
@@ -222,9 +173,40 @@ bool ModuleBase_ParamSpinBox::isAcceptVariables() const
   return myAcceptVariables;
 }
 
-bool ModuleBase_ParamSpinBox::hasVariables() const
+bool ModuleBase_ParamSpinBox::hasVariable() const
+{
+  if (myTextValue.isEmpty())
+    return false;
+  return hasVariable(myTextValue);
+}
+
+bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
 {
-  return myHasVariables;
+  //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,7 +216,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) {
@@ -248,18 +230,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
@@ -276,35 +246,29 @@ bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const
 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;
+  ResultParameterPtr aParam;
+  return ModelAPI_Tools::findVariable(FeaturePtr(), theName.toStdString(), outValue, aParam);
 }
 
 /*!
- \brief This function is called when the spinbox recieves key press event.
+ \brief This function is called when the spinbox receives 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.
+ \brief This function is called when the spinbox receives show event.
  */
-void ModuleBase_ParamSpinBox::showEvent(QShowEvent*)
+void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent)
 {
-  setText(myTextValue);
+  ModuleBase_DoubleSpinBox::showEvent(theEvent);
+  if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) {
+    setText(myTextValue);
+  }
 }