1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
7 #include "XGUI_ActionsMgr.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_OperationMgr.h"
10 #include "XGUI_SalomeConnector.h"
12 #include <AppElements_Command.h>
14 #include <ModelAPI_Session.h>
16 #include <ModuleBase_Operation.h>
17 #include <Events_Error.h>
26 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
28 myWorkshop(theParent),
29 myOperationMgr(theParent->operationMgr())
32 myShortcuts << QKeySequence::Save;
33 myShortcuts << QKeySequence::Undo;
34 myShortcuts << QKeySequence::Redo;
35 myShortcuts << QKeySequence::Open;
36 myShortcuts << QKeySequence::Close;
39 XGUI_ActionsMgr::~XGUI_ActionsMgr()
43 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
45 QString aId = theCmd->data().toString();
49 myActions.insert(aId, theCmd);
50 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
52 myNestedActions[aId] = aXCmd->nestedCommands();
54 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
55 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
59 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
61 myNestedActions[theId] = theCommands;
64 void XGUI_ActionsMgr::update()
66 if (myOperationMgr->hasOperation()) {
67 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
68 FeaturePtr aFeature = anOperation->feature();
71 QString aFeatureId = QString::fromStdString(aFeature->getKind());
72 setActionEnabled(aFeatureId, true);
73 setNestedStackEnabled(anOperation);
77 setNestedCommandsEnabled(false);
79 updateByDocumentKind();
83 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
85 foreach(QString eachAction, myActions.keys())
87 setActionEnabled(eachAction, isEnabled);
91 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
93 if(!theOperation || !theOperation->feature())
95 FeaturePtr aFeature = theOperation->feature();
96 QString aFeatureId = QString::fromStdString(aFeature->getKind());
97 bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
98 setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
100 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
104 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
106 QStringList ltNestedActions;
107 if (theParent.isEmpty()) { //Disable ALL nested
108 foreach(QString eachParent, myNestedActions.keys()) {
109 ltNestedActions << myNestedActions[eachParent];
112 ltNestedActions << myNestedActions[theParent];
114 foreach(QString eachNested, ltNestedActions) {
115 setActionEnabled(eachNested, theEnabled);
119 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
121 if (myActions.contains(theId)) {
122 QAction* anAction = myActions[theId];
123 if (anAction->isCheckable()) {
124 anAction->setChecked(theChecked);
130 * Disables all actions which have the Document Kind different to
131 * the current document's kind
133 void XGUI_ActionsMgr::updateByDocumentKind()
135 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
136 QString aDocKind = QString::fromStdString(aStdDocKind);
137 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
138 foreach(QAction* eachAction, myActions.values()) {
139 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
142 aCmdDocKind = aCmd->documentKind();
144 QString aId = eachAction->data().toString();
145 if (!aId.isEmpty()) {
146 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
149 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
150 eachAction->setEnabled(false);
155 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
157 if (myActions.contains(theId)) {
158 myActions[theId]->setEnabled(theEnabled);
162 void XGUI_ActionsMgr::updateCheckState()
164 QString eachCommand = QString();
165 foreach(eachCommand, myActions.keys()) {
166 setActionChecked(eachCommand, false);
168 QStringList ltActiveCommands = myOperationMgr->operationList();
169 foreach(eachCommand, ltActiveCommands) {
170 setActionChecked(eachCommand, true);
174 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
176 if (myNestedActions.contains(theId))
177 return myNestedActions[theId];
178 return QStringList();
181 bool XGUI_ActionsMgr::isNested(const QString& theId) const
183 foreach(QString aId, myNestedActions.keys())
185 QStringList aList = myNestedActions[aId];
186 if (aList.contains(theId))
192 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
194 if (theKeySequence.isEmpty()) {
195 return QKeySequence();
197 QKeySequence aResult(theKeySequence);
198 if (myShortcuts.contains(aResult)) {
199 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
200 Events_Error::send(aMessage.toStdString());
201 return QKeySequence();
203 myShortcuts.append(aResult);