Salome HOME
#1119 Confirmation box for deleting parts
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
index d9d19a2cfa03cdf025d189c8e8f8cf97a7cb3821..936c30afcf40d0f48135c87027979881a1a2e5c6 100755 (executable)
 #include <SketchPlugin_ConstraintFillet.h>
 #include <SketchPlugin_ConstraintMirror.h>
 #include <SketchPlugin_ConstraintAngle.h>
+#include <SketchPlugin_ConstraintCollinear.h>
+#include <SketchPlugin_ConstraintMiddle.h>
 #include <SketchPlugin_MultiRotation.h>
 #include <SketchPlugin_MultiTranslation.h>
+#include <SketchPlugin_IntersectionPoint.h>
 
 #include <SketcherPrs_Tools.h>
 
@@ -77,6 +80,7 @@
 #include <QMouseEvent>
 #include <QApplication>
 #include <QCursor>
+#include <QMessageBox>
 
 //#define DEBUG_DO_NOT_BY_ENTER
 
@@ -396,7 +400,7 @@ void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseE
       myIsDragging = true;
       get2dPoint(theWnd, theEvent, myCurrentPoint);
       myDragDone = false;
-      // TODO: Has to be uncommented when SALOME patch on draw mode become avialable
+
       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
       launchEditing();
       if (aFeature.get() != NULL) {
@@ -417,7 +421,7 @@ void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseE
       myIsDragging = true;
       get2dPoint(theWnd, theEvent, myCurrentPoint);
       myDragDone = false;
-      // TODO: Has to be uncommented when SALOME patch on draw mode become avialable
+
       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
 
       // this is temporary commented in order to avoid the following wrong case:
@@ -463,9 +467,8 @@ void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouse
       }
     }
   }
-      // TODO: Has to be uncommented when SALOME patch on draw mode become avialable
+
   aWorkshop->viewer()->enableDrawMode(myPreviousDrawModeEnabled);
-  //aWorkshop->viewer()->enableSelection(myPreviousDrawModeEnabled);
   myIsDragging = false;
 }
 
@@ -507,10 +510,6 @@ void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEve
     // 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.
 
-    // TODO: Has to be commented out when SALOME patch on draw mode become avialable
-    //ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
-    //aViewer->enableSelection(false);
-
     ModuleBase_Operation* aCurrentOperation = getCurrentOperation();
     if (!aCurrentOperation)
       return;
@@ -735,6 +734,10 @@ const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
     aIds << SketchPlugin_Arc::ID().c_str();
     aIds << SketchPlugin_Circle::ID().c_str();
     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
+    aIds << SketchPlugin_IntersectionPoint::ID().c_str();
+    // TODO
+    // SketchRectangle is a python feature, so its ID is passed just as a string
+    aIds << "SketchRectangle";
     aIds.append(constraintsIdList());
   }
   return aIds;
@@ -759,6 +762,8 @@ const QStringList& PartSet_SketcherMgr::constraintsIdList()
     aIds << SketchPlugin_ConstraintAngle::ID().c_str();
     aIds << SketchPlugin_MultiRotation::ID().c_str();
     aIds << SketchPlugin_MultiTranslation::ID().c_str();
+    aIds << SketchPlugin_ConstraintCollinear::ID().c_str();
+    aIds << SketchPlugin_ConstraintMiddle::ID().c_str();
   }
   return aIds;
 }
@@ -859,7 +864,6 @@ void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
 
   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
-
   bool aHasPlane = false;
   std::shared_ptr<GeomAPI_Pln> aPln;
   if (aFOperation->isEditOperation()) {
@@ -982,6 +986,14 @@ void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
   }
 }
 
+void PartSet_SketcherMgr::activatePlaneFilter(const bool& toActivate)
+{
+  if (toActivate)
+    myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
+  else
+    myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
+}
+
 void PartSet_SketcherMgr::operationActivatedByPreselection()
 {
   ModuleBase_Operation* anOperation = getCurrentOperation();
@@ -1165,6 +1177,84 @@ bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
   return aCanDisplay;
 }
 
+void PartSet_SketcherMgr::processHiddenObject(const std::list<ObjectPtr>& theObjects)
+{
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                                           (getCurrentOperation());
+  if (aFOperation && myCurrentSketch.get()) {
+    // find results of the current operation
+    // these results should not be proposed to be deleted
+    FeaturePtr anOperationFeature = aFOperation->feature();
+    std::list<ResultPtr> anOperationResultList = anOperationFeature->results();
+    std::set<ResultPtr> anOperationResults;
+    std::list<ResultPtr>::const_iterator aRIt = anOperationResultList.begin(),
+                                        aRLast = anOperationResultList.end();
+    for (; aRIt != aRLast; aRIt++)
+      anOperationResults.insert(*aRIt);
+
+    std::set<FeaturePtr> anObjectsToBeDeleted;
+    QStringList anObjectsToBeDeletedNames;
+    std::list<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+    for (; anIt != aLast; anIt++) {
+      ObjectPtr anObject = *anIt;
+      bool aCanErase = true;
+      // when the sketch operation is active, results of sketch sub-feature can not be hidden
+      ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
+      // the result is found between current feature results
+      if (anOperationResults.find(aResult) != anOperationResults.end())
+        continue;
+
+      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;
+          }
+        }
+      }
+      if (!aCanErase) {
+        FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+        if (aFeature.get() && anObjectsToBeDeleted.find(aFeature) == anObjectsToBeDeleted.end()) {
+          anObjectsToBeDeleted.insert(aFeature);
+          anObjectsToBeDeletedNames.append(aFeature->name().c_str());
+        }
+      }
+    }
+    if (!anObjectsToBeDeleted.empty()) {
+      QString aFeatureNames = anObjectsToBeDeletedNames.join(", ");
+      QString aMessage = tr("The following features have incorrect presentation and \
+will be hidden: %1. Would you like to delete them?")
+                         .arg(aFeatureNames);
+      int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Features hide"),
+                                           aMessage, QMessageBox::Ok | QMessageBox::Cancel,
+                                           QMessageBox::Cancel);
+      if (anAnswer == QMessageBox::Ok) {
+        QObjectPtrList anObjects;
+        std::set<FeaturePtr>::const_iterator anIt = anObjectsToBeDeleted.begin(),
+                                             aLast = anObjectsToBeDeleted.end();
+        for (; anIt != aLast; anIt++)
+          anObjects.append(*anIt);
+        SessionPtr aMgr = ModelAPI_Session::get();
+        DocumentPtr aDoc = aMgr->activeDocument();
+        bool aIsOp = aMgr->isOperation();
+        if (!aIsOp)
+          aMgr->startOperation();
+        workshop()->deleteFeatures(anObjects);
+        //static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
+        //static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
+        //Events_Loop::loop()->flush(aDeletedEvent);
+        //Events_Loop::loop()->flush(aRedispEvent);
+
+        if (!aIsOp)
+          aMgr->finishOperation();
+      }
+    }
+  }
+}
+
 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
 {
   bool aCanDisplay = myIsMouseOverWindow;
@@ -1490,12 +1580,15 @@ void PartSet_SketcherMgr::onShowConstraintsToggle(bool theState, int theType)
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
 }
 
-XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
+XGUI_Workshop* PartSet_SketcherMgr::workshop() const
 {
   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  return aConnector->workshop();
+}
 
-  return aWorkshop->operationMgr();
+XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
+{
+  return workshop()->operationMgr();
 }