Salome HOME
Issue #1407: Provide selection of feature from result
[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_Error.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
24
25 #include <QAction>
26
27 #ifdef _DEBUG
28 #include <iostream>
29 #include <QDebug>
30 #endif
31
32 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
33     : QObject(theParent),
34       myWorkshop(theParent),
35       myOperationMgr(theParent->operationMgr())
36 {
37   // Default shortcuts
38   myShortcuts << QKeySequence::Save;
39   myShortcuts << QKeySequence::Undo;
40   myShortcuts << QKeySequence::Redo;
41   myShortcuts << QKeySequence::Open;
42   myShortcuts << QKeySequence::Close;
43
44   //Initialize event listening
45   Events_Loop* aLoop = Events_Loop::loop();
46   static Events_ID aStateResponseEventId =
47       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
48   aLoop->registerListener(this, aStateResponseEventId, NULL, true);
49 }
50
51 XGUI_ActionsMgr::~XGUI_ActionsMgr()
52 {
53 }
54
55 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
56 {
57   QString aId = theCmd->data().toString();
58   if (aId.isEmpty()) {
59     return;
60   }
61   myActions.insert(aId, theCmd);
62 #ifdef HAVE_SALOME
63     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
64     const std::shared_ptr<Config_FeatureMessage>& anInfo =
65                          aWorkshop->salomeConnector()->featureInfo(aId);
66     if (anInfo.get())
67       myNestedActions[aId] = QString::fromStdString(anInfo->nestedFeatures())
68                                    .split(" ", QString::SkipEmptyParts);
69 #else
70   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
71   myNestedActions[aId] = aXCmd->nestedCommands();
72 #endif
73 }
74
75 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
76 {
77   myNestedActions[theId] = theCommands;
78 }
79
80 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
81 {
82   if (myNestedActions.contains(theId))
83     return myNestedActions[theId];
84   return QStringList();
85 }
86
87 bool XGUI_ActionsMgr::isNested(const QString& theId) const
88 {
89   foreach(QString aId, myNestedActions.keys())
90   {
91     QStringList aList = myNestedActions[aId];
92     if (aList.contains(theId))
93       return true;
94   }
95   return false;
96 }
97
98 void XGUI_ActionsMgr::updateCommandsStatus()
99 {
100   setAllEnabled();
101   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
102   if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
103     updateOnViewSelection();
104
105   FeaturePtr anActiveFeature = FeaturePtr();
106   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
107                                                          (myOperationMgr->currentOperation());
108   if (aFOperation) {
109     anActiveFeature = aFOperation->feature();  
110     QStringList aNested = allNestedCommands(aFOperation);
111     foreach(QString aAction, myActions.keys()) {
112       if (!aNested.contains(aAction))
113         setActionEnabled(aAction, false);
114     }
115   } else 
116     setNestedCommandsEnabled(false);
117
118   updateByPlugins(anActiveFeature);
119   updateByDocumentKind();
120   updateCheckState();
121 }
122
123 void XGUI_ActionsMgr::updateCheckState()
124 {
125   QString eachCommand = QString();
126   foreach(eachCommand, myActions.keys()) {
127     setActionChecked(eachCommand, false);
128   }
129   QStringList ltActiveCommands = myOperationMgr->operationList();
130   foreach(eachCommand, ltActiveCommands) {
131     setActionChecked(eachCommand, true);
132   }
133 }
134
135 void XGUI_ActionsMgr::updateOnViewSelection()
136 {
137   if (!myOperationMgr->hasOperation())
138     return;
139
140   QStringList aIdList = myOperationMgr->operationList();
141   //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
142   //FeaturePtr anActiveFeature = anOperation->feature();
143   //if(!anActiveFeature.get())
144   if (aIdList.isEmpty())
145     return;
146
147   ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
148   //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
149   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
150   // only viewer selection is processed
151   SessionPtr aMgr = ModelAPI_Session::get();
152   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
153   foreach(QString aFeatureId, aIdList) {
154     foreach(QString aId, nestedCommands(aFeatureId)) {
155       ModelAPI_ValidatorsFactory::Validators aValidators;
156       aFactory->validators(aId.toStdString(), aValidators);
157       ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
158       for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
159         const ModuleBase_SelectionValidator* aSelValidator =
160             dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
161         if (aSelValidator)
162           setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
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         aResult->setToolTip("Apply");
234       }
235       break;
236       case Abort:
237       case AbortAll: {
238         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
239         aResult->setToolTip("Cancel");
240         if (theId == Abort) {
241           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
242         }
243       }
244       break;
245       case Help: {
246         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
247         aResult->setToolTip("Help");
248       }
249       break;
250       case Preview: {
251         aResult = new QAction("See preview", theParent);
252         aResult->setToolTip("Compute preview");
253       }
254       break;
255       default:
256         break;
257     }
258     myOperationActions.insert(theId, aResult);
259   }
260   return aResult;
261 }
262
263 QAction* XGUI_ActionsMgr::action(const QString& theId)
264 {
265   QAction* anAction = 0;
266   if(myActions.contains(theId)) {
267     anAction = myActions.value(theId);
268   }
269   return anAction;
270 }
271
272 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
273 {
274   ActionInfo aResult;
275   if(myActions.contains(theId)) {
276     aResult.initFrom(myActions.value(theId));
277   } else {
278    aResult.id = theId;
279    aResult.text = theId;
280   }
281   return aResult;
282 }
283
284 void XGUI_ActionsMgr::setAllEnabled()
285 {
286   foreach(QString eachAction, myActions.keys()) {
287     if (myActions.contains(eachAction)) {
288       QAction* aAction = myActions[eachAction];
289       aAction->setEnabled(true);
290     }
291   }
292 }
293
294
295 //!
296 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
297 {
298   QStringList ltNestedActions;
299   if (theParent.isEmpty()) {  //Disable ALL nested
300     foreach(QString eachParent, myNestedActions.keys()) {
301       ltNestedActions << myNestedActions[eachParent];
302     }
303   } else {
304     ltNestedActions << myNestedActions[theParent];
305   }
306   foreach(QString eachNested, ltNestedActions) {
307     setActionEnabled(eachNested, theEnabled);
308   }
309 }
310
311 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
312 {
313   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
314   if(!anOperation || !anOperation->feature())
315     return;
316   FeaturePtr aFeature = anOperation->feature();
317   QString aFeatureId = QString::fromStdString(aFeature->getKind());
318   //setActionEnabled(aFeatureId, true);
319   setNestedCommandsEnabled(true, aFeatureId);
320
321   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
322 }
323
324 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
325 {
326   QStringList aFeatures;
327   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
328   if(!anOperation || !anOperation->feature())
329     return aFeatures;
330   FeaturePtr aFeature = anOperation->feature();
331   QString aFeatureId = QString::fromStdString(aFeature->getKind());
332
333   aFeatures << myNestedActions[aFeatureId];
334   aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
335   return aFeatures;
336 }
337
338 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
339 {
340   if (myActions.contains(theId)) {
341     QAction* anAction = myActions[theId];
342     if (anAction->isCheckable()) {
343       anAction->setChecked(theChecked);
344     }
345   }
346 }
347
348 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
349 {
350   if (myActions.contains(theId)) {
351     QAction* aAction = myActions[theId];
352     // Initially all actions are enabled
353     // If it was disabled for any reason then we can not enable it
354     if (aAction->isEnabled())
355       aAction->setEnabled(theEnabled);
356   }
357 }
358
359 /*
360  * Disables all actions which have the Document Kind different to
361  * the current document's kind
362  */
363 void XGUI_ActionsMgr::updateByDocumentKind()
364 {
365   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
366   QString aDocKind = QString::fromStdString(aStdDocKind);
367   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
368   foreach(QAction* eachAction, myActions.values()) {
369     QString aCmdDocKind;
370 #ifdef HAVE_SALOME
371     QString aId = eachAction->data().toString();
372     if (!aId.isEmpty()) {
373       aCmdDocKind = QString::fromStdString(
374                  aWorkshop->salomeConnector()->featureInfo(aId)->documentKind());
375     }
376 #else
377     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
378     aCmdDocKind = QString::fromStdString(aCmd->featureMessage()->documentKind());
379 #endif
380     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
381       eachAction->setEnabled(false);
382     }
383   }
384 }
385
386 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
387 {
388   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
389       EVENT_FEATURE_STATE_REQUEST);
390   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
391       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
392   aMsg->setFeature(anActiveFeature);
393   Events_Loop::loop()->send(aMsg, false);
394 }