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