1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_ContextMenuMgr.h"
4 #include "XGUI_Workshop.h"
5 #include "XGUI_ObjectsBrowser.h"
6 #include "XGUI_SelectionMgr.h"
7 #include "XGUI_Displayer.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Selection.h"
11 #include <AppElements_MainWindow.h>
13 //#include "PartSetPlugin_Part.h"
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_AttributeDocRef.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_ResultPart.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_ResultGroup.h>
22 #include <ModuleBase_IModule.h>
25 #include <QContextMenuEvent>
29 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
35 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
39 void XGUI_ContextMenuMgr::createActions()
41 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
42 addAction("EDIT_CMD", aAction);
44 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
45 addAction("ACTIVATE_PART_CMD", aAction);
47 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
48 addAction("DEACTIVATE_PART_CMD", aAction);
50 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
51 addAction("DELETE_CMD", aAction);
53 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
54 addAction("SHOW_CMD", aAction);
56 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
57 addAction("SHOW_ONLY_CMD", aAction);
59 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
60 addAction("HIDE_CMD", aAction);
62 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
63 addAction("HIDEALL_CMD", aAction);
65 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
66 addAction("SHADING_CMD", aAction);
68 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
69 addAction("WIREFRAME_CMD", aAction);
72 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
74 if (myActions.contains(theId))
75 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
76 theAction->setData(theId);
77 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
78 myActions[theId] = theAction;
81 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
83 if (myActions.contains(theId))
84 return myActions[theId];
88 QStringList XGUI_ContextMenuMgr::actionIds() const
90 return myActions.keys();
93 void XGUI_ContextMenuMgr::onAction(bool isChecked)
95 QAction* aAction = static_cast<QAction*>(sender());
96 emit actionTriggered(aAction->data().toString(), isChecked);
99 void XGUI_ContextMenuMgr::updateCommandsStatus()
103 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
106 if (sender() == myWorkshop->objectBrowser())
107 aMenu = objectBrowserMenu();
108 else if (sender() == myWorkshop->viewer()) {
109 aMenu = viewerMenu();
112 if (aMenu && (aMenu->actions().size() > 0)) {
113 aMenu->exec(theEvent->globalPos());
118 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
120 QMenu* aMenu = new QMenu();
121 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
122 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
123 int aSelected = aObjects.size();
125 SessionPtr aMgr = ModelAPI_Session::get();
126 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
127 bool hasResult = false;
128 bool hasFeature = false;
129 foreach(ObjectPtr aObj, aObjects)
131 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
132 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
137 if (hasFeature && hasResult) // && hasGroup)
141 if (aSelected == 1) {
142 ObjectPtr aObject = aObjects.first();
144 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
146 if (aMgr->activeDocument() == aPart->partDoc())
147 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
149 aMenu->addAction(action("ACTIVATE_PART_CMD"));
150 } else if (hasFeature) {
151 aMenu->addAction(action("EDIT_CMD"));
153 if (aDisplayer->isVisible(aObject)) {
154 if (aDisplayer->canBeShaded(aObject)) {
155 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
156 aMenu->addAction(action("WIREFRAME_CMD"));
158 aMenu->addAction(action("SHADING_CMD"));
160 aMenu->addSeparator();
161 aMenu->addAction(action("HIDE_CMD"));
163 aMenu->addAction(action("SHOW_CMD"));
165 aMenu->addAction(action("SHOW_ONLY_CMD"));
167 } else { // If feature is 0 the it means that selected root object (document)
168 if (aMgr->activeDocument() != aMgr->moduleDocument())
169 aMenu->addAction(action("ACTIVATE_PART_CMD"));
173 aMenu->addAction(action("SHOW_CMD"));
174 aMenu->addAction(action("HIDE_CMD"));
175 aMenu->addAction(action("SHOW_ONLY_CMD"));
176 aMenu->addSeparator();
177 aMenu->addAction(action("SHADING_CMD"));
178 aMenu->addAction(action("WIREFRAME_CMD"));
182 aMenu->addAction(action("DELETE_CMD"));
184 aMenu->addSeparator();
185 aMenu->addActions(myWorkshop->objectBrowser()->actions());
187 ModuleBase_IModule* aModule = myWorkshop->module();
189 aModule->addObjectBrowserItems(aMenu);
191 if (aMenu->actions().size() > 0) {
198 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
200 QMenu* aMenu = new QMenu();
201 addViewerItems(aMenu);
202 if (aMenu->actions().size() > 0) {
209 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
211 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
212 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
213 if (aObjects.size() > 0) {
214 //if (aObjects.size() == 1)
215 // theMenu->addAction(action("EDIT_CMD"));
216 bool isVisible = false;
217 bool isShading = false;
218 bool canBeShaded = false;
219 foreach(ObjectPtr aObject, aObjects)
221 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
222 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
224 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
225 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
232 theMenu->addAction(action("WIREFRAME_CMD"));
234 theMenu->addAction(action("SHADING_CMD"));
236 theMenu->addSeparator();
237 theMenu->addAction(action("SHOW_ONLY_CMD"));
238 theMenu->addAction(action("HIDE_CMD"));
240 theMenu->addAction(action("SHOW_CMD"));
241 //theMenu->addAction(action("DELETE_CMD"));
243 if (myWorkshop->displayer()->objectsCount() > 0)
244 theMenu->addAction(action("HIDEALL_CMD"));
245 if (!myWorkshop->isSalomeMode()) {
246 theMenu->addSeparator();
247 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
248 if (aMDI->actions().size() > 0) {
249 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
250 aSubMenu->addActions(aMDI->actions());
254 ModuleBase_IModule* aModule = myWorkshop->module();
256 aModule->addViewerItems(theMenu);
259 void XGUI_ContextMenuMgr::connectObjectBrowser() const
261 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
262 SLOT(onContextMenuRequest(QContextMenuEvent*)));
265 void XGUI_ContextMenuMgr::connectViewer() const
267 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
268 SLOT(onContextMenuRequest(QContextMenuEvent*)));