Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_ParamSpinBox.cpp
index 8a556c240b0aa543587738673f2daaaa839452dd..9c9cc761329f1a34dc7c03becabc4e3864fabac0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "ModuleBase_ParamSpinBox.h"
 
 #include <string>
 #include <iostream>
+#include <cfloat>
+
+
+bool isVariableSymbol(const QChar& theChar) {
+  if (theChar.isLetterOrNumber())
+    return true;
+  if (theChar == '_')
+    return true;
+  return false;
+}
 
 ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision)
   : QAbstractSpinBox(theParent),
-  myPrecision(thePrecision),
   myIsEquation(false),
   myAcceptVariables(true),
-  mySingleStep(1),
-  myMinimum(DBL_MIN),
-  myMaximum(DBL_MAX)
+  myMinimum(-DBL_MAX),
+  myMaximum(DBL_MAX),
+  mySingleStep(1)
 {
   myCompleter = new QCompleter(this);
   myCompleter->setWidget(lineEdit());
@@ -49,12 +57,12 @@ ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrec
 
   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);
   connect(myCompleter, SIGNAL(highlighted(const QString&)),
     this, SLOT(insertCompletion(const QString&)));
 
+  QAbstractItemView* aPopup = myCompleter->popup();
+  aPopup->installEventFilter(this);
+
   //  connectSignalsAndSlots();
   myEnabledBaseColor = palette().color(QPalette::Active, QPalette::Base);
   connect(lineEdit(), SIGNAL(textChanged(const QString&)),
@@ -65,14 +73,25 @@ ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrec
   myValidator = new QDoubleValidator(this);
   myValidator->setLocale(locale());
   myValidator->setRange(myMinimum, myMaximum);
-  myValidator->setDecimals(3);
+  myValidator->setDecimals(thePrecision);
 }
 
 void ModuleBase_ParamSpinBox::setCompletionList(QStringList& theList)
 {
-  theList.sort();
   theList.removeDuplicates();
+  theList.sort();
   myCompleterModel->setStringList(theList);
+
+  QAbstractItemView* aPopup = myCompleter->popup();
+  QFontMetrics aMetric = aPopup->fontMetrics();
+  int aWidth = 0;
+  QRect aRect;
+  foreach(QString aStr, theList) {
+    aRect = aMetric.boundingRect(aStr);
+    if (aRect.width() > aWidth)
+      aWidth = aRect.width();
+  }
+  aPopup->setMinimumWidth(aWidth + 25);
 }
 
 /*!
@@ -100,7 +119,7 @@ void ModuleBase_ParamSpinBox::stepBy(int steps)
   double aVal = lineEdit()->text().toDouble();
   aVal += steps * mySingleStep;
   setValue(aVal);
-  QAbstractSpinBox::stepBy(steps);
+  //QAbstractSpinBox::stepBy(steps);
 }
 
 void ModuleBase_ParamSpinBox::onTextChanged(const QString& theText)
@@ -143,8 +162,11 @@ void ModuleBase_ParamSpinBox::setValue(double value)
     aVal = myMinimum;
   else if (aVal > myMaximum)
     aVal = myMaximum;
-  QString aText = QString::number(aVal, 'g', decimals());
+  QString aText = (myValidator->decimals() == 0) ? QString::number((int)aVal) :
+    QString::number(aVal, 'g', decimals());
+  lineEdit()->blockSignals(true);
   lineEdit()->setText(aText);
+  lineEdit()->blockSignals(false);
   emit textChanged(aText);
 }
 
@@ -164,6 +186,12 @@ void ModuleBase_ParamSpinBox::setText(const QString& value)
     lineEdit()->setText(value);
     emit textChanged(value);
   }
+  else {
+    bool isConv = false;
+    double aVal = value.toDouble(&isConv);
+    if (isConv)
+      setValue(aVal);
+  }
 }
 
 /*!
@@ -199,31 +227,64 @@ bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const
   return !isDouble;
 }
 
+void ModuleBase_ParamSpinBox::showCompletion(bool checkPrefix)
+{
+  myCompletePos = lineEdit()->cursorPosition();
+  int aStart, aEnd;
+  QString aPrefix;
+  aPrefix = getPrefix(aStart, aEnd);
+  if (checkPrefix) {
+    if (aPrefix.length() > 0) {
+      myCompleter->setCompletionPrefix(aPrefix);
+      myCompleter->complete();
+    }
+  } else {
+    myCompleter->setCompletionPrefix(aPrefix);
+    myCompleter->complete();
+  }
+}
+
 void ModuleBase_ParamSpinBox::keyReleaseEvent(QKeyEvent* e)
 {
+  QString aText;
+
   switch (e->key()) {
+  case Qt::Key_Backspace:
+    if (myCompleter->popup()->isVisible()) {
+      myCompleter->popup()->hide();
+    }
+    showCompletion(true);
+    break;
   case Qt::Key_Return:
   case Qt::Key_Enter:
-  {
     if (myCompleter->popup()->isVisible()) {
       myCompleter->popup()->hide();
       myIsEquation = true;
     }
     emit textChanged(lineEdit()->text());
-    return;
-  }
+    break;
   case Qt::Key_Space:
     if (e->modifiers() & Qt::ControlModifier) {
-      myCompletePos = lineEdit()->cursorPosition();
-      int aStart, aEnd;
-      QString aPrefix = getPrefix(aStart, aEnd);
-      myCompleter->setCompletionPrefix(aPrefix);
-      myCompleter->complete();
+      showCompletion(false);
     }
-    break;
-  default:
-    QAbstractSpinBox::keyReleaseEvent(e);
+    break;  default:
+    aText = e->text();
+    if (aText.length() == 1) {
+      QChar aChar = aText.at(0);
+      if (isVariableSymbol(aChar)) {
+        showCompletion(true);
+      }
+    }
+  }
+  QAbstractSpinBox::keyReleaseEvent(e);
+}
+
+bool ModuleBase_ParamSpinBox::eventFilter(QObject* theObj, QEvent* theEvent)
+{
+  if (theEvent->type() == QEvent::KeyRelease) {
+    keyReleaseEvent((QKeyEvent*)theEvent);
   }
+  return QAbstractSpinBox::eventFilter(theObj, theEvent);
 }
 
 
@@ -237,7 +298,7 @@ QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const
     if (myCompletePos > 0) {
       int aLastChar = myCompletePos - 1;
       QChar aChar = aText.at(aLastChar);
-      while (aChar.isLetter() || aChar.isDigit()) {
+      while (isVariableSymbol(aChar)) {
         aPrefix.prepend(aText.at(aLastChar));
         aLastChar--;
         if (aLastChar < 0)
@@ -249,7 +310,7 @@ QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const
     if (myCompletePos < aLen) {
       int aLastChar = myCompletePos;
       QChar aChar = aText.at(aLastChar);
-      while (aChar.isLetter() || aChar.isDigit()) {
+      while (isVariableSymbol(aChar)) {
         aPrefix.append(aText.at(aLastChar));
         aLastChar++;
         if (aLastChar >= aLen)