]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ActionsMgr.cpp
Salome HOME
b1b5f1cf512760d49678b83e0528254f82381170
[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   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           continue;
159         setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
160       }
161     }
162   }
163 }
164
165 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
166 {
167   if (theKeySequence.isEmpty()) {
168     return QKeySequence();
169   }
170   if (myShortcuts.contains(theKeySequence)) {
171     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
172     aMessage = aMessage.arg(theKeySequence.toString());
173     Events_Error::send(aMessage.toStdString());
174     return QKeySequence();
175   }
176   myShortcuts.append(theKeySequence);
177   return theKeySequence;
178 }
179
180 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
181 {
182   if (theKeySequence.isEmpty()) {
183     return QKeySequence();
184   }
185   QKeySequence aResult(theKeySequence);
186   registerShortcut(aResult);
187   return aResult;
188 }
189
190 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
191 {
192   const Events_ID kResponseEvent =
193       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
194   if (theMessage->eventID() == kResponseEvent) {
195     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
196         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
197     if (!aStateMessage.get())
198       return;
199     std::list<std::string> aFeaturesList = aStateMessage->features();
200     std::list<std::string>::iterator it = aFeaturesList.begin();
201     for( ; it != aFeaturesList.end(); ++it) {
202       QString anActionId = QString::fromStdString(*it);
203       bool theDefaultState = false;
204       if (myActions.contains(anActionId)) {
205         theDefaultState = myActions[anActionId]->isEnabled();
206       }
207       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
208     }
209   } else if (theMessage.get()) {
210     #ifdef _DEBUG
211     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
212               << theMessage->eventID().eventText() << std::endl;
213     #endif
214   }
215 }
216
217 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
218 {
219   QAction* aResult = NULL;
220   if (myOperationActions.contains(theId)) {
221     aResult = myOperationActions.value(theId);
222     if (theParent && aResult->parent() != theParent) {
223       aResult->setParent(theParent);
224     }
225   } else {
226     switch (theId) {
227       case Accept:
228       case AcceptAll:
229         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
230         break;
231       case Abort:
232       case AbortAll: {
233         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
234         if(theId == Abort) {
235           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
236         }
237       }
238       break;
239       case Help:
240         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
241         break;
242       default:
243         break;
244     }
245     myOperationActions.insert(theId, aResult);
246   }
247   return aResult;
248 }
249
250 QAction* XGUI_ActionsMgr::action(const QString& theId)
251 {
252   QAction* anAction = 0;
253   if(myActions.contains(theId)) {
254     anAction = myActions.value(theId);
255   }
256   return anAction;
257 }
258
259 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
260 {
261   ActionInfo aResult;
262   if(myActions.contains(theId)) {
263     aResult.initFrom(myActions.value(theId));
264   } else {
265    aResult.id = theId;
266    aResult.text = theId;
267   }
268   return aResult;
269 }
270
271 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
272 {
273   foreach(QString eachAction, myActions.keys())
274   {
275     setActionEnabled(eachAction, isEnabled);
276   }
277 }
278
279
280 //!
281 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
282 {
283   QStringList ltNestedActions;
284   if (theParent.isEmpty()) {  //Disable ALL nested
285     foreach(QString eachParent, myNestedActions.keys()) {
286       ltNestedActions << myNestedActions[eachParent];
287     }
288   } else {
289     ltNestedActions << myNestedActions[theParent];
290   }
291   foreach(QString eachNested, ltNestedActions) {
292     setActionEnabled(eachNested, theEnabled);
293   }
294 }
295
296 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
297 {
298   ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
299   if(!anOperation || !anOperation->feature())
300     return;
301   FeaturePtr aFeature = anOperation->feature();
302   QString aFeatureId = QString::fromStdString(aFeature->getKind());
303   setActionEnabled(aFeatureId, true);
304   setNestedCommandsEnabled(true, aFeatureId);
305
306   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
307 }
308
309 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
310 {
311   if (myActions.contains(theId)) {
312     QAction* anAction = myActions[theId];
313     if (anAction->isCheckable()) {
314       anAction->setChecked(theChecked);
315     }
316   }
317 }
318
319 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
320 {
321   if (myActions.contains(theId)) {
322     myActions[theId]->setEnabled(theEnabled);
323   }
324 }
325
326 /*
327  * Disables all actions which have the Document Kind different to
328  * the current document's kind
329  */
330 void XGUI_ActionsMgr::updateByDocumentKind()
331 {
332   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
333   QString aDocKind = QString::fromStdString(aStdDocKind);
334   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
335   foreach(QAction* eachAction, myActions.values()) {
336     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
337     QString aCmdDocKind;
338     if(aCmd) {
339       aCmdDocKind = aCmd->documentKind();
340     } else {
341       QString aId = eachAction->data().toString();
342       if (!aId.isEmpty()) {
343         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
344       }
345     }
346     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
347       eachAction->setEnabled(false);
348     }
349   }
350 }
351
352 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
353 {
354   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
355       EVENT_FEATURE_STATE_REQUEST);
356   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
357       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
358   aMsg->setFeature(anActiveFeature);
359   Events_Loop::loop()->send(aMsg, false);
360 }