From 1dd189cbd4f1c769ffefedaf5d40d179d0c2c293 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Wed, 18 Oct 2023 16:00:32 +0200
Subject: [PATCH] Results: Implement results reload (for same solver) #16.

---
 src/Model/Results/Results.py       | 14 ++++++++-
 src/Solver/Mage.py                 |  8 +++--
 src/View/Results/PlotAC.py         |  5 ++++
 src/View/Results/PlotH.py          |  5 ++++
 src/View/Results/PlotKPC.py        |  5 ++++
 src/View/Results/PlotSedProfile.py | 10 +++++++
 src/View/Results/PlotSedReach.py   | 10 +++++++
 src/View/Results/PlotXY.py         |  5 ++++
 src/View/Results/Window.py         | 48 +++++++++++++++++++++++++++---
 src/View/Tools/Plot/APlot.py       |  4 +++
 src/View/ui/Results.ui             | 30 +++++++++++++++++++
 11 files changed, 137 insertions(+), 7 deletions(-)

diff --git a/src/Model/Results/Results.py b/src/Model/Results/Results.py
index 7c9ed54c..b8dff0cc 100644
--- a/src/Model/Results/Results.py
+++ b/src/Model/Results/Results.py
@@ -26,8 +26,13 @@ logger = logging.getLogger()
 
 
 class Results(object):
-    def __init__(self, study=None):
+    def __init__(self, study=None, solver=None,
+                 repertory="", name="0"):
         self._study = study
+        self._solver = solver
+        self._repertory = repertory
+        self._name = name
+
         self._river = River(self._study)
 
         self._meta_data = {
@@ -53,3 +58,10 @@ class Results(object):
 
     def get(self, key):
         return self._meta_data[key]
+
+    def reload(self):
+        return self._solver.results(
+            self._study,
+            self._repertory,
+            qlog=None,
+        )
diff --git a/src/Solver/Mage.py b/src/Solver/Mage.py
index f6346b66..f0df6e95 100644
--- a/src/Solver/Mage.py
+++ b/src/Solver/Mage.py
@@ -438,8 +438,12 @@ class Mage(AbstractSolver):
 
     @timer
     def results(self, study, repertory, qlog=None, name="0"):
-        results = Results(study=study)
-
+        results = Results(
+            study=study,
+            solver=self,
+            repertory=repertory,
+            name=name,
+        )
         self.read_bin(study, repertory, results, qlog, name=name)
 
         return results
diff --git a/src/View/Results/PlotAC.py b/src/View/Results/PlotAC.py
index 075e879f..345b5824 100644
--- a/src/View/Results/PlotAC.py
+++ b/src/View/Results/PlotAC.py
@@ -46,6 +46,11 @@ class PlotAC(PamhyrPlot):
     def results(self):
         return self.data
 
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     @timer
     def draw(self, highlight=None):
         self.canvas.axes.cla()
diff --git a/src/View/Results/PlotH.py b/src/View/Results/PlotH.py
index dd5a332b..f4f38e7c 100644
--- a/src/View/Results/PlotH.py
+++ b/src/View/Results/PlotH.py
@@ -55,6 +55,11 @@ class PlotH(PamhyrPlot):
     def results(self):
         return self.data
 
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     @timer
     def draw(self, highlight=None):
         self.canvas.axes.cla()
diff --git a/src/View/Results/PlotKPC.py b/src/View/Results/PlotKPC.py
index 3dd789a2..827ca104 100644
--- a/src/View/Results/PlotKPC.py
+++ b/src/View/Results/PlotKPC.py
@@ -46,6 +46,11 @@ class PlotKPC(PamhyrPlot):
     def results(self):
         return self.data
 
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     @timer
     def draw(self, highlight=None):
         self.canvas.axes.cla()
diff --git a/src/View/Results/PlotSedProfile.py b/src/View/Results/PlotSedProfile.py
index d16fb0b8..8d8f7b91 100644
--- a/src/View/Results/PlotSedProfile.py
+++ b/src/View/Results/PlotSedProfile.py
@@ -34,6 +34,16 @@ class PlotSedProfile(PamhyrPlot):
         self._current_reach_id = reach_id
         self._current_profile_id = profile_id
 
+    @property
+    def results(self):
+        return self.data
+
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._results = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     def get_zsl(self, profile):
         x = profile.geometry.get_station()
         z = profile.geometry.z()
diff --git a/src/View/Results/PlotSedReach.py b/src/View/Results/PlotSedReach.py
index 5cdc4ad7..e404f09c 100644
--- a/src/View/Results/PlotSedReach.py
+++ b/src/View/Results/PlotSedReach.py
@@ -34,6 +34,16 @@ class PlotSedReach(PamhyrPlot):
         self._current_reach_id = reach_id
         self._current_profile_id = profile_id
 
+    @property
+    def results(self):
+        return self.data
+
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._results = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     # DEPRECATED version of sediment layser display
     # def _get_zsl(self, reach):
     #     kp = reach.geometry.get_kp()
diff --git a/src/View/Results/PlotXY.py b/src/View/Results/PlotXY.py
index 5a513322..78d4c66b 100644
--- a/src/View/Results/PlotXY.py
+++ b/src/View/Results/PlotXY.py
@@ -57,6 +57,11 @@ class PlotXY(PamhyrPlot):
     def results(self):
         return self.data
 
+    @results.setter
+    def results(self, results):
+        self.data = results
+        self._current_timestamp = max(results.get("timestamps"))
+
     @timer
     def draw(self, highlight=None):
         self.canvas.axes.cla()
diff --git a/src/View/Results/Window.py b/src/View/Results/Window.py
index 15167fd3..d076fd42 100644
--- a/src/View/Results/Window.py
+++ b/src/View/Results/Window.py
@@ -75,8 +75,7 @@ class ResultsWindow(PamhyrWindow):
         name = (
             self._pamhyr_name + " - "
             + study.name + " - "
-            + self._solver.name + " - "
-            + self._results.date
+            + self._solver.name
         )
 
         super(ResultsWindow, self).__init__(
@@ -272,6 +271,16 @@ class ResultsWindow(PamhyrWindow):
         self._status_label.setText(txt)
 
     def setup_connections(self):
+        # Action
+        actions = {
+            "action_reload": self._reload,
+        }
+
+        for action in actions:
+            self.find(QAction, action).triggered.connect(
+                actions[action]
+            )
+
         fun = {
             "reach": self._set_current_reach,
             "profile": self._set_current_profile,
@@ -384,12 +393,43 @@ class ResultsWindow(PamhyrWindow):
         pid = self._slider_profile.value()
         self.update(profile_id=pid)
 
-        return
-
     def _set_current_timestamp(self):
         timestamp = self._timestamps[self._slider_time.value()]
         self.update(timestamp=timestamp)
 
+    def _reload_plots(self):
+        self.plot_xy.results = self._results
+        self.plot_ac.results = self._results
+        self.plot_kpc.results = self._results
+        self.plot_h.results = self._results
+
+        if self._study.river.has_sediment():
+            self.plot_sed_reach.results = self._results
+            self.plot_sed_profile.results = self._results
+
+        self.plot_xy.draw()
+        self.plot_ac.draw()
+        self.plot_kpc.draw()
+        self.plot_h.draw()
+
+        if self._study.river.has_sediment():
+            self.plot_sed_reach.draw()
+            self.plot_sed_profile.draw()
+
+    def _reload_slider(self):
+        self._slider_time = self.find(QSlider, f"horizontalSlider_time")
+        self._slider_time.setMaximum(len(self._timestamps) - 1)
+        self._slider_time.setValue(len(self._timestamps) - 1)
+
+    def _reload(self):
+        logger.debug("Reload results...")
+        self._results = self._results.reload()
+
+        self._timestamps = sorted(list(self._results.get("timestamps")))
+
+        self._reload_plots()
+        self._reload_slider()
+
     def _copy(self):
         logger.info("TODO: copy")
 
diff --git a/src/View/Tools/Plot/APlot.py b/src/View/Tools/Plot/APlot.py
index 3fe98fbc..439ae2e9 100644
--- a/src/View/Tools/Plot/APlot.py
+++ b/src/View/Tools/Plot/APlot.py
@@ -30,6 +30,10 @@ class APlot(object):
     def data(self):
         return self._data
 
+    @data.setter
+    def data(self, data):
+        self._data = data
+
     def draw(self):
         """Draw plot
 
diff --git a/src/View/ui/Results.ui b/src/View/ui/Results.ui
index 5b797724..bb8aaae6 100644
--- a/src/View/ui/Results.ui
+++ b/src/View/ui/Results.ui
@@ -140,6 +140,36 @@
    </property>
   </widget>
   <widget class="QStatusBar" name="statusbar"/>
+  <widget class="QToolBar" name="toolBar">
+   <property name="windowTitle">
+    <string>toolBar</string>
+   </property>
+   <attribute name="toolBarArea">
+    <enum>TopToolBarArea</enum>
+   </attribute>
+   <attribute name="toolBarBreak">
+    <bool>false</bool>
+   </attribute>
+   <addaction name="action_add"/>
+   <addaction name="action_reload"/>
+  </widget>
+  <action name="action_add">
+   <property name="icon">
+    <iconset>
+     <normaloff>ressources/gtk-add.png</normaloff>ressources/gtk-add.png</iconset>
+   </property>
+   <property name="text">
+    <string>Add</string>
+   </property>
+   <property name="toolTip">
+    <string>Add custom visualization</string>
+   </property>
+  </action>
+  <action name="action_reload">
+   <property name="text">
+    <string>Reload</string>
+   </property>
+  </action>
  </widget>
  <resources/>
  <connections/>
-- 
GitLab