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