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>
22 #include <ModelAPI_ResultParameter.h>
24 #include <ModuleBase_IModule.h>
27 #include <QContextMenuEvent>
31 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
37 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
41 void XGUI_ContextMenuMgr::createActions()
43 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
44 addAction("EDIT_CMD", aAction);
46 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
47 addAction("ACTIVATE_PART_CMD", aAction);
49 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
50 addAction("DEACTIVATE_PART_CMD", aAction);
52 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
53 QMainWindow* aDesktop = myWorkshop->mainWindow();
55 aDesktop = myWorkshop->salomeConnector()->desktop();
56 aDesktop->addAction(aAction);
58 addAction("DELETE_CMD", aAction);
59 aAction->setShortcut(Qt::Key_Delete);
60 aAction->setShortcutContext(Qt::ApplicationShortcut);
62 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
63 addAction("COLOR_CMD", aAction);
65 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
66 addAction("SHOW_CMD", aAction);
68 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
69 addAction("SHOW_ONLY_CMD", aAction);
71 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
72 addAction("HIDE_CMD", aAction);
74 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
75 addAction("HIDEALL_CMD", aAction);
77 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
78 addAction("SHADING_CMD", aAction);
80 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
81 addAction("WIREFRAME_CMD", aAction);
84 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
86 if (myActions.contains(theId))
87 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
88 theAction->setData(theId);
89 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
90 myActions[theId] = theAction;
93 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
95 if (myActions.contains(theId))
96 return myActions[theId];
100 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
102 foreach(QAction* eachAction, myActions) {
103 if (eachAction->text() == theName) {
110 QStringList XGUI_ContextMenuMgr::actionIds() const
112 return myActions.keys();
115 void XGUI_ContextMenuMgr::onAction(bool isChecked)
117 QAction* aAction = static_cast<QAction*>(sender());
118 emit actionTriggered(aAction->data().toString(), isChecked);
121 void XGUI_ContextMenuMgr::updateCommandsStatus()
125 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
128 if (sender() == myWorkshop->objectBrowser())
129 aMenu = objectBrowserMenu();
130 else if (sender() == myWorkshop->viewer()) {
131 aMenu = viewerMenu();
134 if (aMenu && (aMenu->actions().size() > 0)) {
135 // it is possible that some objects should do something before and after the popup menu exec
136 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
138 emit beforeContextMenu();
139 aMenu->exec(theEvent->globalPos());
140 emit afterContextMenu();
145 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
147 QMenu* aMenu = new QMenu();
148 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
149 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
150 int aSelected = aObjects.size();
152 SessionPtr aMgr = ModelAPI_Session::get();
153 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
154 bool hasResult = false;
155 bool hasFeature = false;
156 bool hasParameter = false;
157 foreach(ObjectPtr aObj, aObjects)
159 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
160 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
161 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
163 hasResult = (aResult.get() != NULL);
164 hasFeature = (aFeature.get() != NULL);
165 hasParameter = (aConstruction.get() != NULL);
166 if (hasFeature && hasResult && hasParameter)
170 if (aSelected == 1) {
171 ObjectPtr aObject = aObjects.first();
173 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
175 if (aMgr->activeDocument() == aPart->partDoc())
176 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
178 aMenu->addAction(action("ACTIVATE_PART_CMD"));
179 } else if (hasFeature && aObject->document() == aMgr->activeDocument()) {
180 aMenu->addAction(action("EDIT_CMD"));
182 if (aDisplayer->isVisible(aObject)) {
183 if (aDisplayer->canBeShaded(aObject)) {
184 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
185 aMenu->addAction(action("WIREFRAME_CMD"));
187 aMenu->addAction(action("SHADING_CMD"));
189 aMenu->addSeparator();
190 aMenu->addAction(action("HIDE_CMD"));
191 } else if (!hasParameter) {
192 aMenu->addAction(action("SHOW_CMD"));
195 aMenu->addAction(action("EDIT_CMD"));
197 aMenu->addAction(action("SHOW_ONLY_CMD"));
199 } else { // If feature is 0 the it means that selected root object (document)
200 if (aMgr->activeDocument() != aMgr->moduleDocument())
201 aMenu->addAction(action("ACTIVATE_PART_CMD"));
204 if (hasResult && (!hasParameter)) {
205 aMenu->addAction(action("SHOW_CMD"));
206 aMenu->addAction(action("HIDE_CMD"));
207 aMenu->addAction(action("SHOW_ONLY_CMD"));
208 aMenu->addSeparator();
209 aMenu->addAction(action("SHADING_CMD"));
210 aMenu->addAction(action("WIREFRAME_CMD"));
213 if (hasFeature || hasParameter)
214 aMenu->addAction(action("DELETE_CMD"));
216 if (myWorkshop->canChangeColor())
217 aMenu->addAction(action("COLOR_CMD"));
219 aMenu->addSeparator();
220 aMenu->addActions(myWorkshop->objectBrowser()->actions());
222 ModuleBase_IModule* aModule = myWorkshop->module();
224 aModule->addObjectBrowserItems(aMenu);
226 if (aMenu->actions().size() > 0) {
233 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
235 QMenu* aMenu = new QMenu();
236 addViewerItems(aMenu);
237 if (aMenu->actions().size() > 0) {
244 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
246 bool aIsDone = false;
247 ModuleBase_IModule* aModule = myWorkshop->module();
249 aIsDone = aModule->addViewerItems(theMenu, myActions);
252 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
253 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
254 if (aObjects.size() > 0) {
255 //if (aObjects.size() == 1)
256 // theMenu->addAction(action("EDIT_CMD"));
257 bool isVisible = false;
258 bool isShading = false;
259 bool canBeShaded = false;
260 foreach(ObjectPtr aObject, aObjects)
262 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
263 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
265 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
266 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
273 theMenu->addAction(action("WIREFRAME_CMD"));
275 theMenu->addAction(action("SHADING_CMD"));
277 theMenu->addSeparator();
278 theMenu->addAction(action("SHOW_ONLY_CMD"));
279 theMenu->addAction(action("HIDE_CMD"));
281 theMenu->addAction(action("SHOW_CMD"));
282 //theMenu->addAction(action("DELETE_CMD"));
284 if (myWorkshop->displayer()->objectsCount() > 0)
285 theMenu->addAction(action("HIDEALL_CMD"));
286 if (myWorkshop->canChangeColor())
287 theMenu->addAction(action("COLOR_CMD"));
289 if (!myWorkshop->isSalomeMode()) {
290 theMenu->addSeparator();
291 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
292 if (aMDI->actions().size() > 0) {
293 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
294 aSubMenu->addActions(aMDI->actions());
300 void XGUI_ContextMenuMgr::connectObjectBrowser() const
302 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
303 SLOT(onContextMenuRequest(QContextMenuEvent*)));
306 void XGUI_ContextMenuMgr::connectViewer() const
308 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
309 SLOT(onContextMenuRequest(QContextMenuEvent*)));