Salome HOME
Fix for the #2716 comment 3 : make smash allow sub-solids selected, but "Apply" butto...
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
index 27b0b64f67ef5d7f56d56c4337305593c639a452..84a0ab0c06c314294fbe07fa4665ac999756d878 100755 (executable)
@@ -19,6 +19,8 @@
 //
 
 #include "PartSet_SketcherMgr.h"
+
+#include "PartSet_Filters.h"
 #include "PartSet_SketcherReentrantMgr.h"
 #include "PartSet_Module.h"
 #include "PartSet_MouseProcessor.h"
@@ -34,6 +36,7 @@
 #include <XGUI_Workshop.h>
 #include <XGUI_ContextMenuMgr.h>
 #include <XGUI_Selection.h>
+#include <XGUI_SelectionActivate.h>
 #include <XGUI_SelectionMgr.h>
 #include <XGUI_ModuleConnector.h>
 #include <XGUI_PropertyPanel.h>
@@ -55,6 +58,7 @@
 #include <ModuleBase_ViewerPrs.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_ResultPrs.h>
+#include <ModuleBase_ViewerFilters.h>
 
 #include <GeomDataAPI_Point2D.h>
 
@@ -187,14 +191,13 @@ PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
 
   mySketchPlane = new PartSet_PreviewSketchPlane();
 
-  myCirclePointFilter = new PartSet_CirclePointFilter(anIWorkshop);
-  myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
+  registerSelectionFilter(SF_SketchCirclePointFilter, new PartSet_CirclePointFilter(anIWorkshop));
+  registerSelectionFilter(SF_SketchPlaneFilter, new ModuleBase_ShapeInPlaneFilter());
 }
 
 PartSet_SketcherMgr::~PartSet_SketcherMgr()
 {
-  myPlaneFilter.Nullify();
-  myCirclePointFilter.Nullify();
+  delete mySketchPlane;
 }
 
 void PartSet_SketcherMgr::onEnterViewPort()
@@ -646,8 +649,10 @@ void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMo
       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
       // Find corresponded widget to activate value editing
       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
-        if (aWgt->attributeID() == SketchPlugin_Constraint::VALUE() ||
-            aWgt->attributeID() == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
+        std::string anId = aWgt->attributeID();
+        if (anId == SketchPlugin_Constraint::VALUE() ||
+          anId == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() ||
+          anId == SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()) {
           PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
           if (anEditor)
             anEditor->showPopupEditor();
@@ -672,8 +677,8 @@ void PartSet_SketcherMgr::onApplicationStarted()
 
     connect(aPropertyPanel, SIGNAL(noMoreWidgets(const std::string&)),
             aReentranceMgr, SLOT(onNoMoreWidgets(const std::string&)));
-    connect(aPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
-            aReentranceMgr, SLOT(onWidgetActivated()));
+    //connect(aPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
+    //        aReentranceMgr, SLOT(onWidgetActivated()));
   }
 
   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
@@ -733,10 +738,15 @@ void PartSet_SketcherMgr::launchEditing()
       if (!aSPFeature->isExternal())
         myModule->editFeature(aSPFeature);
       else {
-        FeaturePtr aProjectionFeature = PartSet_Tools::findRefsToMeFeature(aFeature,
+        // need to edit a feature (Projection/IntersectionPoint),
+        // which produces current External feature
+        FeaturePtr aProducerFeature = PartSet_Tools::findRefsToMeFeature(aFeature,
                                                         SketchPlugin_Projection::ID());
-        if (aProjectionFeature.get())
-          myModule->editFeature(aProjectionFeature);
+        if (!aProducerFeature.get())
+          aProducerFeature = PartSet_Tools::findRefsToMeFeature(aFeature,
+                                                        SketchPlugin_IntersectionPoint::ID());
+        if (aProducerFeature.get())
+          myModule->editFeature(aProducerFeature);
       }
     }
   }
@@ -812,9 +822,11 @@ const QStringList& PartSet_SketcherMgr::constraintsIdList()
   return aConstraintIds;
 }
 
-void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
+void PartSet_SketcherMgr::sketchSelectionModes(const CompositeFeaturePtr& theSketch,
+                                               QIntList& theModes)
 {
-  theModes.clear();
+  if (!theSketch.get() || !PartSet_Tools::sketchPlane(theSketch).get())
+    return;
 
   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
@@ -938,6 +950,13 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
 
   // Display all sketcher sub-Objects
   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFOperation->feature());
+  double aSizeOfView = 0;
+  std::shared_ptr<GeomAPI_Pnt> aCentralPoint;
+  if (aFOperation->isEditOperation() &&
+      mySketchPlane->getDefaultSizeOfView(myCurrentSketch, aSizeOfView, aCentralPoint)) {
+    mySketchPlane->setSizeOfView(aSizeOfView, true, aCentralPoint);
+  }
+
   mySketchPlane->createSketchPlane(myCurrentSketch, myModule->workshop());
   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
 
@@ -952,7 +971,8 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
   // Remove invalid sketch entities
   std::set<FeaturePtr> anInvalidFeatures;
   ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
-  for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
+  int aNumberOfSubs = myCurrentSketch->numberOfSubs();
+  for (int i = 0; i < aNumberOfSubs; i++) {
     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
     if (aFeature.get()) {
       if (!aFactory->validate(aFeature))
@@ -993,7 +1013,8 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
   QStringList anInfo;
   Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
   const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
-  for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
+  aNumberOfSubs = myCurrentSketch->numberOfSubs();
+  for (int i = 0; i < aNumberOfSubs; i++) {
     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
 #ifdef DEBUG_SKETCHER_ENTITIES
     anInfo.append(ModuleBase_Tools::objectInfo(aFeature));
@@ -1020,15 +1041,14 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
   bool aHasPlane = false;
   std::shared_ptr<GeomAPI_Pln> aPln;
   aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
-  myPlaneFilter->setPlane(aPln);
+  Handle(SelectMgr_Filter) aFilter = myModule->selectionFilter(SF_SketchPlaneFilter);
+  if (!aFilter.IsNull())
+    Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
 
-  activateSelectionFilters();
+  workshop()->selectionActivate()->updateSelectionFilters();
+  workshop()->selectionActivate()->updateSelectionModes();
 
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
-  // all displayed objects should be activated in current selection modes according to switched
-  // plane filter
-  if (aPln.get())
-    aConnector->activateModuleSelectionModes();
 
   myExternalPointsMgr = new PartSet_ExternalPointsMgr(myModule->workshop(), myCurrentSketch);
 }
@@ -1064,7 +1084,8 @@ void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
   }
   else {
     // Hide all sketcher sub-Objects
-    for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
+    int aNumberOfSubs = myCurrentSketch->numberOfSubs();
+    for (int i = 0; i < aNumberOfSubs; i++) {
       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
       std::list<ResultPtr> aResults = aFeature->results();
       std::list<ResultPtr>::const_iterator aIt;
@@ -1097,10 +1118,8 @@ void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
 
     Events_Loop::loop()->flush(aDispEvent);
   }
-  deactivateSelectionFilters();
-
-  // restore the module selection modes, which were changed on startSketch
-  aConnector->activateModuleSelectionModes();
+  workshop()->selectionActivate()->updateSelectionFilters();
+  workshop()->selectionActivate()->updateSelectionModes();
 }
 
 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
@@ -1160,24 +1179,16 @@ void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
   }
 }
 
-void PartSet_SketcherMgr::activateSelectionFilters()
+bool PartSet_SketcherMgr::sketchSelectionFilter(const XGUI_SelectionFilterType theFilterType)
 {
-  myModule->workshop()->viewer()->addSelectionFilter(myCirclePointFilter);
-  myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
+  return mySelectionFilterTypes.find(theFilterType) != mySelectionFilterTypes.end();
 }
 
-void PartSet_SketcherMgr::deactivateSelectionFilters()
+void PartSet_SketcherMgr::registerSelectionFilter(const XGUI_SelectionFilterType theFilterType,
+                                                  const Handle(SelectMgr_Filter)& theFilter)
 {
-  myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
-  myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
-}
-
-void PartSet_SketcherMgr::activatePlaneFilter(const bool& toActivate)
-{
-  if (toActivate)
-    myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
-  else
-    myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
+  mySelectionFilterTypes.insert(theFilterType);
+  myModule->registerSelectionFilter(theFilterType, theFilter);
 }
 
 bool PartSet_SketcherMgr::operationActivatedByPreselection()
@@ -1472,7 +1483,7 @@ bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
   if (anObjectFeature.get()) {
     int aSize = myCurrentSketch->numberOfSubs();
-    for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
+    for (int i = 0; i < aSize && !isFoundObject; i++) {
       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
     }
@@ -1480,9 +1491,13 @@ bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
   return isFoundObject;
 }
 
-void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
+void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePlane)
 {
-  myPlaneFilter->setPlane(thePln);
+  Handle(SelectMgr_Filter) aFilter = myModule->selectionFilter(SF_SketchPlaneFilter);
+  if (!aFilter.IsNull())
+    Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(thePlane);
+
+  workshop()->selectionActivate()->updateSelectionModes();
 }
 
 bool PartSet_SketcherMgr::setDistanceValueByPreselection(ModuleBase_Operation* theOperation,
@@ -1889,7 +1904,8 @@ void PartSet_SketcherMgr::updateBySketchParameters(
       if (aPrevState != theState) {
         ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
         XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
-        for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
+        int aNumberOfSubs = myCurrentSketch->numberOfSubs();
+        for (int i = 0; i < aNumberOfSubs; i++) {
           FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
           bool aProcessed = false;
           bool aConstraintDisplayed = canDisplayConstraint(aSubFeature, theType, aProcessed);