Salome HOME
History menu: improvments and bugfixes
[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   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
132   if (aSelection->getSelected().size() == 0 || !myOperationMgr->hasOperation())
133     return;
134   ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
135   FeaturePtr anActiveFeature = anOperation->feature();
136   if(!anActiveFeature.get())
137     return;
138   QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
139
140   SessionPtr aMgr = ModelAPI_Session::get();
141   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
142   foreach(QString aId, nestedCommands(aFeatureId)) {
143     std::list<ModelAPI_Validator*> aValidators;
144     std::list<std::list<std::string> > anArguments;
145     if (!anArguments.empty()) {
146       std::list<std::string> firstArg = anArguments.front();
147     }
148     aFactory->validators(aId.toStdString(), aValidators, anArguments);
149     std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
150     std::list<std::list<std::string> >::iterator aValidatorArgs = anArguments.begin();
151     for (; aValidator != aValidators.end(); aValidator++, aValidatorArgs++) {
152       if (!(*aValidator))
153         continue;
154       const ModuleBase_SelectionValidator* aSelValidator =
155           dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
156       if (!aSelValidator)
157         continue;
158       setActionEnabled(aId, aSelValidator->isValid(aSelection, *aValidatorArgs));
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         break;
230       case Abort:
231       case AbortAll: {
232         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
233         if(theId == Abort) {
234           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
235         }
236       }
237       break;
238       case Help:
239         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
240         break;
241       default:
242         break;
243     }
244     myOperationActions.insert(theId, aResult);
245   }
246   return aResult;
247 }
248
249 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
250 {
251   ActionInfo aResult;
252   if(myActions.contains(theId)) {
253     aResult.initFrom(myActions.value(theId));
254   } else {
255    aResult.id = theId;
256    aResult.text = theId;
257   }
258   return aResult;
259 }
260
261 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
262 {
263   foreach(QString eachAction, myActions.keys())
264   {
265     setActionEnabled(eachAction, isEnabled);
266   }
267 }
268
269
270 //!
271 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
272 {
273   QStringList ltNestedActions;
274   if (theParent.isEmpty()) {  //Disable ALL nested
275     foreach(QString eachParent, myNestedActions.keys()) {
276       ltNestedActions << myNestedActions[eachParent];
277     }
278   } else {
279     ltNestedActions << myNestedActions[theParent];
280   }
281   foreach(QString eachNested, ltNestedActions) {
282     setActionEnabled(eachNested, theEnabled);
283   }
284 }
285
286 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
287 {
288   if(!theOperation || !theOperation->feature())
289     return;
290   FeaturePtr aFeature = theOperation->feature();
291   QString aFeatureId = QString::fromStdString(aFeature->getKind());
292   setNestedCommandsEnabled(true, aFeatureId);
293
294   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
295 }
296
297 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
298 {
299   if (myActions.contains(theId)) {
300     QAction* anAction = myActions[theId];
301     if (anAction->isCheckable()) {
302       anAction->setChecked(theChecked);
303     }
304   }
305 }
306
307 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
308 {
309   if (myActions.contains(theId)) {
310     myActions[theId]->setEnabled(theEnabled);
311   }
312 }
313
314 /*
315  * Disables all actions which have the Document Kind different to
316  * the current document's kind
317  */
318 void XGUI_ActionsMgr::updateByDocumentKind()
319 {
320   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
321   QString aDocKind = QString::fromStdString(aStdDocKind);
322   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
323   foreach(QAction* eachAction, myActions.values()) {
324     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
325     QString aCmdDocKind;
326     if(aCmd) {
327       aCmdDocKind = aCmd->documentKind();
328     } else {
329       QString aId = eachAction->data().toString();
330       if (!aId.isEmpty()) {
331         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
332       }
333     }
334     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
335       eachAction->setEnabled(false);
336     }
337   }
338 }
339
340 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
341 {
342   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
343       EVENT_FEATURE_STATE_REQUEST);
344   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
345       std::make_shared<ModelAPI_FeatureStateMessage>(aStateRequestEventId, this);
346   aMsg->setFeature(anActiveFeature);
347   Events_Loop::loop()->send(aMsg, false);
348 }