1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
7 #include <AppElements_Command.h>
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>
16 #include <Events_Loop.h>
17 #include <Events_Error.h>
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>
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);
63 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
65 myNestedActions[aId] = aXCmd->nestedCommands();
67 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
68 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
72 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
74 myNestedActions[theId] = theCommands;
77 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
79 if (myNestedActions.contains(theId))
80 return myNestedActions[theId];
84 bool XGUI_ActionsMgr::isNested(const QString& theId) const
86 foreach(QString aId, myNestedActions.keys())
88 QStringList aList = myNestedActions[aId];
89 if (aList.contains(theId))
95 void XGUI_ActionsMgr::update()
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);
106 setNestedStackEnabled(anOperation);
109 setNestedCommandsEnabled(false);
111 // TODO(SBH): Get defaults state of actions from XML and remove the following method
112 updateByDocumentKind();
114 updateByPlugins(anActiveFeature);
117 void XGUI_ActionsMgr::updateCheckState()
119 QString eachCommand = QString();
120 foreach(eachCommand, myActions.keys()) {
121 setActionChecked(eachCommand, false);
123 QStringList ltActiveCommands = myOperationMgr->operationList();
124 foreach(eachCommand, ltActiveCommands) {
125 setActionChecked(eachCommand, true);
129 void XGUI_ActionsMgr::updateOnViewSelection()
131 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
132 if (aSelection->getSelected().size() == 0 || !myOperationMgr->hasOperation())
134 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
135 FeaturePtr anActiveFeature = anOperation->feature();
136 if(!anActiveFeature.get())
138 QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
140 SessionPtr aMgr = ModelAPI_Session::get();
141 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
142 foreach(QString aId, nestedCommands(aFeatureId)) {
143 std::list<ModelAPI_Validator*> aValidators;
144 std::list<std::list<std::string> > anArguments;
145 if (!anArguments.empty()) {
146 std::list<std::string> firstArg = anArguments.front();
148 aFactory->validators(aId.toStdString(), aValidators, anArguments);
149 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
150 std::list<std::list<std::string> >::iterator aValidatorArgs = anArguments.begin();
151 for (; aValidator != aValidators.end(); aValidator++, aValidatorArgs++) {
154 const ModuleBase_SelectionValidator* aSelValidator =
155 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
158 setActionEnabled(aId, aSelValidator->isValid(aSelection, *aValidatorArgs));
164 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
166 if (theKeySequence.isEmpty()) {
167 return QKeySequence();
169 QKeySequence aResult(theKeySequence);
170 if (myShortcuts.contains(aResult)) {
171 QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
172 Events_Error::send(aMessage.toStdString());
173 return QKeySequence();
175 myShortcuts.append(aResult);
179 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
181 const Events_ID kResponseEvent =
182 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
183 if (theMessage->eventID() == kResponseEvent) {
184 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
185 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
186 if (!aStateMessage.get())
188 std::list<std::string> aFeaturesList = aStateMessage->features();
189 std::list<std::string>::iterator it = aFeaturesList.begin();
190 for( ; it != aFeaturesList.end(); ++it) {
191 QString anActionId = QString::fromStdString(*it);
192 bool theDefaultState = false;
193 if (myActions.contains(anActionId)) {
194 theDefaultState = myActions[anActionId]->isEnabled();
196 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
198 } else if (theMessage.get()) {
200 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
201 << theMessage->eventID().eventText() << std::endl;
206 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
208 foreach(QString eachAction, myActions.keys())
210 setActionEnabled(eachAction, isEnabled);
216 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
218 QStringList ltNestedActions;
219 if (theParent.isEmpty()) { //Disable ALL nested
220 foreach(QString eachParent, myNestedActions.keys()) {
221 ltNestedActions << myNestedActions[eachParent];
224 ltNestedActions << myNestedActions[theParent];
226 foreach(QString eachNested, ltNestedActions) {
227 setActionEnabled(eachNested, theEnabled);
231 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
233 if(!theOperation || !theOperation->feature())
235 FeaturePtr aFeature = theOperation->feature();
236 QString aFeatureId = QString::fromStdString(aFeature->getKind());
237 setNestedCommandsEnabled(true, aFeatureId);
239 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
242 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
244 if (myActions.contains(theId)) {
245 QAction* anAction = myActions[theId];
246 if (anAction->isCheckable()) {
247 anAction->setChecked(theChecked);
252 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
254 if (myActions.contains(theId)) {
255 myActions[theId]->setEnabled(theEnabled);
260 * Disables all actions which have the Document Kind different to
261 * the current document's kind
263 void XGUI_ActionsMgr::updateByDocumentKind()
265 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
266 QString aDocKind = QString::fromStdString(aStdDocKind);
267 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
268 foreach(QAction* eachAction, myActions.values()) {
269 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
272 aCmdDocKind = aCmd->documentKind();
274 QString aId = eachAction->data().toString();
275 if (!aId.isEmpty()) {
276 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
279 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
280 eachAction->setEnabled(false);
285 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
287 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
288 EVENT_FEATURE_STATE_REQUEST);
289 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg =
290 std::make_shared<ModelAPI_FeatureStateMessage>(aStateRequestEventId, this);
291 aMsg->setFeature(anActiveFeature);
292 Events_Loop::loop()->send(aMsg, false);