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_OperationFeature.h>
24 #include <ModuleBase_SelectionValidator.h>
34 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
36 myWorkshop(theParent),
37 myOperationMgr(theParent->operationMgr())
40 myShortcuts << QKeySequence::Save;
41 myShortcuts << QKeySequence::Undo;
42 myShortcuts << QKeySequence::Redo;
43 myShortcuts << QKeySequence::Open;
44 myShortcuts << QKeySequence::Close;
46 //Initialize event listening
47 Events_Loop* aLoop = Events_Loop::loop();
48 static Events_ID aStateResponseEventId =
49 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
50 aLoop->registerListener(this, aStateResponseEventId, NULL, true);
53 XGUI_ActionsMgr::~XGUI_ActionsMgr()
57 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
59 QString aId = theCmd->data().toString();
63 myActions.insert(aId, theCmd);
64 AppElements_Command* aXCmd = dynamic_cast<AppElements_Command*>(theCmd);
66 myNestedActions[aId] = aXCmd->nestedCommands();
68 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
69 myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
73 void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
75 myNestedActions[theId] = theCommands;
78 QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
80 if (myNestedActions.contains(theId))
81 return myNestedActions[theId];
85 bool XGUI_ActionsMgr::isNested(const QString& theId) const
87 foreach(QString aId, myNestedActions.keys())
89 QStringList aList = myNestedActions[aId];
90 if (aList.contains(theId))
96 void XGUI_ActionsMgr::update()
98 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
99 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() > 0) {
100 updateOnViewSelection();
102 FeaturePtr anActiveFeature = FeaturePtr();
103 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
104 (myOperationMgr->currentOperation());
106 anActiveFeature = aFOperation->feature();
107 if(anActiveFeature.get()) {
108 setAllEnabled(false);
109 QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
110 setActionEnabled(aFeatureId, true);
112 setNestedStackEnabled(aFOperation);
115 setNestedCommandsEnabled(false);
117 // TODO(SBH): Get defaults state of actions from XML and remove the following method
118 updateByDocumentKind();
120 updateByPlugins(anActiveFeature);
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 = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
237 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
239 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
244 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
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())
279 setActionEnabled(eachAction, isEnabled);
285 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
287 QStringList ltNestedActions;
288 if (theParent.isEmpty()) { //Disable ALL nested
289 foreach(QString eachParent, myNestedActions.keys()) {
290 ltNestedActions << myNestedActions[eachParent];
293 ltNestedActions << myNestedActions[theParent];
295 foreach(QString eachNested, ltNestedActions) {
296 setActionEnabled(eachNested, theEnabled);
300 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
302 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
303 if(!anOperation || !anOperation->feature())
305 FeaturePtr aFeature = anOperation->feature();
306 QString aFeatureId = QString::fromStdString(aFeature->getKind());
307 setActionEnabled(aFeatureId, true);
308 setNestedCommandsEnabled(true, aFeatureId);
310 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
313 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
315 if (myActions.contains(theId)) {
316 QAction* anAction = myActions[theId];
317 if (anAction->isCheckable()) {
318 anAction->setChecked(theChecked);
323 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
325 if (myActions.contains(theId)) {
326 myActions[theId]->setEnabled(theEnabled);
331 * Disables all actions which have the Document Kind different to
332 * the current document's kind
334 void XGUI_ActionsMgr::updateByDocumentKind()
336 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
337 QString aDocKind = QString::fromStdString(aStdDocKind);
338 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
339 foreach(QAction* eachAction, myActions.values()) {
340 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
343 aCmdDocKind = aCmd->documentKind();
345 QString aId = eachAction->data().toString();
346 if (!aId.isEmpty()) {
347 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
350 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
351 eachAction->setEnabled(false);
356 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
358 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
359 EVENT_FEATURE_STATE_REQUEST);
360 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
361 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
362 aMsg->setFeature(anActiveFeature);
363 Events_Loop::loop()->send(aMsg, false);