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 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
133 foreach(QAction* eachAction, myActions.values()) {
134 XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(eachAction);
137 aCmdDocKind = aCmd->documentKind();
140 QString aId = eachAction->data().toString();
141 if (!aId.isEmpty()) {
142 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
145 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
146 eachAction->setEnabled(false);
151 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
153 QAction* anAction = myActions[theId];
155 anAction->setEnabled(theEnabled);
159 void XGUI_ActionsMgr::updateCheckState()
161 QString eachCommand = QString();
162 foreach(eachCommand, myActions.keys()) {
163 setActionChecked(eachCommand, false);
165 QStringList ltActiveCommands = myOperationMgr->operationList();
166 foreach(eachCommand, ltActiveCommands) {
167 setActionChecked(eachCommand, true);
171 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
173 if (myNestedActions.contains(theId))
174 return myNestedActions[theId];
175 return QStringList();
178 bool XGUI_ActionsMgr::isNested(const QString& theId) const
180 foreach(QString aId, myNestedActions.keys())
182 QStringList aList = myNestedActions[aId];
183 if (aList.contains(theId))
189 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
191 if (theKeySequence.isEmpty()) {
192 return QKeySequence();
194 QKeySequence aResult(theKeySequence);
195 if (myShortcuts.contains(aResult)) {
196 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
197 Events_Error::send(aMessage.toStdString());
198 return QKeySequence();
200 myShortcuts.append(aResult);