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_Error.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>
33 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
35 myWorkshop(theParent),
36 myOperationMgr(theParent->operationMgr())
39 myShortcuts << QKeySequence::Save;
40 myShortcuts << QKeySequence::Undo;
41 myShortcuts << QKeySequence::Redo;
42 myShortcuts << QKeySequence::Open;
43 myShortcuts << QKeySequence::Close;
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);
52 XGUI_ActionsMgr::~XGUI_ActionsMgr()
56 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
58 QString aId = theCmd->data().toString();
62 myActions.insert(aId, theCmd);
64 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
65 const std::shared_ptr<Config_FeatureMessage>& anInfo =
66 aWorkshop->salomeConnector()->featureInfo(aId);
68 myNestedActions[aId] = QString::fromStdString(anInfo->nestedFeatures())
69 .split(" ", QString::SkipEmptyParts);
71 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
72 myNestedActions[aId] = aXCmd->nestedCommands();
76 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
78 myNestedActions[theId] = theCommands;
81 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
83 if (myNestedActions.contains(theId))
84 return myNestedActions[theId];
88 bool XGUI_ActionsMgr::isNested(const QString& theId) const
90 foreach(QString aId, myNestedActions.keys())
92 QStringList aList = myNestedActions[aId];
93 if (aList.contains(theId))
99 void XGUI_ActionsMgr::updateCommandsStatus()
102 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
103 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
104 updateOnViewSelection();
106 FeaturePtr anActiveFeature = FeaturePtr();
107 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
108 (myOperationMgr->currentOperation());
110 anActiveFeature = aFOperation->feature();
111 QStringList aNested = allNestedCommands(aFOperation);
112 foreach(QString aAction, myActions.keys()) {
113 if (!aNested.contains(aAction))
114 setActionEnabled(aAction, false);
117 setNestedCommandsEnabled(false);
119 updateByPlugins(anActiveFeature);
120 updateByDocumentKind();
124 void XGUI_ActionsMgr::updateCheckState()
126 QString eachCommand = QString();
127 foreach(eachCommand, myActions.keys()) {
128 setActionChecked(eachCommand, false);
130 QStringList ltActiveCommands = myOperationMgr->operationList();
131 foreach(eachCommand, ltActiveCommands) {
132 setActionChecked(eachCommand, true);
136 void XGUI_ActionsMgr::updateOnViewSelection()
138 if (!myOperationMgr->hasOperation())
141 QStringList aIdList = myOperationMgr->operationList();
142 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
143 //FeaturePtr anActiveFeature = anOperation->feature();
144 //if(!anActiveFeature.get())
145 if (aIdList.isEmpty())
148 ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
149 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
150 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
151 // only viewer selection is processed
152 SessionPtr aMgr = ModelAPI_Session::get();
153 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
154 foreach(QString aFeatureId, aIdList) {
155 foreach(QString aId, nestedCommands(aFeatureId)) {
156 ModelAPI_ValidatorsFactory::Validators aValidators;
157 aFactory->validators(aId.toStdString(), aValidators);
158 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
159 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
160 const ModuleBase_SelectionValidator* aSelValidator =
161 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
163 setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
169 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
171 if (theKeySequence.isEmpty()) {
172 return QKeySequence();
174 if (myShortcuts.contains(theKeySequence)) {
175 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
176 aMessage = aMessage.arg(theKeySequence.toString());
177 Events_Error::send(aMessage.toStdString());
178 return QKeySequence();
180 myShortcuts.append(theKeySequence);
181 return theKeySequence;
184 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
186 if (theKeySequence.isEmpty()) {
187 return QKeySequence();
189 QKeySequence aResult(theKeySequence);
190 registerShortcut(aResult);
194 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
196 const Events_ID kResponseEvent =
197 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
198 if (theMessage->eventID() == kResponseEvent) {
199 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
200 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
201 if (!aStateMessage.get())
203 std::list<std::string> aFeaturesList = aStateMessage->features();
204 std::list<std::string>::iterator it = aFeaturesList.begin();
205 for( ; it != aFeaturesList.end(); ++it) {
206 QString anActionId = QString::fromStdString(*it);
207 bool theDefaultState = false;
208 if (myActions.contains(anActionId)) {
209 theDefaultState = myActions[anActionId]->isEnabled();
211 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
213 } else if (theMessage.get()) {
215 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
216 << theMessage->eventID().eventText() << std::endl;
221 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
223 QAction* aResult = NULL;
224 if (myOperationActions.contains(theId)) {
225 aResult = myOperationActions.value(theId);
226 if (theParent && aResult->parent() != theParent) {
227 aResult->setParent(theParent);
233 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_ok.png"),
234 "", /*empty to show error*/, theParent);
239 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_cancel.png"), "Cancel",
241 if (theId == Abort) {
242 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
247 aResult = ModuleBase_Tools::createAction(QIcon(":pictures/button_help.png"), "Help",
252 aResult = ModuleBase_Tools::createAction(QIcon(), tr("See preview"), theParent, 0, 0, "Compute preview");
253 aResult->setStatusTip(aResult->toolTip());
259 myOperationActions.insert(theId, aResult);
264 QAction* XGUI_ActionsMgr::action(const QString& theId)
266 QAction* anAction = 0;
267 if(myActions.contains(theId)) {
268 anAction = myActions.value(theId);
273 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
276 if(myActions.contains(theId)) {
277 aResult.initFrom(myActions.value(theId));
280 aResult.text = theId;
285 void XGUI_ActionsMgr::setAllEnabled()
287 foreach(QString eachAction, myActions.keys()) {
288 if (myActions.contains(eachAction)) {
289 QAction* aAction = myActions[eachAction];
290 aAction->setEnabled(true);
297 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
299 QStringList ltNestedActions;
300 if (theParent.isEmpty()) { //Disable ALL nested
301 foreach(QString eachParent, myNestedActions.keys()) {
302 ltNestedActions << myNestedActions[eachParent];
305 ltNestedActions << myNestedActions[theParent];
307 foreach(QString eachNested, ltNestedActions) {
308 setActionEnabled(eachNested, theEnabled);
312 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
314 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
315 if(!anOperation || !anOperation->feature())
317 FeaturePtr aFeature = anOperation->feature();
318 QString aFeatureId = QString::fromStdString(aFeature->getKind());
319 //setActionEnabled(aFeatureId, true);
320 setNestedCommandsEnabled(true, aFeatureId);
322 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
325 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
327 QStringList aFeatures;
328 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
329 if(!anOperation || !anOperation->feature())
331 FeaturePtr aFeature = anOperation->feature();
332 QString aFeatureId = QString::fromStdString(aFeature->getKind());
334 aFeatures << myNestedActions[aFeatureId];
335 aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
339 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
341 if (myActions.contains(theId)) {
342 QAction* anAction = myActions[theId];
343 if (anAction->isCheckable()) {
344 anAction->setChecked(theChecked);
349 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
351 if (myActions.contains(theId)) {
352 QAction* aAction = myActions[theId];
353 // Initially all actions are enabled
354 // If it was disabled for any reason then we can not enable it
355 if (aAction->isEnabled())
356 aAction->setEnabled(theEnabled);
361 * Disables all actions which have the Document Kind different to
362 * the current document's kind
364 void XGUI_ActionsMgr::updateByDocumentKind()
366 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
367 QString aDocKind = QString::fromStdString(aStdDocKind);
368 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
369 foreach(QAction* eachAction, myActions.values()) {
372 QString aId = eachAction->data().toString();
373 if (!aId.isEmpty()) {
374 aCmdDocKind = QString::fromStdString(
375 aWorkshop->salomeConnector()->featureInfo(aId)->documentKind());
378 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
379 aCmdDocKind = QString::fromStdString(aCmd->featureMessage()->documentKind());
381 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
382 eachAction->setEnabled(false);
387 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
389 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
390 EVENT_FEATURE_STATE_REQUEST);
391 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
392 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
393 aMsg->setFeature(anActiveFeature);
394 Events_Loop::loop()->send(aMsg, false);