1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <XGUI_MenuMgr.h>
22 #include <XGUI_Workshop.h>
23 #include <XGUI_ActionsMgr.h>
24 #include <XGUI_OperationMgr.h>
25 #include <XGUI_MenuWorkbench.h>
26 #include <XGUI_MenuGroup.h>
28 #include <Events_Loop.h>
29 #include <Config_FeatureMessage.h>
30 #include <Config_Keywords.h>
33 #include <AppElements_Workbench.h>
34 #include <AppElements_Command.h>
35 #include <AppElements_MainMenu.h>
36 #include <AppElements_MainWindow.h>
37 #include <AppElements_MenuGroupPanel.h>
38 #include <AppElements_Button.h>
40 #include <XGUI_SalomeConnector.h>
43 #include <ModuleBase_IModule.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.";
79 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = findWorkbench(theMessage->workbenchId());
80 std::shared_ptr<XGUI_MenuGroup> aGroup = aWorkbench->findGroup(theMessage->groupId());
81 aGroup->setFeatureInfo(theMessage);
83 ActionInfo aFeatureInfo;
84 aFeatureInfo.initFrom(theMessage);
86 QString aWchName = QString::fromStdString(theMessage->workbenchId());
87 QStringList aNestedFeatures =
88 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
89 QList<QAction*> aNestedActList;
90 bool isColumnButton = !aNestedFeatures.isEmpty();
92 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
93 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
94 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
95 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
96 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
97 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
98 aNestedActList << anAction;
100 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
101 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
102 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
103 aNestedActList << anAction;
107 //Find or create Workbench
108 AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
109 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
111 aPage = myWorkshop->addWorkbench(aWchName);
113 //Find or create Group
114 QString aGroupName = QString::fromStdString(theMessage->groupId());
115 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
117 aGroup = aPage->addGroup(aGroupName);
119 // Check if hotkey sequence is already defined:
120 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
121 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
122 if(aHotKey != aFeatureInfo.shortcut) {
123 aFeatureInfo.shortcut = aHotKey;
125 AppElements_Command* aCommand = aGroup->addFeature(theMessage);
126 // Enrich created button with accept/abort buttons if necessary
127 AppElements_Button* aButton = aCommand->button();
128 if (aButton->isColumnButton()) {
129 aButton->setAdditionalButtons(aNestedActList);
131 myWorkshop->actionsMgr()->addCommand(aCommand);
132 myWorkshop->module()->actionCreated(aCommand);
136 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(const std::string& theWorkbenchName)
138 std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
139 aLast = myWorkbenches.end();
140 std::shared_ptr<XGUI_MenuWorkbench> aResultWorkbench;
141 for (; anIt != aLast && !aResultWorkbench; anIt++) {
142 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
143 if (aWorkbench->getName() == theWorkbenchName)
144 aResultWorkbench = aWorkbench;
146 if (!aResultWorkbench) {
148 std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
149 myWorkbenches.push_back(aResultWorkbench);
151 return aResultWorkbench;
154 void XGUI_MenuMgr::createFeatureActions()
157 std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
158 aLast = myWorkbenches.end();
159 XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
160 for (; anIt != aLast; anIt++) {
161 std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
162 std::string aWchName = aWorkbench->getName();
163 const std::list<std::shared_ptr<XGUI_MenuGroup> >& aGroups = aWorkbench->groups();
164 std::list<std::shared_ptr<XGUI_MenuGroup> >::const_iterator aGIt = aGroups.begin(),
165 aGLast = aGroups.end();
166 for (; aGIt != aGLast; aGIt++) {
167 const std::shared_ptr<XGUI_MenuGroup> aGroup = *aGIt;
168 std::string aGName = aGroup->getName();
169 const std::list<std::shared_ptr<Config_FeatureMessage> >& aFeaturesInfo =
170 aGroup->featuresInfo();
171 std::list<std::shared_ptr<Config_FeatureMessage> >::const_iterator aFIt =
172 aFeaturesInfo.begin(), aFLast = aFeaturesInfo.end();
173 size_t aFSize = aFeaturesInfo.size();
174 for(int i = 0; aFIt != aFLast; aFIt++, i++) {
175 std::shared_ptr<Config_FeatureMessage> aMessage = *aFIt;
176 bool aUseSeparator = i == aFSize-1;
177 QAction* aAction = buildAction(aMessage, aWchName, aUseSeparator);
179 aSalomeConnector->setFeatureInfo(QString::fromStdString(aMessage->id()), aMessage);
180 myWorkshop->actionsMgr()->addCommand(aAction);
181 myWorkshop->module()->actionCreated(aAction);
188 QAction* XGUI_MenuMgr::buildAction(const std::shared_ptr<Config_FeatureMessage>& theMessage,
189 const std::string& theWchName, const bool aUseSeparator) const
191 QAction* anAction = 0;
194 XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
196 ActionInfo aFeatureInfo;
197 aFeatureInfo.initFrom(theMessage);
198 QStringList aNestedFeatures =
199 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
200 QList<QAction*> aNestedActList;
201 if (!aNestedFeatures.isEmpty()) {
202 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
203 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
204 XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
205 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
206 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
207 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
208 aNestedActList << anAction;
210 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
211 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
212 QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
213 aNestedActList << anAction;
215 anAction = aSalomeConnector->addFeatureOfNested(theWchName.c_str(), aFeatureInfo,
219 anAction = aSalomeConnector->addFeature(theWchName.c_str(), aFeatureInfo, aUseSeparator);