]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
Update commands state on any selection (not only in viewer)
[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   if (aSelection->getSelected().size() > 0)
106     updateOnViewSelection();
107
108   FeaturePtr anActiveFeature = FeaturePtr();
109   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
110                                                          (myOperationMgr->currentOperation());
111   if (aFOperation) {
112     anActiveFeature = aFOperation->feature();
113     QStringList aNested = allNestedCommands(aFOperation);
114     foreach(QString aAction, myActions.keys()) {
115       if (!aNested.contains(aAction))
116         setActionEnabled(aAction, false);
117     }
118   } else
119     setNestedCommandsEnabled(false);
120
121   updateByPlugins(anActiveFeature);
122   updateByDocumentKind();
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*>
164             (aFactory->validator(aValidatorIt->first));
165         if (aSelValidator)
166           setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
167       }
168     }
169   }
170 }
171
172 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
173 {
174   if (theKeySequence.isEmpty()) {
175     return QKeySequence();
176   }
177   if (myShortcuts.contains(theKeySequence)) {
178     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
179     aMessage = aMessage.arg(theKeySequence.toString());
180     Events_InfoMessage("XGUI_ActionsMgr", aMessage.toStdString()).send();
181     return QKeySequence();
182   }
183   myShortcuts.append(theKeySequence);
184   return theKeySequence;
185 }
186
187 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
188 {
189   if (theKeySequence.isEmpty()) {
190     return QKeySequence();
191   }
192   QKeySequence aResult(theKeySequence);
193   registerShortcut(aResult);
194   return aResult;
195 }
196
197 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
198 {
199   const Events_ID kResponseEvent =
200       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
201   if (theMessage->eventID() == kResponseEvent) {
202     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
203         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
204     if (!aStateMessage.get())
205       return;
206     std::list<std::string> aFeaturesList = aStateMessage->features();
207     std::list<std::string>::iterator it = aFeaturesList.begin();
208     for( ; it != aFeaturesList.end(); ++it) {
209       QString anActionId = QString::fromStdString(*it);
210       bool theDefaultState = false;
211       if (myActions.contains(anActionId)) {
212         theDefaultState = myActions[anActionId]->isEnabled();
213       }
214       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
215     }
216   } else if (theMessage.get()) {
217     #ifdef _DEBUG
218     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
219               << theMessage->eventID().eventText() << std::endl;
220     #endif
221   }
222 }
223
224 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId)
225 {
226   QAction* aResult = NULL;
227   if (myOperationActions.contains(theId)) {
228     aResult = myOperationActions.value(theId);
229     //if (theParent && aResult->parent() != theParent) {
230     //  aResult->setParent(theParent);
231     //}
232   } else {
233     QWidget* aParent = myWorkshop->desktop();
234     switch (theId) {
235       case Accept:
236       case AcceptAll: {
237         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_ok.png"),
238                             "Apply" /*empty to show error*/, aParent);
239       }
240       break;
241       case Abort:
242       case AbortAll: {
243         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_cancel.png"), "Cancel",
244                                                  aParent);
245         if (theId == Abort) {
246           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
247         }
248       }
249       break;
250       case Help: {
251         aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_help.png"), "Help",
252                                                  aParent);
253       }
254       break;
255       case Preview: {
256         aResult = ModuleBase_Tools::createAction(QIcon(), tr("See preview"),
257                                                  aParent, 0, 0, "Compute preview");
258         aResult->setStatusTip(aResult->toolTip());
259       }
260       break;
261       default:
262         break;
263     }
264     myOperationActions.insert(theId, aResult);
265   }
266   return aResult;
267 }
268
269 QAction* XGUI_ActionsMgr::action(const QString& theId)
270 {
271   QAction* anAction = 0;
272   if(myActions.contains(theId)) {
273     anAction = myActions.value(theId);
274   }
275   return anAction;
276 }
277
278 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
279 {
280   ActionInfo aResult;
281   if(myActions.contains(theId)) {
282     aResult.initFrom(myActions.value(theId));
283   } else {
284    aResult.id = theId;
285    aResult.text = theId;
286   }
287   return aResult;
288 }
289
290 void XGUI_ActionsMgr::setAllEnabled()
291 {
292   foreach(QString eachAction, myActions.keys()) {
293     if (myActions.contains(eachAction)) {
294       QAction* aAction = myActions[eachAction];
295       aAction->setEnabled(true);
296     }
297   }
298 }
299
300
301 //!
302 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
303 {
304   QStringList ltNestedActions;
305   if (theParent.isEmpty()) {  //Disable ALL nested
306     foreach(QString eachParent, myNestedActions.keys()) {
307       ltNestedActions << myNestedActions[eachParent];
308     }
309   } else {
310     ltNestedActions << myNestedActions[theParent];
311   }
312   foreach(QString eachNested, ltNestedActions) {
313     setActionEnabled(eachNested, theEnabled);
314   }
315 }
316
317 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
318 {
319   ModuleBase_OperationFeature* anOperation =
320     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 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
332 {
333   QStringList aFeatures;
334   ModuleBase_OperationFeature* anOperation =
335     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
336   if(!anOperation || !anOperation->feature())
337     return aFeatures;
338   FeaturePtr aFeature = anOperation->feature();
339   QString aFeatureId = QString::fromStdString(aFeature->getKind());
340
341   aFeatures << myNestedActions[aFeatureId];
342   aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
343   return aFeatures;
344 }
345
346 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
347 {
348   if (myActions.contains(theId)) {
349     QAction* anAction = myActions[theId];
350     if (anAction->isCheckable()) {
351       anAction->setChecked(theChecked);
352     }
353   }
354 }
355
356 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
357 {
358   if (myActions.contains(theId)) {
359     QAction* aAction = myActions[theId];
360     // Initially all actions are enabled
361     // If it was disabled for any reason then we can not enable it
362     if (aAction->isEnabled())
363       aAction->setEnabled(theEnabled);
364   }
365 }
366
367 /*
368  * Disables all actions which have the Document Kind different to
369  * the current document's kind
370  */
371 void XGUI_ActionsMgr::updateByDocumentKind()
372 {
373   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
374   QString aDocKind = QString::fromStdString(aStdDocKind);
375   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
376   foreach(QAction* eachAction, myActions.values()) {
377     QString aCmdDocKind;
378 #ifdef HAVE_SALOME
379     QString aId = eachAction->data().toString();
380     if (!aId.isEmpty()) {
381       aCmdDocKind = QString::fromStdString(
382                  aWorkshop->salomeConnector()->featureInfo(aId)->documentKind());
383     }
384 #else
385     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
386     aCmdDocKind = QString::fromStdString(aCmd->featureMessage()->documentKind());
387 #endif
388     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
389       eachAction->setEnabled(false);
390     }
391   }
392 }
393
394 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
395 {
396   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
397       EVENT_FEATURE_STATE_REQUEST);
398   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
399       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
400   aMsg->setFeature(anActiveFeature);
401   Events_Loop::loop()->send(aMsg, false);
402 }