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