]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
081ab537f9c7a6ab02e2b24ab4873b78388813f9
[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     }
139     else {
140       QString aId = eachAction->data().toString();
141       if (!aId.isEmpty()) {
142         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
143       }
144     }
145     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
146       eachAction->setEnabled(false);
147     }
148   }
149 }
150
151 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
152 {
153   QAction* anAction = myActions[theId];
154   if (anAction) {
155     anAction->setEnabled(theEnabled);
156   }
157 }
158
159 void XGUI_ActionsMgr::updateCheckState()
160 {
161   QString eachCommand = QString();
162   foreach(eachCommand, myActions.keys()) {
163     setActionChecked(eachCommand, false);
164   }
165   QStringList ltActiveCommands = myOperationMgr->operationList();
166   foreach(eachCommand, ltActiveCommands) {
167     setActionChecked(eachCommand, true);
168   }
169 }
170
171 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
172 {
173   if (myNestedActions.contains(theId))
174     return myNestedActions[theId];
175   return QStringList();
176 }
177
178 bool XGUI_ActionsMgr::isNested(const QString& theId) const
179 {
180   foreach(QString aId, myNestedActions.keys())
181   {
182     QStringList aList = myNestedActions[aId];
183     if (aList.contains(theId))
184       return true;
185   }
186   return false;
187 }
188
189 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
190 {
191   if (theKeySequence.isEmpty()) {
192     return QKeySequence();
193   }
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();
199   }
200   myShortcuts.append(aResult);
201   return aResult;
202 }