]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
Document's kind dependent features implemented
[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     setAllEnabled(false);
65     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
66     FeaturePtr aFeature = anOperation->feature();
67     QString aFeatureId = QString::fromStdString(aFeature->getKind());
68     setActionEnabled(aFeatureId, true);
69     setNestedStackEnabled(anOperation);
70   } else {
71     setAllEnabled(true);
72     setNestedCommandsEnabled(false);
73   }
74   updateByDocumentKind();
75   updateCheckState();
76 }
77
78 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
79 {
80   foreach(QString eachAction, myActions.keys())
81   {
82     setActionEnabled(eachAction, isEnabled);
83   }
84 }
85
86 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
87 {
88   if(theOperation == NULL)
89     return;
90   FeaturePtr aFeature = theOperation->feature();
91   QString aFeatureId = QString::fromStdString(aFeature->getKind());
92   bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
93   setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
94
95   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
96 }
97
98 //!
99 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
100 {
101   QStringList ltNestedActions;
102   if (theParent.isEmpty()) {  //Disable ALL nested
103     foreach(QString eachParent, myNestedActions.keys()) {
104       ltNestedActions << myNestedActions[eachParent];
105     }
106   } else {
107     ltNestedActions << myNestedActions[theParent];
108   }
109   foreach(QString eachNested, ltNestedActions) {
110     setActionEnabled(eachNested, theEnabled);
111   }
112 }
113
114 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
115 {
116   QAction* anAction = myActions[theId];
117   if (anAction && anAction->isCheckable()) {
118     anAction->setChecked(theChecked);
119   }
120 }
121
122 /*
123  * Disables all actions which have the Document Kind different to
124  * the current document's kind
125  */
126 void XGUI_ActionsMgr::updateByDocumentKind()
127 {
128   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
129   QString aDocKind = QString::fromStdString(aStdDocKind);
130   foreach(QAction* eachAction, myActions.values()) {
131     XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(eachAction);
132     if(aCmd) {
133       QString aCmdDocKind = aCmd->documentKind();
134       if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
135         eachAction->setEnabled(false);
136       }
137     }
138   }
139 }
140
141 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
142 {
143   QAction* anAction = myActions[theId];
144   if (anAction) {
145     anAction->setEnabled(theEnabled);
146   }
147 }
148
149 void XGUI_ActionsMgr::updateCheckState()
150 {
151   QString eachCommand = QString();
152   foreach(eachCommand, myActions.keys()) {
153     setActionChecked(eachCommand, false);
154   }
155   QStringList ltActiveCommands = myOperationMgr->operationList();
156   foreach(eachCommand, ltActiveCommands) {
157     setActionChecked(eachCommand, true);
158   }
159 }
160
161 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
162 {
163   if (myNestedActions.contains(theId))
164     return myNestedActions[theId];
165   return QStringList();
166 }
167
168 bool XGUI_ActionsMgr::isNested(const QString& theId) const
169 {
170   foreach(QString aId, myNestedActions.keys())
171   {
172     QStringList aList = myNestedActions[aId];
173     if (aList.contains(theId))
174       return true;
175   }
176   return false;
177 }
178
179 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
180 {
181   if (theKeySequence.isEmpty()) {
182     return QKeySequence();
183   }
184   QKeySequence aResult(theKeySequence);
185   if (myShortcuts.contains(aResult)) {
186     QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
187     Events_Error::send(aMessage.toStdString());
188     return QKeySequence();
189   }
190   myShortcuts.append(aResult);
191   return aResult;
192 }