]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #596: Redesign of pop-up menu management
authorvsv <vitaly.smetannikov@opencascade.com>
Mon, 29 Jun 2015 16:23:52 +0000 (19:23 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Mon, 29 Jun 2015 16:24:05 +0000 (19:24 +0300)
src/NewGeom/NewGeom_Module.cpp
src/XGUI/XGUI_ContextMenuMgr.cpp
src/XGUI/XGUI_ContextMenuMgr.h

index 16dc0216c9886d621ea7d940ff34cd1a64350cdb..3a0227476b3619f0307882bd85da043d8f3f7b1c 100644 (file)
@@ -41,6 +41,7 @@
 #include <QDockWidget>
 #include <QAction>
 #include <QTimer>
+#include <QMenu>
 
 
 extern "C" {
@@ -522,7 +523,8 @@ void NewGeom_Module::selectionChanged()
 //******************************************************
 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
 {
-  myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
+  theMenu->addActions(myWorkshop->contextMenuMgr()->viewerMenu()->actions());
+  //myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
 }
 
index c9a9689f4f39c0f12c6ac3df9431a3752bc9d0c7..e3cae01c2948bd8a595dd07f873a319cb81137d3 100644 (file)
@@ -19,6 +19,8 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_ResultBody.h>
 
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_Tools.h>
 #include <QMenu>
 #include <QMdiArea>
 
+
 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
     : QObject(theParent),
-      myWorkshop(theParent)
+      myWorkshop(theParent),
+      mySeparator(0)
 {
 }
 
@@ -50,7 +54,7 @@ void XGUI_ContextMenuMgr::createActions()
   aAction->setShortcut(Qt::Key_Delete);
   aAction->setShortcutContext(Qt::ApplicationShortcut);
 
-  aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
+  aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
   addAction("COLOR_CMD", aAction);
 
   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
@@ -70,6 +74,13 @@ void XGUI_ContextMenuMgr::createActions()
 
   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
   addAction("WIREFRAME_CMD", aAction);
+
+  mySeparator = new QAction(this);
+  mySeparator->setSeparator(true);
+
+
+  buildObjBrowserMenu();
+  buildViewerMenu();
 }
 
 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
@@ -116,9 +127,11 @@ void XGUI_ContextMenuMgr::updateCommandsStatus()
 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
 {
   QMenu* aMenu = 0;
-  if (sender() == myWorkshop->objectBrowser())
-    aMenu = objectBrowserMenu();
-  else if (sender() == myWorkshop->viewer()) {
+  if (sender() == myWorkshop->objectBrowser()) {
+    updateObjectBrowserMenu();
+    aMenu = objBrowserMenu();
+  } else if (sender() == myWorkshop->viewer()) {
+    updateViewerMenu();
     aMenu = viewerMenu();
   }
 
@@ -133,9 +146,11 @@ void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
   }
 }
 
-QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
+void XGUI_ContextMenuMgr::updateObjectBrowserMenu() 
 {
-  QMenu* aMenu = new QMenu();
+  foreach(QAction* aAction, myActions)
+    aAction->setEnabled(false);
+
   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
   int aSelected = aObjects.size();
@@ -154,43 +169,180 @@ QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
         if (!hasFeature) {
           if (aObject->isDisplayed()) {
             if (aDisplayer->canBeShaded(aObject)) {
-              if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
-                aMenu->addAction(action("WIREFRAME_CMD"));
-              else
-                aMenu->addAction(action("SHADING_CMD"));
+              action("WIREFRAME_CMD")->setEnabled(true);
+              action("SHADING_CMD")->setEnabled(true);
             }
-            aMenu->addSeparator();
-            aMenu->addAction(action("HIDE_CMD"));
+            action("HIDE_CMD")->setEnabled(true);
           } else if (hasResult && (!hasParameter)) {
-            aMenu->addAction(action("SHOW_CMD"));
+            action("SHOW_CMD")->setEnabled(true);
           }
 
           ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
           if (aPartRes) {
-            aMenu->addAction(action("SHOW_CMD"));
+            action("SHOW_CMD")->setEnabled(true);
           }
 
           if (!(hasParameter || hasFeature))
-            aMenu->addAction(action("SHOW_ONLY_CMD"));
+            action("SHOW_ONLY_CMD")->setEnabled(true);
         }
       } 
     } else {
       if (hasResult && (!hasParameter)) {
-        aMenu->addAction(action("SHOW_CMD"));
-        aMenu->addAction(action("HIDE_CMD"));
-        aMenu->addAction(action("SHOW_ONLY_CMD"));
-        aMenu->addSeparator();
-        aMenu->addAction(action("SHADING_CMD"));
-        aMenu->addAction(action("WIREFRAME_CMD"));
+        action("SHOW_CMD")->setEnabled(true);
+        action("HIDE_CMD")->setEnabled(true);
+        action("SHOW_ONLY_CMD")->setEnabled(true);
+        action("SHADING_CMD")->setEnabled(true);
+        action("WIREFRAME_CMD")->setEnabled(true);
       }
     }
     if (hasFeature || hasParameter)
-      aMenu->addAction(action("DELETE_CMD"));
+      action("DELETE_CMD")->setEnabled(true);
   }
   if (myWorkshop->canChangeColor())
-    aMenu->addAction(action("COLOR_CMD"));
+    action("COLOR_CMD")->setEnabled(true);
+}
+
+void XGUI_ContextMenuMgr::updateViewerMenu()
+{
+  foreach(QAction* aAction, myActions)
+    aAction->setEnabled(false);
+
+  //bool aIsDone = false;
+  //ModuleBase_IModule* aModule = myWorkshop->module();
+  //if (aModule) 
+  //  aIsDone = aModule->addViewerMenu(theMenu, myActions);
+
+  XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
+  QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
+  if (aObjects.size() > 0) {
+    bool isVisible = false;
+    bool isShading = false;
+    bool canBeShaded = false;
+    foreach(ObjectPtr aObject, aObjects)
+    {
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+      if (aRes && aRes->isDisplayed()) {
+        isVisible = true;
+        canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
+        isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
+        break;
+      }
+    }
+    if (isVisible) {
+      if (canBeShaded) {
+        action("WIREFRAME_CMD")->setEnabled(true);
+        action("SHADING_CMD")->setEnabled(true);
+      }
+      action("SHOW_ONLY_CMD")->setEnabled(true);
+      action("HIDE_CMD")->setEnabled(true);
+    } else
+      action("SHOW_CMD")->setEnabled(true);
+  }
+  if (myWorkshop->displayer()->objectsCount() > 0)
+    action("HIDEALL_CMD")->setEnabled(true);
+  if (myWorkshop->canChangeColor())
+    action("COLOR_CMD")->setEnabled(true);
+}
+
+void XGUI_ContextMenuMgr::connectObjectBrowser()
+{
+  connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
+          SLOT(onContextMenuRequest(QContextMenuEvent*)));
+}
+
+void XGUI_ContextMenuMgr::connectViewer()
+{
+  connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
+          SLOT(onContextMenuRequest(QContextMenuEvent*)));
+}
+
+
+void XGUI_ContextMenuMgr::buildObjBrowserMenu()
+{
+  QAction* aSeparator = new QAction(this);
+  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"));
+  myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
+  // Result part menu
+  myObjBrowserMenus[ModelAPI_ResultPart::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(action("SHOW_CMD"));
+  aList.append(action("HIDE_CMD"));
+  aList.append(action("SHOW_ONLY_CMD"));
+  myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
+  // Group menu
+  myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
+  //-------------------------------------
+  // Feature menu
+  aList.clear();
+  aList.append(action("DELETE_CMD"));
+  myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
+  myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
+  //-------------------------------------
+}
+
+void XGUI_ContextMenuMgr::buildViewerMenu()
+{
+  QActionsList aList;
+  // Result construction menu
+  aList.append(action("HIDE_CMD"));
+  aList.append(action("SHOW_ONLY_CMD"));
+  aList.append(action("HIDEALL_CMD"));
+  aList.append(action("COLOR_CMD"));
+  myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
+  // Result part menu
+  myViewerMenu[ModelAPI_ResultPart::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(action("HIDE_CMD"));
+  aList.append(action("SHOW_ONLY_CMD"));
+  aList.append(action("HIDEALL_CMD"));
+  myViewerMenu[ModelAPI_ResultBody::group()] = aList;
+  // Group menu
+  myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
+  //-------------------------------------
+
+}
+
+
+QMenu* XGUI_ContextMenuMgr::objBrowserMenu() const
+{
+  QMenu* aMenu = new QMenu();
+  XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
+  QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
+  int aSelected = aObjects.size();
+  QActionsList aActions;
+  if (aSelected == 1) {
+    ObjectPtr aObject = aObjects.first();
+    if (myViewerMenu.contains(aObject->groupName()))
+      aActions = myObjBrowserMenus[aObject->groupName()];
+  } else if (aSelected > 1) {
+      aActions.append(action("HIDE_CMD"));
+      aActions.append(action("SHOW_ONLY_CMD"));
+      aActions.append(mySeparator);
+      aActions.append(action("SHADING_CMD"));
+      aActions.append(action("WIREFRAME_CMD"));
+  }
+  aMenu->addActions(aActions);
 
-  aMenu->addSeparator();
   ModuleBase_IModule* aModule = myWorkshop->module();
   if (aModule) {
     aModule->addObjectBrowserMenu(aMenu);
@@ -198,87 +350,36 @@ QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
   }
   aMenu->addActions(myWorkshop->objectBrowser()->actions());
 
-
-  if (aMenu->actions().size() > 0) {
-    return aMenu;
-  }
-  delete aMenu;
-  return 0;
+  return aMenu;
 }
 
 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
 {
   QMenu* aMenu = new QMenu();
-  addViewerMenu(aMenu);
-  if (aMenu->actions().size() > 0) {
-    return aMenu;
-  }
-  delete aMenu;
-  return 0;
-}
-
-void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
-{
-  bool aIsDone = false;
   ModuleBase_IModule* aModule = myWorkshop->module();
   if (aModule) 
-    aIsDone = aModule->addViewerMenu(theMenu, myActions);
-
-  if (!aIsDone) {
-    XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
-    QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
-    if (aObjects.size() > 0) {
-      bool isVisible = false;
-      bool isShading = false;
-      bool canBeShaded = false;
-      foreach(ObjectPtr aObject, aObjects)
-      {
-        ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
-        if (aRes && aRes->isDisplayed()) {
-          isVisible = true;
-          canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
-          isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
-          break;
-        }
-      }
-      if (isVisible) {
-        if (canBeShaded) {
-          if (isShading)
-            theMenu->addAction(action("WIREFRAME_CMD"));
-          else
-            theMenu->addAction(action("SHADING_CMD"));
-        }
-        theMenu->addSeparator();
-        theMenu->addAction(action("SHOW_ONLY_CMD"));
-        theMenu->addAction(action("HIDE_CMD"));
-      } else
-        theMenu->addAction(action("SHOW_CMD"));
-    }
-    if (myWorkshop->displayer()->objectsCount() > 0)
-      theMenu->addAction(action("HIDEALL_CMD"));
-    if (myWorkshop->canChangeColor())
-      theMenu->addAction(action("COLOR_CMD"));
+    aModule->addViewerMenu(aMenu, myActions);
+
+  XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
+  QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
+  int aSelected = aObjects.size();
+  QActionsList aActions;
+  if (aSelected == 1) {
+    ObjectPtr aObject = aObjects.first();
+    if (myViewerMenu.contains(aObject->groupName()))
+      aActions = myViewerMenu[aObject->groupName()];
+  } else if (aSelected > 1) {
+    aActions.append(action("HIDE_CMD"));
   }
+  aMenu->addActions(aActions);
+
   if (!myWorkshop->isSalomeMode()) {
-    theMenu->addSeparator();
+    aMenu->addSeparator();
     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
     if (aMDI->actions().size() > 0) {
-      QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
+      QMenu* aSubMenu = aMenu->addMenu(tr("Windows"));
       aSubMenu->addActions(aMDI->actions());
     }
   }
-
-}
-
-void XGUI_ContextMenuMgr::connectObjectBrowser() const
-{
-  connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
-          SLOT(onContextMenuRequest(QContextMenuEvent*)));
+  return aMenu;
 }
-
-void XGUI_ContextMenuMgr::connectViewer() const
-{
-  connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
-          SLOT(onContextMenuRequest(QContextMenuEvent*)));
-}
-
index 3f490a59b22d0450ab458c975df741c58b63766f..80905f05a8818b21ac76ac2a0a6222916fd9da58 100644 (file)
@@ -44,14 +44,14 @@ Q_OBJECT
   void updateCommandsStatus();
 
   /// Connect to object browser from workshop. Has to called at creation of viewer.
-  void connectObjectBrowser() const;
+  void connectObjectBrowser();
 
   /// Connect to viewer from workshop. Has to called at creation of viewer.
-  void connectViewer() const;
+  void connectViewer();
+
+  QMenu* objBrowserMenu() const;
+  QMenu* viewerMenu() const;
 
-  /// Add menu atems for viewer into the given menu (used in SALOME mode)
-  /// \param theMenu a popup menu to be shown in the viewer
-  void addViewerMenu(QMenu* theMenu) const;
 
 signals:
   /// Signal aabout triggered action
@@ -82,17 +82,29 @@ signals:
    */
   void addAction(const QString& theId, QAction* theAction);
 
+  /// Updates menu for object browser
+  void updateObjectBrowserMenu();
+
+  /// Updates menu for viewer
+  void updateViewerMenu();
+
   /// Creates menu for object browser
-  QMenu* objectBrowserMenu() const;
+  void buildObjBrowserMenu();
 
   /// Creates menu for viewer
-  QMenu* viewerMenu() const;
+  void buildViewerMenu();
 
   /// Map of created actions [id : Action]
   QMap<QString, QAction*> myActions;
 
   /// Reference to workshop
   XGUI_Workshop* myWorkshop;
+
+  typedef QList<QAction*> QActionsList;
+  QMap<std::string, QActionsList> myObjBrowserMenus;
+  QMap<std::string, QActionsList> myViewerMenu;
+
+   QAction* mySeparator;
 };
 
 #endif