2 #include "XGUI_ContextMenuMgr.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_ObjectsBrowser.h"
5 #include "XGUI_SelectionMgr.h"
6 #include "XGUI_Displayer.h"
7 #include "XGUI_MainWindow.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Selection.h"
11 #include "PartSetPlugin_Part.h"
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <ModelAPI_Object.h>
18 #include <QContextMenuEvent>
22 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
23 QObject(theParent), myWorkshop(theParent)
27 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
31 void XGUI_ContextMenuMgr::createActions()
33 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
34 addAction("EDIT_CMD", aAction);
36 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
37 addAction("ACTIVATE_PART_CMD", aAction);
39 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
40 addAction("DEACTIVATE_PART_CMD", aAction);
42 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
43 addAction("DELETE_CMD", aAction);
45 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
46 addAction("SHOW_CMD", aAction);
48 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
49 addAction("HIDE_CMD", aAction);
52 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
54 if (myActions.contains(theId))
55 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
56 theAction->setData(theId);
57 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
58 myActions[theId] = theAction;
61 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
63 if (myActions.contains(theId))
64 return myActions[theId];
68 QStringList XGUI_ContextMenuMgr::actionIds() const
70 return myActions.keys();
73 void XGUI_ContextMenuMgr::onAction(bool isChecked)
75 QAction* aAction = static_cast<QAction*>(sender());
76 emit actionTriggered(aAction->data().toString(), isChecked);
79 void XGUI_ContextMenuMgr::updateCommandsStatus()
83 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
86 if (sender() == myWorkshop->objectBrowser())
87 aMenu = objectBrowserMenu();
88 else if (sender() == myWorkshop->viewer()) {
92 if (aMenu && (aMenu->actions().size() > 0)) {
93 aMenu->exec(theEvent->globalPos());
98 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
100 QMenu* aMenu = new QMenu();
101 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
102 QFeatureList aFeatures = aSelMgr->selection()->selectedFeatures();
103 if (aFeatures.size() == 1) {
104 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
105 FeaturePtr aFeature = aFeatures.first();
108 if (aFeature->getKind() == PARTSET_PART_KIND) {
109 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
110 DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
111 if (aMgr->currentDocument() == aFeaDoc)
112 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
114 aMenu->addAction(action("ACTIVATE_PART_CMD"));
116 aMenu->addAction(action("EDIT_CMD"));
118 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
119 if (aDisplayer->isVisible(aFeature))
120 aMenu->addAction(action("HIDE_CMD"));
122 aMenu->addAction(action("SHOW_CMD"));
124 aMenu->addAction(action("DELETE_CMD"));
125 aMenu->addSeparator();
127 // Process Root object (document)
128 } else { // If feature is 0 the it means that selected root object (document)
129 if (aMgr->currentDocument() != aMgr->rootDocument()) {
130 aMenu->addAction(action("ACTIVATE_PART_CMD"));
134 aMenu->addActions(myWorkshop->objectBrowser()->actions());
135 if (aMenu->actions().size() > 0) {
142 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
144 QMenu* aMenu = new QMenu();
145 addViewerItems(aMenu);
146 if (aMenu->actions().size() > 0) {
153 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
155 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
156 QFeatureList aFeatures = aSelMgr->selection()->selectedFeatures();
157 if (aFeatures.size() > 0) {
158 if (aFeatures.size() == 1)
159 theMenu->addAction(action("EDIT_CMD"));
160 bool isVisible = false;
161 foreach(FeaturePtr aFeature, aFeatures) {
162 if (myWorkshop->displayer()->isVisible(aFeature)) {
168 theMenu->addAction(action("HIDE_CMD"));
170 theMenu->addAction(action("SHOW_CMD"));
171 theMenu->addAction(action("DELETE_CMD"));
173 if (!myWorkshop->isSalomeMode()) {
174 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
175 if (aMDI->actions().size() > 0) {
176 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
177 aSubMenu->addActions(aMDI->actions());
182 void XGUI_ContextMenuMgr::connectObjectBrowser() const
184 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
185 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
188 void XGUI_ContextMenuMgr::connectViewer() const
190 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
191 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));