Salome HOME
Rectangle correction to have coincidence with point/line selected for the first point...
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2dDistance.cpp
index 15061b90c586312eb48fc13b238b55e0312d2eff..9e92051b7964bace54cb3943faae3cf5215c1b58 100644 (file)
@@ -6,7 +6,10 @@
 
 #include "PartSet_WidgetPoint2dDistance.h"
 #include "PartSet_Tools.h"
-#include "PartSet_LockApplyMgr.h"
+
+#include <XGUI_Tools.h>
+#include <XGUI_Workshop.h>
+#include <XGUI_Displayer.h>
 
 #include <ModuleBase_ParamSpinBox.h>
 #include <ModuleBase_IWorkshop.h>
 
 #include <QMouseEvent>
 
-#define APPLY_BY_ENTER_OR_TAB
-
 PartSet_WidgetPoint2dDistance::PartSet_WidgetPoint2dDistance(QWidget* theParent,
                                                              ModuleBase_IWorkshop* theWorkshop,
                                                              const Config_WidgetAPI* theData,
                                                              const std::string& theParentId)
- : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop)
+: ModuleBase_WidgetDoubleValue(theParent, theData, theParentId), myWorkshop(theWorkshop),
+  myValueIsCashed(false), myIsFeatureVisibleInCash(true), myValueInCash(0)
 {
-  myLockApplyMgr = new PartSet_LockApplyMgr(theParent, myWorkshop);
-
   myFirstPntName = theData->getProperty("first_point");
-
-  // Reconnect to local slot
-#ifdef APPLY_BY_ENTER_OR_TAB
-  // Apply widget value change by enter/tab event.
-  disconnect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
-  connect(mySpinBox, SIGNAL(editingFinished()), this, SLOT(onValuesChanged()));
-  connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesModified()));
-#else
-  disconnect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
-  connect(mySpinBox, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
-#endif
 }
 
 PartSet_WidgetPoint2dDistance::~PartSet_WidgetPoint2dDistance()
 {
 }
 
+bool PartSet_WidgetPoint2dDistance::resetCustom()
+{
+  bool aDone = false;
+  if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
+    aDone = false;
+  }
+  else {
+    if (myValueIsCashed) {
+      // if the restored value should be hidden, aDone = true to set
+      // reset state for the widget in the parent
+      aDone = restoreCurentValue();
+    }
+    else
+      aDone = ModuleBase_WidgetDoubleValue::resetCustom();
+  }
+  return aDone;
+}
+
 void PartSet_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
                                              const std::shared_ptr<GeomAPI_Pnt2d>& thePnt)
 {
@@ -83,19 +90,16 @@ void PartSet_WidgetPoint2dDistance::activateCustom()
           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
-
-  myLockApplyMgr->activate();
 }
 
 void PartSet_WidgetPoint2dDistance::deactivate()
 {
+  ModuleBase_ModelWidget::deactivate();
   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
-
-  myLockApplyMgr->deactivate();
 }
 
 void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
@@ -114,7 +118,10 @@ void PartSet_WidgetPoint2dDistance::onMouseRelease(ModuleBase_IViewWindow* theWn
 
   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
   setPoint(feature(), aPnt);
-  emit focusOutWidget(this);
+
+  // if the validator of the control returns false, focus should not be switched
+  if (getError().isEmpty())
+    emit focusOutWidget(this);
 }
 
 void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
@@ -131,16 +138,56 @@ void PartSet_WidgetPoint2dDistance::onMouseMove(ModuleBase_IViewWindow* theWnd,
   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, aY);
 
   std::shared_ptr<GeomAPI_Pnt2d> aPnt = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
+  if (myState != ModifiedInViewer)
+    storeCurentValue();
+
+  bool isBlocked = blockValueState(true);
   setPoint(feature(), aPnt);
+  blockValueState(isBlocked);
+  setValueState(ModifiedInViewer);
 }
 
-void PartSet_WidgetPoint2dDistance::onValuesChanged()
+void PartSet_WidgetPoint2dDistance::storeCurentValue()
 {
-  myLockApplyMgr->valuesChanged();
-  emit valuesChanged();
+  // do not use cash if a variable is used
+  if (mySpinBox->hasVariable())
+    return;
+
+  myValueIsCashed = true;
+  myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
+                       XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
+  myValueInCash = mySpinBox->value();
+}
+
+bool PartSet_WidgetPoint2dDistance::restoreCurentValue()
+{
+  bool aRestoredAndHidden = true;
+
+  bool isVisible = myIsFeatureVisibleInCash;
+  // fill the control widgets by the cashed value
+
+  myValueIsCashed = false;
+  myIsFeatureVisibleInCash = true;
+  ModuleBase_Tools::setSpinValue(mySpinBox, myValueInCash);
+
+  // store value to the model
+  storeValueCustom();
+  if (isVisible) {
+    setValueState(Stored);
+    aRestoredAndHidden = false;
+  }
+  else
+    aRestoredAndHidden = true;
+
+  return aRestoredAndHidden;
 }
 
-bool PartSet_WidgetPoint2dDistance::isEventProcessed(QKeyEvent* theEvent)
+bool PartSet_WidgetPoint2dDistance::processEnter()
 {
-  return mySpinBox->isEventProcessed(theEvent);
+  bool isModified = getValueState() == ModifiedInPP;
+  if (isModified) {
+    emit valuesChanged();
+    mySpinBox->selectAll();
+  }
+  return isModified;
 }