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 579be3621db1f647d1b28e799c2b3976803a7292..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>
@@ -28,6 +29,8 @@
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
 
+#include <Config_DataModelReader.h>
+
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_OperationAction.h>
@@ -39,6 +42,7 @@
 #include <QMenu>
 #include <QMdiArea>
 #include <QMainWindow>
+#include <QModelIndex>
 
 
 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
@@ -128,6 +132,9 @@ void XGUI_ContextMenuMgr::createActions()
   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();
 }
@@ -209,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();
@@ -246,13 +253,12 @@ void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
         else if (hasFeature && myWorkshop->canMoveFeature())
           action("MOVE_CMD")->setEnabled(true);
 
-        else if (hasFeature || hasParameter)
-          action("CLEAN_HISTORY_CMD")->setEnabled(!hasSubFeature);
-
         if( aMgr->activeDocument() == aObject->document() )
         {
           action("RENAME_CMD")->setEnabled(true);
-          action("DELETE_CMD")->setEnabled(!hasSubFeature);
+          action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
+          action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
+                                                  (hasFeature || hasParameter));
         }
       }
     } else {
@@ -271,14 +277,15 @@ void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
         allActive = false;
         break;
       }
-    if (!hasSubFeature && allActive ) {
+    if (!hasCompositeOwner && allActive ) {
       if (hasFeature || hasParameter)
         action("DELETE_CMD")->setEnabled(true);
     }
-    if (!hasSubFeature && allActive && (hasFeature|| hasParameter))
+    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
@@ -348,10 +355,6 @@ 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();
@@ -377,9 +380,15 @@ void XGUI_ContextMenuMgr::updateViewerMenu()
     } 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()
@@ -401,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;
@@ -430,20 +443,19 @@ void XGUI_ContextMenuMgr::buildObjBrowserMenu()
   //-------------------------------------
   // Feature menu
   aList.clear();
-  aList.append(action("DELETE_CMD"));
-  aList.append(action("MOVE_CMD"));
-  aList.append(action("CLEAN_HISTORY_CMD"));
-  aList.append(mySeparator);
   aList.append(action("RENAME_CMD"));
-  aList.append(mySeparator);
   aList.append(action("SHOW_RESULTS_CMD"));
+  aList.append(action("MOVE_CMD"));
+  aList.append(mySeparator);
+  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(action("CLEAN_HISTORY_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;
   //-------------------------------------
 }
@@ -454,6 +466,7 @@ void XGUI_ContextMenuMgr::buildViewerMenu()
   // Result construction menu
   aList.append(action("HIDE_CMD"));
   aList.append(action("SHOW_ONLY_CMD"));
+  aList.append(mySeparator);
   aList.append(action("COLOR_CMD"));
   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
   // Result part menu
@@ -463,20 +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(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();
@@ -487,36 +506,28 @@ 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("CLEAN_HISTORY_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_ViewerPrsPtr> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
   int aSelected = aPrsList.size();
@@ -542,15 +553,35 @@ void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
       if (myViewerMenu.contains(aName))
         aActions = myViewerMenu[aName];
     }
-    aActions.append(action("COLOR_CMD"));
   } 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();
@@ -591,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);
+          }
+        }
+    }
+  }
+}