Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
index 60d6e9f8392ecb04d1ddb1c74adce731dfd13c0c..f951d7a279fc590625f6225a739585838f92caf9 100644 (file)
@@ -1,20 +1,30 @@
-
 #include "XGUI_ContextMenuMgr.h"
 #include "XGUI_Workshop.h"
 #include "XGUI_ObjectsBrowser.h"
 #include "XGUI_SelectionMgr.h"
+#include "XGUI_Displayer.h"
+#include "XGUI_MainWindow.h"
+#include "XGUI_ViewerProxy.h"
+#include "XGUI_Selection.h"
+
+#include "PartSetPlugin_Part.h"
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_AttributeDocRef.h>
+#include <ModelAPI_Object.h>
+#include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_ResultGroup.h>
 
 #include <QAction>
 #include <QContextMenuEvent>
 #include <QMenu>
+#include <QMdiArea>
 
-XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
-QObject(theParent), myWorkshop(theParent)
+XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
+    : QObject(theParent),
+      myWorkshop(theParent)
 {
-
 }
 
 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
@@ -31,6 +41,27 @@ void XGUI_ContextMenuMgr::createActions()
 
   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
   addAction("DEACTIVATE_PART_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
+  addAction("DELETE_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
+  addAction("SHOW_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
+  addAction("SHOW_ONLY_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
+  addAction("HIDE_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
+  addAction("HIDEALL_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
+  addAction("SHADING_CMD", aAction);
+
+  aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
+  addAction("WIREFRAME_CMD", aAction);
 }
 
 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
@@ -69,8 +100,11 @@ void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
   QMenu* aMenu = 0;
   if (sender() == myWorkshop->objectBrowser())
     aMenu = objectBrowserMenu();
+  else if (sender() == myWorkshop->viewer()) {
+    aMenu = viewerMenu();
+  }
 
-  if (aMenu) {
+  if (aMenu && (aMenu->actions().size() > 0)) {
     aMenu->exec(theEvent->globalPos());
     delete aMenu;
   }
@@ -78,42 +112,145 @@ void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
 
 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
 {
-  QList<QAction*> aActions;
+  QMenu* aMenu = new QMenu();
   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
-  QFeatureList aFeatures = aSelMgr->selectedFeatures();
-  if (aFeatures.size() == 1) {
-    PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
-    FeaturePtr aFeature = aFeatures.first();
+  QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
+  int aSelected = aObjects.size();
+  if (aSelected > 0) {
+    SessionPtr aMgr = ModelAPI_Session::get();
+    XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+    bool hasResult = false;
+    bool hasFeature = false;
+    bool hasGroup = false;
+    foreach(ObjectPtr aObj, aObjects)
+    {
+      FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+      ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+      ResultGroupPtr aGroupRes = boost::dynamic_pointer_cast<ModelAPI_ResultGroup>(aObj);
+      if (aResult)
+        hasResult = true;
+      if (aFeature)
+        hasFeature = true;
+      if (aGroupRes)
+        hasGroup = true;
+      if (hasFeature && hasResult && hasGroup)
+        break;
+    }
     //Process Feature
-    if (aFeature) {
-      if (aFeature->getKind() == "Part") {
-        boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
-        if (aMgr->currentDocument() == aFeaDoc)
-          aActions.append(action("DEACTIVATE_PART_CMD"));
-        else 
-          aActions.append(action("ACTIVATE_PART_CMD"));
+    if (!hasGroup) {
+      if (aSelected == 1) {
+        ObjectPtr aObject = aObjects.first();
+        if (aObject) {
+          ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
+          if (aPart) {
+            if (aMgr->activeDocument() == aPart->partDoc())
+              aMenu->addAction(action("DEACTIVATE_PART_CMD"));
+            else
+              aMenu->addAction(action("ACTIVATE_PART_CMD"));
+          } else if (hasFeature) {
+            aMenu->addAction(action("EDIT_CMD"));
+          } else {
+            if (aDisplayer->isVisible(aObject)) {
+              if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
+                aMenu->addAction(action("WIREFRAME_CMD"));
+              else
+                aMenu->addAction(action("SHADING_CMD"));
+              aMenu->addSeparator();
+              aMenu->addAction(action("HIDE_CMD"));
+            } else {
+              aMenu->addAction(action("SHOW_CMD"));
+            }
+            aMenu->addAction(action("SHOW_ONLY_CMD"));
+          }
+        } else {  // If feature is 0 the it means that selected root object (document)
+          if (aMgr->activeDocument() != aMgr->moduleDocument())
+            aMenu->addAction(action("ACTIVATE_PART_CMD"));
+        }
       } else {
-        aActions.append(action("EDIT_CMD"));
-      }
-
-    // Process Root object (document)
-    } else { // If feature is 0 the it means that selected root object (document)
-      if (aMgr->currentDocument() != aMgr->rootDocument()) {
-        aActions.append(action("ACTIVATE_PART_CMD"));
+        if (hasResult) {
+          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"));
+        }
       }
+      if (hasFeature)
+        aMenu->addAction(action("DELETE_CMD"));
     }
   }
-  aActions.append(myWorkshop->objectBrowser()->actions());
-  if (aActions.size() > 0) {
-    QMenu* aMenu = new QMenu();
-    aMenu->addActions(aActions);
+  aMenu->addSeparator();
+  aMenu->addActions(myWorkshop->objectBrowser()->actions());
+  if (aMenu->actions().size() > 0) {
+    return aMenu;
+  }
+  delete aMenu;
+  return 0;
+}
+
+QMenu* XGUI_ContextMenuMgr::viewerMenu() const
+{
+  QMenu* aMenu = new QMenu();
+  addViewerItems(aMenu);
+  if (aMenu->actions().size() > 0) {
     return aMenu;
   }
+  delete aMenu;
   return 0;
 }
 
+void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
+{
+  XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
+  QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
+  if (aObjects.size() > 0) {
+    //if (aObjects.size() == 1)
+    //  theMenu->addAction(action("EDIT_CMD"));
+    bool isVisible = false;
+    bool isShading = false;
+    foreach(ObjectPtr aObject, aObjects)
+    {
+      ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+      if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
+        isVisible = true;
+        isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
+        break;
+      }
+    }
+    if (isVisible) {
+      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"));
+    //theMenu->addAction(action("DELETE_CMD"));
+  }
+  if (myWorkshop->displayer()->objectsCount() > 0)
+    theMenu->addAction(action("HIDEALL_CMD"));
+  if (!myWorkshop->isSalomeMode()) {
+    theMenu->addSeparator();
+    QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
+    if (aMDI->actions().size() > 0) {
+      QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
+      aSubMenu->addActions(aMDI->actions());
+    }
+  }
+}
+
 void XGUI_ContextMenuMgr::connectObjectBrowser() const
 {
-  connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
-    this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
-}
\ No newline at end of file
+  connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
+          SLOT(onContextMenuRequest(QContextMenuEvent*)));
+}
+
+void XGUI_ContextMenuMgr::connectViewer() const
+{
+  connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
+          SLOT(onContextMenuRequest(QContextMenuEvent*)));
+}
+