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 if (!myOperationMgr->hasOperation())
134 QStringList aIdList = myOperationMgr->operationList();
135 //ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
136 //FeaturePtr anActiveFeature = anOperation->feature();
137 //if(!anActiveFeature.get())
138 if (aIdList.isEmpty())
141 //QString aFeatureId = QString::fromStdString(anActiveFeature->getKind());
142 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
143 // only viewer selection is processed
144 if (aSelection->getSelected(ModuleBase_ISelection::Viewer).size() == 0) {
145 // it seems that this code is not nesessary anymore. It leads to incorrect case:
146 // sketch operation start, click in any place in the viewer. The result is all nested
147 // entities are enabled(but the sketch plane is not selected yet). Any sketch operation
148 // can be started but will be incorrect on preview build before it uses the sketch unset plane.
149 /*foreach(QString aFeatureId, aIdList) {
150 foreach(QString aId, nestedCommands(aFeatureId)) {
151 setActionEnabled(aId, true);
155 SessionPtr aMgr = ModelAPI_Session::get();
156 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
157 foreach(QString aFeatureId, aIdList) {
158 foreach(QString aId, nestedCommands(aFeatureId)) {
159 ModelAPI_ValidatorsFactory::Validators aValidators;
160 aFactory->validators(aId.toStdString(), aValidators);
161 ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin();
162 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
163 const ModuleBase_SelectionValidator* aSelValidator =
164 dynamic_cast<const ModuleBase_SelectionValidator*>(aFactory->validator(aValidatorIt->first));
167 setActionEnabled(aId, aSelValidator->isValid(aSelection, aValidatorIt->second));
174 QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence)
176 if (theKeySequence.isEmpty()) {
177 return QKeySequence();
179 if (myShortcuts.contains(theKeySequence)) {
180 QString aMessage = tr("Shortcut %1 is already defined. Ignore.");
181 aMessage = aMessage.arg(theKeySequence.toString());
182 Events_Error::send(aMessage.toStdString());
183 return QKeySequence();
185 myShortcuts.append(theKeySequence);
186 return theKeySequence;
189 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
191 if (theKeySequence.isEmpty()) {
192 return QKeySequence();
194 QKeySequence aResult(theKeySequence);
195 registerShortcut(aResult);
199 void XGUI_ActionsMgr::processEvent(const std::shared_ptr<Events_Message>& theMessage)
201 const Events_ID kResponseEvent =
202 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
203 if (theMessage->eventID() == kResponseEvent) {
204 std::shared_ptr<ModelAPI_FeatureStateMessage> aStateMessage =
205 std::dynamic_pointer_cast<ModelAPI_FeatureStateMessage>(theMessage);
206 if (!aStateMessage.get())
208 std::list<std::string> aFeaturesList = aStateMessage->features();
209 std::list<std::string>::iterator it = aFeaturesList.begin();
210 for( ; it != aFeaturesList.end(); ++it) {
211 QString anActionId = QString::fromStdString(*it);
212 bool theDefaultState = false;
213 if (myActions.contains(anActionId)) {
214 theDefaultState = myActions[anActionId]->isEnabled();
216 setActionEnabled(anActionId, aStateMessage->state(*it, theDefaultState));
218 } else if (theMessage.get()) {
220 std::cout << "XGUI_ActionsMgr::processEvent: unhandled message caught: " << std::endl
221 << theMessage->eventID().eventText() << std::endl;
226 QAction* XGUI_ActionsMgr::operationStateAction(OperationStateActionId theId, QObject* theParent)
228 QAction* aResult = NULL;
229 if (myOperationActions.contains(theId)) {
230 aResult = myOperationActions.value(theId);
231 if (theParent && aResult->parent() != theParent) {
232 aResult->setParent(theParent);
238 aResult = new QAction(QIcon(":pictures/button_ok.png"), "", theParent);
239 // the default value is disabled, some modifications should be performed in order
240 // to update enable state of this action.
241 // AcceptAll is blinked when sketch is started from enabled to disabled.
242 aResult->setEnabled(false);
246 aResult = new QAction(QIcon(":pictures/button_cancel.png"), "", theParent);
248 aResult->setShortcut(QKeySequence(Qt::Key_Escape));
253 aResult = new QAction(QIcon(":pictures/button_help.png"), "", theParent);
258 myOperationActions.insert(theId, aResult);
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 if(!theOperation || !theOperation->feature())
304 FeaturePtr aFeature = theOperation->feature();
305 QString aFeatureId = QString::fromStdString(aFeature->getKind());
306 setActionEnabled(aFeatureId, true);
307 setNestedCommandsEnabled(true, aFeatureId);
309 setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
312 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
314 if (myActions.contains(theId)) {
315 QAction* anAction = myActions[theId];
316 if (anAction->isCheckable()) {
317 anAction->setChecked(theChecked);
322 void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
324 if (myActions.contains(theId)) {
325 myActions[theId]->setEnabled(theEnabled);
330 * Disables all actions which have the Document Kind different to
331 * the current document's kind
333 void XGUI_ActionsMgr::updateByDocumentKind()
335 std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
336 QString aDocKind = QString::fromStdString(aStdDocKind);
337 XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
338 foreach(QAction* eachAction, myActions.values()) {
339 AppElements_Command* aCmd = dynamic_cast<AppElements_Command*>(eachAction);
342 aCmdDocKind = aCmd->documentKind();
344 QString aId = eachAction->data().toString();
345 if (!aId.isEmpty()) {
346 aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
349 if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
350 eachAction->setEnabled(false);
355 void XGUI_ActionsMgr::updateByPlugins(FeaturePtr anActiveFeature)
357 static Events_ID aStateRequestEventId = Events_Loop::loop()->eventByName(
358 EVENT_FEATURE_STATE_REQUEST);
359 std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
360 new ModelAPI_FeatureStateMessage(aStateRequestEventId, this));
361 aMsg->setFeature(anActiveFeature);
362 Events_Loop::loop()->send(aMsg, false);