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"
10 #include "XGUI_SalomeConnector.h"
12 #include <AppElements_MainWindow.h>
14 //#include "PartSetPlugin_Part.h"
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_AttributeDocRef.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_ResultPart.h>
20 #include <ModelAPI_Session.h>
21 #include <ModelAPI_ResultGroup.h>
23 #include <ModuleBase_IModule.h>
26 #include <QContextMenuEvent>
30 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
36 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
40 void XGUI_ContextMenuMgr::createActions()
42 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
43 addAction("EDIT_CMD", aAction);
45 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
46 addAction("ACTIVATE_PART_CMD", aAction);
48 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
49 addAction("DEACTIVATE_PART_CMD", aAction);
51 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
52 QMainWindow* aDesktop = myWorkshop->mainWindow();
54 aDesktop = myWorkshop->salomeConnector()->desktop();
55 aDesktop->addAction(aAction);
57 addAction("DELETE_CMD", aAction);
58 aAction->setShortcut(Qt::Key_Delete);
59 aAction->setShortcutContext(Qt::ApplicationShortcut);
61 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
62 addAction("COLOR_CMD", aAction);
64 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
65 addAction("SHOW_CMD", aAction);
67 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
68 addAction("SHOW_ONLY_CMD", aAction);
70 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
71 addAction("HIDE_CMD", aAction);
73 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
74 addAction("HIDEALL_CMD", aAction);
76 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
77 addAction("SHADING_CMD", aAction);
79 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
80 addAction("WIREFRAME_CMD", aAction);
83 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
85 if (myActions.contains(theId))
86 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
87 theAction->setData(theId);
88 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
89 myActions[theId] = theAction;
92 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
94 if (myActions.contains(theId))
95 return myActions[theId];
99 QStringList XGUI_ContextMenuMgr::actionIds() const
101 return myActions.keys();
104 void XGUI_ContextMenuMgr::onAction(bool isChecked)
106 QAction* aAction = static_cast<QAction*>(sender());
107 emit actionTriggered(aAction->data().toString(), isChecked);
110 void XGUI_ContextMenuMgr::updateCommandsStatus()
114 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
117 if (sender() == myWorkshop->objectBrowser())
118 aMenu = objectBrowserMenu();
119 else if (sender() == myWorkshop->viewer()) {
120 aMenu = viewerMenu();
123 if (aMenu && (aMenu->actions().size() > 0)) {
124 // it is possible that some objects should do something before and after the popup menu exec
125 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
127 emit beforeContextMenu();
128 aMenu->exec(theEvent->globalPos());
129 emit afterContextMenu();
134 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
136 QMenu* aMenu = new QMenu();
137 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
138 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
139 int aSelected = aObjects.size();
141 SessionPtr aMgr = ModelAPI_Session::get();
142 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
143 bool hasResult = false;
144 bool hasFeature = false;
145 foreach(ObjectPtr aObj, aObjects)
147 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
148 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
153 if (hasFeature && hasResult) // && hasGroup)
157 if (aSelected == 1) {
158 ObjectPtr aObject = aObjects.first();
160 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
162 if (aMgr->activeDocument() == aPart->partDoc())
163 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
165 aMenu->addAction(action("ACTIVATE_PART_CMD"));
166 } else if (hasFeature && aObject->document() == aMgr->activeDocument()) {
167 aMenu->addAction(action("EDIT_CMD"));
169 if (aDisplayer->isVisible(aObject)) {
170 if (aDisplayer->canBeShaded(aObject)) {
171 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
172 aMenu->addAction(action("WIREFRAME_CMD"));
174 aMenu->addAction(action("SHADING_CMD"));
176 aMenu->addSeparator();
177 aMenu->addAction(action("HIDE_CMD"));
179 aMenu->addAction(action("SHOW_CMD"));
181 aMenu->addAction(action("SHOW_ONLY_CMD"));
183 } else { // If feature is 0 the it means that selected root object (document)
184 if (aMgr->activeDocument() != aMgr->moduleDocument())
185 aMenu->addAction(action("ACTIVATE_PART_CMD"));
189 aMenu->addAction(action("SHOW_CMD"));
190 aMenu->addAction(action("HIDE_CMD"));
191 aMenu->addAction(action("SHOW_ONLY_CMD"));
192 aMenu->addSeparator();
193 aMenu->addAction(action("SHADING_CMD"));
194 aMenu->addAction(action("WIREFRAME_CMD"));
198 aMenu->addAction(action("DELETE_CMD"));
200 if (myWorkshop->canChangeColor())
201 aMenu->addAction(action("COLOR_CMD"));
203 aMenu->addSeparator();
204 aMenu->addActions(myWorkshop->objectBrowser()->actions());
206 ModuleBase_IModule* aModule = myWorkshop->module();
208 aModule->addObjectBrowserItems(aMenu);
210 if (aMenu->actions().size() > 0) {
217 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
219 QMenu* aMenu = new QMenu();
220 addViewerItems(aMenu);
221 if (aMenu->actions().size() > 0) {
228 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
230 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
231 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
232 if (aObjects.size() > 0) {
233 //if (aObjects.size() == 1)
234 // theMenu->addAction(action("EDIT_CMD"));
235 bool isVisible = false;
236 bool isShading = false;
237 bool canBeShaded = false;
238 foreach(ObjectPtr aObject, aObjects)
240 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
241 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
243 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
244 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
251 theMenu->addAction(action("WIREFRAME_CMD"));
253 theMenu->addAction(action("SHADING_CMD"));
255 theMenu->addSeparator();
256 theMenu->addAction(action("SHOW_ONLY_CMD"));
257 theMenu->addAction(action("HIDE_CMD"));
259 theMenu->addAction(action("SHOW_CMD"));
260 //theMenu->addAction(action("DELETE_CMD"));
262 if (myWorkshop->displayer()->objectsCount() > 0)
263 theMenu->addAction(action("HIDEALL_CMD"));
264 if (!myWorkshop->isSalomeMode()) {
265 theMenu->addSeparator();
266 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
267 if (aMDI->actions().size() > 0) {
268 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
269 aSubMenu->addActions(aMDI->actions());
272 if (myWorkshop->canChangeColor())
273 theMenu->addAction(action("COLOR_CMD"));
275 ModuleBase_IModule* aModule = myWorkshop->module();
277 aModule->addViewerItems(theMenu);
280 void XGUI_ContextMenuMgr::connectObjectBrowser() const
282 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
283 SLOT(onContextMenuRequest(QContextMenuEvent*)));
286 void XGUI_ContextMenuMgr::connectViewer() const
288 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
289 SLOT(onContextMenuRequest(QContextMenuEvent*)));