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 QList<ObjectPtr> 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 bool hasGroup = false;
125 foreach(ObjectPtr aObj, aObjects)
127 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
128 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
129 ResultGroupPtr aGroupRes = boost::dynamic_pointer_cast<ModelAPI_ResultGroup>(aObj);
136 if (hasFeature && hasResult && hasGroup)
141 if (aSelected == 1) {
142 ObjectPtr aObject = aObjects.first();
144 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
146 if (aMgr->activeDocument() == aPart->partDoc())
147 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
149 aMenu->addAction(action("ACTIVATE_PART_CMD"));
150 } else if (hasFeature) {
151 aMenu->addAction(action("EDIT_CMD"));
153 if (aDisplayer->isVisible(aObject)) {
154 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
155 aMenu->addAction(action("WIREFRAME_CMD"));
157 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"));
183 aMenu->addSeparator();
184 aMenu->addActions(myWorkshop->objectBrowser()->actions());
185 if (aMenu->actions().size() > 0) {
192 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
194 QMenu* aMenu = new QMenu();
195 addViewerItems(aMenu);
196 if (aMenu->actions().size() > 0) {
203 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
205 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
206 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
207 if (aObjects.size() > 0) {
208 //if (aObjects.size() == 1)
209 // theMenu->addAction(action("EDIT_CMD"));
210 bool isVisible = false;
211 bool isShading = false;
212 foreach(ObjectPtr aObject, aObjects)
214 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
215 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
217 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
223 theMenu->addAction(action("WIREFRAME_CMD"));
225 theMenu->addAction(action("SHADING_CMD"));
226 theMenu->addSeparator();
227 theMenu->addAction(action("SHOW_ONLY_CMD"));
228 theMenu->addAction(action("HIDE_CMD"));
230 theMenu->addAction(action("SHOW_CMD"));
231 //theMenu->addAction(action("DELETE_CMD"));
233 if (myWorkshop->displayer()->objectsCount() > 0)
234 theMenu->addAction(action("HIDEALL_CMD"));
235 if (!myWorkshop->isSalomeMode()) {
236 theMenu->addSeparator();
237 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
238 if (aMDI->actions().size() > 0) {
239 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
240 aSubMenu->addActions(aMDI->actions());
245 void XGUI_ContextMenuMgr::connectObjectBrowser() const
247 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
248 SLOT(onContextMenuRequest(QContextMenuEvent*)));
251 void XGUI_ContextMenuMgr::connectViewer() const
253 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
254 SLOT(onContextMenuRequest(QContextMenuEvent*)));