Salome HOME
542e1b5e4979febf30f10433af2c16026c7eb5ec
[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_OperationFeature.h>
24 #include <ModuleBase_SelectionValidator.h>
25
26
27 #include <QAction>
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   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
65   if (aXCmd) {
66     myNestedActions[aId] = aXCmd->nestedCommands();
67   } else {
68     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
69     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
70   }
71 }
72
73 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
74 {
75   myNestedActions[theId] = theCommands;
76 }
77
78 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
79 {
80   if (myNestedActions.contains(theId))
81     return myNestedActions[theId];
82   return QStringList();
83 }
84
85 bool XGUI_ActionsMgr::isNested(const QString& theId) const
86 {
87   foreach(QString aId, myNestedActions.keys())
88   {
89     QStringList aList = myNestedActions[aId];
90     if (aList.contains(theId))
91       return true;
92   }
93   return false;
94 }
95
96 void XGUI_ActionsMgr::update()
97 {
98   FeaturePtr anActiveFeature = FeaturePtr();
99   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
100                                                          (myOperationMgr->currentOperation());
101   if (aFOperation) {
102     anActiveFeature = aFOperation->feature();
103     if(anActiveFeature.get()) {
104       setAllEnabled(false);
105       QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
106       setActionEnabled(aFeatureId, true);
107     }
108     setNestedStackEnabled(aFOperation);
109   } else {
110     setAllEnabled(true);
111     setNestedCommandsEnabled(false);
112   }
113   // TODO(SBH): Get defaults state of actions from XML and remove the following method
114   updateByDocumentKind();
115   updateCheckState();
116   updateByPlugins(anActiveFeature);
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   //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
144   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
145   // only viewer selection is processed
146   if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
147     // it seems that this code is not nesessary anymore. It leads to incorrect case:
148     // sketch operation start, click in any place in the viewer. The result is all nested
149     // entities are enabled(but the sketch plane is not selected yet). Any sketch operation
150     // can be started but will be incorrect on preview build before it uses the sketch unset plane.
151     /*foreach(QString aFeatureId, aIdList) {
152       foreach(QString aId, nestedCommands(aFeatureId)) {
153         setActionEnabled(aId, true);
154       }
155     }*/
156   } else { 
157     SessionPtr aMgr = ModelAPI_Session::get();
158     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
159     foreach(QString aFeatureId, aIdList) {
160       foreach(QString aId, nestedCommands(aFeatureId)) {
161         ModelAPI_ValidatorsFactory::Validators aValidators;
162         aFactory->validators(aId.toStdString(), aValidators);
163         ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
164         for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
165           const ModuleBase_SelectionValidator* aSelValidator =
166               dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
167           if (!aSelValidator)
168             continue;
169           setActionEnabled(aId, aSelValidator->isValid(aSelection, aValidatorIt->second));
170         }
171       }
172     }
173   }
174 }
175
176 void XGUI_ActionsMgr::onAcceptAllToggled(bool theState)
177 {
178   if (!theState) {
179     QAction* anAcceptAllAction = operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
180     anAcceptAllAction->setEnabled(theState);
181   }
182 }
183
184 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
185 {
186   if (theKeySequence.isEmpty()) {
187     return QKeySequence();
188   }
189   if (myShortcuts.contains(theKeySequence)) {
190     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
191     aMessage = aMessage.arg(theKeySequence.toString());
192     Events_Error::send(aMessage.toStdString());
193     return QKeySequence();
194   }
195   myShortcuts.append(theKeySequence);
196   return theKeySequence;
197 }
198
199 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
200 {
201   if (theKeySequence.isEmpty()) {
202     return QKeySequence();
203   }
204   QKeySequence aResult(theKeySequence);
205   registerShortcut(aResult);
206   return aResult;
207 }
208
209 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
210 {
211   const Events_ID kResponseEvent =
212       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
213   if (theMessage->eventID() == kResponseEvent) {
214     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
215         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
216     if (!aStateMessage.get())
217       return;
218     std::list<std::string> aFeaturesList = aStateMessage->features();
219     std::list<std::string>::iterator it = aFeaturesList.begin();
220     for( ; it != aFeaturesList.end(); ++it) {
221       QString anActionId = QString::fromStdString(*it);
222       bool theDefaultState = false;
223       if (myActions.contains(anActionId)) {
224         theDefaultState = myActions[anActionId]->isEnabled();
225       }
226       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
227     }
228   } else if (theMessage.get()) {
229     #ifdef _DEBUG
230     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
231               << theMessage->eventID().eventText() << std::endl;
232     #endif
233   }
234 }
235
236 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
237 {
238   QAction* aResult = NULL;
239   if (myOperationActions.contains(theId)) {
240     aResult = myOperationActions.value(theId);
241     if (theParent && aResult->parent() != theParent) {
242       aResult->setParent(theParent);
243     }
244   } else {
245     switch (theId) {
246       case Accept:
247       case AcceptAll:
248         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
249         // the default value is disabled, so the next connect is used to restore this
250         // default state by untoggle this action
251         connect(this, SIGNAL(toggled(bool)), this, SLOT(onAcceptAllToggled(bool)));
252         break;
253       case Abort:
254       case AbortAll: {
255         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
256         if(theId == Abort) {
257           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
258         }
259       }
260       break;
261       case Help:
262         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
263         break;
264       default:
265         break;
266     }
267     myOperationActions.insert(theId, aResult);
268   }
269   return aResult;
270 }
271
272 QAction* XGUI_ActionsMgr::action(const QString& theId)
273 {
274   QAction* anAction;
275   if(myActions.contains(theId)) {
276     anAction = myActions.value(theId);
277   }
278   return anAction;
279 }
280
281 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
282 {
283   ActionInfo aResult;
284   if(myActions.contains(theId)) {
285     aResult.initFrom(myActions.value(theId));
286   } else {
287    aResult.id = theId;
288    aResult.text = theId;
289   }
290   return aResult;
291 }
292
293 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
294 {
295   foreach(QString eachAction, myActions.keys())
296   {
297     setActionEnabled(eachAction, isEnabled);
298   }
299 }
300
301
302 //!
303 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
304 {
305   QStringList ltNestedActions;
306   if (theParent.isEmpty()) {  //Disable ALL nested
307     foreach(QString eachParent, myNestedActions.keys()) {
308       ltNestedActions << myNestedActions[eachParent];
309     }
310   } else {
311     ltNestedActions << myNestedActions[theParent];
312   }
313   foreach(QString eachNested, ltNestedActions) {
314     setActionEnabled(eachNested, theEnabled);
315   }
316 }
317
318 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
319 {
320   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
321   if(!anOperation || !anOperation->feature())
322     return;
323   FeaturePtr aFeature = anOperation->feature();
324   QString aFeatureId = QString::fromStdString(aFeature->getKind());
325   setActionEnabled(aFeatureId, true);
326   setNestedCommandsEnabled(true, aFeatureId);
327
328   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
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     myActions[theId]->setEnabled(theEnabled);
345   }
346 }
347
348 /*
349  * Disables all actions which have the Document Kind different to
350  * the current document's kind
351  */
352 void XGUI_ActionsMgr::updateByDocumentKind()
353 {
354   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
355   QString aDocKind = QString::fromStdString(aStdDocKind);
356   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
357   foreach(QAction* eachAction, myActions.values()) {
358     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
359     QString aCmdDocKind;
360     if(aCmd) {
361       aCmdDocKind = aCmd->documentKind();
362     } else {
363       QString aId = eachAction->data().toString();
364       if (!aId.isEmpty()) {
365         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
366       }
367     }
368     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
369       eachAction->setEnabled(false);
370     }
371   }
372 }
373
374 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
375 {
376   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
377       EVENT_FEATURE_STATE_REQUEST);
378   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
379       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
380   aMsg->setFeature(anActiveFeature);
381   Events_Loop::loop()->send(aMsg, false);
382 }