Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom.git into Dev_1.2.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
index 7f77acd5a97ac1064fdfd533699a7e85dd71b4ad..1c26b11ebf3b0aa43ee8ac060a66976cfa7c160a 100644 (file)
@@ -5,7 +5,7 @@
 // Author:      Natalia ERMOLAEVA
 
 #include <ModuleBase_WidgetEditor.h>
-#include <ModuleBase_DoubleSpinBox.h>
+#include <ModuleBase_ParamSpinBox.h>
 #include <ModuleBase_Tools.h>
 
 #include <Config_Keywords.h>
 
 #include <GeomDataAPI_Point2D.h>
 
-#include <QWidget>
-#include <QLineEdit>
-#include <QTimer>
-#include <QDialog>
-#include <QLayout>
 #include <QApplication>
+#include <QLineEdit>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
 
 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
                                                  const Config_WidgetAPI* theData,
@@ -39,62 +40,64 @@ ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
 {
 }
 
-double editedValue(double theValue, bool& isDone)
+void editedValue(double& outValue, QString& outText)
 {
-  QDialog aDlg;
-  aDlg.setWindowFlags(Qt::FramelessWindowHint);
-  QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
-  ModuleBase_Tools::zeroMargins(aLay);
-
-  QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
-  aEditor->setValidator(new QDoubleValidator(aEditor));
-  QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
-  aLay->addWidget(aEditor);
-
-  QPoint aPoint = QCursor::pos();
-  aDlg.move(aPoint);
-
-  isDone = aDlg.exec() == QDialog::Accepted;
-  double aValue = theValue;
-  if (isDone)
-    aValue = aEditor->text().toDouble();
-  return aValue;
+  QMenu* aPopup = new QMenu();
+
+  QLineEdit* aEditor = new QLineEdit(QString::number(outValue), aPopup);
+  QWidgetAction* aLineEditAction = new QWidgetAction(aPopup);
+  aLineEditAction->setDefaultWidget(aEditor);
+  aPopup->addAction(aLineEditAction);
+
+  aEditor->setFocus();
+  aEditor->selectAll();
+  QString anExpression("([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([_a-zA-Z][a-zA-Z0-9_]*)");
+  aEditor->setValidator(new QRegExpValidator(QRegExp(anExpression), aEditor));
+  QObject::connect(aEditor, SIGNAL(returnPressed()), aLineEditAction, SIGNAL(triggered()));
+  QObject::connect(aLineEditAction, SIGNAL(triggered()), aPopup, SLOT(hide()));
+
+  QAction* aResult = aPopup->exec(QCursor::pos());
+  outText = aEditor->text();
+  bool isDouble;
+  double aValue = outText.toDouble(&isDouble);
+  if (isDouble) {
+    outValue = aValue;
+  //  outText = ""; // return empty string, if it's can be converted to a double
+  }
+  aPopup->deleteLater();
 }
 
 bool ModuleBase_WidgetEditor::focusTo()
 {
+  // nds: it seems, that the timer is not necessary anymore
+
   // We can not launch here modal process for value editing because 
   // it can be called on other focusOutWidget event and will block it
-  QTimer::singleShot(1, this, SLOT(showPopupEditor()));
+  //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
+
+  showPopupEditor();
+
   return true;
 }
 
 void ModuleBase_WidgetEditor::showPopupEditor()
 {
+  // we need to emit the focus in event manually in order to save the widget as an active
+  // in the property panel before the mouse leave event happens in the viewer. The module
+  // ask an active widget and change the feature visualization if the widget is not the current one.
+  emit focusInWidget(this);
+
+  // nds: it seems, that the envents processing is not necessary anymore
   // White while all events will be processed
-  QApplication::processEvents();
+  //QApplication::processEvents();
   double aValue = mySpinBox->value();
-  bool isDone;
-  aValue = editedValue(aValue, isDone);
-
-  if (isDone) {
-    bool isBlocked = mySpinBox->blockSignals(true);
-    mySpinBox->setValue(aValue);
-    mySpinBox->blockSignals(isBlocked);
+  QString aText;
+  editedValue(aValue, aText);
+  if (aText.isEmpty()) {
+    ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
+  } else {
+    ModuleBase_Tools::setSpinText(mySpinBox, aText);
   }
   emit valuesChanged();
   emit focusOutWidget(this);
 }
-
-void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
-                                               const std::string theAttribute)
-{
-  DataPtr aData = theFeature->data();
-  AttributeDoublePtr aRef = aData->real(theAttribute);
-  double aValue = aRef->value();
-
-  bool isDone;
-  aValue = editedValue(aValue, isDone);
-  if (isDone)
-    aRef->setValue(aValue);
-}