Salome HOME
Add new test
[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   // only viewer selection is processed
144   if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
145     // it seems that this code is not nesessary anymore. It leads to incorrect case:
146     // sketch operation start, click in any place in the viewer. The result is all nested
147     // entities are enabled(but the sketch plane is not selected yet). Any sketch operation
148     // can be started but will be incorrect on preview build before it uses the sketch unset plane.
149     /*foreach(QString aFeatureId, aIdList) {
150       foreach(QString aId, nestedCommands(aFeatureId)) {
151         setActionEnabled(aId, true);
152       }
153     }*/
154   } else { 
155     SessionPtr aMgr = ModelAPI_Session::get();
156     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
157     foreach(QString aFeatureId, aIdList) {
158       foreach(QString aId, nestedCommands(aFeatureId)) {
159         ModelAPI_ValidatorsFactory::Validators aValidators;
160         aFactory->validators(aId.toStdString(), aValidators);
161         ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
162         for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
163           const ModuleBase_SelectionValidator* aSelValidator =
164               dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
165           if (!aSelValidator)
166             continue;
167           setActionEnabled(aId, aSelValidator->isValid(aSelection, aValidatorIt->second));
168         }
169       }
170     }
171   }
172 }
173
174 void XGUI_ActionsMgr::onAcceptAllToggled(bool theState)
175 {
176   if (!theState) {
177     QAction* anAcceptAllAction = operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
178     anAcceptAllAction->setEnabled(theState);
179   }
180 }
181
182 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
183 {
184   if (theKeySequence.isEmpty()) {
185     return QKeySequence();
186   }
187   if (myShortcuts.contains(theKeySequence)) {
188     QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
189     aMessage = aMessage.arg(theKeySequence.toString());
190     Events_Error::send(aMessage.toStdString());
191     return QKeySequence();
192   }
193   myShortcuts.append(theKeySequence);
194   return theKeySequence;
195 }
196
197 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
198 {
199   if (theKeySequence.isEmpty()) {
200     return QKeySequence();
201   }
202   QKeySequence aResult(theKeySequence);
203   registerShortcut(aResult);
204   return aResult;
205 }
206
207 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
208 {
209   const Events_ID kResponseEvent =
210       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
211   if (theMessage->eventID() == kResponseEvent) {
212     std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
213         std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
214     if (!aStateMessage.get())
215       return;
216     std::list<std::string> aFeaturesList = aStateMessage->features();
217     std::list<std::string>::iterator it = aFeaturesList.begin();
218     for( ; it != aFeaturesList.end(); ++it) {
219       QString anActionId = QString::fromStdString(*it);
220       bool theDefaultState = false;
221       if (myActions.contains(anActionId)) {
222         theDefaultState = myActions[anActionId]->isEnabled();
223       }
224       setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
225     }
226   } else if (theMessage.get()) {
227     #ifdef _DEBUG
228     std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
229               << theMessage->eventID().eventText() << std::endl;
230     #endif
231   }
232 }
233
234 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
235 {
236   QAction* aResult = NULL;
237   if (myOperationActions.contains(theId)) {
238     aResult = myOperationActions.value(theId);
239     if (theParent && aResult->parent() != theParent) {
240       aResult->setParent(theParent);
241     }
242   } else {
243     switch (theId) {
244       case Accept:
245       case AcceptAll:
246         aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
247         // the default value is disabled, so the next connect is used to restore this
248         // default state by untoggle this action
249         connect(this, SIGNAL(toggled(bool)), this, SLOT(onAcceptAllToggled(bool)));
250         break;
251       case Abort:
252       case AbortAll: {
253         aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
254         if(theId == Abort) {
255           aResult->setShortcut(QKeySequence(Qt::Key_Escape));
256         }
257       }
258       break;
259       case Help:
260         aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
261         break;
262       default:
263         break;
264     }
265     myOperationActions.insert(theId, aResult);
266   }
267   return aResult;
268 }
269
270 QAction* XGUI_ActionsMgr::action(const QString& theId)
271 {
272   QAction* anAction;
273   if(myActions.contains(theId)) {
274     anAction = myActions.value(theId);
275   }
276   return anAction;
277 }
278
279 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
280 {
281   ActionInfo aResult;
282   if(myActions.contains(theId)) {
283     aResult.initFrom(myActions.value(theId));
284   } else {
285    aResult.id = theId;
286    aResult.text = theId;
287   }
288   return aResult;
289 }
290
291 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
292 {
293   foreach(QString eachAction, myActions.keys())
294   {
295     setActionEnabled(eachAction, isEnabled);
296   }
297 }
298
299
300 //!
301 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
302 {
303   QStringList ltNestedActions;
304   if (theParent.isEmpty()) {  //Disable ALL nested
305     foreach(QString eachParent, myNestedActions.keys()) {
306       ltNestedActions << myNestedActions[eachParent];
307     }
308   } else {
309     ltNestedActions << myNestedActions[theParent];
310   }
311   foreach(QString eachNested, ltNestedActions) {
312     setActionEnabled(eachNested, theEnabled);
313   }
314 }
315
316 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
317 {
318   if(!theOperation || !theOperation->feature())
319     return;
320   FeaturePtr aFeature = theOperation->feature();
321   QString aFeatureId = QString::fromStdString(aFeature->getKind());
322   setActionEnabled(aFeatureId, true);
323   setNestedCommandsEnabled(true, aFeatureId);
324
325   setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
326 }
327
328 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
329 {
330   if (myActions.contains(theId)) {
331     QAction* anAction = myActions[theId];
332     if (anAction->isCheckable()) {
333       anAction->setChecked(theChecked);
334     }
335   }
336 }
337
338 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
339 {
340   if (myActions.contains(theId)) {
341     myActions[theId]->setEnabled(theEnabled);
342   }
343 }
344
345 /*
346  * Disables all actions which have the Document Kind different to
347  * the current document's kind
348  */
349 void XGUI_ActionsMgr::updateByDocumentKind()
350 {
351   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
352   QString aDocKind = QString::fromStdString(aStdDocKind);
353   XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
354   foreach(QAction* eachAction, myActions.values()) {
355     AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
356     QString aCmdDocKind;
357     if(aCmd) {
358       aCmdDocKind = aCmd->documentKind();
359     } else {
360       QString aId = eachAction->data().toString();
361       if (!aId.isEmpty()) {
362         aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
363       }
364     }
365     if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
366       eachAction->setEnabled(false);
367     }
368   }
369 }
370
371 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
372 {
373   static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
374       EVENT_FEATURE_STATE_REQUEST);
375   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
376       new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
377   aMsg->setFeature(anActiveFeature);
378   Events_Loop::loop()->send(aMsg, false);
379 }