]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[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       default:
247         break;
248     }
249     myOperationActions.insert(theId, aResult);
250   }
251   return aResult;
252 }
253
254 QAction* XGUI_ActionsMgr::action(const QString& theId)
255 {
256   QAction* anAction = 0;
257   if(myActions.contains(theId)) {
258     anAction = myActions.value(theId);
259   }
260   return anAction;
261 }
262
263 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
264 {
265   ActionInfo aResult;
266   if(myActions.contains(theId)) {
267     aResult.initFrom(myActions.value(theId));
268   } else {
269    aResult.id = theId;
270    aResult.text = theId;
271   }
272   return aResult;
273 }
274
275 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
276 {
277   foreach(QString eachAction, myActions.keys()) {
278     setActionEnabled(eachAction, isEnabled);
279   }
280 }
281
282
283 //!
284 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
285 {
286   QStringList ltNestedActions;
287   if (theParent.isEmpty()) {  //Disable ALL nested
288     foreach(QString eachParent, myNestedActions.keys()) {
289       ltNestedActions << myNestedActions[eachParent];
290     }
291   } else {
292     ltNestedActions << myNestedActions[theParent];
293   }
294   foreach(QString eachNested, ltNestedActions) {
295     setActionEnabled(eachNested, theEnabled);
296   }
297 }
298
299 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
300 {
301   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
302   if(!anOperation || !anOperation->feature())
303     return;
304   FeaturePtr aFeature = anOperation->feature();
305   QString aFeatureId = QString::fromStdString(aFeature->getKind());
306   //setActionEnabled(aFeatureId, true);
307   setNestedCommandsEnabled(true, aFeatureId);
308
309   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
310 }
311
312 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
313 {
314   QStringList aFeatures;
315   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
316   if(!anOperation || !anOperation->feature())
317     return aFeatures;
318   FeaturePtr aFeature = anOperation->feature();
319   QString aFeatureId = QString::fromStdString(aFeature->getKind());
320
321   aFeatures << myNestedActions[aFeatureId];
322   aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
323   return aFeatures;
324 }
325
326 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
327 {
328   if (myActions.contains(theId)) {
329     QAction* anAction = myActions[theId];
330     if (anAction->isCheckable()) {
331       anAction->setChecked(theChecked);
332     }
333   }
334 }
335
336 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
337 {
338   if (myActions.contains(theId)) {
339     QAction* aAction = myActions[theId];
340     // Initially all actions are enabled
341     // If it was disabled for any reason then we can not enable it
342     if (aAction->isEnabled())
343       aAction->setEnabled(theEnabled);
344   }
345 }
346
347 /*
348  * Disables all actions which have the Document Kind different to
349  * the current document's kind
350  */
351 void XGUI_ActionsMgr::updateByDocumentKind()
352 {
353   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
354   QString aDocKind = QString::fromStdString(aStdDocKind);
355   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
356   foreach(QAction* eachAction, myActions.values()) {
357     QString aCmdDocKind;
358 #ifdef HAVE_SALOME
359     QString aId = eachAction->data().toString();
360     if (!aId.isEmpty()) {
361       aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
362     }
363 #else
364     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
365     aCmdDocKind = aCmd->documentKind();
366 #endif
367     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
368       eachAction->setEnabled(false);
369     }
370   }
371 }
372
373 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
374 {
375   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
376       EVENT_FEATURE_STATE_REQUEST);
377   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
378       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
379   aMsg->setFeature(anActiveFeature);
380   Events_Loop::loop()->send(aMsg, false);
381 }