Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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_SelectionValidator.h>
24
25
26 #include <QAction>
27
28 #ifdef _DEBUG
29 #include <iostream>
30 #include <QDebug>
31 #endif
32
33 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
34     : QObject(theParent),
35       myWorkshop(theParent),
36       myOperationMgr(theParent->operationMgr())
37 {
38   // Default shortcuts
39   myShortcuts << QKeySequence::Save;
40   myShortcuts << QKeySequence::Undo;
41   myShortcuts << QKeySequence::Redo;
42   myShortcuts << QKeySequence::Open;
43   myShortcuts << QKeySequence::Close;
44
45   //Initialize event listening
46   Events_Loop* aLoop = Events_Loop::loop();
47   static Events_ID aStateResponseEventId =
48       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
49   aLoop->registerListener(this, aStateResponseEventId, NULL, true);
50 }
51
52 XGUI_ActionsMgr::~XGUI_ActionsMgr()
53 {
54 }
55
56 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
57 {
58   QString aId = theCmd->data().toString();
59   if (aId.isEmpty()) {
60     return;
61   }
62   myActions.insert(aId, theCmd);
63   AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
64   if (aXCmd) {
65     myNestedActions[aId] = aXCmd->nestedCommands();
66   } else {
67     XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
68     myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
69   }
70 }
71
72 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
73 {
74   myNestedActions[theId] = theCommands;
75 }
76
77 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
78 {
79   if (myNestedActions.contains(theId))
80     return myNestedActions[theId];
81   return QStringList();
82 }
83
84 bool XGUI_ActionsMgr::isNested(const QString& theId) const
85 {
86   foreach(QString aId, myNestedActions.keys())
87   {
88     QStringList aList = myNestedActions[aId];
89     if (aList.contains(theId))
90       return true;
91   }
92   return false;
93 }
94
95 void XGUI_ActionsMgr::update()
96 {
97   FeaturePtr anActiveFeature = FeaturePtr();
98   if (myOperationMgr->hasOperation()) {
99     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
100     anActiveFeature = anOperation->feature();
101     if(anActiveFeature.get()) {
102       setAllEnabled(false);
103       QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
104       setActionEnabled(aFeatureId, true);
105     }
106     setNestedStackEnabled(anOperation);
107   } else {
108     setAllEnabled(true);
109     setNestedCommandsEnabled(false);
110   }
111   // TODO(SBH): Get defaults state of actions from XML and remove the following method
112   updateByDocumentKind();
113   updateCheckState();
114   updateByPlugins(anActiveFeature);
115 }
116
117 void XGUI_ActionsMgr::updateCheckState()
118 {
119   QString eachCommand = QString();
120   foreach(eachCommand, myActions.keys()) {
121     setActionChecked(eachCommand, false);
122   }
123   QStringList ltActiveCommands = myOperationMgr->operationList();
124   foreach(eachCommand, ltActiveCommands) {
125     setActionChecked(eachCommand, true);
126   }
127 }
128
129 void XGUI_ActionsMgr::updateOnViewSelection()
130 {
131   if (!myOperationMgr->hasOperation())
132     return;
133
134   QStringList aIdList = myOperationMgr->operationList();
135   //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
136   //FeaturePtr anActiveFeature = anOperation->feature();
137   //if(!anActiveFeature.get())
138   if (aIdList.isEmpty())
139     return;
140
141   //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
142   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
143   if (aSelection->getSelected().size() == 0) {
144     foreach(QString aFeatureId, aIdList) {
145       foreach(QString aId, nestedCommands(aFeatureId)) {
146         setActionEnabled(aId, true);
147       }
148     }
149   } else { 
150     SessionPtr aMgr = ModelAPI_Session::get();
151     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
152     foreach(QString aFeatureId, aIdList) {
153       foreach(QString aId, nestedCommands(aFeatureId)) {
154         std::list<ModelAPI_Validator*> aValidators;
155         std::list<std::list<std::string> > anArguments;
156         aFactory->validators(aId.toStdString(), aValidators, anArguments);
157         std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
158         std::list<std::list<std::string> >::iterator aValidatorArgs = anArguments.begin();
159         for (; aValidator != aValidators.end(); aValidator++, aValidatorArgs++) {
160           if (!(*aValidator))
161             continue;
162           const ModuleBase_SelectionValidator* aSelValidator =
163               dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
164           if (!aSelValidator)
165             continue;
166           setActionEnabled(aId, aSelValidator->isValid(aSelection, *aValidatorArgs));
167
168         }
169       }
170     }
171   }
172 }
173
174 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
175 {
176   if (theKeySequence.isEmpty()) {
177     return QKeySequence();
178   }
179   if (myShortcuts.contains(theKeySequence)) {
180     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
181     aMessage = aMessage.arg(theKeySequence.toString());
182     Events_Error::send(aMessage.toStdString());
183     return QKeySequence();
184   }
185   myShortcuts.append(theKeySequence);
186   return theKeySequence;
187 }
188
189 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
190 {
191   if (theKeySequence.isEmpty()) {
192     return QKeySequence();
193   }
194   QKeySequence aResult(theKeySequence);
195   registerShortcut(aResult);
196   return aResult;
197 }
198
199 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
200 {
201   const Events_ID kResponseEvent =
202       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
203   if (theMessage->eventID() == kResponseEvent) {
204     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
205         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
206     if (!aStateMessage.get())
207       return;
208     std::list<std::string> aFeaturesList = aStateMessage->features();
209     std::list<std::string>::iterator it = aFeaturesList.begin();
210     for( ; it != aFeaturesList.end(); ++it) {
211       QString anActionId = QString::fromStdString(*it);
212       bool theDefaultState = false;
213       if (myActions.contains(anActionId)) {
214         theDefaultState = myActions[anActionId]->isEnabled();
215       }
216       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
217     }
218   } else if (theMessage.get()) {
219     #ifdef _DEBUG
220     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
221               << theMessage->eventID().eventText() << std::endl;
222     #endif
223   }
224 }
225
226 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
227 {
228   QAction* aResult = NULL;
229   if (myOperationActions.contains(theId)) {
230     aResult = myOperationActions.value(theId);
231     if (theParent && aResult->parent() != theParent) {
232       aResult->setParent(theParent);
233     }
234   } else {
235     switch (theId) {
236       case Accept:
237       case AcceptAll:
238         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
239         break;
240       case Abort:
241       case AbortAll: {
242         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
243         if(theId == Abort) {
244           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
245         }
246       }
247       break;
248       case Help:
249         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
250         break;
251       default:
252         break;
253     }
254     myOperationActions.insert(theId, aResult);
255   }
256   return aResult;
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   if(!theOperation || !theOperation->feature())
299     return;
300   FeaturePtr aFeature = theOperation->feature();
301   QString aFeatureId = QString::fromStdString(aFeature->getKind());
302   setActionEnabled(aFeatureId, true);
303   setNestedCommandsEnabled(true, aFeatureId);
304
305   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
306 }
307
308 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
309 {
310   if (myActions.contains(theId)) {
311     QAction* anAction = myActions[theId];
312     if (anAction->isCheckable()) {
313       anAction->setChecked(theChecked);
314     }
315   }
316 }
317
318 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
319 {
320   if (myActions.contains(theId)) {
321     myActions[theId]->setEnabled(theEnabled);
322   }
323 }
324
325 /*
326  * Disables all actions which have the Document Kind different to
327  * the current document's kind
328  */
329 void XGUI_ActionsMgr::updateByDocumentKind()
330 {
331   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
332   QString aDocKind = QString::fromStdString(aStdDocKind);
333   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
334   foreach(QAction* eachAction, myActions.values()) {
335     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
336     QString aCmdDocKind;
337     if(aCmd) {
338       aCmdDocKind = aCmd->documentKind();
339     } else {
340       QString aId = eachAction->data().toString();
341       if (!aId.isEmpty()) {
342         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
343       }
344     }
345     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
346       eachAction->setEnabled(false);
347     }
348   }
349 }
350
351 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
352 {
353   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
354       EVENT_FEATURE_STATE_REQUEST);
355   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
356       std::make_shared<ModelAPI_FeatureStateMessage>(aStateRequestEventId, this);
357   aMsg->setFeature(anActiveFeature);
358   Events_Loop::loop()->send(aMsg, false);
359 }