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_ViewerProxy.h"
7 #include "XGUI_Selection.h"
9 #include <AppElements_MainWindow.h>
11 //#include "PartSetPlugin_Part.h"
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_ResultPart.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_ResultGroup.h>
21 #include <QContextMenuEvent>
25 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
31 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
35 void XGUI_ContextMenuMgr::createActions()
37 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
38 addAction("EDIT_CMD", aAction);
40 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
41 addAction("ACTIVATE_PART_CMD", aAction);
43 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
44 addAction("DEACTIVATE_PART_CMD", aAction);
46 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
47 addAction("DELETE_CMD", aAction);
49 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
50 addAction("SHOW_CMD", aAction);
52 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
53 addAction("SHOW_ONLY_CMD", aAction);
55 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
56 addAction("HIDE_CMD", aAction);
58 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
59 addAction("HIDEALL_CMD", aAction);
61 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
62 addAction("SHADING_CMD", aAction);
64 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
65 addAction("WIREFRAME_CMD", aAction);
68 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
70 if (myActions.contains(theId))
71 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
72 theAction->setData(theId);
73 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
74 myActions[theId] = theAction;
77 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
79 if (myActions.contains(theId))
80 return myActions[theId];
84 QStringList XGUI_ContextMenuMgr::actionIds() const
86 return myActions.keys();
89 void XGUI_ContextMenuMgr::onAction(bool isChecked)
91 QAction* aAction = static_cast<QAction*>(sender());
92 emit actionTriggered(aAction->data().toString(), isChecked);
95 void XGUI_ContextMenuMgr::updateCommandsStatus()
99 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
102 if (sender() == myWorkshop->objectBrowser())
103 aMenu = objectBrowserMenu();
104 else if (sender() == myWorkshop->viewer()) {
105 aMenu = viewerMenu();
108 if (aMenu && (aMenu->actions().size() > 0)) {
109 aMenu->exec(theEvent->globalPos());
114 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
116 QMenu* aMenu = new QMenu();
117 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
118 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
119 int aSelected = aObjects.size();
121 SessionPtr aMgr = ModelAPI_Session::get();
122 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
123 bool hasResult = false;
124 bool hasFeature = false;
125 foreach(ObjectPtr aObj, aObjects)
127 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
128 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
133 if (hasFeature && hasResult) // && hasGroup)
137 if (aSelected == 1) {
138 ObjectPtr aObject = aObjects.first();
140 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
142 if (aMgr->activeDocument() == aPart->partDoc())
143 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
145 aMenu->addAction(action("ACTIVATE_PART_CMD"));
146 } else if (hasFeature) {
147 aMenu->addAction(action("EDIT_CMD"));
149 if (aDisplayer->isVisible(aObject)) {
150 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
151 aMenu->addAction(action("WIREFRAME_CMD"));
153 aMenu->addAction(action("SHADING_CMD"));
154 aMenu->addSeparator();
155 aMenu->addAction(action("HIDE_CMD"));
157 aMenu->addAction(action("SHOW_CMD"));
159 aMenu->addAction(action("SHOW_ONLY_CMD"));
161 } else { // If feature is 0 the it means that selected root object (document)
162 if (aMgr->activeDocument() != aMgr->moduleDocument())
163 aMenu->addAction(action("ACTIVATE_PART_CMD"));
167 aMenu->addAction(action("SHOW_CMD"));
168 aMenu->addAction(action("HIDE_CMD"));
169 aMenu->addAction(action("SHOW_ONLY_CMD"));
170 aMenu->addSeparator();
171 aMenu->addAction(action("SHADING_CMD"));
172 aMenu->addAction(action("WIREFRAME_CMD"));
176 aMenu->addAction(action("DELETE_CMD"));
178 aMenu->addSeparator();
179 aMenu->addActions(myWorkshop->objectBrowser()->actions());
180 if (aMenu->actions().size() > 0) {
187 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
189 QMenu* aMenu = new QMenu();
190 addViewerItems(aMenu);
191 if (aMenu->actions().size() > 0) {
198 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
200 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
201 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
202 if (aObjects.size() > 0) {
203 //if (aObjects.size() == 1)
204 // theMenu->addAction(action("EDIT_CMD"));
205 bool isVisible = false;
206 bool isShading = false;
207 foreach(ObjectPtr aObject, aObjects)
209 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
210 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
212 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
218 theMenu->addAction(action("WIREFRAME_CMD"));
220 theMenu->addAction(action("SHADING_CMD"));
221 theMenu->addSeparator();
222 theMenu->addAction(action("SHOW_ONLY_CMD"));
223 theMenu->addAction(action("HIDE_CMD"));
225 theMenu->addAction(action("SHOW_CMD"));
226 //theMenu->addAction(action("DELETE_CMD"));
228 if (myWorkshop->displayer()->objectsCount() > 0)
229 theMenu->addAction(action("HIDEALL_CMD"));
230 if (!myWorkshop->isSalomeMode()) {
231 theMenu->addSeparator();
232 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
233 if (aMDI->actions().size() > 0) {
234 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
235 aSubMenu->addActions(aMDI->actions());
240 void XGUI_ContextMenuMgr::connectObjectBrowser() const
242 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
243 SLOT(onContextMenuRequest(QContextMenuEvent*)));
246 void XGUI_ContextMenuMgr::connectViewer() const
248 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
249 SLOT(onContextMenuRequest(QContextMenuEvent*)));