Salome HOME
ab05c8928ee63e69035b4fe8787ecf09c369837f
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
1 /*
2  * XGUI_ActionsMgr.cpp
3  */
4
5 #include "XGUI_ActionsMgr.h"
6 #include "XGUI_Workshop.h"
7 #include "XGUI_OperationMgr.h"
8 #include "XGUI_SalomeConnector.h"
9
10 #include <AppElements_Command.h>
11
12 #include <ModelAPI_Session.h>
13
14 #include <ModuleBase_Operation.h>
15 #include <Events_Error.h>
16
17 #include <QAction>
18
19 #ifdef _DEBUG
20 #include <iostream>
21 #include <QDebug>
22 #endif
23
24 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
25     : QObject(theParent),
26       myWorkshop(theParent),
27       myOperationMgr(theParent->operationMgr())
28 {
29   // Default shortcuts
30   myShortcuts << QKeySequence::Save;
31   myShortcuts << QKeySequence::Undo;
32   myShortcuts << QKeySequence::Redo;
33   myShortcuts << QKeySequence::Open;
34   myShortcuts << QKeySequence::Close;
35 }
36
37 XGUI_ActionsMgr::~XGUI_ActionsMgr()
38 {
39 }
40
41 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
42 {
43   QString aId = theCmd->data().toString();
44   if (aId.isEmpty()) {
45     return;
46   }
47   myActions.insert(aId, theCmd);
48   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
49   if (aXCmd) {
50     myNestedActions[aId] = aXCmd->nestedCommands();
51   } else {
52     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
53     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
54   }
55 }
56
57 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
58 {
59   myNestedActions[theId] = theCommands;
60 }
61
62 void XGUI_ActionsMgr::update()
63 {
64   if (myOperationMgr->hasOperation()) {
65     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
66     FeaturePtr aFeature = anOperation->feature();
67     if(aFeature) {
68       setAllEnabled(false);
69       QString aFeatureId = QString::fromStdString(aFeature->getKind());
70       setActionEnabled(aFeatureId, true);
71       setNestedStackEnabled(anOperation);
72     }
73   } else {
74     setAllEnabled(true);
75     setNestedCommandsEnabled(false);
76   }
77   updateByDocumentKind();
78   updateCheckState();
79 }
80
81 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
82 {
83   foreach(QString eachAction, myActions.keys())
84   {
85     setActionEnabled(eachAction, isEnabled);
86   }
87 }
88
89 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
90 {
91   if(!theOperation || !theOperation->feature())
92     return;
93   FeaturePtr aFeature = theOperation->feature();
94   QString aFeatureId = QString::fromStdString(aFeature->getKind());
95   bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
96   setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
97
98   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
99 }
100
101 //!
102 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
103 {
104   QStringList ltNestedActions;
105   if (theParent.isEmpty()) {  //Disable ALL nested
106     foreach(QString eachParent, myNestedActions.keys()) {
107       ltNestedActions << myNestedActions[eachParent];
108     }
109   } else {
110     ltNestedActions << myNestedActions[theParent];
111   }
112   foreach(QString eachNested, ltNestedActions) {
113     setActionEnabled(eachNested, theEnabled);
114   }
115 }
116
117 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
118 {
119   if (myActions.contains(theId)) {
120     QAction* anAction = myActions[theId];
121     if (anAction->isCheckable()) {
122       anAction->setChecked(theChecked);
123     }
124   }
125 }
126
127 /*
128  * Disables all actions which have the Document Kind different to
129  * the current document's kind
130  */
131 void XGUI_ActionsMgr::updateByDocumentKind()
132 {
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);
138     QString aCmdDocKind;
139     if(aCmd) {
140       aCmdDocKind = aCmd->documentKind();
141     } else {
142       QString aId = eachAction->data().toString();
143       if (!aId.isEmpty()) {
144         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
145       }
146     }
147     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
148       eachAction->setEnabled(false);
149     }
150   }
151 }
152
153 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
154 {
155   if (myActions.contains(theId)) {
156     myActions[theId]->setEnabled(theEnabled);
157   }
158 }
159
160 void XGUI_ActionsMgr::updateCheckState()
161 {
162   QString eachCommand = QString();
163   foreach(eachCommand, myActions.keys()) {
164     setActionChecked(eachCommand, false);
165   }
166   QStringList ltActiveCommands = myOperationMgr->operationList();
167   foreach(eachCommand, ltActiveCommands) {
168     setActionChecked(eachCommand, true);
169   }
170 }
171
172 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
173 {
174   if (myNestedActions.contains(theId))
175     return myNestedActions[theId];
176   return QStringList();
177 }
178
179 bool XGUI_ActionsMgr::isNested(const QString& theId) const
180 {
181   foreach(QString aId, myNestedActions.keys())
182   {
183     QStringList aList = myNestedActions[aId];
184     if (aList.contains(theId))
185       return true;
186   }
187   return false;
188 }
189
190 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
191 {
192   if (theKeySequence.isEmpty()) {
193     return QKeySequence();
194   }
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();
200   }
201   myShortcuts.append(aResult);
202   return aResult;
203 }