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();
138 } else if (eachAction) {
139 QString aId = eachAction->data().toString();
140 if (!aId.isEmpty()) {
141 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
144 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
145 eachAction->setEnabled(false);
150 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
152 QAction* anAction = myActions[theId];
154 anAction->setEnabled(theEnabled);
158 void XGUI_ActionsMgr::updateCheckState()
160 QString eachCommand = QString();
161 foreach(eachCommand, myActions.keys()) {
162 setActionChecked(eachCommand, false);
164 QStringList ltActiveCommands = myOperationMgr->operationList();
165 foreach(eachCommand, ltActiveCommands) {
166 setActionChecked(eachCommand, true);
170 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
172 if (myNestedActions.contains(theId))
173 return myNestedActions[theId];
174 return QStringList();
177 bool XGUI_ActionsMgr::isNested(const QString& theId) const
179 foreach(QString aId, myNestedActions.keys())
181 QStringList aList = myNestedActions[aId];
182 if (aList.contains(theId))
188 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
190 if (theKeySequence.isEmpty()) {
191 return QKeySequence();
193 QKeySequence aResult(theKeySequence);
194 if (myShortcuts.contains(aResult)) {
195 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
196 Events_Error::send(aMessage.toStdString());
197 return QKeySequence();
199 myShortcuts.append(aResult);