1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: XGUI_MenuMgr.cpp
4 // Created: 13 Apr 2016
5 // Author: Natalia ERMOLAEVA
7 #include <XGUI_MenuMgr.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_ActionsMgr.h>
10 #include <XGUI_OperationMgr.h>
11 #include <XGUI_MenuWorkbench.h>
12 #include <XGUI_MenuGroup.h>
14 #include <Events_Loop.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_Keywords.h>
19 #include <AppElements_Workbench.h>
20 #include <AppElements_Command.h>
21 #include <AppElements_MainMenu.h>
22 #include <AppElements_MainWindow.h>
23 #include <AppElements_MenuGroupPanel.h>
24 #include <AppElements_Button.h>
26 #include <XGUI_SalomeConnector.h>
29 #include <ModuleBase_IModule.h>
35 XGUI_MenuMgr::XGUI_MenuMgr(XGUI_Workshop* theWorkshop)
36 : myWorkshop(theWorkshop)
38 Events_Loop* aLoop = Events_Loop::loop();
40 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
43 void XGUI_MenuMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
45 //A message to start feature creation received.
46 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
47 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
48 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
49 if (!aFeatureMsg->isInternal()) {
50 addFeature(aFeatureMsg);
55 void XGUI_MenuMgr::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
59 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
64 std::string aWchName = theMessage->workbenchId();
65 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = findWorkbench(aWchName);
67 std::string aGroupName = theMessage->groupId();
68 std::shared_ptr<XGUI_MenuGroup> aGroup = aWorkbench->findGroup(aGroupName);
70 aGroup->setFeatureInfo(theMessage);
73 ActionInfo aFeatureInfo;
74 aFeatureInfo.initFrom(theMessage);
76 QString aWchName = QString::fromStdString(theMessage->workbenchId());
77 QStringList aNestedFeatures =
78 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
79 QList<QAction*> aNestedActList;
80 bool isColumnButton = !aNestedFeatures.isEmpty();
82 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
83 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
84 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
85 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
86 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
87 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
88 aNestedActList << anAction;
90 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
91 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
92 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
93 aNestedActList << anAction;
98 XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
100 if (isColumnButton) {
101 aAction = aSalomeConnector->addFeatureOfNested(aWchName, aFeatureInfo, aNestedActList);
103 //Issue #650: in the SALOME mode the tooltip should be same as text
104 aFeatureInfo.toolTip = aFeatureInfo.text;
105 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
107 aSalomeConnector->setFeatureInfo(aFeatureInfo.id, theMessage);
109 myWorkshop->actionsMgr()->addCommand(aAction);
110 myWorkshop->module()->actionCreated(aAction);
112 //Find or create Workbench
113 AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
114 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
116 aPage = myWorkshop->addWorkbench(aWchName);
118 //Find or create Group
119 QString aGroupName = QString::fromStdString(theMessage->groupId());
120 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
122 aGroup = aPage->addGroup(aGroupName);
124 // Check if hotkey sequence is already defined:
125 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
126 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
127 if(aHotKey != aFeatureInfo.shortcut) {
128 aFeatureInfo.shortcut = aHotKey;
130 AppElements_Command* aCommand = aGroup->addFeature(theMessage);
131 // Enrich created button with accept/abort buttons if necessary
132 AppElements_Button* aButton = aCommand->button();
133 if (aButton->isColumnButton()) {
134 aButton->setAdditionalButtons(aNestedActList);
136 myWorkshop->actionsMgr()->addCommand(aCommand);
137 myWorkshop->module()->actionCreated(aCommand);
141 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(std::string& theWorkbenchName)
143 std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
144 aLast = myWorkbenches.end();
145 std::shared_ptr<XGUI_MenuWorkbench> aResultWorkbench;
146 for (; anIt != aLast && !aResultWorkbench; anIt++) {
147 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
148 if (aWorkbench->getName() == theWorkbenchName)
149 aResultWorkbench = aWorkbench;
151 if (!aResultWorkbench) {
152 aResultWorkbench = std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
153 myWorkbenches.push_back(aResultWorkbench);
155 return aResultWorkbench;