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 FeaturePtr anActiveFeature = FeaturePtr();
99 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
100 (myOperationMgr->currentOperation());
102 anActiveFeature = aFOperation->feature();
103 if(anActiveFeature.get()) {
104 setAllEnabled(false);
105 QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
106 setActionEnabled(aFeatureId, true);
108 setNestedStackEnabled(aFOperation);
111 setNestedCommandsEnabled(false);
113 // TODO(SBH): Get defaults state of actions from XML and remove the following method
114 updateByDocumentKind();
116 updateByPlugins(anActiveFeature);
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 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
144 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
145 // only viewer selection is processed
146 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
147 // it seems that this code is not nesessary anymore. It leads to incorrect case:
148 // sketch operation start, click in any place in the viewer. The result is all nested
149 // entities are enabled(but the sketch plane is not selected yet). Any sketch operation
150 // can be started but will be incorrect on preview build before it uses the sketch unset plane.
151 /*foreach(QString aFeatureId, aIdList) {
152 foreach(QString aId, nestedCommands(aFeatureId)) {
153 setActionEnabled(aId, true);
157 SessionPtr aMgr = ModelAPI_Session::get();
158 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
159 foreach(QString aFeatureId, aIdList) {
160 foreach(QString aId, nestedCommands(aFeatureId)) {
161 ModelAPI_ValidatorsFactory::Validators aValidators;
162 aFactory->validators(aId.toStdString(), aValidators);
163 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
164 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
165 const ModuleBase_SelectionValidator* aSelValidator =
166 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
169 setActionEnabled(aId, aSelValidator->isValid(aSelection, aValidatorIt->second));
176 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
178 if (theKeySequence.isEmpty()) {
179 return QKeySequence();
181 if (myShortcuts.contains(theKeySequence)) {
182 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
183 aMessage = aMessage.arg(theKeySequence.toString());
184 Events_Error::send(aMessage.toStdString());
185 return QKeySequence();
187 myShortcuts.append(theKeySequence);
188 return theKeySequence;
191 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
193 if (theKeySequence.isEmpty()) {
194 return QKeySequence();
196 QKeySequence aResult(theKeySequence);
197 registerShortcut(aResult);
201 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
203 const Events_ID kResponseEvent =
204 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
205 if (theMessage->eventID() == kResponseEvent) {
206 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
207 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
208 if (!aStateMessage.get())
210 std::list<std::string> aFeaturesList = aStateMessage->features();
211 std::list<std::string>::iterator it = aFeaturesList.begin();
212 for( ; it != aFeaturesList.end(); ++it) {
213 QString anActionId = QString::fromStdString(*it);
214 bool theDefaultState = false;
215 if (myActions.contains(anActionId)) {
216 theDefaultState = myActions[anActionId]->isEnabled();
218 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
220 } else if (theMessage.get()) {
222 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
223 << theMessage->eventID().eventText() << std::endl;
228 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
230 QAction* aResult = NULL;
231 if (myOperationActions.contains(theId)) {
232 aResult = myOperationActions.value(theId);
233 if (theParent && aResult->parent() != theParent) {
234 aResult->setParent(theParent);
240 aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
244 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
246 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
251 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
256 myOperationActions.insert(theId, aResult);
261 QAction* XGUI_ActionsMgr::action(const QString& theId)
263 QAction* anAction = 0;
264 if(myActions.contains(theId)) {
265 anAction = myActions.value(theId);
270 ActionInfo XGUI_ActionsMgr::actionInfoById(const QString& theId)
273 if(myActions.contains(theId)) {
274 aResult.initFrom(myActions.value(theId));
277 aResult.text = theId;
282 void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
284 foreach(QString eachAction, myActions.keys())
286 setActionEnabled(eachAction, isEnabled);
292 void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
294 QStringList ltNestedActions;
295 if (theParent.isEmpty()) { //Disable ALL nested
296 foreach(QString eachParent, myNestedActions.keys()) {
297 ltNestedActions << myNestedActions[eachParent];
300 ltNestedActions << myNestedActions[theParent];
302 foreach(QString eachNested, ltNestedActions) {
303 setActionEnabled(eachNested, theEnabled);
307 void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
309 ModuleBase_OperationFeature* anOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
310 if(!anOperation || !anOperation->feature())
312 FeaturePtr aFeature = anOperation->feature();
313 QString aFeatureId = QString::fromStdString(aFeature->getKind());
314 setActionEnabled(aFeatureId, true);
315 setNestedCommandsEnabled(true, aFeatureId);
317 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
320 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
322 if (myActions.contains(theId)) {
323 QAction* anAction = myActions[theId];
324 if (anAction->isCheckable()) {
325 anAction->setChecked(theChecked);
330 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
332 if (myActions.contains(theId)) {
333 myActions[theId]->setEnabled(theEnabled);
338 * Disables all actions which have the Document Kind different to
339 * the current document's kind
341 void XGUI_ActionsMgr::updateByDocumentKind()
343 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
344 QString aDocKind = QString::fromStdString(aStdDocKind);
345 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
346 foreach(QAction* eachAction, myActions.values()) {
347 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
350 aCmdDocKind = aCmd->documentKind();
352 QString aId = eachAction->data().toString();
353 if (!aId.isEmpty()) {
354 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
357 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
358 eachAction->setEnabled(false);
363 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
365 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
366 EVENT_FEATURE_STATE_REQUEST);
367 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
368 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
369 aMsg->setFeature(anActiveFeature);
370 Events_Loop::loop()->send(aMsg, false);