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::updateCommandsStatus()
101 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
102 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0)
103 updateOnViewSelection();
105 FeaturePtr anActiveFeature = FeaturePtr();
106 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
107 (myOperationMgr->currentOperation());
109 anActiveFeature = aFOperation->feature();
110 QStringList aNested = allNestedCommands(aFOperation);
111 foreach(QString aAction, myActions.keys()) {
112 if (!aNested.contains(aAction))
113 setActionEnabled(aAction, false);
116 setNestedCommandsEnabled(false);
118 updateByPlugins(anActiveFeature);
119 updateByDocumentKind();
123 void XGUI_ActionsMgr::updateCheckState()
125 QString eachCommand = QString();
126 foreach(eachCommand, myActions.keys()) {
127 setActionChecked(eachCommand, false);
129 QStringList ltActiveCommands = myOperationMgr->operationList();
130 foreach(eachCommand, ltActiveCommands) {
131 setActionChecked(eachCommand, true);
135 void XGUI_ActionsMgr::updateOnViewSelection()
137 if (!myOperationMgr->hasOperation())
140 QStringList aIdList = myOperationMgr->operationList();
141 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
142 //FeaturePtr anActiveFeature = anOperation->feature();
143 //if(!anActiveFeature.get())
144 if (aIdList.isEmpty())
147 ModuleBase_Operation* theOperation = myOperationMgr->currentOperation();
148 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
149 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
150 // only viewer selection is processed
151 SessionPtr aMgr = ModelAPI_Session::get();
152 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
153 foreach(QString aFeatureId, aIdList) {
154 foreach(QString aId, nestedCommands(aFeatureId)) {
155 ModelAPI_ValidatorsFactory::Validators aValidators;
156 aFactory->validators(aId.toStdString(), aValidators);
157 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
158 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
159 const ModuleBase_SelectionValidator* aSelValidator =
160 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
162 setActionEnabled(aId, aSelValidator->isValid(aSelection, theOperation));
168 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
170 if (theKeySequence.isEmpty()) {
171 return QKeySequence();
173 if (myShortcuts.contains(theKeySequence)) {
174 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
175 aMessage = aMessage.arg(theKeySequence.toString());
176 Events_Error::send(aMessage.toStdString());
177 return QKeySequence();
179 myShortcuts.append(theKeySequence);
180 return theKeySequence;
183 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
185 if (theKeySequence.isEmpty()) {
186 return QKeySequence();
188 QKeySequence aResult(theKeySequence);
189 registerShortcut(aResult);
193 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
195 const Events_ID kResponseEvent =
196 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
197 if (theMessage->eventID() == kResponseEvent) {
198 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
199 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
200 if (!aStateMessage.get())
202 std::list<std::string> aFeaturesList = aStateMessage->features();
203 std::list<std::string>::iterator it = aFeaturesList.begin();
204 for( ; it != aFeaturesList.end(); ++it) {
205 QString anActionId = QString::fromStdString(*it);
206 bool theDefaultState = false;
207 if (myActions.contains(anActionId)) {
208 theDefaultState = myActions[anActionId]->isEnabled();
210 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
212 } else if (theMessage.get()) {
214 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
215 << theMessage->eventID().eventText() << std::endl;
220 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
222 QAction* aResult = NULL;
223 if (myOperationActions.contains(theId)) {
224 aResult = myOperationActions.value(theId);
225 if (theParent && aResult->parent() != theParent) {
226 aResult->setParent(theParent);
232 aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
236 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
238 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
243 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
248 myOperationActions.insert(theId, aResult);
253 QAction* XGUI_ActionsMgr::action(const QString& theId)
255 QAction* anAction = 0;
256 if(myActions.contains(theId)) {
257 anAction = myActions.value(theId);
262 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
265 if(myActions.contains(theId)) {
266 aResult.initFrom(myActions.value(theId));
269 aResult.text = theId;
274 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
276 foreach(QString eachAction, myActions.keys()) {
277 setActionEnabled(eachAction, isEnabled);
283 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
285 QStringList ltNestedActions;
286 if (theParent.isEmpty()) { //Disable ALL nested
287 foreach(QString eachParent, myNestedActions.keys()) {
288 ltNestedActions << myNestedActions[eachParent];
291 ltNestedActions << myNestedActions[theParent];
293 foreach(QString eachNested, ltNestedActions) {
294 setActionEnabled(eachNested, theEnabled);
298 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
300 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
301 if(!anOperation || !anOperation->feature())
303 FeaturePtr aFeature = anOperation->feature();
304 QString aFeatureId = QString::fromStdString(aFeature->getKind());
305 //setActionEnabled(aFeatureId, true);
306 setNestedCommandsEnabled(true, aFeatureId);
308 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
311 QStringList XGUI_ActionsMgr::allNestedCommands(ModuleBase_Operation* theOperation)
313 QStringList aFeatures;
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());
320 aFeatures << myNestedActions[aFeatureId];
321 aFeatures << allNestedCommands(myOperationMgr->previousOperation(theOperation));
325 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
327 if (myActions.contains(theId)) {
328 QAction* anAction = myActions[theId];
329 if (anAction->isCheckable()) {
330 anAction->setChecked(theChecked);
335 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
337 if (myActions.contains(theId)) {
338 QAction* aAction = myActions[theId];
339 // Initially all actions are enabled
340 // If it was disabled for any reason then we can not enable it
341 if (aAction->isEnabled())
342 aAction->setEnabled(theEnabled);
347 * Disables all actions which have the Document Kind different to
348 * the current document's kind
350 void XGUI_ActionsMgr::updateByDocumentKind()
352 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
353 QString aDocKind = QString::fromStdString(aStdDocKind);
354 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
355 foreach(QAction* eachAction, myActions.values()) {
358 QString aId = eachAction->data().toString();
359 if (!aId.isEmpty()) {
360 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
363 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
364 aCmdDocKind = aCmd->documentKind();
366 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
367 eachAction->setEnabled(false);
372 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
374 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
375 EVENT_FEATURE_STATE_REQUEST);
376 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
377 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
378 aMsg->setFeature(anActiveFeature);
379 Events_Loop::loop()->send(aMsg, false);