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>
32 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
34 myWorkshop(theParent),
35 myOperationMgr(theParent->operationMgr())
38 myShortcuts << QKeySequence::Save;
39 myShortcuts << QKeySequence::Undo;
40 myShortcuts << QKeySequence::Redo;
41 myShortcuts << QKeySequence::Open;
42 myShortcuts << QKeySequence::Close;
44 //Initialize event listening
45 Events_Loop* aLoop = Events_Loop::loop();
46 static Events_ID aStateResponseEventId =
47 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
48 aLoop->registerListener(this, aStateResponseEventId, NULL, true);
51 XGUI_ActionsMgr::~XGUI_ActionsMgr()
55 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
57 QString aId = theCmd->data().toString();
61 myActions.insert(aId, theCmd);
63 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
64 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
66 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
67 myNestedActions[aId] = aXCmd->nestedCommands();
71 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
73 myNestedActions[theId] = theCommands;
76 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
78 if (myNestedActions.contains(theId))
79 return myNestedActions[theId];
83 bool XGUI_ActionsMgr::isNested(const QString& theId) const
85 foreach(QString aId, myNestedActions.keys())
87 QStringList aList = myNestedActions[aId];
88 if (aList.contains(theId))
94 void XGUI_ActionsMgr::updateCommandsStatus()
97 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
98 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
99 updateOnViewSelection();
101 FeaturePtr anActiveFeature = FeaturePtr();
102 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
103 (myOperationMgr->currentOperation());
105 anActiveFeature = aFOperation->feature();
106 QStringList aNested = allNestedCommands(aFOperation);
107 foreach(QString aAction, myActions.keys()) {
108 if (!aNested.contains(aAction))
109 setActionEnabled(aAction, false);
112 setNestedCommandsEnabled(false);
114 updateByPlugins(anActiveFeature);
115 updateByDocumentKind();
119 void XGUI_ActionsMgr::updateCheckState()
121 QString eachCommand = QString();
122 foreach(eachCommand, myActions.keys()) {
123 setActionChecked(eachCommand, false);
125 QStringList ltActiveCommands = myOperationMgr->operationList();
126 foreach(eachCommand, ltActiveCommands) {
127 setActionChecked(eachCommand, true);
131 void XGUI_ActionsMgr::updateOnViewSelection()
133 if (!myOperationMgr->hasOperation())
136 QStringList aIdList = myOperationMgr->operationList();
137 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
138 //FeaturePtr anActiveFeature = anOperation->feature();
139 //if(!anActiveFeature.get())
140 if (aIdList.isEmpty())
143 ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
144 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
145 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
146 // only viewer selection is processed
147 SessionPtr aMgr = ModelAPI_Session::get();
148 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
149 foreach(QString aFeatureId, aIdList) {
150 foreach(QString aId, nestedCommands(aFeatureId)) {
151 ModelAPI_ValidatorsFactory::Validators aValidators;
152 aFactory->validators(aId.toStdString(), aValidators);
153 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
154 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
155 const ModuleBase_SelectionValidator* aSelValidator =
156 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
158 setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
164 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
166 if (theKeySequence.isEmpty()) {
167 return QKeySequence();
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();
175 myShortcuts.append(theKeySequence);
176 return theKeySequence;
179 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
181 if (theKeySequence.isEmpty()) {
182 return QKeySequence();
184 QKeySequence aResult(theKeySequence);
185 registerShortcut(aResult);
189 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
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())
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();
206 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
208 } else if (theMessage.get()) {
210 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
211 << theMessage->eventID().eventText() << std::endl;
216 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
218 QAction* aResult = NULL;
219 if (myOperationActions.contains(theId)) {
220 aResult = myOperationActions.value(theId);
221 if (theParent && aResult->parent() != theParent) {
222 aResult->setParent(theParent);
228 aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
229 aResult->setToolTip("Apply");
234 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
235 aResult->setToolTip("Cancel");
236 if (theId == Abort) {
237 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
242 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
243 aResult->setToolTip("Help");
249 myOperationActions.insert(theId, aResult);
254 QAction* XGUI_ActionsMgr::action(const QString& theId)
256 QAction* anAction = 0;
257 if(myActions.contains(theId)) {
258 anAction = myActions.value(theId);
263 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
266 if(myActions.contains(theId)) {
267 aResult.initFrom(myActions.value(theId));
270 aResult.text = theId;
275 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
277 foreach(QString eachAction, myActions.keys()) {
278 setActionEnabled(eachAction, isEnabled);
284 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
286 QStringList ltNestedActions;
287 if (theParent.isEmpty()) { //Disable ALL nested
288 foreach(QString eachParent, myNestedActions.keys()) {
289 ltNestedActions << myNestedActions[eachParent];
292 ltNestedActions << myNestedActions[theParent];
294 foreach(QString eachNested, ltNestedActions) {
295 setActionEnabled(eachNested, theEnabled);
299 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
301 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
302 if(!anOperation || !anOperation->feature())
304 FeaturePtr aFeature = anOperation->feature();
305 QString aFeatureId = QString::fromStdString(aFeature->getKind());
306 //setActionEnabled(aFeatureId, true);
307 setNestedCommandsEnabled(true, aFeatureId);
309 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
312 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
314 QStringList aFeatures;
315 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
316 if(!anOperation || !anOperation->feature())
318 FeaturePtr aFeature = anOperation->feature();
319 QString aFeatureId = QString::fromStdString(aFeature->getKind());
321 aFeatures << myNestedActions[aFeatureId];
322 aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
326 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
328 if (myActions.contains(theId)) {
329 QAction* anAction = myActions[theId];
330 if (anAction->isCheckable()) {
331 anAction->setChecked(theChecked);
336 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
338 if (myActions.contains(theId)) {
339 QAction* aAction = myActions[theId];
340 // Initially all actions are enabled
341 // If it was disabled for any reason then we can not enable it
342 if (aAction->isEnabled())
343 aAction->setEnabled(theEnabled);
348 * Disables all actions which have the Document Kind different to
349 * the current document's kind
351 void XGUI_ActionsMgr::updateByDocumentKind()
353 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
354 QString aDocKind = QString::fromStdString(aStdDocKind);
355 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
356 foreach(QAction* eachAction, myActions.values()) {
359 QString aId = eachAction->data().toString();
360 if (!aId.isEmpty()) {
361 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
364 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
365 aCmdDocKind = aCmd->documentKind();
367 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
368 eachAction->setEnabled(false);
373 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
375 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
376 EVENT_FEATURE_STATE_REQUEST);
377 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
378 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
379 aMsg->setFeature(anActiveFeature);
380 Events_Loop::loop()->send(aMsg, false);