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