1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
8 #include <AppElements_Command.h>
11 #include <XGUI_ActionsMgr.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_SalomeConnector.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_SelectionMgr.h>
18 #include <Events_Loop.h>
19 #include <Events_Error.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_OperationFeature.h>
26 #include <ModuleBase_SelectionValidator.h>
36 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
38 myWorkshop(theParent),
39 myOperationMgr(theParent->operationMgr())
42 myShortcuts << QKeySequence::Save;
43 myShortcuts << QKeySequence::Undo;
44 myShortcuts << QKeySequence::Redo;
45 myShortcuts << QKeySequence::Open;
46 myShortcuts << QKeySequence::Close;
48 //Initialize event listening
49 Events_Loop* aLoop = Events_Loop::loop();
50 static Events_ID aStateResponseEventId =
51 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
52 aLoop->registerListener(this, aStateResponseEventId, NULL, true);
55 XGUI_ActionsMgr::~XGUI_ActionsMgr()
59 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
61 QString aId = theCmd->data().toString();
65 myActions.insert(aId, theCmd);
67 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
68 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
70 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
71 myNestedActions[aId] = aXCmd->nestedCommands();
75 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
77 myNestedActions[theId] = theCommands;
80 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
82 if (myNestedActions.contains(theId))
83 return myNestedActions[theId];
87 bool XGUI_ActionsMgr::isNested(const QString& theId) const
89 foreach(QString aId, myNestedActions.keys())
91 QStringList aList = myNestedActions[aId];
92 if (aList.contains(theId))
98 void XGUI_ActionsMgr::update()
100 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
101 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0) {
102 updateOnViewSelection();
104 FeaturePtr anActiveFeature = FeaturePtr();
105 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
106 (myOperationMgr->currentOperation());
108 anActiveFeature = aFOperation->feature();
109 if(anActiveFeature.get()) {
110 setAllEnabled(false);
111 QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
112 setActionEnabled(aFeatureId, true);
114 setNestedStackEnabled(aFOperation);
117 setNestedCommandsEnabled(false);
119 // TODO(SBH): Get defaults state of actions from XML and remove the following method
120 updateByDocumentKind();
121 updateByPlugins(anActiveFeature);
126 void XGUI_ActionsMgr::updateCheckState()
128 QString eachCommand = QString();
129 foreach(eachCommand, myActions.keys()) {
130 setActionChecked(eachCommand, false);
132 QStringList ltActiveCommands = myOperationMgr->operationList();
133 foreach(eachCommand, ltActiveCommands) {
134 setActionChecked(eachCommand, true);
138 void XGUI_ActionsMgr::updateOnViewSelection()
140 if (!myOperationMgr->hasOperation())
143 QStringList aIdList = myOperationMgr->operationList();
144 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
145 //FeaturePtr anActiveFeature = anOperation->feature();
146 //if(!anActiveFeature.get())
147 if (aIdList.isEmpty())
150 ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
151 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
152 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
153 // only viewer selection is processed
154 SessionPtr aMgr = ModelAPI_Session::get();
155 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
156 foreach(QString aFeatureId, aIdList) {
157 foreach(QString aId, nestedCommands(aFeatureId)) {
158 ModelAPI_ValidatorsFactory::Validators aValidators;
159 aFactory->validators(aId.toStdString(), aValidators);
160 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
161 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
162 const ModuleBase_SelectionValidator* aSelValidator =
163 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
165 setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
171 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
173 if (theKeySequence.isEmpty()) {
174 return QKeySequence();
176 if (myShortcuts.contains(theKeySequence)) {
177 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
178 aMessage = aMessage.arg(theKeySequence.toString());
179 Events_Error::send(aMessage.toStdString());
180 return QKeySequence();
182 myShortcuts.append(theKeySequence);
183 return theKeySequence;
186 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
188 if (theKeySequence.isEmpty()) {
189 return QKeySequence();
191 QKeySequence aResult(theKeySequence);
192 registerShortcut(aResult);
196 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
198 const Events_ID kResponseEvent =
199 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
200 if (theMessage->eventID() == kResponseEvent) {
201 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
202 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
203 if (!aStateMessage.get())
205 std::list<std::string> aFeaturesList = aStateMessage->features();
206 std::list<std::string>::iterator it = aFeaturesList.begin();
207 for( ; it != aFeaturesList.end(); ++it) {
208 QString anActionId = QString::fromStdString(*it);
209 bool theDefaultState = false;
210 if (myActions.contains(anActionId)) {
211 theDefaultState = myActions[anActionId]->isEnabled();
213 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
215 } else if (theMessage.get()) {
217 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
218 << theMessage->eventID().eventText() << std::endl;
223 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
225 QAction* aResult = NULL;
226 if (myOperationActions.contains(theId)) {
227 aResult = myOperationActions.value(theId);
228 if (theParent && aResult->parent() != theParent) {
229 aResult->setParent(theParent);
235 aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
239 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
241 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
246 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
251 myOperationActions.insert(theId, aResult);
256 QAction* XGUI_ActionsMgr::action(const QString& theId)
258 QAction* anAction = 0;
259 if(myActions.contains(theId)) {
260 anAction = myActions.value(theId);
265 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
268 if(myActions.contains(theId)) {
269 aResult.initFrom(myActions.value(theId));
272 aResult.text = theId;
277 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
279 foreach(QString eachAction, myActions.keys())
281 setActionEnabled(eachAction, isEnabled);
287 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
289 QStringList ltNestedActions;
290 if (theParent.isEmpty()) { //Disable ALL nested
291 foreach(QString eachParent, myNestedActions.keys()) {
292 ltNestedActions << myNestedActions[eachParent];
295 ltNestedActions << myNestedActions[theParent];
297 foreach(QString eachNested, ltNestedActions) {
298 setActionEnabled(eachNested, theEnabled);
302 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
304 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
305 if(!anOperation || !anOperation->feature())
307 FeaturePtr aFeature = anOperation->feature();
308 QString aFeatureId = QString::fromStdString(aFeature->getKind());
309 setActionEnabled(aFeatureId, true);
310 setNestedCommandsEnabled(true, aFeatureId);
312 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
315 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
317 if (myActions.contains(theId)) {
318 QAction* anAction = myActions[theId];
319 if (anAction->isCheckable()) {
320 anAction->setChecked(theChecked);
325 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
327 if (myActions.contains(theId)) {
328 myActions[theId]->setEnabled(theEnabled);
333 * Disables all actions which have the Document Kind different to
334 * the current document's kind
336 void XGUI_ActionsMgr::updateByDocumentKind()
338 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
339 QString aDocKind = QString::fromStdString(aStdDocKind);
340 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
341 foreach(QAction* eachAction, myActions.values()) {
344 QString aId = eachAction->data().toString();
345 if (!aId.isEmpty()) {
346 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
349 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
350 aCmdDocKind = aCmd->documentKind();
352 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
353 eachAction->setEnabled(false);
358 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
360 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
361 EVENT_FEATURE_STATE_REQUEST);
362 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
363 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
364 aMsg->setFeature(anActiveFeature);
365 Events_Loop::loop()->send(aMsg, false);