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 myWorkshop->mainWindow()->addAction(aAction);
52 addAction("DELETE_CMD", aAction);
53 aAction->setShortcut(Qt::Key_Delete);
54 aAction->setShortcutContext(Qt::ApplicationShortcut);
56 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
57 addAction("SHOW_CMD", aAction);
59 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
60 addAction("SHOW_ONLY_CMD", aAction);
62 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
63 addAction("HIDE_CMD", aAction);
65 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
66 addAction("HIDEALL_CMD", aAction);
68 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
69 addAction("SHADING_CMD", aAction);
71 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
72 addAction("WIREFRAME_CMD", aAction);
75 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
77 if (myActions.contains(theId))
78 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
79 theAction->setData(theId);
80 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
81 myActions[theId] = theAction;
84 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
86 if (myActions.contains(theId))
87 return myActions[theId];
91 QStringList XGUI_ContextMenuMgr::actionIds() const
93 return myActions.keys();
96 void XGUI_ContextMenuMgr::onAction(bool isChecked)
98 QAction* aAction = static_cast<QAction*>(sender());
99 emit actionTriggered(aAction->data().toString(), isChecked);
102 void XGUI_ContextMenuMgr::updateCommandsStatus()
106 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
109 if (sender() == myWorkshop->objectBrowser())
110 aMenu = objectBrowserMenu();
111 else if (sender() == myWorkshop->viewer()) {
112 aMenu = viewerMenu();
115 if (aMenu && (aMenu->actions().size() > 0)) {
116 aMenu->exec(theEvent->globalPos());
121 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
123 QMenu* aMenu = new QMenu();
124 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
125 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
126 int aSelected = aObjects.size();
128 SessionPtr aMgr = ModelAPI_Session::get();
129 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
130 bool hasResult = false;
131 bool hasFeature = false;
132 foreach(ObjectPtr aObj, aObjects)
134 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
135 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
140 if (hasFeature && hasResult) // && hasGroup)
144 if (aSelected == 1) {
145 ObjectPtr aObject = aObjects.first();
147 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
149 if (aMgr->activeDocument() == aPart->partDoc())
150 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
152 aMenu->addAction(action("ACTIVATE_PART_CMD"));
153 } else if (hasFeature && aObject->document() == aMgr->activeDocument()) {
154 aMenu->addAction(action("EDIT_CMD"));
156 if (aDisplayer->isVisible(aObject)) {
157 if (aDisplayer->canBeShaded(aObject)) {
158 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
159 aMenu->addAction(action("WIREFRAME_CMD"));
161 aMenu->addAction(action("SHADING_CMD"));
163 aMenu->addSeparator();
164 aMenu->addAction(action("HIDE_CMD"));
166 aMenu->addAction(action("SHOW_CMD"));
168 aMenu->addAction(action("SHOW_ONLY_CMD"));
170 } else { // If feature is 0 the it means that selected root object (document)
171 if (aMgr->activeDocument() != aMgr->moduleDocument())
172 aMenu->addAction(action("ACTIVATE_PART_CMD"));
176 aMenu->addAction(action("SHOW_CMD"));
177 aMenu->addAction(action("HIDE_CMD"));
178 aMenu->addAction(action("SHOW_ONLY_CMD"));
179 aMenu->addSeparator();
180 aMenu->addAction(action("SHADING_CMD"));
181 aMenu->addAction(action("WIREFRAME_CMD"));
185 aMenu->addAction(action("DELETE_CMD"));
187 aMenu->addSeparator();
188 aMenu->addActions(myWorkshop->objectBrowser()->actions());
190 ModuleBase_IModule* aModule = myWorkshop->module();
192 aModule->addObjectBrowserItems(aMenu);
194 if (aMenu->actions().size() > 0) {
201 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
203 QMenu* aMenu = new QMenu();
204 addViewerItems(aMenu);
205 if (aMenu->actions().size() > 0) {
212 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
214 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
215 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
216 if (aObjects.size() > 0) {
217 //if (aObjects.size() == 1)
218 // theMenu->addAction(action("EDIT_CMD"));
219 bool isVisible = false;
220 bool isShading = false;
221 bool canBeShaded = false;
222 foreach(ObjectPtr aObject, aObjects)
224 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
225 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
227 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
228 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
235 theMenu->addAction(action("WIREFRAME_CMD"));
237 theMenu->addAction(action("SHADING_CMD"));
239 theMenu->addSeparator();
240 theMenu->addAction(action("SHOW_ONLY_CMD"));
241 theMenu->addAction(action("HIDE_CMD"));
243 theMenu->addAction(action("SHOW_CMD"));
244 //theMenu->addAction(action("DELETE_CMD"));
246 if (myWorkshop->displayer()->objectsCount() > 0)
247 theMenu->addAction(action("HIDEALL_CMD"));
248 if (!myWorkshop->isSalomeMode()) {
249 theMenu->addSeparator();
250 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
251 if (aMDI->actions().size() > 0) {
252 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
253 aSubMenu->addActions(aMDI->actions());
257 ModuleBase_IModule* aModule = myWorkshop->module();
259 aModule->addViewerItems(theMenu);
262 void XGUI_ContextMenuMgr::connectObjectBrowser() const
264 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
265 SLOT(onContextMenuRequest(QContextMenuEvent*)));
268 void XGUI_ContextMenuMgr::connectViewer() const
270 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
271 SLOT(onContextMenuRequest(QContextMenuEvent*)));