]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_MenuMgr.cpp
Salome HOME
Translate title of toolbars.
[modules/shaper.git] / src / XGUI / XGUI_MenuMgr.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
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>
26
27 #include <Events_Loop.h>
28 #include <Config_FeatureMessage.h>
29 #include <Config_Keywords.h>
30
31 #ifndef HAVE_SALOME
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>
38 #else
39 #include <XGUI_SalomeConnector.h>
40 #endif
41
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Tools.h>
44
45 #include <QObject>
46 #include <QAction>
47 #include <QDebug>
48
49 XGUI_MenuMgr::XGUI_MenuMgr(XGUI_Workshop* theWorkshop)
50 : myWorkshop(theWorkshop)
51 {
52   Events_Loop* aLoop = Events_Loop::loop();
53
54   aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
55 }
56
57 void XGUI_MenuMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
58 {
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);
66     }
67   }
68 }
69
70 void XGUI_MenuMgr::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
71 {
72   if (!theMessage) {
73 #ifdef _DEBUG
74     qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
75 #endif
76     return;
77   }
78   QString aWchName = ModuleBase_Tools::translate("workshop", theMessage->workbenchId());
79   theMessage->setToolBarId(ModuleBase_Tools::translate("workshop", theMessage->workbenchId()).toStdString());
80 #ifdef HAVE_SALOME
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);
85 #else
86   ActionInfo aFeatureInfo;
87   aFeatureInfo.initFrom(theMessage);
88
89   QStringList aNestedFeatures =
90       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
91   QList<QAction*> aNestedActList;
92   bool isColumnButton = !aNestedFeatures.isEmpty();
93   if (isColumnButton) {
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;
101     }
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;
106     }
107   }
108
109   //Find or create Workbench
110   AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
111   AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
112   if (!aPage) {
113     aPage = myWorkshop->addWorkbench(aWchName);
114   }
115   //Find or create Group
116   QString aGroupName = QString::fromStdString(theMessage->groupId());
117   AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
118   if (!aGroup) {
119     aGroup = aPage->addGroup(aGroupName);
120   }
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;
126   }
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);
132   }
133   myWorkshop->actionsMgr()->addCommand(aCommand);
134   myWorkshop->module()->actionCreated(aCommand);
135 #endif
136 }
137
138 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(const std::string& theWorkbenchName)
139 {
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;
147   }
148   if (!aResultWorkbench) {
149     aResultWorkbench =
150       std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
151     myWorkbenches.push_back(aResultWorkbench);
152   }
153   return aResultWorkbench;
154 }
155
156 void XGUI_MenuMgr::createFeatureActions()
157 {
158 #ifdef HAVE_SALOME
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);
180
181         aSalomeConnector->setFeatureInfo(QString::fromStdString(aMessage->id()), aMessage);
182         myWorkshop->actionsMgr()->addCommand(aAction);
183         myWorkshop->module()->actionCreated(aAction);
184       }
185     }
186   }
187 #endif
188 }
189
190 QAction* XGUI_MenuMgr::buildAction(const std::shared_ptr<Config_FeatureMessage>& theMessage,
191                                    const std::string& theWchName, const bool aUseSeparator) const
192 {
193   QAction* anAction = 0;
194
195 #ifdef HAVE_SALOME
196   XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
197
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;
211     }
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;
216     }
217     anAction = aSalomeConnector->addFeatureOfNested(theWchName.c_str(), aFeatureInfo,
218                                                     aNestedActList);
219   }
220   else {
221     anAction = aSalomeConnector->addFeature(theWchName.c_str(), aFeatureInfo, aUseSeparator);
222   }
223 #endif
224   return anAction;
225 }