From 67d24d7e0b71e15557c3c8743be98c8cf260c408 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Wed, 8 Dec 2021 07:19:47 +0100
Subject: [PATCH] rectify import-from-python-action enabling

---
 GUI/View/Main/ActionManager.cpp | 19 ++++++++++++-------
 GUI/View/Main/ActionManager.h   |  1 +
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/GUI/View/Main/ActionManager.cpp b/GUI/View/Main/ActionManager.cpp
index 1fe45c74196..2adf9d2fbdb 100644
--- a/GUI/View/Main/ActionManager.cpp
+++ b/GUI/View/Main/ActionManager.cpp
@@ -42,6 +42,7 @@ ActionManager::ActionManager(MainWindow* parent)
     , m_saveAsAction(nullptr)
     , m_exitAction(nullptr)
     , m_aboutAction(nullptr)
+    , m_importFromPythonAction(nullptr)
     , m_menuBar(nullptr)
     , m_fileMenu(nullptr)
     , m_settingsMenu(nullptr)
@@ -70,6 +71,10 @@ void ActionManager::updateActionEnabling()
     m_saveAction->setEnabled(documentExists);
     m_saveAsAction->setEnabled(documentExists);
     m_closeProjectAction->setEnabled(documentExists);
+    m_importFromPythonAction->setEnabled(documentExists);
+    if (GUI::Util::OS::HostOsInfo::isMacHost())
+        if (BaseUtils::System::getenv("PYTHONHOME").empty())
+            m_importFromPythonAction->setEnabled(false);
 }
 
 void ActionManager::createActions()
@@ -152,18 +157,18 @@ void ActionManager::createMenus()
     m_fileMenu->addSeparator();
     m_importMenu = m_fileMenu->addMenu("&Import");
     m_importMenu->setToolTipsVisible(true);
-    QAction* action = m_importMenu->addAction("&Import from Python script (experimental)");
-    action->setToolTip("Import sample from Python script.\n The script should contain a function "
-                       "returning a valid multi-layer.");
+    m_importFromPythonAction = m_importMenu->addAction("&Import from Python script (experimental)");
+    m_importFromPythonAction->setToolTip(
+        "Import sample from Python script.\n The script should contain a function "
+        "returning a valid multi-layer.");
 
 #ifdef BORNAGAIN_PYTHON
-    connect(action, &QAction::triggered, this, &ActionManager::onImportFromPythonScript);
+    connect(m_importFromPythonAction, &QAction::triggered, this,
+            &ActionManager::onImportFromPythonScript);
     if (GUI::Util::OS::HostOsInfo::isMacHost())
         if (BaseUtils::System::getenv("PYTHONHOME").empty())
-            action->setEnabled(false);
+            m_importFromPythonAction->setEnabled(false);
 #endif // BORNAGAIN_PYTHON
-    if (!gSessionData->projectDocument)
-        action->setEnabled(false);
 
     m_fileMenu->addSeparator();
     m_fileMenu->addAction(m_exitAction);
diff --git a/GUI/View/Main/ActionManager.h b/GUI/View/Main/ActionManager.h
index 5511848037e..3b9982bc886 100644
--- a/GUI/View/Main/ActionManager.h
+++ b/GUI/View/Main/ActionManager.h
@@ -55,6 +55,7 @@ private:
     QAction* m_exitAction;
     QAction* m_webdocAction;
     QAction* m_aboutAction;
+    QAction* m_importFromPythonAction;
 
     QMenuBar* m_menuBar;
     QMenu* m_fileMenu;
-- 
GitLab