Salome HOME
30.10.2014. Redesigned boolean feature.
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
index 46b15993acda2bb84d885ceb361dfed832cab112..081ab537f9c7a6ab02e2b24ab4873b78388813f9 100644 (file)
 #include "XGUI_ActionsMgr.h"
 #include "XGUI_Command.h"
 #include "XGUI_Workshop.h"
+#include "XGUI_OperationMgr.h"
 #include "XGUI_SalomeConnector.h"
 
+#include <ModelAPI_Session.h>
+
+#include <ModuleBase_Operation.h>
+#include <Events_Error.h>
+
 #include <QAction>
 
+#ifdef _DEBUG
+#include <iostream>
+#include <QDebug>
+#endif
 
 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
- : QObject(theParent), myWorkshop(theParent)
+    : QObject(theParent),
+      myWorkshop(theParent),
+      myOperationMgr(theParent->operationMgr())
 {
-  
+  // Default shortcuts
+  myShortcuts << QKeySequence::Save;
+  myShortcuts << QKeySequence::Undo;
+  myShortcuts << QKeySequence::Redo;
+  myShortcuts << QKeySequence::Open;
+  myShortcuts << QKeySequence::Close;
 }
 
 XGUI_ActionsMgr::~XGUI_ActionsMgr()
 {
 }
 
+void XGUI_ActionsMgr::addCommand(QAction* theCmd)
+{
+  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(QString theId, QAction* theCmd)
+void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList& theCommands)
 {
-  myActions.insert(theId,theCmd);
-  myActionsState.insert(theId, theCmd->isEnabled());
-  connect(theCmd, SIGNAL(triggered(bool)), this, SLOT(setActionsDisabled(bool)));
+  myNestedActions[theId] = theCommands;
 }
 
-void XGUI_ActionsMgr::addCommand(XGUI_Command* theCmd)
+void XGUI_ActionsMgr::update()
 {
-  myActions.insert(theCmd->id(),theCmd);
-  myActionsState.insert(theCmd->id(), theCmd->enabled());
-  theCmd->connectTo(this, SLOT(setActionsDisabled(bool)));
+  if (myOperationMgr->hasOperation()) {
+    ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
+    FeaturePtr aFeature = anOperation->feature();
+    if(aFeature) {
+      setAllEnabled(false);
+      QString aFeatureId = QString::fromStdString(aFeature->getKind());
+      setActionEnabled(aFeatureId, true);
+      setNestedStackEnabled(anOperation);
+    }
+  } else {
+    setAllEnabled(true);
+    setNestedCommandsEnabled(false);
+  }
+  updateByDocumentKind();
+  updateCheckState();
 }
 
-void XGUI_ActionsMgr::setActionsDisabled(bool isDisabled)
+void XGUI_ActionsMgr::setAllEnabled(bool isEnabled)
 {
-  //Re-enable actions (just restore their state)
-  if (!isDisabled) {
-    myNestedActions.clear();
-    restoreCommandState();
-    return;
+  foreach(QString eachAction, myActions.keys())
+  {
+    setActionEnabled(eachAction, isEnabled);
   }
-  //Disable all actions, but caller and unblockable (defined in a xml)
-  saveCommandsState();
+}
 
-  QString aSkippedId;
-  if (myWorkshop->isSalomeMode()) {
-    QAction* aToggledFeature = dynamic_cast<QAction*>(sender());
-    aSkippedId = myWorkshop->salomeConnector()->commandId(aToggledFeature);
+void XGUI_ActionsMgr::setNestedStackEnabled(ModuleBase_Operation* theOperation)
+{
+  if(!theOperation || !theOperation->feature())
+    return;
+  FeaturePtr aFeature = theOperation->feature();
+  QString aFeatureId = QString::fromStdString(aFeature->getKind());
+  bool isNestedEnabled = theOperation->isNestedOperationsEnabled();
+  setNestedCommandsEnabled(isNestedEnabled, aFeatureId);
+
+  setNestedStackEnabled(myOperationMgr->previousOperation(theOperation));
+}
+
+//!
+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];
+    }
   } else {
-    XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
-    aSkippedId = aToggledFeature->id();
+    ltNestedActions << myNestedActions[theParent];
   }
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    if (eachKey == aSkippedId) {
-      continue;
-    }
-    myActions[eachKey]->setEnabled(false);
+  foreach(QString eachNested, ltNestedActions) {
+    setActionEnabled(eachNested, theEnabled);
   }
-  if (myWorkshop->isSalomeMode()) {
-    myNestedActions = myWorkshop->salomeConnector()->nestedActions(aSkippedId);
-  } else {
-    XGUI_Command* aToggledFeature = dynamic_cast<XGUI_Command*>(sender());
-    myNestedActions = aToggledFeature->unblockableCommands();
+}
+
+void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
+{
+  QAction* anAction = myActions[theId];
+  if (anAction && anAction->isCheckable()) {
+    anAction->setChecked(theChecked);
   }
 }
 
-void XGUI_ActionsMgr::saveCommandsState()
+/*
+ * Disables all actions which have the Document Kind different to
+ * the current document's kind
+ */
+void XGUI_ActionsMgr::updateByDocumentKind()
 {
-  myActionsState.clear();
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    myActionsState.insert(eachKey, myActions[eachKey]->isEnabled());
+  std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
+  QString aDocKind = QString::fromStdString(aStdDocKind);
+  XGUI_Workshop* aWorkshop = static_cast<XGUI_Workshop*>(parent());
+  foreach(QAction* eachAction, myActions.values()) {
+    XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(eachAction);
+    QString aCmdDocKind;
+    if(aCmd) {
+      aCmdDocKind = aCmd->documentKind();
+    }
+    else {
+      QString aId = eachAction->data().toString();
+      if (!aId.isEmpty()) {
+        aCmdDocKind = aWorkshop->salomeConnector()->documentKind(aId);
+      }
+    }
+    if(!aCmdDocKind.isEmpty() && aCmdDocKind != aDocKind) {
+      eachAction->setEnabled(false);
+    }
   }
+}
 
+void XGUI_ActionsMgr::setActionEnabled(const QString& theId, const bool theEnabled)
+{
+  QAction* anAction = myActions[theId];
+  if (anAction) {
+    anAction->setEnabled(theEnabled);
+  }
 }
 
-void XGUI_ActionsMgr::restoreCommandState()
+void XGUI_ActionsMgr::updateCheckState()
 {
-  QStringList anActionIdsList = myActions.keys();
-  foreach(QString eachKey, anActionIdsList) {
-    myActions[eachKey]->setEnabled(myActionsState[eachKey]);
-    myActions[eachKey]->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::updateAction(const QString& theId)
+QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
 {
-  if(myActions.contains(theId)){
-    myActions[theId]->setEnabled(myActionsState[theId]);
-    myActions[theId]->setChecked(false);
+  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;
 }
 
-void XGUI_ActionsMgr::setNestedActionsEnabled(bool isEnabled)
+QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
 {
-  foreach(QString eachKey, myNestedActions) {
-    if (myActions.contains(eachKey))
-      myActions[eachKey]->setEnabled(isEnabled);
+  if (theKeySequence.isEmpty()) {
+    return QKeySequence();
+  }
+  QKeySequence aResult(theKeySequence);
+  if (myShortcuts.contains(aResult)) {
+    QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
+    Events_Error::send(aMessage.toStdString());
+    return QKeySequence();
   }
+  myShortcuts.append(aResult);
+  return aResult;
 }