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