Salome HOME
Issue #2420 Invalid sketch after creation second circle (reentrant operation) Jenkins...
[modules/shaper.git] / src / PartSet / PartSet_SketcherReentrantMgr.cpp
index b9c92f940c6a08d417f4ea475cb1d873f8698882..839da3fa082c2d3a161d055e2527272ab2e100ee 100644 (file)
@@ -1,6 +1,25 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "PartSet_SketcherReentrantMgr.h"
+#include "PartSet_ExternalObjectsMgr.h"
 #include "PartSet_Module.h"
 #include "PartSet_SketcherMgr.h"
 #include "PartSet_WidgetPoint2d.h"
 #include "ModelAPI_Session.h"
 #include "ModelAPI_AttributeString.h"
 #include "ModelAPI_AttributeRefAttr.h"
+#include "ModelAPI_AttributeReference.h"
 #include "ModelAPI_EventReentrantMessage.h"
 
 #include "GeomDataAPI_Point2D.h"
 
 #include <ModuleBase_IPropertyPanel.h>
+#include <ModuleBase_ISelectionActivate.h>
 #include <ModuleBase_OperationFeature.h>
 #include <ModuleBase_ModelWidget.h>
 #include <ModuleBase_ViewerPrs.h>
@@ -23,6 +44,7 @@
 #include <ModuleBase_OperationDescription.h>
 #include "ModuleBase_ToolBox.h"
 #include "ModuleBase_ISelection.h"
+#include "ModuleBase_ISelectionActivate.h"
 
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_Line.h>
@@ -30,6 +52,7 @@
 #include <SketchPlugin_MacroCircle.h>
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Trim.h>
+#include <SketchPlugin_Split.h>
 
 #include <XGUI_Workshop.h>
 #include <XGUI_ModuleConnector.h>
@@ -117,13 +140,13 @@ void PartSet_SketcherReentrantMgr::operationStarted(ModuleBase_Operation* theOpe
   if (!isActiveMgr())
     return;
 
-  if (myPreviousFeature.get() && myRestartingMode == RM_LastFeatureUsed) {
-    ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
-                                                                myWorkshop->currentOperation());
-    CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
-    if (myPreviousFeature.get() && myPreviousFeature->data()->isValid()) // it is not removed
-      copyReetntrantAttributes(myPreviousFeature, aCurrentOperation->feature(), aSketch);
-  }
+  //if (myPreviousFeature.get() && myRestartingMode == RM_LastFeatureUsed) {
+    //ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
+    //                                                            myWorkshop->currentOperation());
+    //CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
+    //if (myPreviousFeature.get() && myPreviousFeature->data()->isValid()) // it is not removed
+      //copyReetntrantAttributes(myPreviousFeature, aCurrentOperation->feature(), aSketch);
+  //}
   resetFlags();
 }
 
@@ -152,17 +175,16 @@ bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* the
       ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
 
       FeaturePtr aCurrentFeature = aFOperation->feature();
-      bool isLineFeature = false, isArcFeature = false;
+      bool isLineFeature = false, isReentrantArcFeature = false;
       std::string anAttributeOnStart;
       if (aCurrentFeature->getKind() == SketchPlugin_Line::ID()) {
         anAttributeOnStart = SketchPlugin_Line::START_ID();
         isLineFeature = anActiveWidget->attributeID() == anAttributeOnStart;
       }
       else if (isTangentArc(aFOperation, module()->sketchMgr()->activeSketch())) {
-        anAttributeOnStart = SketchPlugin_MacroArc::TANGENT_POINT_ID();
-        isArcFeature = anActiveWidget->attributeID() == anAttributeOnStart;
+        isReentrantArcFeature = true;
       }
-      bool aCanBeActivatedByMove = isLineFeature || isArcFeature;
+      bool aCanBeActivatedByMove = isLineFeature || isReentrantArcFeature;
       if (aCanBeActivatedByMove) {
         /// before restarting of operation we need to clear selection, as it may take part in
         /// new feature creation, e.g. tangent arc. But it is not necessary as it was processed
@@ -174,9 +196,8 @@ bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* the
         myPreviousFeature = FeaturePtr();
 
         anActiveWidget = module()->activeWidget();
-        aCurrentFeature = anActiveWidget->feature();
         aProcessed = true;
-        if (anActiveWidget->attributeID() == anAttributeOnStart) {
+        if (anActiveWidget && anActiveWidget->attributeID() == anAttributeOnStart) {
           // it was not deactivated by preselection processing
           aPanel->activateNextWidget(anActiveWidget);
         }
@@ -234,10 +255,32 @@ bool PartSet_SketcherReentrantMgr::processMouseReleased(ModuleBase_IViewWindow*
 
       myClickedSketchPoint = PartSet_Tools::getPnt2d(theEvent, theWindow,
                                                      module()->sketchMgr()->activeSketch());
-      if (!aPreSelected.empty())
-        module()->getGeomSelection(aPreSelected.first(), mySelectedObject, mySelectedAttribute);
+      FeaturePtr anExternalCreatedFeature;
+      if (!aPreSelected.empty()) {
+        ModuleBase_ViewerPrsPtr aValue = aPreSelected.first();
+        module()->getGeomSelection(aValue, mySelectedObject, mySelectedAttribute);
+
+        PartSet_WidgetPoint2D* aPointWidget = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
+        if (aPointWidget) {
+          GeomShapePtr aShape;
+          aPointWidget->getGeomSelection_(aValue, mySelectedObject, aShape);
+          ObjectPtr anExternalObject =
+            aPointWidget->getExternalObjectMgr()->getExternalObjectValidated();
+          // if external object is during reentrant operation and is used as a parameter of feature
+          // it should be removed after the operation is restarted. (Circle feature, Projection)
+          if (anExternalObject.get())
+            anExternalCreatedFeature = ModelAPI_Feature::feature(anExternalObject);
+        }
+      }
 
       restartOperation();
+      // remove created external feature
+      if (anExternalCreatedFeature.get()) {
+        QObjectPtrList anObjects;
+        anObjects.append(anExternalCreatedFeature);
+        workshop()->deleteFeatures(anObjects);
+      }
+
       myClickedSketchPoint = std::shared_ptr<GeomAPI_Pnt2d>();
       mySelectedObject = ObjectPtr();
       mySelectedAttribute = AttributePtr();
@@ -263,7 +306,7 @@ bool PartSet_SketcherReentrantMgr::processMouseReleased(ModuleBase_IViewWindow*
             && !aSelectedPrs->object()->data()->isValid()) {
           // the selected object was removed diring restart, e.g. presentable macro feature
           // there are created objects to replace the object depending on created feature kind
-          aSelectedPrs = generatePreSelection();
+          aSelectedPrs = std::shared_ptr<ModuleBase_ViewerPrs>();
         }
         aMouseProcessor->setPreSelection(aSelectedPrs, theWindow, theEvent);
         //aPoint2DWdg->mouseReleased(theWindow, theEvent);
@@ -294,22 +337,24 @@ void PartSet_SketcherReentrantMgr::setReentrantPreSelection(
   aReentrantMessage->setClickedPoint(myClickedSketchPoint);
 }
 
-void PartSet_SketcherReentrantMgr::onWidgetActivated()
-{
-  if (!isActiveMgr())
-    return;
-  if (!myIsInternalEditOperation)
-    return;
-
-  PartSet_Module* aModule = module();
-  ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
-  ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
-  if (aFirstWidget != aPanel->activeWidget()) {
-    ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
-    if (aWSelector)
-      aWSelector->activateSelectionAndFilters(true);
-  }
-}
+//void PartSet_SketcherReentrantMgr::onWidgetActivated()
+//{
+//  if (!isActiveMgr())
+//    return;
+//  if (!myIsInternalEditOperation)
+//    return;
+//
+//  PartSet_Module* aModule = module();
+//  ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
+//  ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
+//  if (aFirstWidget != aPanel->activeWidget()) {
+//    ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>
+//      (aFirstWidget);
+//    if (aWSelector) {
+//      myWorkshop->selectionActivate()->updateSelectionModesAndFilters(aWSelector);
+//    }
+//  }
+//}
 
 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
 {
@@ -335,15 +380,20 @@ void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviou
     return;
 
   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
-    bool isStarted = false;
-    if (!module()->sketchMgr()->sketchSolverError()) {
-      if (myRestartingMode != RM_Forbided) {
-        myRestartingMode = RM_LastFeatureUsed;
-        isStarted = startInternalEdit(thePreviousAttributeID);
+    if (aFOperation->isNeedToBeAborted()) {
+      aFOperation->abort();
+    }
+    else {
+      bool isStarted = false;
+      if (!module()->sketchMgr()->sketchSolverError()) {
+        if (myRestartingMode != RM_Forbided) {
+          myRestartingMode = RM_LastFeatureUsed;
+          isStarted = startInternalEdit(thePreviousAttributeID);
+        }
       }
+      if (!isStarted)
+        aFOperation->commit();
     }
-    if (!isStarted)
-      aFOperation->commit();
   }
 }
 
@@ -460,6 +510,10 @@ bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePrevi
                                                      (myWorkshop->currentOperation());
 
   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
+    /// improvement to deselect automatically all eventual selected objects, when
+    // returning to the neutral point of the Sketcher or start internal edit
+    workshop()->selector()->clearSelection();
+
     aFOperation->setEditOperation(true/*, false*/);
     createInternalFeature();
 
@@ -471,7 +525,7 @@ bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePrevi
     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
 
     // activate selection filters of the first widget in the viewer
-    onWidgetActivated();
+    //onWidgetActivated();
 
     // activate the last active widget in the Property Panel
     if (!thePreviousAttributeID.empty()) {
@@ -492,6 +546,7 @@ bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePrevi
         if (aPreviousAttributeWidget) {
           if (!aPreviousAttributeWidget->isViewerSelector()) {
             aPreviousAttributeWidget->focusTo();
+            aPreviousAttributeWidget->emitFocusInWidget();
             aPreviousAttributeWidget->selectContent();
           }
           else {
@@ -508,7 +563,7 @@ bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePrevi
             QToolButton* anOkBtn =
               dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
             if (anOkBtn)
-              anOkBtn->setFocus(Qt::TabFocusReason);
+              ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::activateNextWidget");
           }
         }
       }
@@ -548,7 +603,7 @@ void PartSet_SketcherReentrantMgr::restartOperation()
 
       if (myInternalFeature.get())
         copyReetntrantAttributes(myInternalFeature, aFOperation->feature(),
-                                  module()->sketchMgr()->activeSketch());
+                                 module()->sketchMgr()->activeSketch());
 
       myNoMoreWidgetsAttribute = "";
       myIsFlagsBlocked = true;
@@ -625,13 +680,8 @@ void PartSet_SketcherReentrantMgr::deleteInternalFeature()
   std::cout << "PartSet_SketcherReentrantMgr::deleteInternalFeature: "
             << myInternalFeature->data()->name() << std::endl;
 #endif
-  if (myInternalActiveWidget) {
-    ModuleBase_WidgetSelector* aWSelector =
-      dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
-    if (aWSelector)
-      aWSelector->activateSelectionAndFilters(false);
+  if (myInternalActiveWidget)
     myInternalActiveWidget = 0;
-  }
   delete myInternalWidget;
   myInternalWidget = 0;
 
@@ -690,16 +740,16 @@ bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& th
     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
-    //ModuleBase_Tools::flushUpdated(theNewFeature);
-    aChanged = true;*/
+    //ModuleBase_Tools::flushUpdated(theNewFeature);*/
+    //aChanged = true;
   }
   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
     // set arc type
-    std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
+    /*std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
-      aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
+      aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());*/
     //// if the arc is tangent, set coincidence to end point of the previous arc
     //std::string anArcType = aSourceFeatureTypeAttr->value();
     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
@@ -720,42 +770,28 @@ bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& th
 
     //}
     //ModuleBase_Tools::flushUpdated(theNewFeature);
-    aChanged = true;
+    //aChanged = true;
   }
-  else if (aFeatureKind == SketchPlugin_Trim::ID()) {
-    /*std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
-                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                      theSourceFeature->data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
-    std::shared_ptr<ModelAPI_AttributeReference> aNRefSelectedAttr =
-                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                      theNewFeature->data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
-    aNRefSelectedAttr->setValue(aRefSelectedAttr->value());*/
-
+  else if (aFeatureKind == SketchPlugin_Trim::ID() ||
+           aFeatureKind == SketchPlugin_Split::ID()) {
+    std::string aPreviewObjectAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
+                SketchPlugin_Trim::PREVIEW_OBJECT(): SketchPlugin_Split::PREVIEW_OBJECT();
+    std::string aPreviewPointAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
+                SketchPlugin_Trim::PREVIEW_POINT(): SketchPlugin_Split::PREVIEW_POINT();
     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                      theSourceFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
+                      theSourceFeature->data()->attribute(aPreviewObjectAttribute));
     std::shared_ptr<ModelAPI_AttributeReference> aNRefPreviewAttr =
                         std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                        theNewFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
+                        theNewFeature->data()->attribute(aPreviewObjectAttribute));
     aNRefPreviewAttr->setValue(aRefPreviewAttr->value());
-
-    /*std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
-                      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                      theSourceFeature->data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
-    std::shared_ptr<GeomDataAPI_Point2D> aNPointSelectedAttr =
-                      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                      theNewFeature->data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
-    aNPointSelectedAttr->setValue(aPointSelectedAttr->x(), aPointSelectedAttr->y());
-    */
     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                      theSourceFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
+                      theSourceFeature->data()->attribute(aPreviewPointAttribute));
     std::shared_ptr<GeomDataAPI_Point2D> aNPointPreviewAttr =
                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                      theNewFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
+                      theNewFeature->data()->attribute(aPreviewPointAttribute));
     aNPointPreviewAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
-
-    aChanged = true;
   }
   return aChanged;
 }
@@ -777,13 +813,6 @@ bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperati
   return aTangentArc;
 }
 
-std::shared_ptr<ModuleBase_ViewerPrs> PartSet_SketcherReentrantMgr::generatePreSelection()
-{
-  std::shared_ptr<ModuleBase_ViewerPrs> aPrs;
-
-  return aPrs;
-}
-
 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
 {
   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();