Salome HOME
Update Delete operation in pop-up
[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_SalomeConnector.h"
9
10 #include <QAction>
11
12
13 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
14  : QObject(theParent), myWorkshop(theParent)
15 {
16   
17 }
18
19 XGUI_ActionsMgr::~XGUI_ActionsMgr()
20 {
21 }
22
23
24 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
25 {
26   QString aId = theCmd->data().toString();
27   myActions.insert(aId, theCmd);
28   myActionsState.insert(aId, theCmd->isEnabled());
29   connect(theCmd, SIGNAL(triggered(bool)), this, SLOT(setActionsDisabled(bool)));
30 }
31
32 void XGUI_ActionsMgr::setActionsDisabled(bool isDisabled)
33 {
34   //Re-enable actions (just restore their state)
35   if (!isDisabled) {
36     myNestedActions.clear();
37     restoreCommandState();
38     return;
39   }
40   //Disable all actions, but caller and unblockable (defined in a xml)
41   saveCommandsState();
42
43   QString aSkippedId;
44   QAction* aToggledFeature = dynamic_cast<QAction*>(sender());
45   aSkippedId = aToggledFeature->data().toString();
46
47   QStringList anActionIdsList = myActions.keys();
48   foreach(QString eachKey, anActionIdsList) {
49     if (eachKey == aSkippedId) {
50       continue;
51     }
52     myActions[eachKey]->setEnabled(false);
53   }
54   if (myWorkshop->isSalomeMode()) {
55     myNestedActions = myWorkshop->salomeConnector()->nestedActions(aSkippedId);
56   } else {
57     XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
58     myNestedActions = aToggledFeature->unblockableCommands();
59   }
60 }
61
62 void XGUI_ActionsMgr::saveCommandsState()
63 {
64   myActionsState.clear();
65   QStringList anActionIdsList = myActions.keys();
66   foreach(QString eachKey, anActionIdsList) {
67     myActionsState.insert(eachKey, myActions[eachKey]->isEnabled());
68   }
69
70 }
71
72 void XGUI_ActionsMgr::restoreCommandState()
73 {
74   QStringList anActionIdsList = myActions.keys();
75   foreach(QString eachKey, anActionIdsList) {
76     myActions[eachKey]->setEnabled(myActionsState[eachKey]);
77     myActions[eachKey]->setChecked(false);
78   }
79 }
80
81 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
82 {
83   if(myActions.contains(theId)) {
84     myActions[theId]->setChecked(theChecked);
85   }
86 }
87
88 void XGUI_ActionsMgr::updateAction(const QString& theId)
89 {
90   if(myActions.contains(theId)){
91     myActions[theId]->setEnabled(myActionsState[theId]);
92     myActions[theId]->setChecked(false);
93   }
94 }
95
96 void XGUI_ActionsMgr::setNestedActionsEnabled(bool isEnabled)
97 {
98   foreach(QString eachKey, myNestedActions) {
99     if (myActions.contains(eachKey))
100       myActions[eachKey]->setEnabled(isEnabled);
101   }
102 }