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()) {
65 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
66 FeaturePtr aFeature = anOperation->feature();
67 QString aFeatureId = QString::fromStdString(aFeature->getKind());
68 setActionEnabled(aFeatureId, true);
69 setNestedStackEnabled(anOperation);
72 setNestedCommandsEnabled(false);
74 updateByDocumentKind();
78 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
80 foreach(QString eachAction, myActions.keys())
82 setActionEnabled(eachAction, isEnabled);
86 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
88 if(theOperation == NULL)
90 FeaturePtr aFeature = theOperation->feature();
91 QString aFeatureId = QString::fromStdString(aFeature->getKind());
92 bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
93 setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
95 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
99 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
101 QStringList ltNestedActions;
102 if (theParent.isEmpty()) { //Disable ALL nested
103 foreach(QString eachParent, myNestedActions.keys()) {
104 ltNestedActions << myNestedActions[eachParent];
107 ltNestedActions << myNestedActions[theParent];
109 foreach(QString eachNested, ltNestedActions) {
110 setActionEnabled(eachNested, theEnabled);
114 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
116 QAction* anAction = myActions[theId];
117 if (anAction && anAction->isCheckable()) {
118 anAction->setChecked(theChecked);
123 * Disables all actions which have the Document Kind different to
124 * the current document's kind
126 void XGUI_ActionsMgr::updateByDocumentKind()
128 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
129 QString aDocKind = QString::fromStdString(aStdDocKind);
130 foreach(QAction* eachAction, myActions.values()) {
131 XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(eachAction);
133 QString aCmdDocKind = aCmd->documentKind();
134 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
135 eachAction->setEnabled(false);
141 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
143 QAction* anAction = myActions[theId];
145 anAction->setEnabled(theEnabled);
149 void XGUI_ActionsMgr::updateCheckState()
151 QString eachCommand = QString();
152 foreach(eachCommand, myActions.keys()) {
153 setActionChecked(eachCommand, false);
155 QStringList ltActiveCommands = myOperationMgr->operationList();
156 foreach(eachCommand, ltActiveCommands) {
157 setActionChecked(eachCommand, true);
161 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
163 if (myNestedActions.contains(theId))
164 return myNestedActions[theId];
165 return QStringList();
168 bool XGUI_ActionsMgr::isNested(const QString& theId) const
170 foreach(QString aId, myNestedActions.keys())
172 QStringList aList = myNestedActions[aId];
173 if (aList.contains(theId))
179 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
181 if (theKeySequence.isEmpty()) {
182 return QKeySequence();
184 QKeySequence aResult(theKeySequence);
185 if (myShortcuts.contains(aResult)) {
186 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
187 Events_Error::send(aMessage.toStdString());
188 return QKeySequence();
190 myShortcuts.append(aResult);