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->displayMode(aObject) == XGUI_Displayer::Shading)
153 aMenu->addAction(action("WIREFRAME_CMD"));
155 aMenu->addAction(action("SHADING_CMD"));
156 aMenu->addSeparator();
157 aMenu->addAction(action("HIDE_CMD"));
159 aMenu->addAction(action("SHOW_CMD"));
161 aMenu->addAction(action("SHOW_ONLY_CMD"));
163 } else { // If feature is 0 the it means that selected root object (document)
164 if (aMgr->activeDocument() != aMgr->moduleDocument())
165 aMenu->addAction(action("ACTIVATE_PART_CMD"));
169 aMenu->addAction(action("SHOW_CMD"));
170 aMenu->addAction(action("HIDE_CMD"));
171 aMenu->addAction(action("SHOW_ONLY_CMD"));
172 aMenu->addSeparator();
173 aMenu->addAction(action("SHADING_CMD"));
174 aMenu->addAction(action("WIREFRAME_CMD"));
178 aMenu->addAction(action("DELETE_CMD"));
180 aMenu->addSeparator();
181 aMenu->addActions(myWorkshop->objectBrowser()->actions());
182 if (aMenu->actions().size() > 0) {
189 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
191 QMenu* aMenu = new QMenu();
192 addViewerItems(aMenu);
193 if (aMenu->actions().size() > 0) {
200 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
202 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
203 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
204 if (aObjects.size() > 0) {
205 //if (aObjects.size() == 1)
206 // theMenu->addAction(action("EDIT_CMD"));
207 bool isVisible = false;
208 bool isShading = false;
209 foreach(ObjectPtr aObject, aObjects)
211 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
212 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
214 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
220 theMenu->addAction(action("WIREFRAME_CMD"));
222 theMenu->addAction(action("SHADING_CMD"));
223 theMenu->addSeparator();
224 theMenu->addAction(action("SHOW_ONLY_CMD"));
225 theMenu->addAction(action("HIDE_CMD"));
227 theMenu->addAction(action("SHOW_CMD"));
228 //theMenu->addAction(action("DELETE_CMD"));
230 if (myWorkshop->displayer()->objectsCount() > 0)
231 theMenu->addAction(action("HIDEALL_CMD"));
232 if (!myWorkshop->isSalomeMode()) {
233 theMenu->addSeparator();
234 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
235 if (aMDI->actions().size() > 0) {
236 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
237 aSubMenu->addActions(aMDI->actions());
242 void XGUI_ContextMenuMgr::connectObjectBrowser() const
244 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
245 SLOT(onContextMenuRequest(QContextMenuEvent*)));
248 void XGUI_ContextMenuMgr::connectViewer() const
250 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
251 SLOT(onContextMenuRequest(QContextMenuEvent*)));