Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
index 84b63cf2835e12c3ccd399b6cd76a872a158983c..be60db9d3b503a620ba69118b8e50af10ef2be79 100644 (file)
@@ -1,14 +1,32 @@
-// File:        ModuleBase_WidgetEditor.cpp
-// Created:     25 Apr 2014
-// Author:      Natalia ERMOLAEVA
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #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 <QKeyEvent>
 #include <QLineEdit>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
+#include <QDesktopWidget>
+#include <QDialog>
+#include <QLayout>
+
+// Dialog is redefined to avoid Escape key processing
+class ModuleBase_EditorDialog : public QDialog
+{
+public:
+  ModuleBase_EditorDialog(QWidget* theParent, Qt::WindowFlags theFlags)
+    : QDialog(theParent, theFlags) {}
+  ~ModuleBase_EditorDialog() {}
+
+protected:
+  // Do nothing if key pressed because it is processing on operation manager level
+  virtual void keyPressEvent(QKeyEvent* theEvent) {
+    if (theEvent->key() == Qt::Key_Escape)
+        return;
+    QDialog::keyPressEvent(theEvent);
+  }
+};
 
 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
                                                  const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData)
+: ModuleBase_WidgetDoubleValue(theParent, theData),
+  myXPosition(-1), myYPosition(-1), myEditorDialog(0)
 {
-  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
+bool ModuleBase_WidgetEditor::editedValue(double theSpinMinValue, double theSpinMaxValue,
+                                          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);
+  bool isValueAccepted = false;
+
+  myEditorDialog = new ModuleBase_EditorDialog(QApplication::desktop(), Qt::FramelessWindowHint);
+
+  QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
+  aLay->setContentsMargins(2, 2, 2, 2);
+
+  ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
+  anEditor->setMinimum(theSpinMinValue);
+  anEditor->setMaximum(theSpinMaxValue);
+  if (outText.isEmpty())
+    anEditor->setValue(outValue);
+  else
+    anEditor->setText(outText);
+
+  aLay->addWidget(anEditor);
+
+  ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
+  anEditor->selectAll();
+
+  QPoint aPoint = QCursor::pos();
+  if (myXPosition >= 0 && myYPosition >= 0)
+    aPoint = QPoint(myXPosition, myYPosition);
+
+  myEditorDialog->move(aPoint);
+  isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
+  if (isValueAccepted) {
+    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
+    }
   }
-  return true;
+  delete myEditorDialog;
+  myEditorDialog = 0;
+  return isValueAccepted;
 }
 
-bool ModuleBase_WidgetEditor::restoreValue(FeaturePtr theFeature)
+bool ModuleBase_WidgetEditor::focusTo()
 {
-  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  AttributeDoublePtr aRef = aData->real(attributeID());
-
-  //bool isBlocked = this->blockSignals(true);
-  myEditor->setText(QString::number(aRef->value()));
-  //this->blockSignals(isBlocked);
-  return true;
+  showPopupEditor();
+  return false;
 }
 
-void ModuleBase_WidgetEditor::focusTo()
+bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
 {
-  QPoint aPoint = QCursor::pos();
-
-  myEditor->move(aPoint);
-  myEditor->show();
+  bool isValueAccepted = 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.  Also we need this widget as active to provide call of processEnter() applyed
+  // by operation manager to the current widget. If not, the myEditorDialog will stay opened
+  emitFocusInWidget();
+
+  // 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();
+
+  isValueAccepted = editedValue(mySpinBox->minimum(), mySpinBox->maximum(), aValue, aText);
+  if (isValueAccepted) {
+    if (aText.isEmpty()) {
+      if (mySpinBox->hasVariable()) {
+        // variable text should be cleared before setting value as the value
+        // will not be set if there is a varable in control
+        ModuleBase_Tools::setSpinText(mySpinBox, "");
+      }
+      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);
+    }
+    else
+      storeValue();
+  }
+  ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
+  mySpinBox->selectAll();
+  // enter is processed, so we need not anymore emit clicked signal
+  //if (theSendSignals && !myIsEditing && isValueAccepted)
+  //  emit enterClicked(this);
 
-  myEditor->selectAll();
-  myEditor->setFocus();
+  return isValueAccepted;
 }
 
-QWidget* ModuleBase_WidgetEditor::getControl() const
+void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
 {
-  return 0;
+  myXPosition = theX;
+  myYPosition = theY;
 }
 
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
+bool ModuleBase_WidgetEditor::processEnter()
 {
-  QList<QWidget*> aControls;
-  return aControls;
+  if (myEditorDialog) {
+    myEditorDialog->accept();
+    return true;
+  }
+
+  return ModuleBase_WidgetDoubleValue::processEnter();
 }
 
-void ModuleBase_WidgetEditor::onStopEditing()
+bool ModuleBase_WidgetEditor::processEscape()
 {
-  myEditor->hide();
-  emit focusOutWidget(this);
+  if (myEditorDialog) {
+    myEditorDialog->reject();
+    return true;
+  }
+
+  return ModuleBase_WidgetDoubleValue::processEscape();
 }