Salome HOME
Translation of sketch operations
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
index 8b09ac9fece6ec394cf21314cd0a580084d3e274..9916aec514e9e907f594822ea431d064321fc2ce 100644 (file)
 #include <QLocale>
 #include <QRegExp>
 #include <QToolTip>
+#include <QApplication>
+
+#include <QStringListModel>
+#include <QCompleter>
+#include <QShortcut>
 
 #include <string>
 #include <iostream>
 
-/*!
- \class ModuleBase_ParamSpinBox
- */
-
-/*!
- \brief Constructor.
-
- 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)
 {
+#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.
  */
@@ -68,28 +85,18 @@ void ModuleBase_ParamSpinBox::connectSignalsAndSlots()
           this, SLOT(onTextChanged(const QString&)));
 }
 
-/*!
- \brief This function is called when value is changed.
- */
 void ModuleBase_ParamSpinBox::onTextChanged(const QString& text)
 {
-  ModuleBase_DoubleSpinBox::onTextChanged(text);
   myTextValue = 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
 {
-  if (!hasVariable(theText)) {
+  if (!hasVariable(theText))
     return ModuleBase_DoubleSpinBox::valueFromText(theText);
-  }
-  // small hack: return length of the string to iniiate valuesChanged signal
-  return theText.length();
+
+  // small hack: return hash of the string to initiate valuesChanged signal
+  return qHash(theText);
 }
 
 QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const
@@ -122,9 +129,14 @@ QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) cons
 /*!
  \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)
 {
+  if (hasVariable())
+    return;
+
   myTextValue = ModuleBase_DoubleSpinBox::textFromValue(value);
   ModuleBase_DoubleSpinBox::setValue(value);
 }
@@ -178,10 +190,19 @@ bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
   //  QRegExp varNameMask(aDigitPattern.arg(","));
   //  aHasDigit = varNameMask.exactMatch(theText);
   //}
-  bool aHasDigit = false;
-  theText.toDouble(&aHasDigit);
-  return !aHasDigit;
-
+  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;
 }
 
 /*!
@@ -222,11 +243,11 @@ bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
                                            double& outValue) const
 {
   ResultParameterPtr aParam;
-  return ModelAPI_Tools::findVariable(theName.toStdString(), outValue, 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)
 //{
@@ -238,7 +259,7 @@ bool ModuleBase_ParamSpinBox::findVariable(const QString& theName,
 //}
 
 /*!
- \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* theEvent)
 {