Salome HOME
Fix for the issue #910: never fully remove the results, just make them disabled when...
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
index 62709442b484c9bc4ed9d34f9f8053fcee24ce6d..61749d78fc0226a9d8bc19711a9edfd11f95daf6 100644 (file)
@@ -64,11 +64,11 @@ PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
 
   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
   ModuleBase_Tools::adjustMargins(aGroupLay);
+  aGroupLay->setSpacing(2);
   aGroupLay->setColumnStretch(1, 1);
   {
     QLabel* aLabel = new QLabel(myGroupBox);
-    aLabel->setText(tr("X"));
-    aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
+    aLabel->setText(tr("X "));
     aGroupLay->addWidget(aLabel, 0, 0);
 
     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
@@ -81,8 +81,7 @@ PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
   }
   {
     QLabel* aLabel = new QLabel(myGroupBox);
-    aLabel->setText(tr("Y"));
-    aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
+    aLabel->setText(tr("Y "));
     aGroupLay->addWidget(aLabel, 1, 0);
 
     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
@@ -122,7 +121,8 @@ PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
 {
 }
 
-bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
+bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
+                                         const bool theToValidate)
 {
   if (theValues.empty())
     return false;
@@ -160,17 +160,18 @@ bool PartSet_WidgetPoint2D::storeValueCustom() const
     return false;
   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(attributeID()));
-  
+
   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
   bool isBlocked = that->blockSignals(true);
   bool isImmutable = aPoint->setImmutable(true);
-  
-  if (myXSpin->hasVariable() || myYSpin->hasVariable()) {
-    aPoint->setText(myXSpin->text().toStdString(), myYSpin->text().toStdString());
-  } else {
-    aPoint->setValue(myXSpin->value(), myYSpin->value());
-    aPoint->setText("", "");
-  }
+
+  // if text is not empty then setValue will be ignored
+  // so we should set the text at first
+  aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
+                  myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
+  aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
+                   !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
+
   // after movement the solver will call the update event: optimization
   moveObject(myFeature);
   aPoint->setImmutable(isImmutable);
@@ -179,21 +180,41 @@ bool PartSet_WidgetPoint2D::storeValueCustom() const
   return true;
 }
 
-bool PartSet_WidgetPoint2D::restoreValue()
+bool PartSet_WidgetPoint2D::restoreValueCustom()
 {
   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(attributeID()));
-  std::string aTextX = aPoint->textX();
-  std::string aTextY = aPoint->textY();
-  if (aTextX.empty() || aTextY.empty()) {
-    double aX = aPoint->x();
+  QString aTextX = QString::fromStdString(aPoint->textX());
+  QString aTextY = QString::fromStdString(aPoint->textY());
+
+  bool isDouble = false;
+  double aVal = 0;
+  if (aTextX.isEmpty()) {
     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
+  } else {
+    aVal = aTextX.toDouble(&isDouble);
+    if (isDouble)
+      ModuleBase_Tools::setSpinValue(myXSpin, aVal);
+    else
+      ModuleBase_Tools::setSpinText(myXSpin, aTextX);
+  }
+  if (aTextY.isEmpty()) {
     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
   } else {
-    ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
-    ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
+    aVal = aTextY.toDouble(&isDouble);
+    if (isDouble)
+      ModuleBase_Tools::setSpinValue(myYSpin, aVal);
+    else
+      ModuleBase_Tools::setSpinText(myYSpin, aTextY);
   }
+  //if (aTextX.empty() || aTextY.empty()) {
+  //  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
+  //  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
+  //} else {
+  //  ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
+  //  ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
+  //}
   return true;
 }
 
@@ -290,57 +311,86 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
     TopoDS_Shape aShape = aShapes.First();
     ObjectPtr aObject = aObjects.front();
     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
+    bool anExternal = false;
     if (aSelectedFeature.get() != NULL) {
       std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
               std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
       if ((!aSPFeature) && (!aShape.IsNull())) {
+        anExternal = true;
         ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
         if (!aFixedObject.get())
           aObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
-        setConstraintWith(aObject);
+
+        double aX, aY;
+        if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
+          // do not create a constraint to the point, which already used by the feature
+          // if the feature contains the point, focus is not switched
+          setPoint(aX, aY);
+        }
+        else {
+          setConstraintWith(aObject);
+          emit vertexSelected();
+          emit focusOutWidget(this);
+        }
+      }
+    }
+    if (!anExternal) {
+      double aX, aY;
+      bool isProcessed = false;
+      if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
+        // when the point is selected, the coordinates of the point should be set into the attribute
+        // if the feature contains the point, focus is not switched
+        setPoint(aX, aY);
+      }
+      else {
+        // do not set a coincidence constraint in the attribute if the feature contains a point
+        // with the same coordinates. It is important for line creation in order to do not set
+        // the same constraints for the same points, oterwise the result line has zero length.
+        if (getPoint2d(aView, aShape, aX, aY))
+          PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
+        else if (aShape.ShapeType() == TopAbs_EDGE)
+          setConstraintWith(aObject);
+
+        // it is important to perform updateObject() in order to the current value is 
+        // processed by Sketch Solver. Test case: line is created from a previous point
+        // to some distance, but in the area of the highlighting of the point. Constraint
+        // coincidence is created, after the solver is performed, the distance between the
+        // points of the line becomes less than the tolerance. Validator of the line returns
+        // false, the line will be aborted, but sketch stays valid.
+        updateObject(feature());
         emit vertexSelected();
         emit focusOutWidget(this);
-        return;
       }
     }
-    double aX, aY;
-    if (getPoint2d(aView, aShape, aX, aY)) {
-      PartSet_Tools::setConstraints(mySketch, feature(), attributeID(),aX, aY);
-      emit vertexSelected();
-      emit focusOutWidget(this);
-      return;
-    } else if (aShape.ShapeType() == TopAbs_EDGE) {
-      setConstraintWith(aObject);
-      emit vertexSelected();
-      emit focusOutWidget(this);
-      return;
-    }
   }
   // End of Bug dependent fragment
+  else {
+    // A case when point is taken from mouse event
+    gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
+    double aX, anY;
+    PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
 
-  // A case when point is taken from mouse event
-  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
-  double aX, anY;
-  PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
-  if (!setPoint(aX, anY))
-    return;
+    // if the feature contains the point, focus is not switched
+    if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
+      return;
 
-  /// Start alternative code
-  //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
-  //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
-  //QList<FeaturePtr> aIgnore;
-  //aIgnore.append(feature());
-
-  //double aTolerance = aView->Convert(7);
-  //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
-  //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
-  //if (aAttrPnt.get() != NULL) {
-  //  aFeaturePoint->setValue(aAttrPnt->pnt());
-  //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
-  //  emit vertexSelected();
-  //}
-  /// End alternative code
-  emit focusOutWidget(this);
+    /// Start alternative code
+    //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
+    //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
+    //QList<FeaturePtr> aIgnore;
+    //aIgnore.append(feature());
+
+    //double aTolerance = aView->Convert(7);
+    //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
+    //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
+    //if (aAttrPnt.get() != NULL) {
+    //  aFeaturePoint->setValue(aAttrPnt->pnt());
+    //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
+    //  emit vertexSelected();
+    //}
+    /// End alternative code
+    emit focusOutWidget(this);
+  }
 }
 
 
@@ -366,6 +416,30 @@ double PartSet_WidgetPoint2D::y() const
   return myYSpin->value();
 }
 
+
+bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
+                                                   double theX, double theY)
+{
+  bool aPointIsFound = false;
+  AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
+
+  std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
+                                    std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
+  std::list<AttributePtr> anAttributes =
+                                myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr>::iterator anIter = anAttributes.begin();
+  for(; anIter != anAttributes.end(); anIter++) {
+    AttributePoint2DPtr aPoint2DAttribute =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
+    if (aPoint2DAttribute == aWidgetAttribute)
+      continue;
+    if (aPoint2DAttribute.get()) {
+      aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
+    }
+  }
+  return aPointIsFound;
+}
+
 void PartSet_WidgetPoint2D::onValuesChanged()
 {
   myLockApplyMgr->valuesChanged();