Salome HOME
Issue #529 : 4.07. Import IGES, export to BREP, STEP, IGES - Add frame (Export icon)
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
index d61d8d7481c00d80c4fe25e001b140520b01debd..3d8c89ad1871602e7608812c70376eb572a6ed4f 100644 (file)
@@ -31,6 +31,7 @@
 #include <GeomAPI_Pnt2d.h>
 
 #include <SketchPlugin_Feature.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
 
 #include <QGroupBox>
 #include <QGridLayout>
@@ -54,6 +55,8 @@ PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
                                               const std::string& theParentId)
     : ModuleBase_ModelWidget(theParent, theData, theParentId)
 {
+  // the control should accept the focus, so the boolen flag is corrected to be true
+  myIsObligatory = true;
   //myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
   myGroupBox = new QGroupBox(aPageName, theParent);
@@ -90,17 +93,44 @@ PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
 
     connect(myYSpin, SIGNAL(valueChanged(double)), this, SLOT(onValuesChanged()));
   }
+  QVBoxLayout* aLayout = new QVBoxLayout(this);
+  ModuleBase_Tools::zeroMargins(aLayout);
+  aLayout->addWidget(myGroupBox);
+  setLayout(aLayout);
+}
+
+void PartSet_WidgetPoint2D::reset()
+{
+  if (isComputedDefault()) {
+    //return;
+    if (myFeature->compute(myAttributeID))
+      restoreValue();
+  }
+  else {
+    bool isOk;
+    double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
+    // it is important to block the spin box control in order to do not through out the
+    // locking of the validating state.
+    ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
+    ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
+    storeValueCustom();
+  }
 }
 
 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
 {
 }
 
-bool PartSet_WidgetPoint2D::setSelection(ModuleBase_ViewerPrs theValue)
+bool PartSet_WidgetPoint2D::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
 {
+  if (thePosition < 0 || thePosition >= theValues.size())
+    return false;
+  ModuleBase_ViewerPrs aValue = theValues[thePosition];
+  thePosition++;
+
   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
   bool isDone = false;
-  TopoDS_Shape aShape = theValue.shape();
+  TopoDS_Shape aShape = aValue.shape();
   double aX, aY;
   if (getPoint2d(aView, aShape, aX, aY)) {
     isDone = setPoint(aX, aY);
@@ -114,21 +144,15 @@ bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
     return false;
   if (fabs(theY) >= MaxCoordinate)
     return false;
-  bool isBlocked = this->blockSignals(true);
-  myXSpin->blockSignals(true);
-  myXSpin->setValue(theX);
-  myXSpin->blockSignals(false);
 
-  myYSpin->blockSignals(true);
-  myYSpin->setValue(theY);
-  myYSpin->blockSignals(false);
-  this->blockSignals(isBlocked);
+  ModuleBase_Tools::setSpinValue(myXSpin, theX);
+  ModuleBase_Tools::setSpinValue(myYSpin, theY);
 
-  emit valuesChanged();
+  storeValue();
   return true;
 }
 
-bool PartSet_WidgetPoint2D::storeValue() const
+bool PartSet_WidgetPoint2D::storeValueCustom() const
 {
   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
   if (!aData) // can be on abort of sketcher element
@@ -164,21 +188,10 @@ bool PartSet_WidgetPoint2D::restoreValue()
   double _X = aPoint->x();
   double _Y = aPoint->y();
 #endif
-  bool isBlocked = this->blockSignals(true);
-  myXSpin->blockSignals(true);
-  myXSpin->setValue(aPoint->x());
-  myXSpin->blockSignals(false);
-
-  myYSpin->blockSignals(true);
-  myYSpin->setValue(aPoint->y());
-  myYSpin->blockSignals(false);
-  this->blockSignals(isBlocked);
-  return true;
-}
 
-QWidget* PartSet_WidgetPoint2D::getControl() const
-{
-  return myGroupBox;
+  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
+  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
+  return true;
 }
 
 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
@@ -200,6 +213,7 @@ void PartSet_WidgetPoint2D::activateCustom()
 
   QIntList aModes;
   aModes << TopAbs_VERTEX;
+  aModes << TopAbs_EDGE;
   myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
 }
 
@@ -235,6 +249,10 @@ bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
 
 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
 {
+  // the contex menu release by the right button should not be processed by this widget
+  if (theEvent->button() != Qt::LeftButton)
+    return;
+
   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
   Handle(V3d_View) aView = theWnd->v3dView();
   // TODO: This fragment doesn't work because bug in OCC Viewer. It can be used after fixing.
@@ -249,8 +267,11 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
     if (aSelectedFeature.get() != NULL) {
       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
-      if ((!aSPFeature) && (!aShape.IsNull()))
-        PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
+      if ((!aSPFeature) && (!aShape.IsNull())) {
+        ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
+        if (!aFixedObject.get())
+          aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
+      }
     }
     double aX, aY;
     if (getPoint2d(aView, aShape, aX, aY)) {
@@ -260,6 +281,25 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
       emit vertexSelected();
       emit focusOutWidget(this);
       return;
+    } else if (aShape.ShapeType() == TopAbs_EDGE) {
+      // Create point-edge coincedence
+      FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
+      std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
+
+      std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
+          ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+      AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
+      std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
+      aRef1->setAttr(aThisPoint);
+
+      std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
+          ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+      aRef2->setObject(aObject);
+      aFeature->execute();
+      emit vertexSelected();
+      emit focusOutWidget(this);
+      return;
     }
   }
   // End of Bug dependent fragment