1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
4 #include <AppElements_Command.h>
7 #include <XGUI_ActionsMgr.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_OperationMgr.h>
10 #include <XGUI_SalomeConnector.h>
11 #include <XGUI_Selection.h>
12 #include <XGUI_SelectionMgr.h>
14 #include <Events_Loop.h>
15 #include <Events_InfoMessage.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_OperationFeature.h>
22 #include <ModuleBase_SelectionValidator.h>
23 #include <ModuleBase_Tools.h>
27 #include <QMainWindow>
34 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
36 myWorkshop(theParent),
37 myOperationMgr(theParent->operationMgr())
40 myShortcuts << QKeySequence::Save;
41 myShortcuts << QKeySequence::Undo;
42 myShortcuts << QKeySequence::Redo;
43 myShortcuts << QKeySequence::Open;
44 myShortcuts << QKeySequence::Close;
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);
53 XGUI_ActionsMgr::~XGUI_ActionsMgr()
57 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
59 QString aId = theCmd->data().toString();
63 myActions.insert(aId, theCmd);
65 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
66 const std::shared_ptr<Config_FeatureMessage>& anInfo =
67 aWorkshop->salomeConnector()->featureInfo(aId);
69 myNestedActions[aId] = QString::fromStdString(anInfo->nestedFeatures())
70 .split(" ", QString::SkipEmptyParts);
72 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
73 myNestedActions[aId] = aXCmd->nestedCommands();
77 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
79 myNestedActions[theId] = theCommands;
82 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
84 if (myNestedActions.contains(theId))
85 return myNestedActions[theId];
89 bool XGUI_ActionsMgr::isNested(const QString& theId) const
91 foreach(QString aId, myNestedActions.keys())
93 QStringList aList = myNestedActions[aId];
94 if (aList.contains(theId))
100 void XGUI_ActionsMgr::updateCommandsStatus()
103 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
104 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
105 updateOnViewSelection();
107 FeaturePtr anActiveFeature = FeaturePtr();
108 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
109 (myOperationMgr->currentOperation());
111 anActiveFeature = aFOperation->feature();
112 QStringList aNested = allNestedCommands(aFOperation);
113 foreach(QString aAction, myActions.keys()) {
114 if (!aNested.contains(aAction))
115 setActionEnabled(aAction, false);
118 setNestedCommandsEnabled(false);
120 updateByPlugins(anActiveFeature);
121 updateByDocumentKind();
125 void XGUI_ActionsMgr::updateCheckState()
127 QString eachCommand = QString();
128 foreach(eachCommand, myActions.keys()) {
129 setActionChecked(eachCommand, false);
131 QStringList ltActiveCommands = myOperationMgr->operationList();
132 foreach(eachCommand, ltActiveCommands) {
133 setActionChecked(eachCommand, true);
137 void XGUI_ActionsMgr::updateOnViewSelection()
139 if (!myOperationMgr->hasOperation())
142 QStringList aIdList = myOperationMgr->operationList();
143 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
144 //FeaturePtr anActiveFeature = anOperation->feature();
145 //if(!anActiveFeature.get())
146 if (aIdList.isEmpty())
149 ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
150 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
151 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
152 // only viewer selection is processed
153 SessionPtr aMgr = ModelAPI_Session::get();
154 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
155 foreach(QString aFeatureId, aIdList) {
156 foreach(QString aId, nestedCommands(aFeatureId)) {
157 ModelAPI_ValidatorsFactory::Validators aValidators;
158 aFactory->validators(aId.toStdString(), aValidators);
159 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
160 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
161 const ModuleBase_SelectionValidator* aSelValidator =
162 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
164 setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
170 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
172 if (theKeySequence.isEmpty()) {
173 return QKeySequence();
175 if (myShortcuts.contains(theKeySequence)) {
176 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
177 aMessage = aMessage.arg(theKeySequence.toString());
178 Events_InfoMessage("XGUI_ActionsMgr", aMessage.toStdString()).send();
179 return QKeySequence();
181 myShortcuts.append(theKeySequence);
182 return theKeySequence;
185 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
187 if (theKeySequence.isEmpty()) {
188 return QKeySequence();
190 QKeySequence aResult(theKeySequence);
191 registerShortcut(aResult);
195 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
197 const Events_ID kResponseEvent =
198 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
199 if (theMessage->eventID() == kResponseEvent) {
200 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
201 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
202 if (!aStateMessage.get())
204 std::list<std::string> aFeaturesList = aStateMessage->features();
205 std::list<std::string>::iterator it = aFeaturesList.begin();
206 for( ; it != aFeaturesList.end(); ++it) {
207 QString anActionId = QString::fromStdString(*it);
208 bool theDefaultState = false;
209 if (myActions.contains(anActionId)) {
210 theDefaultState = myActions[anActionId]->isEnabled();
212 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
214 } else if (theMessage.get()) {
216 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
217 << theMessage->eventID().eventText() << std::endl;
222 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId)
224 QAction* aResult = NULL;
225 if (myOperationActions.contains(theId)) {
226 aResult = myOperationActions.value(theId);
227 //if (theParent && aResult->parent() != theParent) {
228 // aResult->setParent(theParent);
231 QWidget* aParent = myWorkshop->desktop();
235 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_ok.png"),
236 "Apply" /*empty to show error*/, aParent);
241 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_cancel.png"), "Cancel",
243 if (theId == Abort) {
244 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
249 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_help.png"), "Help",
254 aResult = ModuleBase_Tools::createAction(QIcon(), tr("See preview"), aParent, 0, 0, "Compute preview");
255 aResult->setStatusTip(aResult->toolTip());
261 myOperationActions.insert(theId, aResult);
266 QAction* XGUI_ActionsMgr::action(const QString& theId)
268 QAction* anAction = 0;
269 if(myActions.contains(theId)) {
270 anAction = myActions.value(theId);
275 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
278 if(myActions.contains(theId)) {
279 aResult.initFrom(myActions.value(theId));
282 aResult.text = theId;
287 void XGUI_ActionsMgr::setAllEnabled()
289 foreach(QString eachAction, myActions.keys()) {
290 if (myActions.contains(eachAction)) {
291 QAction* aAction = myActions[eachAction];
292 aAction->setEnabled(true);
299 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
301 QStringList ltNestedActions;
302 if (theParent.isEmpty()) { //Disable ALL nested
303 foreach(QString eachParent, myNestedActions.keys()) {
304 ltNestedActions << myNestedActions[eachParent];
307 ltNestedActions << myNestedActions[theParent];
309 foreach(QString eachNested, ltNestedActions) {
310 setActionEnabled(eachNested, theEnabled);
314 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
316 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
317 if(!anOperation || !anOperation->feature())
319 FeaturePtr aFeature = anOperation->feature();
320 QString aFeatureId = QString::fromStdString(aFeature->getKind());
321 //setActionEnabled(aFeatureId, true);
322 setNestedCommandsEnabled(true, aFeatureId);
324 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
327 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
329 QStringList aFeatures;
330 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
331 if(!anOperation || !anOperation->feature())
333 FeaturePtr aFeature = anOperation->feature();
334 QString aFeatureId = QString::fromStdString(aFeature->getKind());
336 aFeatures << myNestedActions[aFeatureId];
337 aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
341 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
343 if (myActions.contains(theId)) {
344 QAction* anAction = myActions[theId];
345 if (anAction->isCheckable()) {
346 anAction->setChecked(theChecked);
351 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
353 if (myActions.contains(theId)) {
354 QAction* aAction = myActions[theId];
355 // Initially all actions are enabled
356 // If it was disabled for any reason then we can not enable it
357 if (aAction->isEnabled())
358 aAction->setEnabled(theEnabled);
363 * Disables all actions which have the Document Kind different to
364 * the current document's kind
366 void XGUI_ActionsMgr::updateByDocumentKind()
368 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
369 QString aDocKind = QString::fromStdString(aStdDocKind);
370 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
371 foreach(QAction* eachAction, myActions.values()) {
374 QString aId = eachAction->data().toString();
375 if (!aId.isEmpty()) {
376 aCmdDocKind = QString::fromStdString(
377 aWorkshop->salomeConnector()->featureInfo(aId)->documentKind());
380 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
381 aCmdDocKind = QString::fromStdString(aCmd->featureMessage()->documentKind());
383 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
384 eachAction->setEnabled(false);
389 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
391 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
392 EVENT_FEATURE_STATE_REQUEST);
393 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
394 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
395 aMsg->setFeature(anActiveFeature);
396 Events_Loop::loop()->send(aMsg, false);