]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
165d0457ece943c650770e30952c222a4eed4288
[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 anOperationId = QString::fromStdString(aFeature->getKind()); //anOperation->id();
66     setActionEnabled(anOperationId, true);
67     bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
68     setNestedCommandsEnabled(isNestedEnabled, anOperationId);
69   } else {
70     setAllEnabled(true);
71     setNestedCommandsEnabled(false);
72   }
73   updateCheckState();
74 }
75
76 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
77 {
78   foreach(QString eachAction, myActions.keys())
79   {
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 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
109 {
110   QAction* anAction = myActions[theId];
111   if (anAction) {
112     anAction->setEnabled(theEnabled);
113   }
114 }
115
116 void XGUI_ActionsMgr::updateCheckState()
117 {
118   QString eachCommand = QString();
119   foreach(eachCommand, myActions.keys()) {
120     setActionChecked(eachCommand, false);
121   }
122   QStringList ltActiveCommands = myOperationMgr->operationList();
123   foreach(eachCommand, ltActiveCommands) {
124     setActionChecked(eachCommand, true);
125   }
126 }
127
128 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
129 {
130   if (myNestedActions.contains(theId))
131     return myNestedActions[theId];
132   return QStringList();
133 }
134
135 bool XGUI_ActionsMgr::isNested(const QString& theId) const
136 {
137   foreach(QString aId, myNestedActions.keys())
138   {
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 }