Salome HOME
#1136 - hidden axis are selected in sketch
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
index f62eae3d3fbe4085328d8c643146d0e566c83850..aa4cd79b70bdaa0d9af645402e6779dd43788e61 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 <QTimer>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
+#include <QDesktopWidget>
 #include <QDialog>
 #include <QLayout>
 
 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
-                                                 const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData), myValue(0)
+                                                 const Config_WidgetAPI* theData,
+                                                 const std::string& theParentId)
+: ModuleBase_WidgetDoubleValue(theParent, theData, theParentId),
+  //myIsEnterPressedEmitted(false),
+  myXPosition(-1), myYPosition(-1)
 {
 }
 
 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
 {
-  //delete myEditor;
 }
 
-bool ModuleBase_WidgetEditor::storeValue(FeaturePtr theFeature) const
+void ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
 {
-  DataPtr aData = theFeature->data();
-  AttributeDoublePtr aReal = aData->real(attributeID());
-  bool isOk;
-  if (isOk && aReal->value() != myValue) {
-    aReal->setValue(myValue);
-    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
-  }
-  return true;
-}
+  QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
+  QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
+  aLay->setContentsMargins(2, 2, 2, 2);
 
-bool ModuleBase_WidgetEditor::restoreValue(FeaturePtr theFeature)
-{
-  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  AttributeDoublePtr aRef = aData->real(attributeID());
+  ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
+  anEditor->enableKeyPressEvent(true);
+  //if (!myIsEditing) {
+  //  connect(anEditor, SIGNAL(enterPressed()), this, SLOT(onEnterPressed()));
+  //}
 
-  myValue = aRef->value();
-  return true;
-}
+  anEditor->setMinimum(0);
+  anEditor->setMaximum(DBL_MAX);
+  if (outText.isEmpty())
+    anEditor->setValue(outValue);
+  else
+    anEditor->setText(outText);
 
-void ModuleBase_WidgetEditor::focusTo()
-{
-  QPoint aPoint = QCursor::pos();
+  aLay->addWidget(anEditor);
 
-  QDialog aDlg;
-  aDlg.setWindowFlags(Qt::FramelessWindowHint);
-  QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
-  aLay->setContentsMargins(0,0,0,0);
+  ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
+  anEditor->selectAll();
+  QObject::connect(anEditor, SIGNAL(enterReleased()), &aDlg, SLOT(accept()));
 
-  QLineEdit* aEditor = new QLineEdit(QString::number(myValue), &aDlg);
-  connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
-  aLay->addWidget(aEditor);
+  QPoint aPoint = QCursor::pos();
+  if (myXPosition >= 0 && myYPosition >= 0)
+    aPoint = QPoint(myXPosition, myYPosition);
 
   aDlg.move(aPoint);
-  int aRes = aDlg.exec();
-
-  if (aRes == QDialog::Accepted)
-    myValue = aEditor->text().toDouble();
-
-  emit focusOutWidget(this);
+  aDlg.exec();
+
+  //if (!myIsEditing) {
+  //  disconnect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onEnterPressed()));
+  //}
+
+  outText = anEditor->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
+  }
 }
 
-QWidget* ModuleBase_WidgetEditor::getControl() const
+bool ModuleBase_WidgetEditor::focusTo()
 {
-  return 0;
+  showPopupEditor();
+  return true;
 }
 
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
+void ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
 {
-  QList<QWidget*> aControls;
-  return aControls;
+  //myIsEnterPressedEmitted = false;
+
+  // 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.
+  if (theSendSignals)
+    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;
+  if (mySpinBox->hasVariable())
+    aText = mySpinBox->text();
+
+  editedValue(aValue, aText);
+  if (aText.isEmpty()) {
+    ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
+  } else {
+    ModuleBase_Tools::setSpinText(mySpinBox, aText);
+  }
+  if (theSendSignals) {
+    emit valuesChanged();
+    // the focus leaves the control automatically by the Enter/Esc event
+    // it is processed in operation manager
+    //emit focusOutWidget(this);
+
+    //if (myIsEnterPressedEmitted)
+    if (!myIsEditing)
+      emit enterClicked();
+  }
+  else
+    storeValue();
 }
 
+/*void ModuleBase_WidgetEditor::onEnterPressed()
+{
+  myIsEnterPressedEmitted = true;
+}*/
+
+void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
+{
+  myXPosition = theX;
+  myYPosition = theY;
+}