Salome HOME
Make finishing operation in python console produce no warnings: isOperation of root...
[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 <ModuleBase_Operation.h>
12 #include <Events_Error.h>
13
14 #include <QAction>
15
16 #ifdef _DEBUG
17 #include <iostream>
18 #include <QDebug>
19 #endif
20
21 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
22     : QObject(theParent),
23       myWorkshop(theParent),
24       myOperationMgr(theParent->operationMgr())
25 {
26   // Default shortcuts
27   myShortcuts << QKeySequence::Save;
28   myShortcuts << QKeySequence::Undo;
29   myShortcuts << QKeySequence::Redo;
30   myShortcuts << QKeySequence::Open;
31   myShortcuts << QKeySequence::Close;
32 }
33
34 XGUI_ActionsMgr::~XGUI_ActionsMgr()
35 {
36 }
37
38 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
39 {
40   QString aId = theCmd->data().toString();
41   if (aId.isEmpty()) {
42     return;
43   }
44   myActions.insert(aId, theCmd);
45   XGUI_Command* aXCmd = dynamic_cast<XGUI_Command*>(theCmd);
46   if (aXCmd) {
47     myNestedActions[aId] = aXCmd->nestedCommands();
48   } else {
49     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
50     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
51   }
52 }
53
54 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
55 {
56   myNestedActions[theId] = theCommands;
57 }
58
59 void XGUI_ActionsMgr::update()
60 {
61   if (myOperationMgr->hasOperation()) {
62     setAllEnabled(false);
63     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
64     FeaturePtr aFeature = anOperation->feature();
65     QString aFeatureId = QString::fromStdString(aFeature->getKind());
66     setActionEnabled(aFeatureId, true);
67     setNestedStackEnabled(anOperation);
68   } else {
69     setAllEnabled(true);
70     setNestedCommandsEnabled(false);
71   }
72   updateCheckState();
73 }
74
75 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
76 {
77   foreach(QString eachAction, myActions.keys())
78   {
79     setActionEnabled(eachAction, isEnabled);
80   }
81 }
82
83 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
84 {
85   if(theOperation == NULL)
86     return;
87   FeaturePtr aFeature = theOperation->feature();
88   QString aFeatureId = QString::fromStdString(aFeature->getKind());
89   bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
90   setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
91
92   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
93 }
94
95 //!
96 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
97 {
98   QStringList ltNestedActions;
99   if (theParent.isEmpty()) {  //Disable ALL nested
100     foreach(QString eachParent, myNestedActions.keys()) {
101       ltNestedActions << myNestedActions[eachParent];
102     }
103   } else {
104     ltNestedActions << myNestedActions[theParent];
105   }
106   foreach(QString eachNested, ltNestedActions) {
107     setActionEnabled(eachNested, theEnabled);
108   }
109 }
110
111 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
112 {
113   QAction* anAction = myActions[theId];
114   if (anAction && anAction->isCheckable()) {
115     anAction->setChecked(theChecked);
116   }
117 }
118
119 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
120 {
121   QAction* anAction = myActions[theId];
122   if (anAction) {
123     anAction->setEnabled(theEnabled);
124   }
125 }
126
127 void XGUI_ActionsMgr::updateCheckState()
128 {
129   QString eachCommand = QString();
130   foreach(eachCommand, myActions.keys()) {
131     setActionChecked(eachCommand, false);
132   }
133   QStringList ltActiveCommands = myOperationMgr->operationList();
134   foreach(eachCommand, ltActiveCommands) {
135     setActionChecked(eachCommand, true);
136   }
137 }
138
139 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
140 {
141   if (myNestedActions.contains(theId))
142     return myNestedActions[theId];
143   return QStringList();
144 }
145
146 bool XGUI_ActionsMgr::isNested(const QString& theId) const
147 {
148   foreach(QString aId, myNestedActions.keys())
149   {
150     QStringList aList = myNestedActions[aId];
151     if (aList.contains(theId))
152       return true;
153   }
154   return false;
155 }
156
157 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
158 {
159   if (theKeySequence.isEmpty()) {
160     return QKeySequence();
161   }
162   QKeySequence aResult(theKeySequence);
163   if (myShortcuts.contains(aResult)) {
164     QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
165     Events_Error::send(aMessage.toStdString());
166     return QKeySequence();
167   }
168   myShortcuts.append(aResult);
169   return aResult;
170 }