]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_MenuMgr.cpp
Salome HOME
Issue 1303 Re-ordering of Sketcher menus: compilation correction for SALOME mode
[modules/shaper.git] / src / XGUI / XGUI_MenuMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        XGUI_MenuMgr.cpp
4 // Created:     13 Apr 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include <XGUI_MenuMgr.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_ActionsMgr.h>
10 #include <XGUI_OperationMgr.h>
11
12 #include <Events_Loop.h>
13 #include <Config_FeatureMessage.h>
14 #include <Config_Keywords.h>
15
16 #ifndef HAVE_SALOME
17 #include <AppElements_Workbench.h>
18 #include <AppElements_Command.h>
19 #include <AppElements_MainMenu.h>
20 #include <AppElements_MainWindow.h>
21 #include <AppElements_MenuGroupPanel.h>
22 #include <AppElements_Button.h>
23 #else
24 #include <XGUI_SalomeConnector.h>
25 #endif
26
27 #include <ModuleBase_IModule.h>
28
29 #include <QObject>
30 #include <QAction>
31 #include <QDebug>
32
33 XGUI_MenuMgr::XGUI_MenuMgr(XGUI_Workshop* theWorkshop)
34 : myWorkshop(theWorkshop)
35 {
36   Events_Loop* aLoop = Events_Loop::loop();
37
38   aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
39 }
40
41 void XGUI_MenuMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
42 {
43   //A message to start feature creation received.
44   if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
45     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
46        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
47     if (!aFeatureMsg->isInternal()) {
48       addFeature(aFeatureMsg);
49     }
50   }
51 }
52
53 void XGUI_MenuMgr::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
54 {
55   if (!theMessage) {
56 #ifdef _DEBUG
57     qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
58 #endif
59     return;
60   }
61
62   ActionInfo aFeatureInfo;
63   aFeatureInfo.initFrom(theMessage);
64
65   QString aWchName = QString::fromStdString(theMessage->workbenchId());
66   QStringList aNestedFeatures =
67       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
68   QList<QAction*> aNestedActList;
69   bool isColumnButton = !aNestedFeatures.isEmpty();
70   if (isColumnButton) {
71     QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
72     XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
73     XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
74     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
75       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
76       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
77       aNestedActList << anAction;
78     }
79     if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
80       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
81       QObject::connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
82       aNestedActList << anAction;
83     }
84   }
85
86 #ifdef HAVE_SALOME
87   XGUI_SalomeConnector* aSalomeConnector = myWorkshop->salomeConnector();
88   QAction* aAction;
89   if (isColumnButton) {
90     aAction = aSalomeConnector->addFeatureOfNested(aWchName, aFeatureInfo, aNestedActList);
91   } else {
92     //Issue #650: in the SALOME mode the tooltip should be same as text
93     aFeatureInfo.toolTip = aFeatureInfo.text;
94     aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
95   }
96   aSalomeConnector->setFeatureInfo(aFeatureInfo.id, theMessage);
97
98   myWorkshop->actionsMgr()->addCommand(aAction);
99   myWorkshop->module()->actionCreated(aAction);
100 #else 
101   //Find or create Workbench
102   AppElements_MainMenu* aMenuBar = myWorkshop->mainWindow()->menuObject();
103   AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
104   if (!aPage) {
105     aPage = myWorkshop->addWorkbench(aWchName);
106   }
107   //Find or create Group
108   QString aGroupName = QString::fromStdString(theMessage->groupId());
109   AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
110   if (!aGroup) {
111     aGroup = aPage->addGroup(aGroupName);
112   }
113   // Check if hotkey sequence is already defined:
114   XGUI_ActionsMgr* anActionsMgr = myWorkshop->actionsMgr();
115   QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
116   if(aHotKey != aFeatureInfo.shortcut) {
117     aFeatureInfo.shortcut = aHotKey;
118   }
119   AppElements_Command* aCommand = aGroup->addFeature(theMessage);
120   // Enrich created button with accept/abort buttons if necessary
121   AppElements_Button* aButton = aCommand->button();
122   if (aButton->isColumnButton()) {
123     aButton->setAdditionalButtons(aNestedActList);
124   }
125   myWorkshop->actionsMgr()->addCommand(aCommand);
126   myWorkshop->module()->actionCreated(aCommand);
127 #endif
128 }