]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
Improved command state processing in ActionMgr. Fixes #41 #36
[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
10 #include <ModuleBase_Operation.h>
11
12 #include <QAction>
13
14 #ifdef _DEBUG
15 #include <QDebug>
16 #endif
17
18
19 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
20  : QObject(theParent), myOperationMgr(theParent->operationMgr())
21 {
22
23 }
24
25 XGUI_ActionsMgr::~XGUI_ActionsMgr()
26 {
27 }
28
29
30 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
31 {
32   QString aId = theCmd->data().toString();
33   if(aId.isEmpty()) {
34     return;
35   }
36   myActions.insert(aId, theCmd);
37   XGUI_Command* aXCmd = dynamic_cast<XGUI_Command*>(theCmd);
38   if (aXCmd) {
39     myNestedActions[aId] = aXCmd->nestedCommands();
40   }
41 }
42
43 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
44 {
45   myNestedActions[theId] = theCommands;
46 }
47
48 void XGUI_ActionsMgr::update()
49 {
50   if(myOperationMgr->hasOperation()) {
51     setAllEnabled(false);
52     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
53     QString anOperationId = anOperation->id();
54     setActionEnabled(anOperationId, true);
55     bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
56     setNestedCommandsEnabled(isNestedEnabled, anOperationId);
57   } else {
58     setAllEnabled(true);
59     setNestedCommandsEnabled(false);
60   }
61   updateCheckState();
62 }
63
64 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
65 {
66   foreach(QString eachAction, myActions.keys()) {
67     setActionEnabled(eachAction, isEnabled);
68   }
69 }
70
71 //!
72 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
73 {
74   QStringList ltNestedActions;
75   if(theParent.isEmpty()) { //Disable ALL nested
76     foreach(QString eachParent, myNestedActions.keys()) {
77       ltNestedActions << myNestedActions[eachParent];
78     }
79   } else {
80     ltNestedActions << myNestedActions[theParent];
81   }
82   foreach(QString eachNested, ltNestedActions) {
83     setActionEnabled(eachNested, theEnabled);
84   }
85 }
86
87 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
88 {
89   QAction* anAction = myActions[theId];
90   if(anAction && anAction->isCheckable()) {
91     anAction->setChecked(theChecked);
92   }
93 }
94
95
96 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
97 {
98   QAction* anAction = myActions[theId];
99   if(anAction) {
100     anAction->setEnabled(theEnabled);
101   }
102 }
103
104 void XGUI_ActionsMgr::updateCheckState()
105 {
106   QString eachCommand = QString();
107   foreach(eachCommand, myActions.keys()) {
108     setActionChecked(eachCommand, false);
109   }
110   QStringList ltActiveCommands = myOperationMgr->operationList();
111   foreach(eachCommand, ltActiveCommands) {
112     setActionChecked(eachCommand, true);
113   }
114 }