1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <XGUI_MenuMgr.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_ActionsMgr.h>
23 #include <XGUI_OperationMgr.h>
24 #include <XGUI_MenuWorkbench.h>
25 #include <XGUI_MenuGroup.h>
27 #include <Events_Loop.h>
28 #include <Config_FeatureMessage.h>
29 #include <Config_Keywords.h>
32 #include <AppElements_Workbench.h>
33 #include <AppElements_Command.h>
34 #include <AppElements_MainMenu.h>
35 #include <AppElements_MainWindow.h>
36 #include <AppElements_MenuGroupPanel.h>
37 #include <AppElements_Button.h>
39 #include <XGUI_SalomeConnector.h>
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Tools.h>
49 XGUI_MenuMgr::XGUI_MenuMgr(XGUI_Workshop* theWorkshop)
50 : myWorkshop(theWorkshop)
52 Events_Loop* aLoop = Events_Loop::loop();
54 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
57 void XGUI_MenuMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
59 //A message to start feature creation received.
60 if (theMessage->eventID() ==
61 Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
62 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
63 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
64 if (!aFeatureMsg->isInternal()) {
65 addFeature(aFeatureMsg);
70 void XGUI_MenuMgr::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
74 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
78 QString aWchName = ModuleBase_Tools::translate("workshop", theMessage->workbenchId());
79 theMessage->setToolBarId(ModuleBase_Tools::translate("workshop", theMessage->workbenchId()).toStdString());
81 std::string aWchNameString = aWchName.toStdString();
82 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = findWorkbench(aWchNameString);
83 std::shared_ptr<XGUI_MenuGroup> aGroup = aWorkbench->findGroup(theMessage->groupId());
84 aGroup->setFeatureInfo(theMessage);
86 ActionInfo aFeatureInfo;
87 aFeatureInfo.initFrom(theMessage);
89 QStringList aNestedFeatures =
90 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
91 QList<QAction*> aNestedActList;
92 bool isColumnButton = !aNestedFeatures.isEmpty();
94 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
95 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
96 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
97 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
98 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
99 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
100 aNestedActList << anAction;
102 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
103 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
104 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
105 aNestedActList << anAction;
109 //Find or create Workbench
110 AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
111 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
113 aPage = myWorkshop->addWorkbench(aWchName);
115 //Find or create Group
116 QString aGroupName = QString::fromStdString(theMessage->groupId());
117 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
119 aGroup = aPage->addGroup(aGroupName);
121 // Check if hotkey sequence is already defined:
122 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
123 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
124 if(aHotKey != aFeatureInfo.shortcut) {
125 aFeatureInfo.shortcut = aHotKey;
127 AppElements_Command* aCommand = aGroup->addFeature(theMessage);
128 // Enrich created button with accept/abort buttons if necessary
129 AppElements_Button* aButton = aCommand->button();
130 if (aButton->isColumnButton()) {
131 aButton->setAdditionalButtons(aNestedActList);
133 myWorkshop->actionsMgr()->addCommand(aCommand);
134 myWorkshop->module()->actionCreated(aCommand);
138 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(const std::string& theWorkbenchName)
140 std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
141 aLast = myWorkbenches.end();
142 std::shared_ptr<XGUI_MenuWorkbench> aResultWorkbench;
143 for (; anIt != aLast && !aResultWorkbench; anIt++) {
144 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
145 if (aWorkbench->getName() == theWorkbenchName)
146 aResultWorkbench = aWorkbench;
148 if (!aResultWorkbench) {
150 std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
151 myWorkbenches.push_back(aResultWorkbench);
153 return aResultWorkbench;
156 void XGUI_MenuMgr::createFeatureActions()
159 std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
160 aLast = myWorkbenches.end();
161 XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
162 for (; anIt != aLast; anIt++) {
163 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
164 std::string aWchName = aWorkbench->getName();
165 const std::list<std::shared_ptr<XGUI_MenuGroup> >& aGroups = aWorkbench->groups();
166 std::list<std::shared_ptr<XGUI_MenuGroup> >::const_iterator aGIt = aGroups.begin(),
167 aGLast = aGroups.end();
168 for (; aGIt != aGLast; aGIt++) {
169 const std::shared_ptr<XGUI_MenuGroup> aGroup = *aGIt;
170 std::string aGName = aGroup->getName();
171 const std::list<std::shared_ptr<Config_FeatureMessage> >& aFeaturesInfo =
172 aGroup->featuresInfo();
173 std::list<std::shared_ptr<Config_FeatureMessage> >::const_iterator aFIt =
174 aFeaturesInfo.begin(), aFLast = aFeaturesInfo.end();
175 size_t aFSize = aFeaturesInfo.size();
176 for(int i = 0; aFIt != aFLast; aFIt++, i++) {
177 std::shared_ptr<Config_FeatureMessage> aMessage = *aFIt;
178 bool aUseSeparator = i == aFSize-1;
179 QAction* aAction = buildAction(aMessage, aWchName, aUseSeparator);
181 aSalomeConnector->setFeatureInfo(QString::fromStdString(aMessage->id()), aMessage);
182 myWorkshop->actionsMgr()->addCommand(aAction);
183 myWorkshop->module()->actionCreated(aAction);
190 QAction* XGUI_MenuMgr::buildAction(const std::shared_ptr<Config_FeatureMessage>& theMessage,
191 const std::string& theWchName, const bool aUseSeparator) const
193 QAction* anAction = 0;
196 XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
198 ActionInfo aFeatureInfo;
199 aFeatureInfo.initFrom(theMessage);
200 QStringList aNestedFeatures =
201 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
202 QList<QAction*> aNestedActList;
203 if (!aNestedFeatures.isEmpty()) {
204 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
205 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
206 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
207 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
208 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
209 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
210 aNestedActList << anAction;
212 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
213 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
214 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
215 aNestedActList << anAction;
217 anAction = aSalomeConnector->addFeatureOfNested(theWchName.c_str(), aFeatureInfo,
221 anAction = aSalomeConnector->addFeature(theWchName.c_str(), aFeatureInfo, aUseSeparator);