5 #include "XGUI_ActionsMgr.h"
6 #include "XGUI_Command.h"
7 #include "XGUI_Workshop.h"
8 #include "XGUI_OperationMgr.h"
9 #include "XGUI_SalomeConnector.h"
11 #include <ModelAPI_Session.h>
13 #include <ModuleBase_Operation.h>
14 #include <Events_Error.h>
23 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
25 myWorkshop(theParent),
26 myOperationMgr(theParent->operationMgr())
29 myShortcuts << QKeySequence::Save;
30 myShortcuts << QKeySequence::Undo;
31 myShortcuts << QKeySequence::Redo;
32 myShortcuts << QKeySequence::Open;
33 myShortcuts << QKeySequence::Close;
36 XGUI_ActionsMgr::~XGUI_ActionsMgr()
40 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
42 QString aId = theCmd->data().toString();
46 myActions.insert(aId, theCmd);
47 XGUI_Command* aXCmd = dynamic_cast<XGUI_Command*>(theCmd);
49 myNestedActions[aId] = aXCmd->nestedCommands();
51 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
52 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
56 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
58 myNestedActions[theId] = theCommands;
61 void XGUI_ActionsMgr::update()
63 if (myOperationMgr->hasOperation()) {
64 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
65 FeaturePtr aFeature = anOperation->feature();
68 QString aFeatureId = QString::fromStdString(aFeature->getKind());
69 setActionEnabled(aFeatureId, true);
70 setNestedStackEnabled(anOperation);
74 setNestedCommandsEnabled(false);
76 updateByDocumentKind();
80 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
82 foreach(QString eachAction, myActions.keys())
84 setActionEnabled(eachAction, isEnabled);
88 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
90 if(!theOperation || !theOperation->feature())
92 FeaturePtr aFeature = theOperation->feature();
93 QString aFeatureId = QString::fromStdString(aFeature->getKind());
94 bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
95 setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
97 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
101 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
103 QStringList ltNestedActions;
104 if (theParent.isEmpty()) { //Disable ALL nested
105 foreach(QString eachParent, myNestedActions.keys()) {
106 ltNestedActions << myNestedActions[eachParent];
109 ltNestedActions << myNestedActions[theParent];
111 foreach(QString eachNested, ltNestedActions) {
112 setActionEnabled(eachNested, theEnabled);
116 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
118 QAction* anAction = myActions[theId];
119 if (anAction && anAction->isCheckable()) {
120 anAction->setChecked(theChecked);
125 * Disables all actions which have the Document Kind different to
126 * the current document's kind
128 void XGUI_ActionsMgr::updateByDocumentKind()
130 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
131 QString aDocKind = QString::fromStdString(aStdDocKind);
132 foreach(QAction* eachAction, myActions.values()) {
133 XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(eachAction);
135 QString aCmdDocKind = aCmd->documentKind();
136 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
137 eachAction->setEnabled(false);
143 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
145 QAction* anAction = myActions[theId];
147 anAction->setEnabled(theEnabled);
151 void XGUI_ActionsMgr::updateCheckState()
153 QString eachCommand = QString();
154 foreach(eachCommand, myActions.keys()) {
155 setActionChecked(eachCommand, false);
157 QStringList ltActiveCommands = myOperationMgr->operationList();
158 foreach(eachCommand, ltActiveCommands) {
159 setActionChecked(eachCommand, true);
163 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
165 if (myNestedActions.contains(theId))
166 return myNestedActions[theId];
167 return QStringList();
170 bool XGUI_ActionsMgr::isNested(const QString& theId) const
172 foreach(QString aId, myNestedActions.keys())
174 QStringList aList = myNestedActions[aId];
175 if (aList.contains(theId))
181 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
183 if (theKeySequence.isEmpty()) {
184 return QKeySequence();
186 QKeySequence aResult(theKeySequence);
187 if (myShortcuts.contains(aResult)) {
188 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
189 Events_Error::send(aMessage.toStdString());
190 return QKeySequence();
192 myShortcuts.append(aResult);