]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_MenuMgr.cpp
Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / XGUI / XGUI_MenuMgr.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
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>
27
28 #include <Events_Loop.h>
29 #include <Config_FeatureMessage.h>
30 #include <Config_Keywords.h>
31
32 #ifndef HAVE_SALOME
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>
39 #else
40 #include <XGUI_SalomeConnector.h>
41 #endif
42
43 #include <ModuleBase_IModule.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 #ifdef HAVE_SALOME
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);
82 #else
83   ActionInfo aFeatureInfo;
84   aFeatureInfo.initFrom(theMessage);
85
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();
91   if (isColumnButton) {
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;
99     }
100     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
101       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll);
102       QObject::connect(anAction, SIGNAL(triggered()),
103                        anOperationMgr, SLOT(onAbortAllOperations()));
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       int 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()),
214                        anOperationMgr, SLOT(onAbortAllOperations()));
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 }