Salome HOME
Issue #1037 : do not find model indices for the same result. If selection mode is...
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
index 7b570f9cc678e355d46742099997b519f4a70d1e..295875c6fc92a0779e92501e1c11f6cab4c617e8 100644 (file)
@@ -11,6 +11,7 @@
 #include "XGUI_DataModel.h"
 #include "XGUI_OperationMgr.h"
 #include "XGUI_Tools.h"
+#include "XGUI_ActionsMgr.h"
 
 #ifndef HAVE_SALOME
 #include <AppElements_MainWindow.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
 
+#include <Config_DataModelReader.h>
+
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_OperationAction.h>
+#include <ModuleBase_ViewerPrs.h>
 
 #include <QAction>
+#include <QActionGroup>
 #include <QContextMenuEvent>
 #include <QMenu>
 #include <QMdiArea>
 #include <QMainWindow>
+#include <QModelIndex>
 
 
 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
@@ -62,16 +68,18 @@ void XGUI_ContextMenuMgr::createActions()
   aDesktop->addAction(aAction);
 
   addAction("DELETE_CMD", aAction);
-  aAction->setShortcut(Qt::Key_Delete);
   aAction->setShortcutContext(Qt::ApplicationShortcut);
 
   aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
   addAction("RENAME_CMD", aAction);
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
 
-  aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
+  aAction = new QAction(QIcon(":pictures/move.png"), XGUI_Workshop::MOVE_TO_END_COMMAND, this);
   addAction("MOVE_CMD", aAction);
 
+  aAction = new QAction(QIcon(":pictures/clean_history.png"), tr("Clean history"), this);
+  addAction("CLEAN_HISTORY_CMD", aAction);
+
   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
   addAction("COLOR_CMD", aAction);
 
@@ -96,6 +104,36 @@ void XGUI_ContextMenuMgr::createActions()
   mySeparator = new QAction(this);
   mySeparator->setSeparator(true);
 
+  mySelectActions = new QActionGroup(this);
+  mySelectActions->setExclusive(true);
+
+  aAction = new QAction(QIcon(":pictures/vertex.png"), tr("Vertices"), this);
+  aAction->setCheckable(true);
+  addAction("SELECT_VERTEX_CMD", aAction);
+  mySelectActions->addAction(aAction);
+
+  aAction = new QAction(QIcon(":pictures/edge.png"), tr("Edges"), this);
+  aAction->setCheckable(true);
+  addAction("SELECT_EDGE_CMD", aAction);
+  mySelectActions->addAction(aAction);
+
+  aAction = new QAction(QIcon(":pictures/face.png"), tr("Faces"), this);
+  aAction->setCheckable(true);
+  addAction("SELECT_FACE_CMD", aAction);
+  mySelectActions->addAction(aAction);
+
+  aAction = new QAction(QIcon(":pictures/result.png"), tr("Result"), this);
+  aAction->setCheckable(true);
+  addAction("SELECT_RESULT_CMD", aAction);
+  mySelectActions->addAction(aAction);
+
+  aAction->setChecked(true);
+
+  aAction = new QAction(QIcon(":pictures/find_result.png"), tr("Select results"), this);
+  addAction("SHOW_RESULTS_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/find_result.png"), tr("Select parent feature"), this);
+  addAction("SHOW_FEATURE_CMD", aAction);
 
   buildObjBrowserMenu();
   buildViewerMenu();
@@ -178,9 +216,9 @@ void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
     bool hasResult = false;
     bool hasFeature = false;
     bool hasParameter = false;
-    bool hasSubFeature = false;
-    ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
-
+    bool hasCompositeOwner = false;
+    ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
+                                   hasCompositeOwner);
     //Process Feature
     if (aSelected == 1) {
       ObjectPtr aObject = aObjects.first();
@@ -218,7 +256,9 @@ void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
         if( aMgr->activeDocument() == aObject->document() )
         {
           action("RENAME_CMD")->setEnabled(true);
-          action("DELETE_CMD")->setEnabled(true);
+          action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
+          action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
+                                                  (hasFeature || hasParameter));
         }
       }
     } else {
@@ -237,10 +277,15 @@ void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
         allActive = false;
         break;
       }
-    if (!hasSubFeature && allActive ) {
+    if (!hasCompositeOwner && allActive ) {
       if (hasFeature || hasParameter)
         action("DELETE_CMD")->setEnabled(true);
     }
+    if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
+      action("CLEAN_HISTORY_CMD")->setEnabled(true);
+
+    action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
+    action("SHOW_FEATURE_CMD")->setEnabled(hasResult);
   }
 
   // Show/Hide command has to be disabled for objects from non active document
@@ -275,14 +320,15 @@ void XGUI_ContextMenuMgr::updateViewerMenu()
     aAction->setEnabled(false);
 
   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
-  QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
+  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+  QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
   if (aPrsList.size() > 0) {
     bool isVisible = false;
     bool isShading = false;
     bool canBeShaded = false;
     ObjectPtr aObject;
-    foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
-      aObject = aPrs.object();
+    foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
+      aObject = aPrs->object();
       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
       if (aRes && aRes->isDisplayed()) {
         isVisible = true;
@@ -293,7 +339,6 @@ void XGUI_ContextMenuMgr::updateViewerMenu()
     }
     if (isVisible) {
       if (canBeShaded) {
-        XGUI_Displayer* aDisplayer = myWorkshop->displayer();
         XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
         if (aMode != XGUI_Displayer::NoMode) {
           action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
@@ -310,14 +355,40 @@ void XGUI_ContextMenuMgr::updateViewerMenu()
   }
   if (myWorkshop->displayer()->objectsCount() > 0)
     action("HIDEALL_CMD")->setEnabled(true);
-  if (myWorkshop->canChangeColor())
-    action("COLOR_CMD")->setEnabled(true);
 
-  action("DELETE_CMD")->setEnabled(true);
+  // Update selection menu
+  QIntList aModes = aDisplayer->activeSelectionModes();
+  if (aModes.count() <= 1) {
+    action("SELECT_VERTEX_CMD")->setEnabled(true);
+    action("SELECT_EDGE_CMD")->setEnabled(true);
+    action("SELECT_FACE_CMD")->setEnabled(true);
+    action("SELECT_RESULT_CMD")->setEnabled(true);
+    if (aModes.count() == 1) {
+      switch (aModes.first()) {
+      case TopAbs_VERTEX: 
+        action("SELECT_VERTEX_CMD")->setChecked(true);
+        break;
+      case TopAbs_EDGE: 
+        action("SELECT_EDGE_CMD")->setChecked(true);
+        break;
+      case TopAbs_FACE:
+        action("SELECT_FACE_CMD")->setChecked(true);
+        break;
+      default:
+        action("SELECT_RESULT_CMD")->setChecked(true);
+      }
+    } else 
+      action("SELECT_RESULT_CMD")->setChecked(true);
+  }
 
   ModuleBase_IModule* aModule = myWorkshop->module();
   if (aModule)
     aModule->updateViewerMenu(myActions);
+
+  if (myWorkshop->canChangeColor())
+    action("COLOR_CMD")->setEnabled(true);
+
+  action("DELETE_CMD")->setEnabled(true);
 }
 
 void XGUI_ContextMenuMgr::connectObjectBrowser()
@@ -339,27 +410,31 @@ void XGUI_ContextMenuMgr::buildObjBrowserMenu()
   aSeparator->setSeparator(true);
 
   QActionsList aList;
-  
+
   // Result construction menu
   aList.append(action("SHOW_CMD"));
   aList.append(action("HIDE_CMD"));
   aList.append(action("SHOW_ONLY_CMD"));
-  aList.append(action("COLOR_CMD"));
   aList.append(mySeparator);
   aList.append(action("RENAME_CMD"));
+  aList.append(action("COLOR_CMD"));
+  aList.append(action("SHOW_FEATURE_CMD"));
   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
+
   //-------------------------------------
   // Result body menu
   aList.clear();
   aList.append(action("WIREFRAME_CMD"));
   aList.append(action("SHADING_CMD"));
-  aList.append(action("COLOR_CMD"));
-  aList.append(mySeparator);
+  aList.append(mySeparator); // this separator is not shown as this action is added after show only
+  // qt list container contains only one instance of the same action
   aList.append(action("SHOW_CMD"));
   aList.append(action("HIDE_CMD"));
   aList.append(action("SHOW_ONLY_CMD"));
   aList.append(mySeparator);
   aList.append(action("RENAME_CMD"));
+  aList.append(action("COLOR_CMD"));
+  aList.append(action("SHOW_FEATURE_CMD"));
   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
   // Group menu
   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
@@ -368,16 +443,19 @@ void XGUI_ContextMenuMgr::buildObjBrowserMenu()
   //-------------------------------------
   // Feature menu
   aList.clear();
-  aList.append(action("DELETE_CMD"));
+  aList.append(action("RENAME_CMD"));
+  aList.append(action("SHOW_RESULTS_CMD"));
   aList.append(action("MOVE_CMD"));
   aList.append(mySeparator);
-  aList.append(action("RENAME_CMD"));
+  aList.append(action("CLEAN_HISTORY_CMD"));
+  aList.append(action("DELETE_CMD"));
   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
 
   aList.clear();
-  aList.append(action("DELETE_CMD"));
-  aList.append(mySeparator);
   aList.append(action("RENAME_CMD"));
+  aList.append(mySeparator);
+  aList.append(action("CLEAN_HISTORY_CMD"));
+  aList.append(action("DELETE_CMD"));
   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
   //-------------------------------------
 }
@@ -388,7 +466,7 @@ void XGUI_ContextMenuMgr::buildViewerMenu()
   // Result construction menu
   aList.append(action("HIDE_CMD"));
   aList.append(action("SHOW_ONLY_CMD"));
-  aList.append(action("HIDEALL_CMD"));
+  aList.append(mySeparator);
   aList.append(action("COLOR_CMD"));
   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
   // Result part menu
@@ -398,21 +476,26 @@ void XGUI_ContextMenuMgr::buildViewerMenu()
   aList.clear();
   aList.append(action("WIREFRAME_CMD"));
   aList.append(action("SHADING_CMD"));
-  aList.append(action("COLOR_CMD"));
   aList.append(mySeparator);
   aList.append(action("HIDE_CMD"));
   aList.append(action("SHOW_ONLY_CMD"));
-  aList.append(action("HIDEALL_CMD"));
+  aList.append(mySeparator);
+  aList.append(action("COLOR_CMD"));
   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
   // Group menu
   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
   //-------------------------------------
-
 }
 
 
 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
 {
+  ModuleBase_IModule* aModule = myWorkshop->module();
+  if (aModule) {
+    theMenu->addSeparator();
+    aModule->addObjectBrowserMenu(theMenu);
+  }
+
   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
   int aSelected = aObjects.size();
@@ -423,51 +506,82 @@ void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
     if (myObjBrowserMenus.contains(aName))
       aActions = myObjBrowserMenus[aName];
   } else if (aSelected > 1) {
-      aActions.append(action("SHADING_CMD"));
       aActions.append(action("WIREFRAME_CMD"));
+      aActions.append(action("SHADING_CMD"));
       aActions.append(mySeparator);
       aActions.append(action("SHOW_CMD"));
       aActions.append(action("HIDE_CMD"));
       aActions.append(action("SHOW_ONLY_CMD"));
       aActions.append(mySeparator);
-      aActions.append(action("DELETE_CMD"));
       //aActions.append(action("MOVE_CMD"));
       aActions.append(action("COLOR_CMD"));
+      aActions.append(action("CLEAN_HISTORY_CMD"));
+      aActions.append(action("DELETE_CMD"));
   }
   theMenu->addActions(aActions);
+  addFeatures(theMenu);
 
-  ModuleBase_IModule* aModule = myWorkshop->module();
-  if (aModule) {
-    theMenu->addSeparator();
-    aModule->addObjectBrowserMenu(theMenu);
-  }
-  theMenu->addSeparator();
-  theMenu->addActions(myWorkshop->objectBrowser()->actions());
+  // It is commented out because Object Browser does not have actions
+  //theMenu->addSeparator();
+  //theMenu->addActions(myWorkshop->objectBrowser()->actions());
 }
 
 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
 {
-  ModuleBase_IModule* aModule = myWorkshop->module();
-  if (aModule) {
-    if (aModule->addViewerMenu(theMenu, myActions))
-      theMenu->addSeparator();
-  }
   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
-  QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
+  QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
   int aSelected = aPrsList.size();
   QActionsList aActions;
+
+  // Create selection menu
+  XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
+  QIntList aModes;
+  myWorkshop->module()->activeSelectionModes(aModes);
+  if ((!aOpMgr->hasOperation()) && aModes.isEmpty()) {
+    QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
+    aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
+    aSelMenu->addAction(action("SELECT_EDGE_CMD"));
+    aSelMenu->addAction(action("SELECT_FACE_CMD"));
+    aSelMenu->addAction(action("SELECT_RESULT_CMD"));
+    theMenu->addMenu(aSelMenu);
+    theMenu->addSeparator();
+  }
   if (aSelected == 1) {
-    ObjectPtr aObject = aPrsList.first().object();
-    std::string aName = aObject->groupName();
-    if (myViewerMenu.contains(aName))
-      aActions = myViewerMenu[aName];
-    aActions.append(action("COLOR_CMD"));
+    ObjectPtr aObject = aPrsList.first()->object();
+    if (aObject.get() != NULL) {
+      std::string aName = aObject->groupName();
+      if (myViewerMenu.contains(aName))
+        aActions = myViewerMenu[aName];
+    }
   } else if (aSelected > 1) {
     aActions.append(action("HIDE_CMD"));
-    aActions.append(action("COLOR_CMD"));
   }
+  // hide all is shown always even if selection in the viewer is empty
+  aActions.append(action("HIDEALL_CMD"));
+  aActions.append(action("COLOR_CMD"));
+
   theMenu->addActions(aActions);
 
+  QMap<int, QAction*> aMenuActions;
+  ModuleBase_IModule* aModule = myWorkshop->module();
+  if (aModule) {
+    if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
+      theMenu->addSeparator();
+  }
+
+  // insert the module menu items on specific positions in the popup menu: some actions should be
+  // in the begin of the list, Delete action should be the last by #1343 issue
+  QList<QAction*> anActions = theMenu->actions();
+  int anActionsSize = anActions.size();
+  QAction* aFirstAction = anActions[0];
+  QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
+  for (; anIt != aLast; anIt++) {
+    if (anIt.key() > anActionsSize)
+      theMenu->addAction(anIt.value());
+    else
+      theMenu->insertAction(aFirstAction, *anIt);
+  }
+
 #ifndef HAVE_SALOME
   theMenu->addSeparator();
   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
@@ -508,3 +622,48 @@ void XGUI_ContextMenuMgr::onRename()
   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
   myWorkshop->objectBrowser()->onEditItem();
 }
+
+void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  DocumentPtr aActiveDoc = aMgr->activeDocument();
+
+  XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
+  XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
+  const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
+  QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
+
+  QString aName;
+  int aLen = 0;
+  bool aIsRoot = false;
+  foreach(QModelIndex aIdx, aSelectedIndexes) {
+    // Process only first column
+    if (aIdx.column() == 0) {
+      aIsRoot = !aIdx.parent().isValid();
+      // Exit if the selected index belongs to non active document
+      if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
+        return;
+      if ((!aIsRoot) && (aIdx.internalPointer() != aActiveDoc.get()))
+        return;
+      
+      // Get name of the selected index
+      aName = aIdx.data().toString();
+      aLen = aName.indexOf('(');
+      if (aLen != -1) {
+        aName = aName.left(--aLen);
+      }
+      std::string aFeaturesStr = aIsRoot? 
+        aDataModelXML->rootFolderFeatures(aName.toStdString()) :
+        aDataModelXML->subFolderFeatures(aName.toStdString());
+        if (aFeaturesStr.length() > 0) {
+          QStringList aFeturesList = 
+            QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
+          foreach(QString aFea, aFeturesList) {
+            QAction* aAction = aActionMgr->action(aFea);
+            if (aAction)
+              theMenu->addAction(aAction);
+          }
+        }
+    }
+  }
+}