From cc4a041dbd61522905467cdb156f4789ce54797f Mon Sep 17 00:00:00 2001
From: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Date: Thu, 23 Feb 2023 17:46:57 +0100
Subject: [PATCH] Simulation legend almost working

---
 b_asic/GUI/main_window.py       |  2 +-
 b_asic/gui_utils/plot_window.py | 63 ++++++++-------------------------
 2 files changed, 15 insertions(+), 50 deletions(-)

diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py
index 8b2290dc..a504a482 100644
--- a/b_asic/GUI/main_window.py
+++ b/b_asic/GUI/main_window.py
@@ -34,7 +34,6 @@ from b_asic.GUI._preferences import GAP, GRID, MINBUTTONSIZE, PORTHEIGHT
 from b_asic.GUI.arrow import Arrow
 from b_asic.GUI.drag_button import DragButton
 from b_asic.GUI.gui_interface import Ui_main_window
-from b_asic.GUI.plot_window import PlotWindow
 from b_asic.GUI.port_button import PortButton
 from b_asic.GUI.select_sfg_window import SelectSFGWindow
 from b_asic.GUI.show_pc_window import ShowPCWindow
@@ -44,6 +43,7 @@ from b_asic.GUI.simulate_sfg_window import SimulateSFGWindow
 from b_asic.GUI.util_dialogs import FaqWindow, KeybindsWindow
 from b_asic.GUI.utils import decorate_class, handle_error
 from b_asic.gui_utils.about_window import AboutWindow
+from b_asic.gui_utils.plot_window import PlotWindow
 from b_asic.operation import Operation
 from b_asic.port import InputPort, OutputPort
 from b_asic.save_load_structure import python_to_sfg, sfg_to_python
diff --git a/b_asic/gui_utils/plot_window.py b/b_asic/gui_utils/plot_window.py
index 658abf33..023ab152 100644
--- a/b_asic/gui_utils/plot_window.py
+++ b/b_asic/gui_utils/plot_window.py
@@ -25,36 +25,6 @@ from qtpy.QtWidgets import (  # QFrame,; QScrollArea,; QLineEdit,; QSizePolicy,;
     QVBoxLayout,
 )
 
-# from qtpy.QtGui import QKeySequence
-
-
-# class PlotCanvas(FigureCanvas):
-#     """PlotCanvas is used as a part in the PlotWindow."""
-
-#     def __init__(self, logger, parent=None, width=5, height=4, dpi=100):
-#         fig = Figure(figsize=(width, height), dpi=dpi)
-#         super().__init__(fig)
-#         self.axes = fig.add_subplot(111)
-#         self.axes.xaxis.set_major_locator(MaxNLocator(integer=True))
-#         self.legend = None
-#         self.logger = logger
-
-#         FigureCanvas.updateGeometry(self)
-#         self.save_figure = QShortcut(QKeySequence("Ctrl+S"), self)
-#         self.save_figure.activated.connect(self._save_plot_figure)
-
-#     def _save_plot_figure(self):
-#         self.logger.info(f"Saving plot of figure: {self.sfg.name}.")
-#         file_choices = "PNG (*.png)|*.png"
-#         path, ext = QFileDialog.getSaveFileName(self, "Save file", "", file_choices)
-#         path = path.encode("utf-8")
-#         if not path[-4:] == file_choices[-4:].encode("utf-8"):
-#             path += file_choices[-4:].encode("utf-8")
-
-#         if path:
-#             self.print_figure(path.decode(), dpi=self.dpi)
-#             self.logger.info(f"Saved plot: {self.sfg.name} to path: {path}.")
-
 
 class PlotWindow(QDialog):
     """Dialog for plotting the result of a simulation."""
@@ -62,25 +32,17 @@ class PlotWindow(QDialog):
     def __init__(
         self,
         sim_result,
-        # sfg_name="{sfg_name}",
-        # window=None,
         logger=print,
         parent=None,
-        # width=5,
-        # height=4,
-        # dpi=100,
     ):
         super().__init__(parent=parent)
-        # self._window = window
         self.setWindowFlags(
             Qt.WindowTitleHint
             | Qt.WindowCloseButtonHint
             | Qt.WindowMinimizeButtonHint
             | Qt.WindowMaximizeButtonHint
-            # | Qt.WindowStaysOnTopHint
         )
         self.setWindowTitle("Simulation result")
-        # self.sim_result = sim_result
         self._auto_redraw = False
 
         # Categorise sim_results into inputs, outputs, delays, others
@@ -119,15 +81,15 @@ class PlotWindow(QDialog):
         # self.plotcanvas = PlotCanvas(
         #    logger=logger, parent=self, width=5, height=4, dpi=100
         # )
-        self._plot_fig = Figure(figsize=(5, 4), dpi=100)
+        self._plot_fig = Figure(figsize=(5, 4), layout="compressed")
         self._plot_axes = self._plot_fig.add_subplot(111)
         self._plot_axes.xaxis.set_major_locator(MaxNLocator(integer=True))
 
         self._lines = {}
         for key in sim_res_others | sim_res_delays | sim_res_ins | sim_res_outs:
             # line = self.plotcanvas.axes.plot(sim_result[key], visible=False, label=key)
-            line = self._plot_axes.plot(sim_result[key], visible=False, label=key)
-            self._lines[key] = line
+            line = self._plot_axes.plot(sim_result[key], label=key)
+            self._lines[key] = line[0]
         # self.plotcanvas.legend = self.plotcanvas.axes.legend()
         self._legend = self._plot_axes.legend()
 
@@ -161,7 +123,7 @@ class PlotWindow(QDialog):
             )
         for key in sim_res_outs:
             listitems[key].setCheckState(Qt.CheckState.Checked)
-        self.checklist.setFixedWidth(150)
+        # self.checklist.setFixedWidth(150)
         listlayout.addWidget(self.checklist)
 
         # Add additional checkboxes
@@ -202,6 +164,10 @@ class PlotWindow(QDialog):
         for x in range(self.checklist.count()):
             self.checklist.item(x).setCheckState(Qt.CheckState.Checked)
         self._auto_redraw = True
+        self._update_legend()
+
+    def _update_legend(self):
+        self._legend = self._plot_axes.legend()
         self._plot_canvas.draw()
 
     def _button_none_click(self, event):
@@ -209,17 +175,16 @@ class PlotWindow(QDialog):
         for x in range(self.checklist.count()):
             self.checklist.item(x).setCheckState(Qt.CheckState.Unchecked)
         self._auto_redraw = True
-        self._plot_canvas.draw()
+        self._update_legend()
 
     def _item_change(self, listitem):
         key = listitem.text()
-        self._lines[key][0].set(
-            visible=(listitem.checkState() == Qt.CheckState.Checked)
-        )
+        if listitem.checkState() == Qt.CheckState.Checked:
+            self._plot_axes.add_line(self._lines[key])
+        else:
+            self._lines[key].remove()
         if self._auto_redraw:
-            if self.legend_checkbox.checkState == Qt.CheckState.Checked:
-                self._legend = self._plot_axes.legend()
-            self._plot_canvas.draw()
+            self._update_legend()
 
 
 # Simple test of the dialog
-- 
GitLab