Salome HOME
Sources formated according to the codeing standards
[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     QString anOperationId = anOperation->id();
65     setActionEnabled(anOperationId, true);
66     bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
67     setNestedCommandsEnabled(isNestedEnabled, anOperationId);
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 //!
84 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
85 {
86   QStringList ltNestedActions;
87   if (theParent.isEmpty()) {  //Disable ALL nested
88     foreach(QString eachParent, myNestedActions.keys())
89     {
90       ltNestedActions << myNestedActions[eachParent];
91     }
92   } else {
93     ltNestedActions << myNestedActions[theParent];
94   }
95   foreach(QString eachNested, ltNestedActions)
96   {
97     setActionEnabled(eachNested, theEnabled);
98   }
99 }
100
101 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
102 {
103   QAction* anAction = myActions[theId];
104   if (anAction && anAction->isCheckable()) {
105     anAction->setChecked(theChecked);
106   }
107 }
108
109 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
110 {
111   QAction* anAction = myActions[theId];
112   if (anAction) {
113     anAction->setEnabled(theEnabled);
114   }
115 }
116
117 void XGUI_ActionsMgr::updateCheckState()
118 {
119   QString eachCommand = QString();
120   foreach(eachCommand, myActions.keys())
121   {
122     setActionChecked(eachCommand, false);
123   }
124   QStringList ltActiveCommands = myOperationMgr->operationList();
125   foreach(eachCommand, ltActiveCommands)
126   {
127     setActionChecked(eachCommand, true);
128   }
129 }
130
131 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
132 {
133   if (myNestedActions.contains(theId))
134     return myNestedActions[theId];
135   return QStringList();
136 }
137
138 bool XGUI_ActionsMgr::isNested(const QString& theId) const
139 {
140   foreach(QString aId, myNestedActions.keys())
141   {
142     QStringList aList = myNestedActions[aId];
143     if (aList.contains(theId))
144       return true;
145   }
146   return false;
147 }
148
149 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
150 {
151   if (theKeySequence.isEmpty()) {
152     return QKeySequence();
153   }
154   QKeySequence aResult(theKeySequence);
155   if (myShortcuts.contains(aResult)) {
156     QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
157     Events_Error::send(aMessage.toStdString());
158     return QKeySequence();
159   }
160   myShortcuts.append(aResult);
161   return aResult;
162 }