Salome HOME
Porting to SALOME_8.2.0 : Selection with SHIFT in sketch, stayed selection of point...
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
index 03781cb7acd0925e0b94556a6ccf5e2cea934281..ff779fa9f2bdf2b4c12a708d81b1ba4c111d27eb 100755 (executable)
@@ -98,8 +98,8 @@
 //#define DEBUG_CURSOR
 
 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
-/// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge, put the function
-/// result as is to the list of results.
+/// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge,
+/// put the function result as is to the list of results.
 /// \param theOwner a viewer selected owner
 /// \param theFeature a feature, where the attribute is searched
 /// \param theSketch a current sketch
@@ -133,8 +133,8 @@ void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
 }
 
 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
-  : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
-    myIsMouseOverWindow(false),
+  : QObject(theModule), myModule(theModule), myIsEditLaunching(false), myIsDragging(false),
+    myDragDone(false), myIsMouseOverWindow(false),
     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
     myIsPopupMenuActive(false)
 {
@@ -287,11 +287,11 @@ void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
     myModule->sketchReentranceMgr()->updateInternalEditActiveState();
     return;
   }
-  // it is necessary to restore current selection in order to restore it after the values are modified
+  // it is necessary to restore current selection in order to restore it after values are modified
   restoreSelection();
   myCurrentSelection.clear();
 
-  // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
+  // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
   // viewer happens by deselect/select the modified objects. The flag should be restored after
   // the selection processing. The update viewer should be also called.
   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
@@ -369,9 +369,9 @@ void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseE
     // Init flyout point for radius rotation
     FeaturePtr aFeature = myCurrentSelection.begin().key();
 
+    get2dPoint(theWnd, theEvent, myCurrentPoint);
     if (isSketcher) {
       myIsDragging = true;
-      get2dPoint(theWnd, theEvent, myCurrentPoint);
       myDragDone = false;
 
       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
@@ -380,22 +380,29 @@ void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseE
       restoreSelection();
       launchEditing();
       if (aFeature.get() != NULL) {
-        std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
+        std::shared_ptr<SketchPlugin_Feature> aSPFeature =
                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
-      if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
+        if (aSPFeature.get() &&
+          (aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID() ||
+           aSPFeature->getKind() == SketchPlugin_ConstraintAngle::ID())) {
           DataPtr aData = aSPFeature->data();
           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
-          std::shared_ptr<GeomDataAPI_Point2D> aFPAttr = 
+          std::shared_ptr<GeomDataAPI_Point2D> aFPAttr =
             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
         }
       }
     } else if (isSketchOpe && isEditing) {
       // If selected another object commit current result
+      bool aPrevLaunchingState = myIsEditLaunching;
+      /// store editing state for Edit operation in order to do not clear highlight by restart
+      /// of edit operation.
+      /// Internal edit should not be stored as editing operation as the result will be a
+      /// creation operation, where previous selection should not be used(and will be cleared)
+      myIsEditLaunching = !myModule->sketchReentranceMgr()->isInternalEditActive();
       aFOperation->commit();
 
       myIsDragging = true;
-      get2dPoint(theWnd, theEvent, myCurrentPoint);
       myDragDone = false;
 
       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
@@ -403,6 +410,20 @@ void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseE
       // selected entities, e.g. selection of point(attribute on a line) should edit the point
       restoreSelection();
       launchEditing();
+      myIsEditLaunching = aPrevLaunchingState;
+      if (aFeature.get() != NULL) {
+        std::shared_ptr<SketchPlugin_Feature> aSPFeature =
+                  std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+        if (aSPFeature.get() &&
+          (aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID() ||
+           aSPFeature->getKind() == SketchPlugin_ConstraintAngle::ID())) {
+          DataPtr aData = aSPFeature->data();
+          AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+          std::shared_ptr<GeomDataAPI_Point2D> aFPAttr =
+            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
+          aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
+        }
+      }
       restoreSelection();
     }
   }
@@ -471,7 +492,7 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
       // the feature is to be erased here, but it is correct to call canDisplayObject because
       // there can be additional check (e.g. editor widget in distance constraint)
       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
-                                                                               (getCurrentOperation());
+                                                 (getCurrentOperation());
       if (aFOperation) {
         FeaturePtr aFeature = aFOperation->feature();
         visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
@@ -481,7 +502,8 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
   //myClickedPoint.clear();
 
   if (myIsDragging) {
-    // 1. the current selection is saved in the mouse press method in order to restore it after moving
+    // 1. the current selection is saved in the mouse press method in order to restore it after
+    //    moving
     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
     // deselected). This flag should be restored in the slot, processed the mouse release signal.
@@ -494,15 +516,15 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
 
     Handle(V3d_View) aView = theWnd->v3dView();
     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
-    double aX, aY;
-    PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
-    double dX =  aX - myCurrentPoint.myCurX;
-    double dY =  aY - myCurrentPoint.myCurY;
+    Point aMousePnt;
+    get2dPoint(theWnd, theEvent, aMousePnt);
+    double dX =  aMousePnt.myCurX - myCurrentPoint.myCurX;
+    double dY =  aMousePnt.myCurY - myCurrentPoint.myCurY;
 
     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
-    // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
+    // 3. the flag to disable the update viewer should be set in order to avoid blinking in the
     // viewer happens by deselect/select the modified objects. The flag should be restored after
     // the selection processing. The update viewer should be also called.
     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
@@ -528,7 +550,7 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
           std::string aAttrId = anAttr->id();
           DataPtr aData = aFeature->data();
           if (aData->isValid()) {
-            std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
+            std::shared_ptr<GeomDataAPI_Point2D> aPoint =
               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
             if (aPoint.get() != NULL) {
               bool isImmutable = aPoint->setImmutable(true);
@@ -565,7 +587,7 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
     aDisplayer->updateViewer();
 
     myDragDone = true;
-    myCurrentPoint.setValue(aX, aY);
+    myCurrentPoint = aMousePnt;
   }
 }
 
@@ -649,7 +671,7 @@ void PartSet_SketcherMgr::onAfterContextMenu()
   myIsPopupMenuActive = false;
 }
 
-void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
+void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
                                      Point& thePoint)
 {
   Handle(V3d_View) aView = theWnd->v3dView();
@@ -663,7 +685,7 @@ void PartSet_SketcherMgr::launchEditing()
 {
   if (!myCurrentSelection.empty()) {
     FeaturePtr aFeature = myCurrentSelection.begin().key();
-    std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
+    std::shared_ptr<SketchPlugin_Feature> aSPFeature =
               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
     if (aSPFeature && (!aSPFeature->isExternal())) {
       myModule->editFeature(aSPFeature);
@@ -684,17 +706,16 @@ bool PartSet_SketcherMgr::sketchSolverError()
 
 QString PartSet_SketcherMgr::getFeatureError(const FeaturePtr& theFeature)
 {
-  std::string anError = "";
+  QString anError;
   if (!theFeature.get() || !theFeature->data()->isValid())
-    return anError.c_str();
+    return anError;
 
   CompositeFeaturePtr aSketch = activeSketch();
   if (aSketch.get() && aSketch == theFeature) {
-    AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
-    anError = aAttributeString->value();
-    ModuleBase_Tools::translate(aSketch->getKind(), anError);
+    std::string aSolverError = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR())->value();
+    anError = ModuleBase_Tools::translate(aSketch->getKind(), aSolverError);
   }
-  return anError.c_str();
+  return anError;
 }
 
 void PartSet_SketcherMgr::clearClickedFlags()
@@ -732,6 +753,9 @@ const QStringList& PartSet_SketcherMgr::constraintsIdList()
     aConstraintIds << SketchPlugin_ConstraintAngle::ID().c_str();
     aConstraintIds << SketchPlugin_ConstraintCollinear::ID().c_str();
     aConstraintIds << SketchPlugin_ConstraintMiddle::ID().c_str();
+    aConstraintIds << SketchPlugin_ConstraintMirror::ID().c_str();
+    aConstraintIds << SketchPlugin_MultiTranslation::ID().c_str();
+    aConstraintIds << SketchPlugin_MultiRotation::ID().c_str();
   }
   return aConstraintIds;
 }
@@ -908,7 +932,7 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
 
   myModule->workshop()->viewer()->addSelectionFilter(myCirclePointFilter);
 
-  if (myPlaneFilter.IsNull()) 
+  if (myPlaneFilter.IsNull())
     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
 
   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
@@ -979,7 +1003,7 @@ void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
     }
     if (!aFOperation->isDisplayedOnStart(myCurrentSketch))
       myCurrentSketch->setDisplayed(true);
-    
+
     myCurrentSketch = CompositeFeaturePtr();
 
     myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
@@ -1017,8 +1041,18 @@ void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
   //}
   /// improvement to deselect automatically all eventual selected objects, when
   // returning to the neutral point of the Sketcher
-  // if the operation is restarted, the previous selection is used to initialize started operation
-  if (!myModule->sketchReentranceMgr()->isInternalEditStarted())
+  bool isClearSelectionPossible = true;
+  if (myIsEditLaunching) {
+    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                                          (theOperation);
+    if (aFOperation) {
+      FeaturePtr aFeature = aFOperation->feature();
+      if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
+        isClearSelectionPossible = false;
+      }
+    }
+  }
+  if (isClearSelectionPossible)
     workshop()->selector()->clearSelection();
 }
 
@@ -1091,19 +1125,7 @@ bool PartSet_SketcherMgr::canEraseObject(const ObjectPtr& theObject) const
   bool aCanErase = true;
   // when the sketch operation is active, results of sketch sub-feature can not be hidden
   if (myCurrentSketch.get()) {
-    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-    if (aResult.get()) {
-      // Display sketcher objects
-      for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
-
-        FeaturePtr aFeature = myCurrentSketch->subFeature(i);
-        std::list<ResultPtr> aResults = aFeature->results();
-        std::list<ResultPtr>::const_iterator anIt;
-        for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
-          aCanErase = *anIt != aResult;
-        }
-      }
-    }
+    return !isObjectOfSketch(theObject);
   }
   return aCanErase;
 }
@@ -1115,7 +1137,7 @@ bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
   bool aHasActiveSketch = activeSketch().get() != NULL;
   if (aHasActiveSketch) {
     // 1. the sketch feature should not be displayed during the sketch active operation
-    // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
+    // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch
     // nested features can be visualized
     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
     if (aFeature.get() != NULL && aFeature == activeSketch()) {
@@ -1169,11 +1191,13 @@ bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
       #ifndef DEBUG_DO_NOT_BY_ENTER
       if (isNestedCreateOperation(getCurrentOperation(), activeSketch())) {
         ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
-        ModuleBase_WidgetEditor* anEditorWdg = anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
-        // the active widget editor should not influence here. The presentation should be visible always
-        // when this widget is active.
+        ModuleBase_WidgetEditor* anEditorWdg =
+          anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
+        // the active widget editor should not influence here. The presentation should be visible
+        // always when this widget is active.
         if (!anEditorWdg && !myIsPopupMenuActive) {
-          // during a nested create operation, the feature is redisplayed only if the mouse over view
+          // during a nested create operation, the feature is redisplayed only
+          // if the mouse over view
           // of there was a value modified in the property panel after the mouse left the view
           aCanDisplay = canDisplayCurrentCreatedFeature();
         }
@@ -1358,7 +1382,7 @@ bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
 
 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
 {
-  if (myPlaneFilter.IsNull()) 
+  if (myPlaneFilter.IsNull())
    myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
 
   myPlaneFilter->setPlane(thePln);
@@ -1480,19 +1504,21 @@ void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
     ResultPtr aResult = *aIt;
     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
     if (aAISObj.get() == NULL)
-      continue; 
+      continue;
     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
 
-    SelectMgr_IndexedMapOfOwner aSelectedOwners;  
+    SelectMgr_IndexedMapOfOwner aSelectedOwners;
     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
-      Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
+      Handle(StdSelect_BRepOwner) anOwner =
+        Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
       if ( anOwner.IsNull() || !anOwner->HasShape() )
         continue;
       const TopoDS_Shape& aShape = anOwner->Shape();
       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
       if (aShapeType == TopAbs_VERTEX) {
-        AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
+        AttributePtr aPntAttr =
+          PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
         if (aPntAttr.get() != NULL &&
             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
           theOwnersToSelect.Add(anOwner);
@@ -1508,7 +1534,8 @@ void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
   }
 }
 
-void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
+void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget,
+                                                 const bool isToConnect)
 {
   if (isToConnect) {
     connect(theWidget, SIGNAL(beforeValuesChanged()),
@@ -1634,7 +1661,8 @@ void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
   // 1. it is necessary to save current selection in order to restore it after the features moving
   myCurrentSelection.clear();
 
-  QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = aStoredPrs.begin(), aLast = aStoredPrs.end();
+  QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = aStoredPrs.begin(),
+                                                aLast = aStoredPrs.end();
 
   CompositeFeaturePtr aSketch = activeSketch();
   for (; anIt != aLast; anIt++) {
@@ -1654,7 +1682,8 @@ void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
     std::set<AttributePtr> aSelectedAttributes;
     std::set<ResultPtr> aSelectedResults;
     if (myCurrentSelection.find(aFeature) != myCurrentSelection.end()) {
-      std::pair<std::set<AttributePtr>, std::set<ResultPtr> > aPair = myCurrentSelection.find(aFeature).value();
+      std::pair<std::set<AttributePtr>, std::set<ResultPtr> > aPair =
+        myCurrentSelection.find(aFeature).value();
       aSelectedAttributes = aPair.first;
       aSelectedResults = aPair.second;
     }
@@ -1689,12 +1718,12 @@ void PartSet_SketcherMgr::restoreSelection()
   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
                                         aSLast = myCurrentSelection.end();
   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
+  anOwnersToSelect.Clear();
   for (; aSIt != aSLast; aSIt++) {
-    anOwnersToSelect.Clear();
     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
                         anOwnersToSelect);
-    aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
   }
+  aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
 }
 
 void PartSet_SketcherMgr::onShowConstraintsToggle(int theType, bool theState)
@@ -1766,7 +1795,8 @@ void PartSet_SketcherMgr::updateSelectionPriority(ObjectPtr theObject,
       // 3. External objects (violet color)
       // 4. Auxiliary segments (dotted)
       // StdSelect_BRepSelectionTool::Load uses priority calculating:
-      // Standard_Integer aPriority = (thePriority == -1) ? GetStandardPriority (theShape, theType) : thePriority;
+      // Standard_Integer aPriority =
+      // (thePriority == -1) ? GetStandardPriority (theShape, theType) : thePriority;
       // Priority of Vertex is 8, edge(segment) is 7.
       // It might be not corrected as provides the condition above.
       bool isExternal = aSPFeature->isExternal();