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 84b63cf2835e12c3ccd399b6cd76a872a158983c..1c26b11ebf3b0aa43ee8ac060a66976cfa7c160a 100644 (file)
@@ -1,14 +1,18 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        ModuleBase_WidgetEditor.cpp
 // Created:     25 Apr 2014
 // Author:      Natalia ERMOLAEVA
 
 #include <ModuleBase_WidgetEditor.h>
+#include <ModuleBase_ParamSpinBox.h>
+#include <ModuleBase_Tools.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
 
 #include <Events_Loop.h>
-#include <Model_Events.h>
+#include <ModelAPI_Events.h>
 
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Data.h>
 
 #include <GeomDataAPI_Point2D.h>
 
-#include <QWidget>
+#include <QApplication>
 #include <QLineEdit>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
 
 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
-                                                 const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData)
+                                                 const Config_WidgetAPI* theData,
+                                                 const std::string& theParentId)
+    : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
 {
-  myEditor = new QLineEdit(0);
-  myEditor->setWindowFlags(Qt::ToolTip);
-  myEditor->setFocusPolicy(Qt::StrongFocus);
-
-  connect(myEditor, SIGNAL(returnPressed()), this, SLOT(onStopEditing()));
-  connect(myEditor, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesChanged()));
 }
 
 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
 {
-  delete myEditor;
 }
 
-bool ModuleBase_WidgetEditor::storeValue(FeaturePtr theFeature) const
+void editedValue(double& outValue, QString& outText)
 {
-  DataPtr aData = theFeature->data();
-  AttributeDoublePtr aReal = aData->real(attributeID());
-  bool isOk;
-  double aValue = myEditor->text().toDouble(&isOk);
-  if (isOk && aReal->value() != aValue) {
-    //ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
-    //bool isBlocked = that->blockSignals(true);
-    aReal->setValue(aValue);
-    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
-    //that->blockSignals(isBlocked);
+  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
   }
-  return true;
+  aPopup->deleteLater();
 }
 
-bool ModuleBase_WidgetEditor::restoreValue(FeaturePtr theFeature)
+bool ModuleBase_WidgetEditor::focusTo()
 {
-  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  AttributeDoublePtr aRef = aData->real(attributeID());
+  // nds: it seems, that the timer is not necessary anymore
 
-  //bool isBlocked = this->blockSignals(true);
-  myEditor->setText(QString::number(aRef->value()));
-  //this->blockSignals(isBlocked);
-  return true;
-}
-
-void ModuleBase_WidgetEditor::focusTo()
-{
-  QPoint aPoint = QCursor::pos();
+  // 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()));
 
-  myEditor->move(aPoint);
-  myEditor->show();
+  showPopupEditor();
 
-  myEditor->selectAll();
-  myEditor->setFocus();
-}
-
-QWidget* ModuleBase_WidgetEditor::getControl() const
-{
-  return 0;
-}
-
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
-{
-  QList<QWidget*> aControls;
-  return aControls;
+  return true;
 }
 
-void ModuleBase_WidgetEditor::onStopEditing()
+void ModuleBase_WidgetEditor::showPopupEditor()
 {
-  myEditor->hide();
+  // 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();
+  double aValue = mySpinBox->value();
+  QString aText;
+  editedValue(aValue, aText);
+  if (aText.isEmpty()) {
+    ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
+  } else {
+    ModuleBase_Tools::setSpinText(mySpinBox, aText);
+  }
+  emit valuesChanged();
   emit focusOutWidget(this);
 }