Salome HOME
Issue #1854 Recover feature control update
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #ifndef HAVE_SALOME
4 #include <AppElements_Command.h>
5 #endif
6
7 #include <XGUI_ActionsMgr.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_OperationMgr.h>
10 #include <XGUI_SalomeConnector.h>
11 #include <XGUI_Selection.h>
12 #include <XGUI_SelectionMgr.h>
13
14 #include <Events_Loop.h>
15 #include <Events_InfoMessage.h>
16
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_OperationFeature.h>
22 #include <ModuleBase_SelectionValidator.h>
23 #include <ModuleBase_Tools.h>
24
25
26 #include <QAction>
27 #include <QMainWindow>
28
29 #ifdef _DEBUG
30 #include <iostream>
31 #include <QDebug>
32 #endif
33
34 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
35     : QObject(theParent),
36       myWorkshop(theParent),
37       myOperationMgr(theParent->operationMgr())
38 {
39   // Default shortcuts
40   myShortcuts << QKeySequence::Save;
41   myShortcuts << QKeySequence::Undo;
42   myShortcuts << QKeySequence::Redo;
43   myShortcuts << QKeySequence::Open;
44   myShortcuts << QKeySequence::Close;
45
46   //Initialize event listening
47   Events_Loop* aLoop = Events_Loop::loop();
48   static Events_ID aStateResponseEventId =
49       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
50   aLoop->registerListener(this, aStateResponseEventId, NULL, true);
51 }
52
53 XGUI_ActionsMgr::~XGUI_ActionsMgr()
54 {
55 }
56
57 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
58 {
59   QString aId = theCmd->data().toString();
60   if (aId.isEmpty()) {
61     return;
62   }
63   myActions.insert(aId, theCmd);
64 #ifdef HAVE_SALOME
65     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
66     const std::shared_ptr<Config_FeatureMessage>& anInfo =
67                          aWorkshop->salomeConnector()->featureInfo(aId);
68     if (anInfo.get())
69       myNestedActions[aId] = QString::fromStdString(anInfo->nestedFeatures())
70                                    .split(" ", QString::SkipEmptyParts);
71 #else
72   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
73   myNestedActions[aId] = aXCmd->nestedCommands();
74 #endif
75 }
76
77 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
78 {
79   myNestedActions[theId] = theCommands;
80 }
81
82 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
83 {
84   if (myNestedActions.contains(theId))
85     return myNestedActions[theId];
86   return QStringList();
87 }
88
89 bool XGUI_ActionsMgr::isNested(const QString& theId) const
90 {
91   foreach(QString aId, myNestedActions.keys())
92   {
93     QStringList aList = myNestedActions[aId];
94     if (aList.contains(theId))
95       return true;
96   }
97   return false;
98 }
99
100 void XGUI_ActionsMgr::updateCommandsStatus()
101 {
102   setAllEnabled();
103   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
104   if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
105     updateOnViewSelection();
106
107   FeaturePtr anActiveFeature = FeaturePtr();
108   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
109                                                          (myOperationMgr->currentOperation());
110   if (aFOperation) {
111     anActiveFeature = aFOperation->feature();
112     QStringList aNested = allNestedCommands(aFOperation);
113     foreach(QString aAction, myActions.keys()) {
114       if (!aNested.contains(aAction))
115         setActionEnabled(aAction, false);
116     }
117   } else
118     setNestedCommandsEnabled(false);
119
120   updateByPlugins(anActiveFeature);
121   updateByDocumentKind();
122   updateCheckState();
123 }
124
125 void XGUI_ActionsMgr::updateCheckState()
126 {
127   QString eachCommand = QString();
128   foreach(eachCommand, myActions.keys()) {
129     setActionChecked(eachCommand, false);
130   }
131   QStringList ltActiveCommands = myOperationMgr->operationList();
132   foreach(eachCommand, ltActiveCommands) {
133     setActionChecked(eachCommand, true);
134   }
135 }
136
137 void XGUI_ActionsMgr::updateOnViewSelection()
138 {
139   if (!myOperationMgr->hasOperation())
140     return;
141
142   QStringList aIdList = myOperationMgr->operationList();
143   //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
144   //FeaturePtr anActiveFeature = anOperation->feature();
145   //if(!anActiveFeature.get())
146   if (aIdList.isEmpty())
147     return;
148
149   ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
150   //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
151   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
152   // only viewer selection is processed
153   SessionPtr aMgr = ModelAPI_Session::get();
154   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
155   foreach(QString aFeatureId, aIdList) {
156     foreach(QString aId, nestedCommands(aFeatureId)) {
157       ModelAPI_ValidatorsFactory::Validators aValidators;
158       aFactory->validators(aId.toStdString(), aValidators);
159       ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
160       for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
161         const ModuleBase_SelectionValidator* aSelValidator =
162             dynamic_cast<const ModuleBase_SelectionValidator*>
163             (aFactory->validator(aValidatorIt->first));
164         if (aSelValidator)
165           setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
166       }
167     }
168   }
169 }
170
171 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
172 {
173   if (theKeySequence.isEmpty()) {
174     return QKeySequence();
175   }
176   if (myShortcuts.contains(theKeySequence)) {
177     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
178     aMessage = aMessage.arg(theKeySequence.toString());
179     Events_InfoMessage("XGUI_ActionsMgr", aMessage.toStdString()).send();
180     return QKeySequence();
181   }
182   myShortcuts.append(theKeySequence);
183   return theKeySequence;
184 }
185
186 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
187 {
188   if (theKeySequence.isEmpty()) {
189     return QKeySequence();
190   }
191   QKeySequence aResult(theKeySequence);
192   registerShortcut(aResult);
193   return aResult;
194 }
195
196 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
197 {
198   const Events_ID kResponseEvent =
199       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
200   if (theMessage->eventID() == kResponseEvent) {
201     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
202         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
203     if (!aStateMessage.get())
204       return;
205     std::list<std::string> aFeaturesList = aStateMessage->features();
206     std::list<std::string>::iterator it = aFeaturesList.begin();
207     for( ; it != aFeaturesList.end(); ++it) {
208       QString anActionId = QString::fromStdString(*it);
209       bool theDefaultState = false;
210       if (myActions.contains(anActionId)) {
211         theDefaultState = myActions[anActionId]->isEnabled();
212       }
213       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
214     }
215   } else if (theMessage.get()) {
216     #ifdef _DEBUG
217     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
218               << theMessage->eventID().eventText() << std::endl;
219     #endif
220   }
221 }
222
223 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId)
224 {
225   QAction* aResult = NULL;
226   if (myOperationActions.contains(theId)) {
227     aResult = myOperationActions.value(theId);
228     //if (theParent && aResult->parent() != theParent) {
229     //  aResult->setParent(theParent);
230     //}
231   } else {
232     QWidget* aParent = myWorkshop->desktop();
233     switch (theId) {
234       case Accept:
235       case AcceptAll: {
236         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_ok.png"),
237                             "Apply" /*empty to show error*/, aParent);
238       }
239       break;
240       case Abort:
241       case AbortAll: {
242         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_cancel.png"), "Cancel",
243                                                  aParent);
244         if (theId == Abort) {
245           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
246         }
247       }
248       break;
249       case Help: {
250         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_help.png"), "Help",
251                                                  aParent);
252       }
253       break;
254       case Preview: {
255         aResult = ModuleBase_Tools::createAction(QIcon(), tr("See preview"),
256                                                  aParent, 0, 0, "Compute preview");
257         aResult->setStatusTip(aResult->toolTip());
258       }
259       break;
260       default:
261         break;
262     }
263     myOperationActions.insert(theId, aResult);
264   }
265   return aResult;
266 }
267
268 QAction* XGUI_ActionsMgr::action(const QString& theId)
269 {
270   QAction* anAction = 0;
271   if(myActions.contains(theId)) {
272     anAction = myActions.value(theId);
273   }
274   return anAction;
275 }
276
277 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
278 {
279   ActionInfo aResult;
280   if(myActions.contains(theId)) {
281     aResult.initFrom(myActions.value(theId));
282   } else {
283    aResult.id = theId;
284    aResult.text = theId;
285   }
286   return aResult;
287 }
288
289 void XGUI_ActionsMgr::setAllEnabled()
290 {
291   foreach(QString eachAction, myActions.keys()) {
292     if (myActions.contains(eachAction)) {
293       QAction* aAction = myActions[eachAction];
294       aAction->setEnabled(true);
295     }
296   }
297 }
298
299
300 //!
301 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
302 {
303   QStringList ltNestedActions;
304   if (theParent.isEmpty()) {  //Disable ALL nested
305     foreach(QString eachParent, myNestedActions.keys()) {
306       ltNestedActions << myNestedActions[eachParent];
307     }
308   } else {
309     ltNestedActions << myNestedActions[theParent];
310   }
311   foreach(QString eachNested, ltNestedActions) {
312     setActionEnabled(eachNested, theEnabled);
313   }
314 }
315
316 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
317 {
318   ModuleBase_OperationFeature* anOperation =
319     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
320   if(!anOperation || !anOperation->feature())
321     return;
322   FeaturePtr aFeature = anOperation->feature();
323   QString aFeatureId = QString::fromStdString(aFeature->getKind());
324   //setActionEnabled(aFeatureId, true);
325   setNestedCommandsEnabled(true, aFeatureId);
326
327   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
328 }
329
330 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
331 {
332   QStringList aFeatures;
333   ModuleBase_OperationFeature* anOperation =
334     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
335   if(!anOperation || !anOperation->feature())
336     return aFeatures;
337   FeaturePtr aFeature = anOperation->feature();
338   QString aFeatureId = QString::fromStdString(aFeature->getKind());
339
340   aFeatures << myNestedActions[aFeatureId];
341   aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
342   return aFeatures;
343 }
344
345 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
346 {
347   if (myActions.contains(theId)) {
348     QAction* anAction = myActions[theId];
349     if (anAction->isCheckable()) {
350       anAction->setChecked(theChecked);
351     }
352   }
353 }
354
355 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
356 {
357   if (myActions.contains(theId)) {
358     QAction* aAction = myActions[theId];
359     // Initially all actions are enabled
360     // If it was disabled for any reason then we can not enable it
361     if (aAction->isEnabled())
362       aAction->setEnabled(theEnabled);
363   }
364 }
365
366 /*
367  * Disables all actions which have the Document Kind different to
368  * the current document's kind
369  */
370 void XGUI_ActionsMgr::updateByDocumentKind()
371 {
372   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
373   QString aDocKind = QString::fromStdString(aStdDocKind);
374   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
375   foreach(QAction* eachAction, myActions.values()) {
376     QString aCmdDocKind;
377 #ifdef HAVE_SALOME
378     QString aId = eachAction->data().toString();
379     if (!aId.isEmpty()) {
380       aCmdDocKind = QString::fromStdString(
381                  aWorkshop->salomeConnector()->featureInfo(aId)->documentKind());
382     }
383 #else
384     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
385     aCmdDocKind = QString::fromStdString(aCmd->featureMessage()->documentKind());
386 #endif
387     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
388       eachAction->setEnabled(false);
389     }
390   }
391 }
392
393 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
394 {
395   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
396       EVENT_FEATURE_STATE_REQUEST);
397   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
398       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
399   aMsg->setFeature(anActiveFeature);
400   Events_Loop::loop()->send(aMsg, false);
401 }