From 8e3cd1d4636990a7a9d3c2358e29376e018d4389 Mon Sep 17 00:00:00 2001
From: Jacob Wahlman <jacwa448@student.liu.se>
Date: Wed, 13 May 2020 17:54:36 +0200
Subject: [PATCH] added save for plot

---
 b_asic/GUI/about_window.py        |  1 +
 b_asic/GUI/main_window.py         |  3 ++-
 b_asic/GUI/simulate_sfg_window.py | 21 ++++++++++++++++++---
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/b_asic/GUI/about_window.py b/b_asic/GUI/about_window.py
index dbf38aed..ea38c7c4 100644
--- a/b_asic/GUI/about_window.py
+++ b/b_asic/GUI/about_window.py
@@ -45,6 +45,7 @@ class KeybindsWindow(QDialog):
             "'Ctrl+R' - Reload the operation list to add any new operations created.\n"
             "'Ctrl+Q' - Quit the application.\n"
             "'Ctrl+LMouseButton' - On a operation will select the operation, without deselecting the other operations.\n"
+            "'Ctrl+S' (Plot) - Save the plot if a plot is visible.\n"
         )
 
         information_layout.addWidget(title_label)
diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py
index 5183eeee..2829fcaa 100644
--- a/b_asic/GUI/main_window.py
+++ b/b_asic/GUI/main_window.py
@@ -296,7 +296,8 @@ class MainWindow(QMainWindow):
 
             if properties["show_plot"]:
                 self.logger.info(f"Opening plot for sfg with name: {sfg.name}.")
-                self.plot = Plot(simulation, sfg)
+                self.logger.info("To save the plot press 'Ctrl+S' when the plot is focused.")
+                self.plot = Plot(simulation, sfg, self)
                 self.plot.show()
 
     def simulate_sfg(self):
diff --git a/b_asic/GUI/simulate_sfg_window.py b/b_asic/GUI/simulate_sfg_window.py
index ba81e62b..75d26120 100644
--- a/b_asic/GUI/simulate_sfg_window.py
+++ b/b_asic/GUI/simulate_sfg_window.py
@@ -1,7 +1,7 @@
 from PySide2.QtWidgets import QDialog, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout,\
-QLabel, QCheckBox, QSpinBox, QGroupBox, QFrame, QFormLayout, QGridLayout, QSizePolicy
+QLabel, QCheckBox, QSpinBox, QGroupBox, QFrame, QFormLayout, QGridLayout, QSizePolicy, QFileDialog, QShortcut
 from PySide2.QtCore import Qt, Signal
-from PySide2.QtGui import QIntValidator
+from PySide2.QtGui import QIntValidator, QKeySequence
 
 from matplotlib.backends import qt_compat
 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
@@ -94,9 +94,11 @@ class SimulateSFGWindow(QDialog):
 
 
 class Plot(FigureCanvas):
-    def __init__(self, simulation, sfg, parent=None, width=5, height=4, dpi=100):
+    def __init__(self, simulation, sfg, window, parent=None, width=5, height=4, dpi=100):
         self.simulation = simulation
         self.sfg = sfg
+        self.dpi = dpi
+        self._window = window
 
         fig = Figure(figsize=(width, height), dpi=dpi)
         fig.suptitle(sfg.name, fontsize=20)
@@ -107,8 +109,21 @@ class Plot(FigureCanvas):
 
         FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
         FigureCanvas.updateGeometry(self)
+        self.save_figure = QShortcut(QKeySequence("Ctrl+S"), self)
+        self.save_figure.activated.connect(self._save_plot_figure)
         self._plot_values_sfg()
 
+    def _save_plot_figure(self):
+        self._window.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)
+
     def _plot_values_sfg(self):
         x_axis = list(range(len(self.simulation.results.keys())))
         for _output in range(self.sfg.output_count):
-- 
GitLab