Salome HOME
Issue #1062: Parameter value is thrown down in the point selector control
[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 #ifndef HAVE_SALOME
8 #include <AppElements_Command.h>
9 #endif
10
11 #include <XGUI_ActionsMgr.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_SalomeConnector.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_SelectionMgr.h>
17
18 #include <Events_Loop.h>
19 #include <Events_Error.h>
20
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_OperationFeature.h>
26 #include <ModuleBase_SelectionValidator.h>
27
28
29 #include <QAction>
30
31 #ifdef _DEBUG
32 #include <iostream>
33 #include <QDebug>
34 #endif
35
36 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
37     : QObject(theParent),
38       myWorkshop(theParent),
39       myOperationMgr(theParent->operationMgr())
40 {
41   // Default shortcuts
42   myShortcuts << QKeySequence::Save;
43   myShortcuts << QKeySequence::Undo;
44   myShortcuts << QKeySequence::Redo;
45   myShortcuts << QKeySequence::Open;
46   myShortcuts << QKeySequence::Close;
47
48   //Initialize event listening
49   Events_Loop* aLoop = Events_Loop::loop();
50   static Events_ID aStateResponseEventId =
51       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
52   aLoop->registerListener(this, aStateResponseEventId, NULL, true);
53 }
54
55 XGUI_ActionsMgr::~XGUI_ActionsMgr()
56 {
57 }
58
59 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
60 {
61   QString aId = theCmd->data().toString();
62   if (aId.isEmpty()) {
63     return;
64   }
65   myActions.insert(aId, theCmd);
66 #ifdef HAVE_SALOME
67     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
68     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
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::update()
99 {
100   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
101   if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0) {
102     updateOnViewSelection();
103   } else {
104     FeaturePtr anActiveFeature = FeaturePtr();
105     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
106                                                            (myOperationMgr->currentOperation());
107     if (aFOperation) {
108       anActiveFeature = aFOperation->feature();
109       if(anActiveFeature.get()) {
110         setAllEnabled(false);
111         //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
112         //setActionEnabled(aFeatureId, true);
113       }
114       setNestedStackEnabled(aFOperation);
115     } else {
116       setAllEnabled(true);
117       setNestedCommandsEnabled(false);
118     }
119     // TODO(SBH): Get defaults state of actions from XML and remove the following method
120     updateByDocumentKind();
121     updateByPlugins(anActiveFeature);
122   }
123   updateCheckState();
124 }
125
126 void XGUI_ActionsMgr::updateCheckState()
127 {
128   QString eachCommand = QString();
129   foreach(eachCommand, myActions.keys()) {
130     setActionChecked(eachCommand, false);
131   }
132   QStringList ltActiveCommands = myOperationMgr->operationList();
133   foreach(eachCommand, ltActiveCommands) {
134     setActionChecked(eachCommand, true);
135   }
136 }
137
138 void XGUI_ActionsMgr::updateOnViewSelection()
139 {
140   if (!myOperationMgr->hasOperation())
141     return;
142
143   QStringList aIdList = myOperationMgr->operationList();
144   //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
145   //FeaturePtr anActiveFeature = anOperation->feature();
146   //if(!anActiveFeature.get())
147   if (aIdList.isEmpty())
148     return;
149
150   ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
151   //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
152   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
153   // only viewer selection is processed
154   SessionPtr aMgr = ModelAPI_Session::get();
155   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
156   foreach(QString aFeatureId, aIdList) {
157     foreach(QString aId, nestedCommands(aFeatureId)) {
158       ModelAPI_ValidatorsFactory::Validators aValidators;
159       aFactory->validators(aId.toStdString(), aValidators);
160       ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
161       for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
162         const ModuleBase_SelectionValidator* aSelValidator =
163             dynamic_cast<const ModuleBase_SelectionValidator*>(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_Error::send(aMessage.toStdString());
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, QObject* theParent)
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     switch (theId) {
233       case Accept:
234       case AcceptAll:
235         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
236         break;
237       case Abort:
238       case AbortAll: {
239         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
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         break;
248       default:
249         break;
250     }
251     myOperationActions.insert(theId, aResult);
252   }
253   return aResult;
254 }
255
256 QAction* XGUI_ActionsMgr::action(const QString& theId)
257 {
258   QAction* anAction = 0;
259   if(myActions.contains(theId)) {
260     anAction = myActions.value(theId);
261   }
262   return anAction;
263 }
264
265 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
266 {
267   ActionInfo aResult;
268   if(myActions.contains(theId)) {
269     aResult.initFrom(myActions.value(theId));
270   } else {
271    aResult.id = theId;
272    aResult.text = theId;
273   }
274   return aResult;
275 }
276
277 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
278 {
279   foreach(QString eachAction, myActions.keys())
280   {
281     setActionEnabled(eachAction, isEnabled);
282   }
283 }
284
285
286 //!
287 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
288 {
289   QStringList ltNestedActions;
290   if (theParent.isEmpty()) {  //Disable ALL nested
291     foreach(QString eachParent, myNestedActions.keys()) {
292       ltNestedActions << myNestedActions[eachParent];
293     }
294   } else {
295     ltNestedActions << myNestedActions[theParent];
296   }
297   foreach(QString eachNested, ltNestedActions) {
298     setActionEnabled(eachNested, theEnabled);
299   }
300 }
301
302 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
303 {
304   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
305   if(!anOperation || !anOperation->feature())
306     return;
307   FeaturePtr aFeature = anOperation->feature();
308   QString aFeatureId = QString::fromStdString(aFeature->getKind());
309   //setActionEnabled(aFeatureId, true);
310   setNestedCommandsEnabled(true, aFeatureId);
311
312   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
313 }
314
315 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
316 {
317   if (myActions.contains(theId)) {
318     QAction* anAction = myActions[theId];
319     if (anAction->isCheckable()) {
320       anAction->setChecked(theChecked);
321     }
322   }
323 }
324
325 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
326 {
327   if (myActions.contains(theId)) {
328     myActions[theId]->setEnabled(theEnabled);
329   }
330 }
331
332 /*
333  * Disables all actions which have the Document Kind different to
334  * the current document's kind
335  */
336 void XGUI_ActionsMgr::updateByDocumentKind()
337 {
338   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
339   QString aDocKind = QString::fromStdString(aStdDocKind);
340   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
341   foreach(QAction* eachAction, myActions.values()) {
342     QString aCmdDocKind;
343 #ifdef HAVE_SALOME
344     QString aId = eachAction->data().toString();
345     if (!aId.isEmpty()) {
346       aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
347     }
348 #else
349     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
350     aCmdDocKind = aCmd->documentKind();
351 #endif
352     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
353       eachAction->setEnabled(false);
354     }
355   }
356 }
357
358 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
359 {
360   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
361       EVENT_FEATURE_STATE_REQUEST);
362   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
363       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
364   aMsg->setFeature(anActiveFeature);
365   Events_Loop::loop()->send(aMsg, false);
366 }