1 #include "XGUI_ContextMenuMgr.h"
2 #include "XGUI_Workshop.h"
3 #include "XGUI_ObjectsBrowser.h"
4 #include "XGUI_SelectionMgr.h"
5 #include "XGUI_Displayer.h"
6 #include "XGUI_MainWindow.h"
7 #include "XGUI_ViewerProxy.h"
8 #include "XGUI_Selection.h"
10 #include "PartSetPlugin_Part.h"
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_ResultPart.h>
16 #include <ModelAPI_Session.h>
17 #include <ModelAPI_ResultGroup.h>
20 #include <QContextMenuEvent>
24 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
30 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
34 void XGUI_ContextMenuMgr::createActions()
36 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
37 addAction("EDIT_CMD", aAction);
39 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
40 addAction("ACTIVATE_PART_CMD", aAction);
42 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
43 addAction("DEACTIVATE_PART_CMD", aAction);
45 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
46 addAction("DELETE_CMD", aAction);
48 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
49 addAction("SHOW_CMD", aAction);
51 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
52 addAction("SHOW_ONLY_CMD", aAction);
54 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
55 addAction("HIDE_CMD", aAction);
57 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
58 addAction("HIDEALL_CMD", aAction);
60 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
61 addAction("SHADING_CMD", aAction);
63 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
64 addAction("WIREFRAME_CMD", aAction);
67 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
69 if (myActions.contains(theId))
70 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
71 theAction->setData(theId);
72 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
73 myActions[theId] = theAction;
76 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
78 if (myActions.contains(theId))
79 return myActions[theId];
83 QStringList XGUI_ContextMenuMgr::actionIds() const
85 return myActions.keys();
88 void XGUI_ContextMenuMgr::onAction(bool isChecked)
90 QAction* aAction = static_cast<QAction*>(sender());
91 emit actionTriggered(aAction->data().toString(), isChecked);
94 void XGUI_ContextMenuMgr::updateCommandsStatus()
98 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
101 if (sender() == myWorkshop->objectBrowser())
102 aMenu = objectBrowserMenu();
103 else if (sender() == myWorkshop->viewer()) {
104 aMenu = viewerMenu();
107 if (aMenu && (aMenu->actions().size() > 0)) {
108 aMenu->exec(theEvent->globalPos());
113 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
115 QMenu* aMenu = new QMenu();
116 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
117 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
118 int aSelected = aObjects.size();
120 SessionPtr aMgr = ModelAPI_Session::get();
121 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
122 bool hasResult = false;
123 bool hasFeature = false;
124 foreach(ObjectPtr aObj, aObjects)
126 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
127 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
132 if (hasFeature && hasResult) // && hasGroup)
136 if (aSelected == 1) {
137 ObjectPtr aObject = aObjects.first();
139 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
141 if (aMgr->activeDocument() == aPart->partDoc())
142 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
144 aMenu->addAction(action("ACTIVATE_PART_CMD"));
145 } else if (hasFeature) {
146 aMenu->addAction(action("EDIT_CMD"));
148 if (aDisplayer->isVisible(aObject)) {
149 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
150 aMenu->addAction(action("WIREFRAME_CMD"));
152 aMenu->addAction(action("SHADING_CMD"));
153 aMenu->addSeparator();
154 aMenu->addAction(action("HIDE_CMD"));
156 aMenu->addAction(action("SHOW_CMD"));
158 aMenu->addAction(action("SHOW_ONLY_CMD"));
160 } else { // If feature is 0 the it means that selected root object (document)
161 if (aMgr->activeDocument() != aMgr->moduleDocument())
162 aMenu->addAction(action("ACTIVATE_PART_CMD"));
166 aMenu->addAction(action("SHOW_CMD"));
167 aMenu->addAction(action("HIDE_CMD"));
168 aMenu->addAction(action("SHOW_ONLY_CMD"));
169 aMenu->addSeparator();
170 aMenu->addAction(action("SHADING_CMD"));
171 aMenu->addAction(action("WIREFRAME_CMD"));
175 aMenu->addAction(action("DELETE_CMD"));
177 aMenu->addSeparator();
178 aMenu->addActions(myWorkshop->objectBrowser()->actions());
179 if (aMenu->actions().size() > 0) {
186 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
188 QMenu* aMenu = new QMenu();
189 addViewerItems(aMenu);
190 if (aMenu->actions().size() > 0) {
197 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
199 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
200 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
201 if (aObjects.size() > 0) {
202 //if (aObjects.size() == 1)
203 // theMenu->addAction(action("EDIT_CMD"));
204 bool isVisible = false;
205 bool isShading = false;
206 foreach(ObjectPtr aObject, aObjects)
208 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
209 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
211 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
217 theMenu->addAction(action("WIREFRAME_CMD"));
219 theMenu->addAction(action("SHADING_CMD"));
220 theMenu->addSeparator();
221 theMenu->addAction(action("SHOW_ONLY_CMD"));
222 theMenu->addAction(action("HIDE_CMD"));
224 theMenu->addAction(action("SHOW_CMD"));
225 //theMenu->addAction(action("DELETE_CMD"));
227 if (myWorkshop->displayer()->objectsCount() > 0)
228 theMenu->addAction(action("HIDEALL_CMD"));
229 if (!myWorkshop->isSalomeMode()) {
230 theMenu->addSeparator();
231 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
232 if (aMDI->actions().size() > 0) {
233 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
234 aSubMenu->addActions(aMDI->actions());
239 void XGUI_ContextMenuMgr::connectObjectBrowser() const
241 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
242 SLOT(onContextMenuRequest(QContextMenuEvent*)));
245 void XGUI_ContextMenuMgr::connectViewer() const
247 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
248 SLOT(onContextMenuRequest(QContextMenuEvent*)));