From d87e7f7c8d4db52688bfa52cf38a475a091a6738 Mon Sep 17 00:00:00 2001
From: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Date: Mon, 24 Apr 2023 16:25:37 +0200
Subject: [PATCH] Fix issue with (re)starting GUIs in some shells

---
 b_asic/GUI/main_window.py           | 18 +++++++++++++++++-
 b_asic/gui_utils/plot_window.py     |  7 +++++--
 b_asic/scheduler_gui/main_window.py | 18 +++++++++++++++++-
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py
index a9aac100..86024d64 100644
--- a/b_asic/GUI/main_window.py
+++ b/b_asic/GUI/main_window.py
@@ -804,7 +804,23 @@ class SFGMainWindow(QMainWindow):
 
 
 def start_editor(sfg: Optional[SFG] = None) -> Dict[str, SFG]:
-    app = QApplication(sys.argv)
+    """
+    Start the SFG editor.
+
+    Parameters
+    ----------
+    sfg : SFG, optional
+        The SFG to start the editor with.
+
+    Returns
+    -------
+    dict
+        All SFGs currently in the editor.
+    """
+    if not QApplication.instance():
+        app = QApplication(sys.argv)
+    else:
+        app = QApplication.instance()
     window = SFGMainWindow()
     if sfg:
         window._load_sfg(sfg)
diff --git a/b_asic/gui_utils/plot_window.py b/b_asic/gui_utils/plot_window.py
index 2c6af52a..991baaea 100644
--- a/b_asic/gui_utils/plot_window.py
+++ b/b_asic/gui_utils/plot_window.py
@@ -256,10 +256,13 @@ def start_simulation_dialog(
     sfg_name : str, optional
         The name of the SFG.
     """
-    app = QApplication(sys.argv)
+    if not QApplication.instance():
+        app = QApplication(sys.argv)
+    else:
+        app = QApplication.instance()
     win = PlotWindow(sim_result=sim_results, sfg_name=sfg_name)
     win.show()
-    sys.exit(app.exec_())
+    app.exec_()
 
 
 # Simple test of the dialog
diff --git a/b_asic/scheduler_gui/main_window.py b/b_asic/scheduler_gui/main_window.py
index 4b379c75..e561725c 100644
--- a/b_asic/scheduler_gui/main_window.py
+++ b/b_asic/scheduler_gui/main_window.py
@@ -706,7 +706,23 @@ class ScheduleMainWindow(QMainWindow, Ui_MainWindow):
 
 
 def start_scheduler(schedule: Optional[Schedule] = None) -> Schedule:
-    app = QApplication(sys.argv)
+    """
+    Start scheduler GUI.
+
+    Parameters
+    ----------
+    schedule : Schedule, optional
+        The schedule to start the editor with.
+
+    Returns
+    -------
+    Schedule
+        The edited schedule.
+    """
+    if not QApplication.instance():
+        app = QApplication(sys.argv)
+    else:
+        app = QApplication.instance()
     window = ScheduleMainWindow()
     if schedule:
         window.open(schedule)
-- 
GitLab