]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_MenuMgr.cpp
Salome HOME
Merge remote-tracking branch 'origin/CEA_2019_TranslationToFrench' into CEA_2019
[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 #ifdef HAVE_SALOME
80   std::string aWchNameString = aWchName.toStdString();
81   std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = findWorkbench(aWchNameString);
82   std::shared_ptr<XGUI_MenuGroup> aGroup = aWorkbench->findGroup(theMessage->groupId());
83   aGroup->setFeatureInfo(theMessage);
84 #else
85   ActionInfo aFeatureInfo;
86   aFeatureInfo.initFrom(theMessage);
87
88   QStringList aNestedFeatures =
89       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
90   QList<QAction*> aNestedActList;
91   bool isColumnButton = !aNestedFeatures.isEmpty();
92   if (isColumnButton) {
93     QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
94     XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
95     XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
96     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
97       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
98       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
99       aNestedActList << anAction;
100     }
101     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
102       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
103       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
104       aNestedActList << anAction;
105     }
106   }
107
108   //Find or create Workbench
109   AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
110   AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
111   if (!aPage) {
112     aPage = myWorkshop->addWorkbench(aWchName);
113   }
114   //Find or create Group
115   QString aGroupName = QString::fromStdString(theMessage->groupId());
116   AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
117   if (!aGroup) {
118     aGroup = aPage->addGroup(aGroupName);
119   }
120   // Check if hotkey sequence is already defined:
121   XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
122   QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
123   if(aHotKey != aFeatureInfo.shortcut) {
124     aFeatureInfo.shortcut = aHotKey;
125   }
126   AppElements_Command* aCommand = aGroup->addFeature(theMessage);
127   // Enrich created button with accept/abort buttons if necessary
128   AppElements_Button* aButton = aCommand->button();
129   if (aButton->isColumnButton()) {
130     aButton->setAdditionalButtons(aNestedActList);
131   }
132   myWorkshop->actionsMgr()->addCommand(aCommand);
133   myWorkshop->module()->actionCreated(aCommand);
134 #endif
135 }
136
137 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(const std::string& theWorkbenchName)
138 {
139   std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
140                                                                    aLast = myWorkbenches.end();
141   std::shared_ptr<XGUI_MenuWorkbench> aResultWorkbench;
142   for (; anIt != aLast && !aResultWorkbench; anIt++) {
143     std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
144     if (aWorkbench->getName() == theWorkbenchName)
145       aResultWorkbench = aWorkbench;
146   }
147   if (!aResultWorkbench) {
148     aResultWorkbench =
149       std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
150     myWorkbenches.push_back(aResultWorkbench);
151   }
152   return aResultWorkbench;
153 }
154
155 void XGUI_MenuMgr::createFeatureActions()
156 {
157 #ifdef HAVE_SALOME
158   std::list< std::shared_ptr<XGUI_MenuWorkbench> >::const_iterator anIt = myWorkbenches.begin(),
159                                                                    aLast = myWorkbenches.end();
160   XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
161   for (; anIt != aLast; anIt++) {
162     std::shared_ptr<XGUI_MenuWorkbench> aWorkbench = *anIt;
163     std::string aWchName = aWorkbench->getName();
164     const std::list<std::shared_ptr<XGUI_MenuGroup> >& aGroups = aWorkbench->groups();
165     std::list<std::shared_ptr<XGUI_MenuGroup> >::const_iterator aGIt = aGroups.begin(),
166                                                                 aGLast = aGroups.end();
167     for (; aGIt != aGLast; aGIt++) {
168       const std::shared_ptr<XGUI_MenuGroup> aGroup = *aGIt;
169       std::string aGName = aGroup->getName();
170       const std::list<std::shared_ptr<Config_FeatureMessage> >& aFeaturesInfo =
171         aGroup->featuresInfo();
172       std::list<std::shared_ptr<Config_FeatureMessage> >::const_iterator aFIt =
173         aFeaturesInfo.begin(), aFLast = aFeaturesInfo.end();
174       size_t aFSize = aFeaturesInfo.size();
175       for(int i = 0; aFIt != aFLast; aFIt++, i++) {
176         std::shared_ptr<Config_FeatureMessage> aMessage = *aFIt;
177         bool aUseSeparator = i == aFSize-1;
178         QAction* aAction = buildAction(aMessage, aWchName, aUseSeparator);
179
180         aSalomeConnector->setFeatureInfo(QString::fromStdString(aMessage->id()), aMessage);
181         myWorkshop->actionsMgr()->addCommand(aAction);
182         myWorkshop->module()->actionCreated(aAction);
183       }
184     }
185   }
186 #endif
187 }
188
189 QAction* XGUI_MenuMgr::buildAction(const std::shared_ptr<Config_FeatureMessage>& theMessage,
190                                    const std::string& theWchName, const bool aUseSeparator) const
191 {
192   QAction* anAction = 0;
193
194 #ifdef HAVE_SALOME
195   XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
196
197   ActionInfo aFeatureInfo;
198   aFeatureInfo.initFrom(theMessage);
199   QStringList aNestedFeatures =
200       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
201   QList<QAction*> aNestedActList;
202   if (!aNestedFeatures.isEmpty()) {
203     QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
204     XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
205     XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
206     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
207       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
208       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
209       aNestedActList << anAction;
210     }
211     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
212       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
213       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(onAbortAllOperation()));
214       aNestedActList << anAction;
215     }
216     anAction = aSalomeConnector->addFeatureOfNested(theWchName.c_str(), aFeatureInfo,
217                                                     aNestedActList);
218   }
219   else {
220     anAction = aSalomeConnector->addFeature(theWchName.c_str(), aFeatureInfo, aUseSeparator);
221   }
222 #endif
223   return anAction;
224 }