]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
Activate all commands when there is no selection
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_ActionsMgr.cpp
5  */
6
7 #include <AppElements_Command.h>
8
9 #include <XGUI_ActionsMgr.h>
10 #include <XGUI_Workshop.h>
11 #include <XGUI_OperationMgr.h>
12 #include <XGUI_SalomeConnector.h>
13 #include <XGUI_Selection.h>
14 #include <XGUI_SelectionMgr.h>
15
16 #include <Events_Loop.h>
17 #include <Events_Error.h>
18
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Events.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModuleBase_Operation.h>
23 #include <ModuleBase_SelectionValidator.h>
24
25
26 #include <QAction>
27
28 #ifdef _DEBUG
29 #include <iostream>
30 #include <QDebug>
31 #endif
32
33 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
34     : QObject(theParent),
35       myWorkshop(theParent),
36       myOperationMgr(theParent->operationMgr())
37 {
38   // Default shortcuts
39   myShortcuts << QKeySequence::Save;
40   myShortcuts << QKeySequence::Undo;
41   myShortcuts << QKeySequence::Redo;
42   myShortcuts << QKeySequence::Open;
43   myShortcuts << QKeySequence::Close;
44
45   //Initialize event listening
46   Events_Loop* aLoop = Events_Loop::loop();
47   static Events_ID aStateResponseEventId =
48       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
49   aLoop->registerListener(this, aStateResponseEventId, NULL, true);
50 }
51
52 XGUI_ActionsMgr::~XGUI_ActionsMgr()
53 {
54 }
55
56 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
57 {
58   QString aId = theCmd->data().toString();
59   if (aId.isEmpty()) {
60     return;
61   }
62   myActions.insert(aId, theCmd);
63   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
64   if (aXCmd) {
65     myNestedActions[aId] = aXCmd->nestedCommands();
66   } else {
67     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
68     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
69   }
70 }
71
72 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
73 {
74   myNestedActions[theId] = theCommands;
75 }
76
77 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
78 {
79   if (myNestedActions.contains(theId))
80     return myNestedActions[theId];
81   return QStringList();
82 }
83
84 bool XGUI_ActionsMgr::isNested(const QString& theId) const
85 {
86   foreach(QString aId, myNestedActions.keys())
87   {
88     QStringList aList = myNestedActions[aId];
89     if (aList.contains(theId))
90       return true;
91   }
92   return false;
93 }
94
95 void XGUI_ActionsMgr::update()
96 {
97   FeaturePtr anActiveFeature = FeaturePtr();
98   if (myOperationMgr->hasOperation()) {
99     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
100     anActiveFeature = anOperation->feature();
101     if(anActiveFeature.get()) {
102       setAllEnabled(false);
103       QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
104       setActionEnabled(aFeatureId, true);
105     }
106     setNestedStackEnabled(anOperation);
107   } else {
108     setAllEnabled(true);
109     setNestedCommandsEnabled(false);
110   }
111   // TODO(SBH): Get defaults state of actions from XML and remove the following method
112   updateByDocumentKind();
113   updateCheckState();
114   updateByPlugins(anActiveFeature);
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 void XGUI_ActionsMgr::updateOnViewSelection()
130 {
131   if (!myOperationMgr->hasOperation())
132     return;
133
134   ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
135   FeaturePtr anActiveFeature = anOperation->feature();
136   if(!anActiveFeature.get())
137     return;
138
139   QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
140   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
141   if (aSelection->getSelected().size() == 0) {
142     foreach(QString aId, nestedCommands(aFeatureId)) {
143       setActionEnabled(aId, true);
144     }
145   } else { 
146     SessionPtr aMgr = ModelAPI_Session::get();
147     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
148     foreach(QString aId, nestedCommands(aFeatureId)) {
149       std::list<ModelAPI_Validator*> aValidators;
150       std::list<std::list<std::string> > anArguments;
151       aFactory->validators(aId.toStdString(), aValidators, anArguments);
152       std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
153       std::list<std::list<std::string> >::iterator aValidatorArgs = anArguments.begin();
154       for (; aValidator != aValidators.end(); aValidator++, aValidatorArgs++) {
155         if (!(*aValidator))
156           continue;
157         const ModuleBase_SelectionValidator* aSelValidator =
158             dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
159         if (!aSelValidator)
160           continue;
161         setActionEnabled(aId, aSelValidator->isValid(aSelection, *aValidatorArgs));
162
163       }
164     }
165   }
166 }
167
168 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
169 {
170   if (theKeySequence.isEmpty()) {
171     return QKeySequence();
172   }
173   if (myShortcuts.contains(theKeySequence)) {
174     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
175     aMessage = aMessage.arg(theKeySequence.toString());
176     Events_Error::send(aMessage.toStdString());
177     return QKeySequence();
178   }
179   myShortcuts.append(theKeySequence);
180   return theKeySequence;
181 }
182
183 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
184 {
185   if (theKeySequence.isEmpty()) {
186     return QKeySequence();
187   }
188   QKeySequence aResult(theKeySequence);
189   registerShortcut(aResult);
190   return aResult;
191 }
192
193 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
194 {
195   const Events_ID kResponseEvent =
196       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
197   if (theMessage->eventID() == kResponseEvent) {
198     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
199         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
200     if (!aStateMessage.get())
201       return;
202     std::list<std::string> aFeaturesList = aStateMessage->features();
203     std::list<std::string>::iterator it = aFeaturesList.begin();
204     for( ; it != aFeaturesList.end(); ++it) {
205       QString anActionId = QString::fromStdString(*it);
206       bool theDefaultState = false;
207       if (myActions.contains(anActionId)) {
208         theDefaultState = myActions[anActionId]->isEnabled();
209       }
210       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
211     }
212   } else if (theMessage.get()) {
213     #ifdef _DEBUG
214     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
215               << theMessage->eventID().eventText() << std::endl;
216     #endif
217   }
218 }
219
220 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
221 {
222   QAction* aResult = NULL;
223   if (myOperationActions.contains(theId)) {
224     aResult = myOperationActions.value(theId);
225     if (theParent && aResult->parent() != theParent) {
226       aResult->setParent(theParent);
227     }
228   } else {
229     switch (theId) {
230       case Accept:
231       case AcceptAll:
232         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
233         break;
234       case Abort:
235       case AbortAll: {
236         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
237         if(theId == Abort) {
238           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
239         }
240       }
241       break;
242       case Help:
243         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
244         break;
245       default:
246         break;
247     }
248     myOperationActions.insert(theId, aResult);
249   }
250   return aResult;
251 }
252
253 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
254 {
255   ActionInfo aResult;
256   if(myActions.contains(theId)) {
257     aResult.initFrom(myActions.value(theId));
258   } else {
259    aResult.id = theId;
260    aResult.text = theId;
261   }
262   return aResult;
263 }
264
265 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
266 {
267   foreach(QString eachAction, myActions.keys())
268   {
269     setActionEnabled(eachAction, isEnabled);
270   }
271 }
272
273
274 //!
275 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
276 {
277   QStringList ltNestedActions;
278   if (theParent.isEmpty()) {  //Disable ALL nested
279     foreach(QString eachParent, myNestedActions.keys()) {
280       ltNestedActions << myNestedActions[eachParent];
281     }
282   } else {
283     ltNestedActions << myNestedActions[theParent];
284   }
285   foreach(QString eachNested, ltNestedActions) {
286     setActionEnabled(eachNested, theEnabled);
287   }
288 }
289
290 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
291 {
292   if(!theOperation || !theOperation->feature())
293     return;
294   FeaturePtr aFeature = theOperation->feature();
295   QString aFeatureId = QString::fromStdString(aFeature->getKind());
296   setNestedCommandsEnabled(true, aFeatureId);
297
298   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
299 }
300
301 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
302 {
303   if (myActions.contains(theId)) {
304     QAction* anAction = myActions[theId];
305     if (anAction->isCheckable()) {
306       anAction->setChecked(theChecked);
307     }
308   }
309 }
310
311 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
312 {
313   if (myActions.contains(theId)) {
314     myActions[theId]->setEnabled(theEnabled);
315   }
316 }
317
318 /*
319  * Disables all actions which have the Document Kind different to
320  * the current document's kind
321  */
322 void XGUI_ActionsMgr::updateByDocumentKind()
323 {
324   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
325   QString aDocKind = QString::fromStdString(aStdDocKind);
326   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
327   foreach(QAction* eachAction, myActions.values()) {
328     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
329     QString aCmdDocKind;
330     if(aCmd) {
331       aCmdDocKind = aCmd->documentKind();
332     } else {
333       QString aId = eachAction->data().toString();
334       if (!aId.isEmpty()) {
335         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
336       }
337     }
338     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
339       eachAction->setEnabled(false);
340     }
341   }
342 }
343
344 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
345 {
346   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
347       EVENT_FEATURE_STATE_REQUEST);
348   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
349       std::make_shared<ModelAPI_FeatureStateMessage>(aStateRequestEventId, this);
350   aMsg->setFeature(anActiveFeature);
351   Events_Loop::loop()->send(aMsg, false);
352 }