Salome HOME
Issue #251. Append Export/Import NewGeom commands in the SALOME desktop.
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureBase.cpp
index c1da278fa020868570d496073c97f06c748d4924..2d53041c0fefa5ef2e8bd84cb707fbfb3e644e53 100644 (file)
 #include <ModuleBase_OperationDescription.h>
 #include <ModuleBase_WidgetPoint2D.h>
 #include <ModuleBase_WidgetValueFeature.h>
-#include <ModuleBase_ViewerPrs.h>
+#include "ModuleBase_IPropertyPanel.h"
+#include "ModuleBase_ISelection.h"
+#include "ModuleBase_IViewer.h"
 
 #include <XGUI_Constants.h>
 
 #include <V3d_View.hxx>
-#include <TopoDS_Vertex.hxx>
-#include <TopoDS.hxx>
 #include <BRep_Tool.hxx>
 
 #ifdef _DEBUG
@@ -44,10 +44,9 @@ using namespace std;
 
 PartSet_OperationFeatureBase::PartSet_OperationFeatureBase(const QString& theId,
                                                                QObject* theParent,
-                                                               FeaturePtr theFeature)
+                                                               CompositeFeaturePtr theFeature)
     : PartSet_OperationSketchBase(theId, theParent),
-      mySketch(theFeature),
-      myActiveWidget(NULL)
+      mySketch(theFeature)
 {
 }
 
@@ -55,51 +54,35 @@ PartSet_OperationFeatureBase::~PartSet_OperationFeatureBase()
 {
 }
 
-void PartSet_OperationFeatureBase::initSelection(
-    const std::list<ModuleBase_ViewerPrs>& theSelected,
-    const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
-{
-  myPreSelection = theSelected;
-}
-
-void PartSet_OperationFeatureBase::initFeature(FeaturePtr theFeature)
-{
-  myInitFeature = theFeature;
-}
-
-FeaturePtr PartSet_OperationFeatureBase::sketch() const
+CompositeFeaturePtr PartSet_OperationFeatureBase::sketch() const
 {
   return mySketch;
 }
 
-void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
-                                                 const std::list<ModuleBase_ViewerPrs>& theSelected,
-                                                 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
+void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
+                                                 ModuleBase_ISelection* theSelection)
 {
-  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
+  Handle(V3d_View) aView = theViewer->activeView();
+  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
   double aX = aPoint.X(), anY = aPoint.Y();
+  QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
 
-  if (theSelected.empty()) {
-    PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
+  if (aSelected.empty()) {
+    PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
   } else {
-    ModuleBase_ViewerPrs aPrs = theSelected.front();
+    ModuleBase_ViewerPrs aPrs = aSelected.first();
+    if (getViewerPoint(aPrs, theViewer, aX, anY)) {
+      ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
+      PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
+    }
     const TopoDS_Shape& aShape = aPrs.shape();
-    if (!aShape.IsNull()) {
-      if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
-        const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
-        if (!aVertex.IsNull()) {
-          aPoint = BRep_Tool::Pnt(aVertex);
-          PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
-          PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX, anY);
-        }
-      } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
-        PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
-      }
+    if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
+      PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
     }
   }
   ObjectPtr aFeature;
-  if (!theSelected.empty()) {
-    ModuleBase_ViewerPrs aPrs = theSelected.front();
+  if (!aSelected.empty()) {
+    ModuleBase_ViewerPrs aPrs = aSelected.first();
     aFeature = aPrs.object();
   } else {
     aFeature = feature();  // for the widget distance only
@@ -108,53 +91,36 @@ void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, Handle(V
   bool isApplyed = setWidgetValue(aFeature, aX, anY);
   if (isApplyed) {
     flushUpdated();
-    emit activateNextWidget(myActiveWidget);
+    myPropertyPanel->activateNextWidget();
   }
-  commit();
+  // the operation can be committed only when there is no an active widget anymore
+  // if this check is absent, the edit operation for constraint perpendicular is stopped
+  // after the first object selection in the viewer(there are two objects to be selected)
+  // the second case is the constraint distance, the edit is stopped after any mouse click
+  // in the viewer whenever it is applyed or not to the selection control
+  if (!myPropertyPanel->activeWidget())
+    commit();
 }
 
-void PartSet_OperationFeatureBase::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
+bool PartSet_OperationFeatureBase::getViewerPoint(ModuleBase_ViewerPrs thePrs,
+                                                       ModuleBase_IViewer* theViewer,
+                                                       double& theX, double& theY)
 {
-  myActiveWidget = theWidget;
-  activateByPreselection();
-  if (myInitFeature && myActiveWidget) {
-    ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
-    if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
-      myInitFeature = FeaturePtr();
-      emit activateNextWidget(myActiveWidget);
-    }
-  }
+  return PartSet_Tools::hasVertexShape(thePrs, sketch(), theViewer->activeView(),
+                                       theX, theY);
 }
 
-void PartSet_OperationFeatureBase::activateByPreselection()
+/*bool PartSet_OperationFeatureBase::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
 {
-  if ((myPreSelection.size() > 0) && myActiveWidget) {
-    const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
-    ModuleBase_WidgetValueFeature aValue;
-    aValue.setObject(aPrs.object());
-    if (myActiveWidget->setValue(&aValue)) {
-      myPreSelection.remove(aPrs);
-      if(isValid()) {
-        myActiveWidget = NULL;
-        commit();
-      } else {
-        emit activateNextWidget(myActiveWidget);
-      }
-    }
-    // If preselection is enough to make a valid feature - apply it immediately
-  }
-}
-
-bool PartSet_OperationFeatureBase::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
-{
-  if (!myActiveWidget)
+  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
+  if (!aActiveWgt)
     return false;
   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
   aValue->setObject(theFeature);
   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
-  bool isApplyed = myActiveWidget->setValue(aValue);
+  bool isApplyed = aActiveWgt->setValue(aValue);
 
   delete aValue;
   myIsModified = (myIsModified || isApplyed);
   return isApplyed;
-}
+}*/