Salome HOME
Merge Dev_2.1.0 with PythonAPI branch
[modules/shaper.git] / src / PartSet / PartSet_MenuMgr.cpp
index e453a552554d43bf271df38a0370c5203fbd917c..d2a2f4efec3e7a80509ad230b6df387ae52fbaca 100644 (file)
@@ -9,6 +9,8 @@
 #include "PartSet_SketcherMgr.h"
 #include "PartSet_Tools.h"
 
+#include <PartSetPlugin_Part.h>
+
 #include <GeomAPI_Pnt2d.h>
 #include <GeomDataAPI_Point2D.h>
 
 
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_Operation.h>
+#include <ModuleBase_OperationAction.h>
+#include <ModuleBase_OperationFeature.h>
 
 #include <XGUI_ModuleConnector.h>
 #include <XGUI_Workshop.h>
 #include <XGUI_Displayer.h>
+#include <XGUI_DataModel.h>
+#include <XGUI_OperationMgr.h>
 
 #include <Events_Loop.h>
 #include <ModelAPI_Events.h>
@@ -33,6 +39,7 @@
 
 #include <QAction>
 #include <QMenu>
+#include <QEvent>
 
 #include <TopoDS.hxx>
 #include <BRep_Tool.hxx>
@@ -72,10 +79,6 @@ void PartSet_MenuMgr::createActions()
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
   myActions["ACTIVATE_PART_CMD"] = aAction;
 
-  aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
-  connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
-  myActions["DEACTIVATE_PART_CMD"] = aAction;
-
   // Activate PartSet
   aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
@@ -84,6 +87,11 @@ void PartSet_MenuMgr::createActions()
   aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
   myActions["EDIT_CMD"] = aAction;
+
+  aAction = new QAction(QIcon(), tr("Select parent feature"), this);
+  aAction->setCheckable(false);
+  connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSelectParentFeature()));
+  myActions["SELECT_PARENT_CMD"] = aAction;
 }
 
 
@@ -97,65 +105,6 @@ void PartSet_MenuMgr::onAction(bool isChecked)
   }
 }
 
-/// Returns point of coincidence feature
-/// \param theFeature the coincidence feature
-/// \param theAttribute the attribute name
-std::shared_ptr<GeomAPI_Pnt2d> getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
-                                        const std::string& theAttribute)
-{
-  std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
-
-  if (!theFeature->data())
-    return std::shared_ptr<GeomAPI_Pnt2d>();
-
-  FeaturePtr aFeature;
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
-      ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
-  if (anAttr)
-    aFeature = ModelAPI_Feature::feature(anAttr->object());
-
-  if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
-
-  else if (anAttr->attr()) {
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
-  }
-  if (aPointAttr.get() != NULL)
-    return aPointAttr->pnt();
-  return std::shared_ptr<GeomAPI_Pnt2d>();
-}
-
-/// Returns list of features connected in a councedence feature point
-/// \param theStartCoin the coincidence feature
-/// \param theList a list which collects lines features
-/// \param theAttr the attribute name
-void findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList, std::string theAttr)
-{
-  AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
-  FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
-  if (!theList.contains(aObj)) {
-    std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(theStartCoin, theAttr);
-    if (aOrig.get() == NULL)
-      return;
-    theList.append(aObj);
-    const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
-    std::set<AttributePtr>::const_iterator aIt;
-    for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
-      std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
-      FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
-      if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
-        std::shared_ptr<GeomAPI_Pnt2d> aPnt = getPoint(aConstrFeature, theAttr);
-        if (aPnt.get() && aOrig->isEqual(aPnt)) {
-          findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
-          findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
-        }
-      }
-    }
-  }
-}
-
-
 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
 {
   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
@@ -170,7 +119,7 @@ bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*
   bool hasAttribute = false;
   bool hasFeature = false;
 
-  QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected();
+  QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
   TopoDS_Shape aShape;
   ResultPtr aResult;
   FeaturePtr aFeature;
@@ -201,32 +150,14 @@ bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*
         // Find coincident in these coordinates
         ObjectPtr aObj = aPrsList.first().object();
         FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
-        const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
-        std::set<AttributePtr>::const_iterator aIt;
-        FeaturePtr aCoincident;
-        for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
-          std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
-          FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
-          if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
-            std::shared_ptr<GeomAPI_Pnt2d> a2dPnt = 
-              getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
-            if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) { 
-              aCoincident = aConstrFeature;
-              break;
-            } else {
-              a2dPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B());
-              if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) { 
-                aCoincident = aConstrFeature;
-                break;
-              }
-            }
-          }
-        }
+        FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
         // If we have coincidence then add Detach menu
         if (aCoincident.get() != NULL) {
           mySelectedFeature = aCoincident;
-          findCoincidences(mySelectedFeature, myCoinsideLines, SketchPlugin_ConstraintCoincidence::ENTITY_A());
-          findCoincidences(mySelectedFeature, myCoinsideLines, SketchPlugin_ConstraintCoincidence::ENTITY_B());
+          PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
+                                          SketchPlugin_ConstraintCoincidence::ENTITY_A());
+          PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
+                                          SketchPlugin_ConstraintCoincidence::ENTITY_B());
           if (myCoinsideLines.size() > 0) {
             aIsDetach = true;
             QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
@@ -259,6 +190,23 @@ bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*
   return true;
 }
 
+void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
+{
+  ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
+
+  bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
+                        PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
+  if (isActiveSketch) {
+    theStdActions["WIREFRAME_CMD"]->setEnabled(false);
+    theStdActions["SHADING_CMD"]->setEnabled(false);
+    theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
+    theStdActions["SHOW_CMD"]->setEnabled(false);
+    theStdActions["HIDE_CMD"]->setEnabled(false);
+    theStdActions["HIDEALL_CMD"]->setEnabled(false);
+  }
+}
+
+
 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
 {
   if (myPrevId != -1) {
@@ -288,57 +236,66 @@ QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theU
 }
 
 
-void PartSet_MenuMgr::onLineDetach(QAction* theAction)
+void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList, 
+  std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
+  QObjectPtrList& theOutList)
 {
-  int aId = theAction->data().toInt();
-  FeaturePtr aLine = myCoinsideLines.at(aId);
-  std::shared_ptr<GeomAPI_Pnt2d> aOrig = getPoint(mySelectedFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
-  if (aOrig.get() == NULL)
-    aOrig = getPoint(mySelectedFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B());
-  
-  gp_Pnt aOr = aOrig->impl<gp_Pnt>();
-  const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
-
-  QObjectPtrList aToDelFeatures;
   std::set<AttributePtr>::const_iterator aIt;
-  // Find all coincedences corresponded to the selected line in the selected point
-  for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
+  for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
-      std::shared_ptr<GeomAPI_Pnt2d> aPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
-      if (aPnt.get() == NULL)
-        aPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B());
+      std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
       if (aPnt.get() == NULL)
         return;
       gp_Pnt aP = aPnt->impl<gp_Pnt>();
-      if (aOrig->isEqual(aPnt)) {
-        aToDelFeatures.append(aConstrFeature);
-      } else {
-        aPnt = getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B());
-        aP = aPnt->impl<gp_Pnt>();
-        if (aOrig->isEqual(aPnt)) {
-          aToDelFeatures.append(aConstrFeature);
-          break;
-        }
-      }
+      if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
+        theOutList.append(aConstrFeature);
+      } 
     }
   }
+}
+
+void PartSet_MenuMgr::onLineDetach(QAction* theAction)
+{
+  int aId = theAction->data().toInt();
+  FeaturePtr aLine = myCoinsideLines.at(aId);
+  std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
+  if (!aOrig.get())
+    return;
+  
+  const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
+
+  QObjectPtrList aToDelFeatures;
+
+  addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
+
+  const std::list<ResultPtr>& aResults = aLine->results();
+  std::list<ResultPtr>::const_iterator aResIt;
+  for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
+    ResultPtr aResult = (*aResIt);
+    const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
+    addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
+  }
   if (aToDelFeatures.size() > 0) {
     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
     XGUI_Workshop* aWorkshop = aConnector->workshop();
     ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
-    if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
-      anOperation->abort();
-
-    SessionPtr aMgr = ModelAPI_Session::get();
-    std::set<FeaturePtr> anIgnoredFeatures;
-    anIgnoredFeatures.insert(myModule->sketchMgr()->activeSketch());
 
-    QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
-    aMgr->startOperation(aName.toStdString());
-    aWorkshop->deleteFeatures(aToDelFeatures, anIgnoredFeatures);
-    aMgr->finishOperation();
+    ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
+                                   tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
+    bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
+    XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
+    // the active nested sketch operation should be aborted unconditionally
+    // the Delete action should be additionally granted for the Sketch operation
+    // in order to do not abort/commit it
+    if (!anOpMgr->canStartOperation(tr("Detach")))
+      return; // the objects are processed but can not be deleted
+
+    anOpMgr->startOperation(anOpAction);
+    aWorkshop->deleteFeatures(aToDelFeatures);
+    
+    anOpMgr->commitOperation();
   }
   myCoinsideLines.clear();
 }
@@ -368,8 +325,11 @@ void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
   bool isUseTransaction = false;
   // 1. change auxiliary type of a created feature
   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
-    PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
-    anObjects.append(anOperation->feature());
+      PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
+      ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                               (anOperation);
+      if (aFOperation)
+        anObjects.append(aFOperation->feature());
   }
   else {
     isUseTransaction = true;
@@ -379,11 +339,18 @@ void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
   }
 
   QAction* anAction = action("AUXILIARY_CMD");
-  SessionPtr aMgr = ModelAPI_Session::get();
+  //SessionPtr aMgr = ModelAPI_Session::get();
+  ModuleBase_OperationAction* anOpAction = 0;
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
+  XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
   if (isUseTransaction) {
-    if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
-      anOperation->abort();
-    aMgr->startOperation(anAction->text().toStdString());
+    anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
+    bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
+
+    if (!anOpMgr->canStartOperation(anOpAction->id()))
+      return; // the objects are processed but can not be deleted
+
+    anOpMgr->startOperation(anOpAction);
   }
   myModule->sketchMgr()->storeSelection();
 
@@ -405,12 +372,8 @@ void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
       }
     }
   }
-  if (isUseTransaction) {
-    aMgr->finishOperation();
-    XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
-    XGUI_Workshop* aWorkshop = aConnector->workshop();
-    aWorkshop->updateCommandStatus();
-  }
+  if (isUseTransaction)
+    anOpMgr->commitOperation();
 
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
   myModule->sketchMgr()->restoreSelection();
@@ -430,7 +393,9 @@ bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
   // 1. change auxiliary type of a created feature
   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
-    anObjects.append(anOperation->feature());
+    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
+    if (aFOperation)
+      anObjects.append(aFOperation->feature());
   }
   else {
     /// The operation should not be aborted here, because the method does not changed
@@ -468,19 +433,47 @@ bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
 
 void PartSet_MenuMgr::onActivatePart(bool)
 {
+  if (myModule->workshop()->currentOperation())
+    return;
   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
   if (aObjects.size() > 0) {
-    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
-    if (aPart) {
-      aPart->activate();
+    ObjectPtr aObj = aObjects.first();
+    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+    if (!aPart.get()) {
+      FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+      if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
+        aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
+      }
     }
+    if (aPart.get())
+      aPart->activate();
   }
 }
 
 void PartSet_MenuMgr::onActivatePartSet(bool)
+{
+  if (myModule->workshop()->currentOperation())
+    return;
+  activatePartSet();
+}
+
+void PartSet_MenuMgr::activatePartSet() const
 {
   SessionPtr aMgr = ModelAPI_Session::get();
+  bool isNewTransaction = !aMgr->isOperation();
+  // activation may cause changes in current features in document, so it must be in transaction
+  if (isNewTransaction) aMgr->startOperation("Activation");
   aMgr->setActiveDocument(aMgr->moduleDocument());
+  if (isNewTransaction) aMgr->finishOperation();
+}
+
+void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
+                                          QStringList& theIds) const
+{
+  if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
+    theIds.append(tr("Detach"));
+    theIds.append(tr("Auxiliary"));
+  }
 }
 
 void PartSet_MenuMgr::onEdit(bool)
@@ -497,3 +490,30 @@ void PartSet_MenuMgr::onEdit(bool)
   if (aFeature.get() != NULL)
     myModule->editFeature(aFeature);
 }
+
+void PartSet_MenuMgr::onSelectParentFeature()
+{
+  QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
+  if (aObjects.size() != 1)
+    return;
+
+  SessionPtr aMgr = ModelAPI_Session::get();
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
+  if( !aResult.get() )
+    return;
+
+  FeaturePtr aParentFeature = aResult->document()->feature( aResult );
+  QObjectPtrList aSelection;
+  aSelection.append( aParentFeature );
+  myModule->workshop()->selection()->setSelectedObjects( aSelection );
+}
+
+bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
+{
+  if (theEvent->type() == QEvent::MouseButtonDblClick) {
+    SessionPtr aMgr = ModelAPI_Session::get();
+    if (aMgr->activeDocument() != aMgr->moduleDocument())
+      activatePartSet();
+  }
+  return QObject::eventFilter(theObj, theEvent);
+}
\ No newline at end of file