Salome HOME
Merge remote-tracking branch 'remotes/origin/master'
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
index 46b15993acda2bb84d885ceb361dfed832cab112..611976e2a91c3da4186b17d8acf72da68f951f9b 100644 (file)
@@ -5,15 +5,22 @@
 #include "XGUI_ActionsMgr.h"
 #include "XGUI_Command.h"
 #include "XGUI_Workshop.h"
+#include "XGUI_OperationMgr.h"
 #include "XGUI_SalomeConnector.h"
 
+#include <ModuleBase_Operation.h>
+
 #include <QAction>
 
+#ifdef _DEBUG
+#include <QDebug>
+#endif
+
 
 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
- : QObject(theParent), myWorkshop(theParent)
+ : QObject(theParent), myOperationMgr(theParent->operationMgr())
 {
-  
+
 }
 
 XGUI_ActionsMgr::~XGUI_ActionsMgr()
@@ -21,85 +28,108 @@ XGUI_ActionsMgr::~XGUI_ActionsMgr()
 }
 
 
-void XGUI_ActionsMgr::addCommand(QString theId, QAction* theCmd)
+void XGUI_ActionsMgr::addCommand(QAction* theCmd)
 {
-  myActions.insert(theId,theCmd);
-  myActionsState.insert(theId, theCmd->isEnabled());
-  connect(theCmd, SIGNAL(triggered(bool)), this, SLOT(setActionsDisabled(bool)));
+  QString aId = theCmd->data().toString();
+  if(aId.isEmpty()) {
+    return;
+  }
+  myActions.insert(aId, theCmd);
+  XGUI_Command* aXCmd = dynamic_cast<XGUI_Command*>(theCmd);
+  if (aXCmd) {
+    myNestedActions[aId] = aXCmd->nestedCommands();
+  } else {
+    XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
+    myNestedActions[aId] = aWorkshop->salomeConnector()->nestedActions(aId);
+  }
 }
 
-void XGUI_ActionsMgr::addCommand(XGUI_Command* theCmd)
+void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
 {
-  myActions.insert(theCmd->id(),theCmd);
-  myActionsState.insert(theCmd->id(), theCmd->enabled());
-  theCmd->connectTo(this, SLOT(setActionsDisabled(bool)));
+  myNestedActions[theId] = theCommands;
 }
 
-void XGUI_ActionsMgr::setActionsDisabled(bool isDisabled)
+void XGUI_ActionsMgr::update()
 {
-  //Re-enable actions (just restore their state)
-  if (!isDisabled) {
-    myNestedActions.clear();
-    restoreCommandState();
-    return;
+  if(myOperationMgr->hasOperation()) {
+    setAllEnabled(false);
+    ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
+    QString anOperationId = anOperation->id();
+    setActionEnabled(anOperationId, true);
+    bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
+    setNestedCommandsEnabled(isNestedEnabled, anOperationId);
+  } else {
+    setAllEnabled(true);
+    setNestedCommandsEnabled(false);
   }
-  //Disable all actions, but caller and unblockable (defined in a xml)
-  saveCommandsState();
+  updateCheckState();
+}
 
-  QString aSkippedId;
-  if (myWorkshop->isSalomeMode()) {
-    QAction* aToggledFeature = dynamic_cast<QAction*>(sender());
-    aSkippedId = myWorkshop->salomeConnector()->commandId(aToggledFeature);
-  } else {
-    XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
-    aSkippedId = aToggledFeature->id();
+void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
+{
+  foreach(QString eachAction, myActions.keys()) {
+    setActionEnabled(eachAction, isEnabled);
   }
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    if (eachKey == aSkippedId) {
-      continue;
+}
+
+//!
+void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& theParent)
+{
+  QStringList ltNestedActions;
+  if(theParent.isEmpty()) { //Disable ALL nested
+    foreach(QString eachParent, myNestedActions.keys()) {
+      ltNestedActions << myNestedActions[eachParent];
     }
-    myActions[eachKey]->setEnabled(false);
-  }
-  if (myWorkshop->isSalomeMode()) {
-    myNestedActions = myWorkshop->salomeConnector()->nestedActions(aSkippedId);
   } else {
-    XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
-    myNestedActions = aToggledFeature->unblockableCommands();
+    ltNestedActions << myNestedActions[theParent];
+  }
+  foreach(QString eachNested, ltNestedActions) {
+    setActionEnabled(eachNested, theEnabled);
   }
 }
 
-void XGUI_ActionsMgr::saveCommandsState()
+void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
 {
-  myActionsState.clear();
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    myActionsState.insert(eachKey, myActions[eachKey]->isEnabled());
+  QAction* anAction = myActions[theId];
+  if(anAction && anAction->isCheckable()) {
+    anAction->setChecked(theChecked);
   }
-
 }
 
-void XGUI_ActionsMgr::restoreCommandState()
+
+void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
 {
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    myActions[eachKey]->setEnabled(myActionsState[eachKey]);
-    myActions[eachKey]->setChecked(false);
+  QAction* anAction = myActions[theId];
+  if(anAction) {
+    anAction->setEnabled(theEnabled);
   }
 }
 
-void XGUI_ActionsMgr::updateAction(const QString& theId)
+void XGUI_ActionsMgr::updateCheckState()
 {
-  if(myActions.contains(theId)){
-    myActions[theId]->setEnabled(myActionsState[theId]);
-    myActions[theId]->setChecked(false);
+  QString eachCommand = QString();
+  foreach(eachCommand, myActions.keys()) {
+    setActionChecked(eachCommand, false);
+  }
+  QStringList ltActiveCommands = myOperationMgr->operationList();
+  foreach(eachCommand, ltActiveCommands) {
+    setActionChecked(eachCommand, true);
   }
 }
 
-void XGUI_ActionsMgr::setNestedActionsEnabled(bool isEnabled)
+QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
 {
-  foreach(QString eachKey, myNestedActions) {
-    if (myActions.contains(eachKey))
-      myActions[eachKey]->setEnabled(isEnabled);
-  }
+  if (myNestedActions.contains(theId))
+    return myNestedActions[theId];
+  return QStringList();
 }
+
+bool XGUI_ActionsMgr::isNested(const QString& theId) const
+{
+  foreach(QString aId, myNestedActions.keys()) {
+    QStringList aList = myNestedActions[aId];
+    if (aList.contains(theId))
+      return true;
+  }
+  return false;
+}
\ No newline at end of file