]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
4bfb31b9d6e31eb7807415815e4a04f815149c66
[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
22 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
23  : QObject(theParent),
24    myWorkshop(theParent),
25    myOperationMgr(theParent->operationMgr())
26 {
27   // Default shortcuts
28   myShortcuts << QKeySequence::Save;
29   myShortcuts << QKeySequence::Undo;
30   myShortcuts << QKeySequence::Redo;
31   myShortcuts << QKeySequence::Open;
32   myShortcuts << QKeySequence::Close;
33 }
34
35 XGUI_ActionsMgr::~XGUI_ActionsMgr()
36 {
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     QString anOperationId = anOperation->id();
67     setActionEnabled(anOperationId, true);
68     bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
69     setNestedCommandsEnabled(isNestedEnabled, anOperationId);
70   } else {
71     setAllEnabled(true);
72     setNestedCommandsEnabled(false);
73   }
74   updateCheckState();
75 }
76
77 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
78 {
79   foreach(QString eachAction, myActions.keys()) {
80     setActionEnabled(eachAction, isEnabled);
81   }
82 }
83
84 //!
85 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
86 {
87   QStringList ltNestedActions;
88   if(theParent.isEmpty()) { //Disable ALL nested
89     foreach(QString eachParent, myNestedActions.keys()) {
90       ltNestedActions << myNestedActions[eachParent];
91     }
92   } else {
93     ltNestedActions << myNestedActions[theParent];
94   }
95   foreach(QString eachNested, ltNestedActions) {
96     setActionEnabled(eachNested, theEnabled);
97   }
98 }
99
100 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
101 {
102   QAction* anAction = myActions[theId];
103   if(anAction && anAction->isCheckable()) {
104     anAction->setChecked(theChecked);
105   }
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     setActionChecked(eachCommand, false);
122   }
123   QStringList ltActiveCommands = myOperationMgr->operationList();
124   foreach(eachCommand, ltActiveCommands) {
125     setActionChecked(eachCommand, true);
126   }
127 }
128
129 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
130 {
131   if (myNestedActions.contains(theId))
132     return myNestedActions[theId];
133   return QStringList();
134 }
135
136 bool XGUI_ActionsMgr::isNested(const QString& theId) const
137 {
138   foreach(QString aId, myNestedActions.keys()) {
139     QStringList aList = myNestedActions[aId];
140     if (aList.contains(theId))
141       return true;
142   }
143   return false;
144 }
145
146 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
147 {
148   if(theKeySequence.isEmpty()) {
149     return QKeySequence();
150   }
151   QKeySequence aResult(theKeySequence);
152   if(myShortcuts.contains(aResult)) {
153     QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
154     Events_Error::send(aMessage.toStdString());
155     return QKeySequence();
156   }
157   myShortcuts.append(aResult);
158   return aResult;
159 }