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>
19 #include <QContextMenuEvent>
23 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
29 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
33 void XGUI_ContextMenuMgr::createActions()
35 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
36 addAction("EDIT_CMD", aAction);
38 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
39 addAction("ACTIVATE_PART_CMD", aAction);
41 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
42 addAction("DEACTIVATE_PART_CMD", aAction);
44 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
45 addAction("DELETE_CMD", aAction);
47 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
48 addAction("SHOW_CMD", aAction);
50 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
51 addAction("SHOW_ONLY_CMD", aAction);
53 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
54 addAction("HIDE_CMD", aAction);
56 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
57 addAction("HIDEALL_CMD", aAction);
59 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
60 addAction("SHADING_CMD", aAction);
62 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
63 addAction("WIREFRAME_CMD", aAction);
66 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
68 if (myActions.contains(theId))
69 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
70 theAction->setData(theId);
71 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
72 myActions[theId] = theAction;
75 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
77 if (myActions.contains(theId))
78 return myActions[theId];
82 QStringList XGUI_ContextMenuMgr::actionIds() const
84 return myActions.keys();
87 void XGUI_ContextMenuMgr::onAction(bool isChecked)
89 QAction* aAction = static_cast<QAction*>(sender());
90 emit actionTriggered(aAction->data().toString(), isChecked);
93 void XGUI_ContextMenuMgr::updateCommandsStatus()
97 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
100 if (sender() == myWorkshop->objectBrowser())
101 aMenu = objectBrowserMenu();
102 else if (sender() == myWorkshop->viewer()) {
103 aMenu = viewerMenu();
106 if (aMenu && (aMenu->actions().size() > 0)) {
107 aMenu->exec(theEvent->globalPos());
112 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
114 QMenu* aMenu = new QMenu();
115 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
116 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
117 int aSelected = aObjects.size();
119 SessionPtr aMgr = ModelAPI_Session::get();
120 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
121 bool hasResult = false;
122 bool hasFeature = false;
123 foreach(ObjectPtr aObj, aObjects)
125 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
126 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
131 if (hasFeature && hasResult)
135 if (aSelected == 1) {
136 ObjectPtr aObject = aObjects.first();
138 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
140 if (aMgr->activeDocument() == aPart->partDoc())
141 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
143 aMenu->addAction(action("ACTIVATE_PART_CMD"));
144 } else if (hasFeature) {
145 aMenu->addAction(action("EDIT_CMD"));
147 if (aDisplayer->isVisible(aObject)) {
148 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
149 aMenu->addAction(action("WIREFRAME_CMD"));
151 aMenu->addAction(action("SHADING_CMD"));
152 aMenu->addSeparator();
153 aMenu->addAction(action("HIDE_CMD"));
155 aMenu->addAction(action("SHOW_CMD"));
157 aMenu->addAction(action("SHOW_ONLY_CMD"));
159 } else { // If feature is 0 the it means that selected root object (document)
160 if (aMgr->activeDocument() != aMgr->moduleDocument())
161 aMenu->addAction(action("ACTIVATE_PART_CMD"));
165 aMenu->addAction(action("SHOW_CMD"));
166 aMenu->addAction(action("HIDE_CMD"));
167 aMenu->addAction(action("SHOW_ONLY_CMD"));
168 aMenu->addSeparator();
169 aMenu->addAction(action("SHADING_CMD"));
170 aMenu->addAction(action("WIREFRAME_CMD"));
174 aMenu->addAction(action("DELETE_CMD"));
176 aMenu->addSeparator();
177 aMenu->addActions(myWorkshop->objectBrowser()->actions());
178 if (aMenu->actions().size() > 0) {
185 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
187 QMenu* aMenu = new QMenu();
188 addViewerItems(aMenu);
189 if (aMenu->actions().size() > 0) {
196 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
198 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
199 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
200 if (aObjects.size() > 0) {
201 //if (aObjects.size() == 1)
202 // theMenu->addAction(action("EDIT_CMD"));
203 bool isVisible = false;
204 bool isShading = false;
205 foreach(ObjectPtr aObject, aObjects)
207 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
208 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
210 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
216 theMenu->addAction(action("WIREFRAME_CMD"));
218 theMenu->addAction(action("SHADING_CMD"));
219 theMenu->addSeparator();
220 theMenu->addAction(action("SHOW_ONLY_CMD"));
221 theMenu->addAction(action("HIDE_CMD"));
223 theMenu->addAction(action("SHOW_CMD"));
224 //theMenu->addAction(action("DELETE_CMD"));
226 if (myWorkshop->displayer()->objectsCount() > 0)
227 theMenu->addAction(action("HIDEALL_CMD"));
228 if (!myWorkshop->isSalomeMode()) {
229 theMenu->addSeparator();
230 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
231 if (aMDI->actions().size() > 0) {
232 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
233 aSubMenu->addActions(aMDI->actions());
238 void XGUI_ContextMenuMgr::connectObjectBrowser() const
240 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
241 SLOT(onContextMenuRequest(QContextMenuEvent*)));
244 void XGUI_ContextMenuMgr::connectViewer() const
246 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
247 SLOT(onContextMenuRequest(QContextMenuEvent*)));