Salome HOME
Issue #2824: Constraints at wrong positions when editing the sketch
[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()), anOperationMgr, SLOT(onAbortAllOperation()));
103       aNestedActList << anAction;
104     }
105   }
106
107   //Find or create Workbench
108   AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
109   AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
110   if (!aPage) {
111     aPage = myWorkshop->addWorkbench(aWchName);
112   }
113   //Find or create Group
114   QString aGroupName = QString::fromStdString(theMessage->groupId());
115   AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
116   if (!aGroup) {
117     aGroup = aPage->addGroup(aGroupName);
118   }
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;
124   }
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);
130   }
131   myWorkshop->actionsMgr()->addCommand(aCommand);
132   myWorkshop->module()->actionCreated(aCommand);
133 #endif
134 }
135
136 std::shared_ptr<XGUI_MenuWorkbench> XGUI_MenuMgr::findWorkbench(const std::string& theWorkbenchName)
137 {
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;
145   }
146   if (!aResultWorkbench) {
147     aResultWorkbench =
148       std::shared_ptr<XGUI_MenuWorkbench>(new XGUI_MenuWorkbench(theWorkbenchName));
149     myWorkbenches.push_back(aResultWorkbench);
150   }
151   return aResultWorkbench;
152 }
153
154 void XGUI_MenuMgr::createFeatureActions()
155 {
156 #ifdef HAVE_SALOME
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);
178
179         aSalomeConnector->setFeatureInfo(QString::fromStdString(aMessage->id()), aMessage);
180         myWorkshop->actionsMgr()->addCommand(aAction);
181         myWorkshop->module()->actionCreated(aAction);
182       }
183     }
184   }
185 #endif
186 }
187
188 QAction* XGUI_MenuMgr::buildAction(const std::shared_ptr<Config_FeatureMessage>& theMessage,
189                                    const std::string& theWchName, const bool aUseSeparator) const
190 {
191   QAction* anAction = 0;
192
193 #ifdef HAVE_SALOME
194   XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
195
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;
209     }
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;
214     }
215     anAction = aSalomeConnector->addFeatureOfNested(theWchName.c_str(), aFeatureInfo,
216                                                     aNestedActList);
217   }
218   else {
219     anAction = aSalomeConnector->addFeature(theWchName.c_str(), aFeatureInfo, aUseSeparator);
220   }
221 #endif
222   return anAction;
223 }