5 #include "XGUI_ActionsMgr.h"
6 #include "XGUI_Workshop.h"
7 #include "XGUI_OperationMgr.h"
8 #include "XGUI_SalomeConnector.h"
10 #include <AppElements_Command.h>
12 #include <ModelAPI_Session.h>
14 #include <ModuleBase_Operation.h>
15 #include <Events_Error.h>
24 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
26 myWorkshop(theParent),
27 myOperationMgr(theParent->operationMgr())
30 myShortcuts << QKeySequence::Save;
31 myShortcuts << QKeySequence::Undo;
32 myShortcuts << QKeySequence::Redo;
33 myShortcuts << QKeySequence::Open;
34 myShortcuts << QKeySequence::Close;
37 XGUI_ActionsMgr::~XGUI_ActionsMgr()
41 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
43 QString aId = theCmd->data().toString();
47 myActions.insert(aId, theCmd);
48 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
50 myNestedActions[aId] = aXCmd->nestedCommands();
52 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
53 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
57 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
59 myNestedActions[theId] = theCommands;
62 void XGUI_ActionsMgr::update()
64 if (myOperationMgr->hasOperation()) {
65 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
66 FeaturePtr aFeature = anOperation->feature();
69 QString aFeatureId = QString::fromStdString(aFeature->getKind());
70 setActionEnabled(aFeatureId, true);
71 setNestedStackEnabled(anOperation);
75 setNestedCommandsEnabled(false);
77 updateByDocumentKind();
81 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
83 foreach(QString eachAction, myActions.keys())
85 setActionEnabled(eachAction, isEnabled);
89 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
91 if(!theOperation || !theOperation->feature())
93 FeaturePtr aFeature = theOperation->feature();
94 QString aFeatureId = QString::fromStdString(aFeature->getKind());
95 bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
96 setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
98 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
102 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
104 QStringList ltNestedActions;
105 if (theParent.isEmpty()) { //Disable ALL nested
106 foreach(QString eachParent, myNestedActions.keys()) {
107 ltNestedActions << myNestedActions[eachParent];
110 ltNestedActions << myNestedActions[theParent];
112 foreach(QString eachNested, ltNestedActions) {
113 setActionEnabled(eachNested, theEnabled);
117 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
119 if (myActions.contains(theId)) {
120 QAction* anAction = myActions[theId];
121 if (anAction->isCheckable()) {
122 anAction->setChecked(theChecked);
128 * Disables all actions which have the Document Kind different to
129 * the current document's kind
131 void XGUI_ActionsMgr::updateByDocumentKind()
133 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
134 QString aDocKind = QString::fromStdString(aStdDocKind);
135 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
136 foreach(QAction* eachAction, myActions.values()) {
137 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
140 aCmdDocKind = aCmd->documentKind();
142 QString aId = eachAction->data().toString();
143 if (!aId.isEmpty()) {
144 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
147 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
148 eachAction->setEnabled(false);
153 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
155 if (myActions.contains(theId)) {
156 myActions[theId]->setEnabled(theEnabled);
160 void XGUI_ActionsMgr::updateCheckState()
162 QString eachCommand = QString();
163 foreach(eachCommand, myActions.keys()) {
164 setActionChecked(eachCommand, false);
166 QStringList ltActiveCommands = myOperationMgr->operationList();
167 foreach(eachCommand, ltActiveCommands) {
168 setActionChecked(eachCommand, true);
172 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
174 if (myNestedActions.contains(theId))
175 return myNestedActions[theId];
176 return QStringList();
179 bool XGUI_ActionsMgr::isNested(const QString& theId) const
181 foreach(QString aId, myNestedActions.keys())
183 QStringList aList = myNestedActions[aId];
184 if (aList.contains(theId))
190 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
192 if (theKeySequence.isEmpty()) {
193 return QKeySequence();
195 QKeySequence aResult(theKeySequence);
196 if (myShortcuts.contains(aResult)) {
197 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
198 Events_Error::send(aMessage.toStdString());
199 return QKeySequence();
201 myShortcuts.append(aResult);