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>
23 #include <QContextMenuEvent>
27 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
33 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
37 void XGUI_ContextMenuMgr::createActions()
39 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
40 addAction("EDIT_CMD", aAction);
42 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
43 addAction("ACTIVATE_PART_CMD", aAction);
45 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
46 addAction("DEACTIVATE_PART_CMD", aAction);
48 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
49 addAction("DELETE_CMD", aAction);
51 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
52 addAction("SHOW_CMD", aAction);
54 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
55 addAction("SHOW_ONLY_CMD", aAction);
57 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
58 addAction("HIDE_CMD", aAction);
60 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
61 addAction("HIDEALL_CMD", aAction);
63 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
64 addAction("SHADING_CMD", aAction);
66 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
67 addAction("WIREFRAME_CMD", aAction);
70 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
72 if (myActions.contains(theId))
73 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
74 theAction->setData(theId);
75 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
76 myActions[theId] = theAction;
79 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
81 if (myActions.contains(theId))
82 return myActions[theId];
86 QStringList XGUI_ContextMenuMgr::actionIds() const
88 return myActions.keys();
91 void XGUI_ContextMenuMgr::onAction(bool isChecked)
93 QAction* aAction = static_cast<QAction*>(sender());
94 emit actionTriggered(aAction->data().toString(), isChecked);
97 void XGUI_ContextMenuMgr::updateCommandsStatus()
101 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
104 if (sender() == myWorkshop->objectBrowser())
105 aMenu = objectBrowserMenu();
106 else if (sender() == myWorkshop->viewer()) {
107 aMenu = viewerMenu();
110 if (aMenu && (aMenu->actions().size() > 0)) {
111 aMenu->exec(theEvent->globalPos());
116 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
118 QMenu* aMenu = new QMenu();
119 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
120 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
121 int aSelected = aObjects.size();
123 SessionPtr aMgr = ModelAPI_Session::get();
124 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
125 bool hasResult = false;
126 bool hasFeature = false;
127 foreach(ObjectPtr aObj, aObjects)
129 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
130 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
135 if (hasFeature && hasResult) // && hasGroup)
139 if (aSelected == 1) {
140 ObjectPtr aObject = aObjects.first();
142 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
144 if (aMgr->activeDocument() == aPart->partDoc())
145 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
147 aMenu->addAction(action("ACTIVATE_PART_CMD"));
148 } else if (hasFeature) {
149 aMenu->addAction(action("EDIT_CMD"));
151 if (aDisplayer->isVisible(aObject)) {
152 if (aDisplayer->canBeShaded(aObject)) {
153 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
154 aMenu->addAction(action("WIREFRAME_CMD"));
156 aMenu->addAction(action("SHADING_CMD"));
158 aMenu->addSeparator();
159 aMenu->addAction(action("HIDE_CMD"));
161 aMenu->addAction(action("SHOW_CMD"));
163 aMenu->addAction(action("SHOW_ONLY_CMD"));
165 } else { // If feature is 0 the it means that selected root object (document)
166 if (aMgr->activeDocument() != aMgr->moduleDocument())
167 aMenu->addAction(action("ACTIVATE_PART_CMD"));
171 aMenu->addAction(action("SHOW_CMD"));
172 aMenu->addAction(action("HIDE_CMD"));
173 aMenu->addAction(action("SHOW_ONLY_CMD"));
174 aMenu->addSeparator();
175 aMenu->addAction(action("SHADING_CMD"));
176 aMenu->addAction(action("WIREFRAME_CMD"));
180 aMenu->addAction(action("DELETE_CMD"));
182 aMenu->addSeparator();
183 aMenu->addActions(myWorkshop->objectBrowser()->actions());
184 if (aMenu->actions().size() > 0) {
191 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
193 QMenu* aMenu = new QMenu();
194 addViewerItems(aMenu);
195 if (aMenu->actions().size() > 0) {
202 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
204 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
205 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
206 if (aObjects.size() > 0) {
207 //if (aObjects.size() == 1)
208 // theMenu->addAction(action("EDIT_CMD"));
209 bool isVisible = false;
210 bool isShading = false;
211 bool canBeShaded = false;
212 foreach(ObjectPtr aObject, aObjects)
214 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
215 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
217 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
218 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
225 theMenu->addAction(action("WIREFRAME_CMD"));
227 theMenu->addAction(action("SHADING_CMD"));
229 theMenu->addSeparator();
230 theMenu->addAction(action("SHOW_ONLY_CMD"));
231 theMenu->addAction(action("HIDE_CMD"));
233 theMenu->addAction(action("SHOW_CMD"));
234 //theMenu->addAction(action("DELETE_CMD"));
236 if (myWorkshop->displayer()->objectsCount() > 0)
237 theMenu->addAction(action("HIDEALL_CMD"));
238 if (!myWorkshop->isSalomeMode()) {
239 theMenu->addSeparator();
240 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
241 if (aMDI->actions().size() > 0) {
242 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
243 aSubMenu->addActions(aMDI->actions());
248 void XGUI_ContextMenuMgr::connectObjectBrowser() const
250 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
251 SLOT(onContextMenuRequest(QContextMenuEvent*)));
254 void XGUI_ContextMenuMgr::connectViewer() const
256 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
257 SLOT(onContextMenuRequest(QContextMenuEvent*)));