Salome HOME
30.10.2014. Redesigned boolean feature.
[modules/shaper.git] / src / XGUI / XGUI_ActionsMgr.cpp
index 4bfb31b9d6e31eb7807415815e4a04f815149c66..081ab537f9c7a6ab02e2b24ab4873b78388813f9 100644 (file)
@@ -8,6 +8,8 @@
 #include "XGUI_OperationMgr.h"
 #include "XGUI_SalomeConnector.h"
 
+#include <ModelAPI_Session.h>
+
 #include <ModuleBase_Operation.h>
 #include <Events_Error.h>
 
 #include <QDebug>
 #endif
 
-
 XGUI_ActionsMgr::XGUI_ActionsMgr(XGUI_Workshop* theParent)
- : QObject(theParent),
-   myWorkshop(theParent),
-   myOperationMgr(theParent->operationMgr())
   : QObject(theParent),
+      myWorkshop(theParent),
+      myOperationMgr(theParent->operationMgr())
 {
   // Default shortcuts
   myShortcuts << QKeySequence::Save;
@@ -36,11 +37,10 @@ XGUI_ActionsMgr::~XGUI_ActionsMgr()
 {
 }
 
-
 void XGUI_ActionsMgr::addCommand(QAction* theCmd)
 {
   QString aId = theCmd->data().toString();
-  if(aId.isEmpty()) {
+  if (aId.isEmpty()) {
     return;
   }
   myActions.insert(aId, theCmd);
@@ -60,32 +60,48 @@ void XGUI_ActionsMgr::addNestedCommands(const QString& theId, const QStringList&
 
 void XGUI_ActionsMgr::update()
 {
-  if(myOperationMgr->hasOperation()) {
-    setAllEnabled(false);
+  if (myOperationMgr->hasOperation()) {
     ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
-    QString anOperationId = anOperation->id();
-    setActionEnabled(anOperationId, true);
-    bool isNestedEnabled = anOperation->isNestedOperationsEnabled();
-    setNestedCommandsEnabled(isNestedEnabled, anOperationId);
+    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::setAllEnabled(bool isEnabled)
 {
-  foreach(QString eachAction, myActions.keys()) {
+  foreach(QString eachAction, myActions.keys())
+  {
     setActionEnabled(eachAction, isEnabled);
   }
 }
 
+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
+  if (theParent.isEmpty()) {  //Disable ALL nested
     foreach(QString eachParent, myNestedActions.keys()) {
       ltNestedActions << myNestedActions[eachParent];
     }
@@ -100,16 +116,42 @@ void XGUI_ActionsMgr::setNestedCommandsEnabled(bool theEnabled, const QString& t
 void XGUI_ActionsMgr::setActionChecked(const QString& theId, const bool theChecked)
 {
   QAction* anAction = myActions[theId];
-  if(anAction && anAction->isCheckable()) {
+  if (anAction && anAction->isCheckable()) {
     anAction->setChecked(theChecked);
   }
 }
 
+/*
+ * Disables all actions which have the Document Kind different to
+ * the current document's kind
+ */
+void XGUI_ActionsMgr::updateByDocumentKind()
+{
+  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) {
+  if (anAction) {
     anAction->setEnabled(theEnabled);
   }
 }
@@ -135,7 +177,8 @@ QStringList XGUI_ActionsMgr::nestedCommands(const QString& theId) const
 
 bool XGUI_ActionsMgr::isNested(const QString& theId) const
 {
-  foreach(QString aId, myNestedActions.keys()) {
+  foreach(QString aId, myNestedActions.keys())
+  {
     QStringList aList = myNestedActions[aId];
     if (aList.contains(theId))
       return true;
@@ -145,11 +188,11 @@ bool XGUI_ActionsMgr::isNested(const QString& theId) const
 
 QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence)
 {
-  if(theKeySequence.isEmpty()) {
+  if (theKeySequence.isEmpty()) {
     return QKeySequence();
   }
   QKeySequence aResult(theKeySequence);
-  if(myShortcuts.contains(aResult)) {
+  if (myShortcuts.contains(aResult)) {
     QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence);
     Events_Error::send(aMessage.toStdString());
     return QKeySequence();