From 9302d1fabc7436e50608dfaf76a9393afad1b0b1 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 16 Feb 2024 14:16:58 +0100
Subject: [PATCH] SL: Apply new PamhyrPlot features.

---
 src/Model/SedimentLayer/SedimentLayer.py      | 28 +++++++
 src/View/SedimentLayers/Edit/Plot.py          | 79 +++++++------------
 src/View/SedimentLayers/Edit/Window.py        |  1 -
 src/View/SedimentLayers/Reach/Plot.py         | 64 ++++++---------
 src/View/SedimentLayers/Reach/Profile/Plot.py | 54 +++++--------
 .../SedimentLayers/Reach/Profile/Window.py    |  2 +-
 .../SedimentLayers/Reach/Profile/translate.py |  8 ++
 src/View/SedimentLayers/Reach/Window.py       |  1 -
 src/View/SedimentLayers/Window.py             |  1 -
 9 files changed, 106 insertions(+), 132 deletions(-)

diff --git a/src/Model/SedimentLayer/SedimentLayer.py b/src/Model/SedimentLayer/SedimentLayer.py
index 44838baa..b31238cd 100644
--- a/src/Model/SedimentLayer/SedimentLayer.py
+++ b/src/Model/SedimentLayer/SedimentLayer.py
@@ -16,6 +16,8 @@
 
 # -*- coding: utf-8 -*-
 
+from functools import reduce
+
 from tools import trace, timer
 
 from Model.Tools.PamhyrDB import SQLSubModel
@@ -362,3 +364,29 @@ class SedimentLayer(SQLSubModel):
             lst[index], lst[prev] = lst[prev], lst[index]
 
             self._status.modified()
+
+    def compute_height_from_bottom(self, bottom_elevation: list):
+        sl_height = self.height()
+
+        sl_height_by_profile = []
+        for i in range(len(sl_height)):
+            cur_profile = []
+            for _ in bottom_elevation:
+                cur_profile.append(sl_height[i])
+
+            sl_height_by_profile.append(cur_profile)
+
+        z_sl = reduce(
+            lambda acc, current_sl: acc + [
+                list(
+                    map(
+                        lambda cur_sl_h, prev_sl_h: prev_sl_h - cur_sl_h,
+                        current_sl, acc[-1]
+                    )
+                )
+            ],
+            sl_height_by_profile,
+            [bottom_elevation]
+        )
+
+        return z_sl
diff --git a/src/View/SedimentLayers/Edit/Plot.py b/src/View/SedimentLayers/Edit/Plot.py
index 0a0487d2..72f7cfb4 100644
--- a/src/View/SedimentLayers/Edit/Plot.py
+++ b/src/View/SedimentLayers/Edit/Plot.py
@@ -28,8 +28,7 @@ logger = logging.getLogger()
 
 class Plot(PamhyrPlot):
     def __init__(self, canvas=None, trad=None, data=None,
-                 toolbar=None, display_current=True,
-                 parent=None):
+                 toolbar=None, parent=None):
         super(Plot, self).__init__(
             canvas=canvas,
             trad=trad,
@@ -38,51 +37,40 @@ class Plot(PamhyrPlot):
             parent=parent
         )
 
-        self._display_current = display_current
+        self.label_x = ""
+        self.canvas.axes.axes.get_xaxis().set_visible(False)
+
+        self.label_y = self._trad["height"]
 
         self.line_kp_zmin = None
         self.line_kp_sl = []
 
+        self._isometric_axis = False
+
+        self._auto_relim = False
+        self._auto_relim_update = False
+        self._autoscale_update = True
+
     @timer
     def draw(self):
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
-
-        self.canvas.axes.axes.get_xaxis().set_visible(False)
-        self.canvas.axes.set_ylabel(
-            self._trad["height"],
-            color='black', fontsize=10
-        )
+        self.init_axes()
 
         if self.data is None:
             return
 
+        self.draw_data()
+
+        self.idle()
+        self._init = True
+
+    def draw_data(self):
         x = [0, 1]
         z = [0, 0]
-        sl = self.data.height()
-        names = ["bottom"] + self.data.names()
 
-        self.canvas.axes.set_xlim(
-            left=min(x), right=max(x)
-        )
+        z_sl = self.data.compute_height_from_bottom(z)
+        names = ["bottom"] + self.data.names()
 
-        lsl = []
-        for i in range(len(sl)):
-            cur = []
-            for _ in x:
-                cur.append(sl[i])
-            lsl.append(cur)
-
-        # Compute sediment layer in function to point z
-        z_sl = reduce(
-            lambda acc, v: acc + [
-                list(
-                    map(lambda x, y: y - x, v, acc[-1])
-                )
-            ],
-            lsl,
-            [z]
-        )
+        self.canvas.axes.set_xlim(*x)
 
         for i, zsl in enumerate(reversed(z_sl)):
             self.line_kp_sl.append(None)
@@ -90,27 +78,14 @@ class Plot(PamhyrPlot):
                 x, zsl,
                 label=names[-(i+1)],
                 linestyle="solid" if i == len(names) - 1 else "--",
-                lw=1.8,
+                lw=1.5,
                 color='grey' if i == len(names) - 1 else None
             )
             self.canvas.axes.text(
-                x[0] + 0.01, zsl[0] + 0.01, f'{names[-(i+1)]}')
-
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-        if self.toolbar is not None:
-            self.toolbar.update()
-
-        self._init = True
+                x[0] + 0.01, zsl[0] + 0.01,
+                f'{names[-(i+1)]}'
+            )
 
     @timer
-    def update(self, ind=None):
-        if not self._init:
-            self.draw()
-            return
-
-        if ind is None:
-            logger.info("TODO: Update")
-
-            self.canvas.axes.autoscale_view(True, True, True)
-            self.canvas.figure.canvas.draw_idle()
+    def update(self):
+        self.draw()
diff --git a/src/View/SedimentLayers/Edit/Window.py b/src/View/SedimentLayers/Edit/Window.py
index 2c19c0e1..4ae43ba6 100644
--- a/src/View/SedimentLayers/Edit/Window.py
+++ b/src/View/SedimentLayers/Edit/Window.py
@@ -114,7 +114,6 @@ class EditSedimentLayersWindow(PamhyrWindow):
             data=self._sl,
             toolbar=self.toolbar,
             trad=self._trad,
-            display_current=False
         )
         self.plot.draw()
 
diff --git a/src/View/SedimentLayers/Reach/Plot.py b/src/View/SedimentLayers/Reach/Plot.py
index ef354b77..5198128a 100644
--- a/src/View/SedimentLayers/Reach/Plot.py
+++ b/src/View/SedimentLayers/Reach/Plot.py
@@ -33,9 +33,8 @@ logger = logging.getLogger()
 
 
 class Plot(PamhyrPlot):
-    def __init__(self, data=None, display_current=True,
-                 canvas=None, trad=None, toolbar=None,
-                 parent=None):
+    def __init__(self, data=None, canvas=None, trad=None,
+                 toolbar=None, parent=None):
         super(Plot, self).__init__(
             canvas=canvas,
             trad=trad,
@@ -44,15 +43,21 @@ class Plot(PamhyrPlot):
             parent=parent
         )
 
-        self._display_current = display_current
+        self.label_x = self._trad["kp"]
+        self.label_y = self._trad["height"]
 
         self.line_kp_zmin = None
         self.line_kp_sl = []
 
+        self._isometric_axis = False
+
+        self._auto_relim = False
+        self._auto_relim_update = False
+        self._autoscale_update = True
+
     @timer
     def draw(self):
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
+        self.init_axes()
 
         if self.data is None:
             return
@@ -60,29 +65,27 @@ class Plot(PamhyrPlot):
         if self.data.number_profiles == 0:
             return
 
-        self.canvas.axes.set_xlabel(
-            self._trad["kp"],
-            color='black', fontsize=10
-        )
-        self.canvas.axes.set_ylabel(
-            self._trad["height"],
-            color='black', fontsize=10
-        )
+        self.draw_data()
 
+        self.idle()
+        self._init = True
+
+    def draw_data(self):
         kp = self.data.get_kp()
         sl = self.data.get_sl()
         z_min = self.data.get_z_min()
-        z_max = self.data.get_z_max()
 
         self.canvas.axes.set_xlim(
             left=min(kp), right=max(kp)
         )
 
-        # Compute sediment layer in function to profile z_min
         z_sl = reduce(
-            lambda acc, v: acc + [
+            lambda acc, current_sl: acc + [
                 list(
-                    map(lambda x, y: y - x, v, acc[-1])
+                    map(
+                        lambda cur_sl_h, prev_sl_h: prev_sl_h - cur_sl_h,
+                        current_sl, acc[-1]
+                    )
                 )
             ],
             sl,
@@ -94,29 +97,10 @@ class Plot(PamhyrPlot):
             self.line_kp_sl[i], = self.canvas.axes.plot(
                 kp, z,
                 linestyle="solid" if i == len(z_sl) - 1 else "--",
-                lw=1.8,
+                lw=1.5,
                 color='grey' if i == len(z_sl) - 1 else None
             )
 
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-        if self.toolbar is not None:
-            self.toolbar.update()
-
-        self._init = True
-
     @timer
-    def update(self, ind=None):
-        if not self._init:
-            self.draw()
-            return
-
-        if ind is None:
-            kp = self.data.get_kp()
-            z_min = self.data.get_z_min()
-            z_max = self.data.get_z_max()
-
-            self.line_kp_zmin.set_data(kp, z_min)
-
-            self.canvas.axes.autoscale_view(True, True, True)
-            self.canvas.figure.canvas.draw_idle()
+    def update(self):
+        self.draw()
diff --git a/src/View/SedimentLayers/Reach/Profile/Plot.py b/src/View/SedimentLayers/Reach/Profile/Plot.py
index 00ab9efe..fe92bf76 100644
--- a/src/View/SedimentLayers/Reach/Profile/Plot.py
+++ b/src/View/SedimentLayers/Reach/Profile/Plot.py
@@ -33,9 +33,8 @@ logger = logging.getLogger()
 
 
 class Plot(PamhyrPlot):
-    def __init__(self, data=None, display_current=True,
-                 canvas=None, trad=None, toolbar=None,
-                 parent=None):
+    def __init__(self, data=None, canvas=None, trad=None,
+                 toolbar=None, parent=None):
         super(Plot, self).__init__(
             canvas=canvas,
             trad=trad,
@@ -44,15 +43,21 @@ class Plot(PamhyrPlot):
             parent=parent
         )
 
-        self._display_current = display_current
+        self.label_x = self._trad["x"]
+        self.label_y = self._trad["height"]
 
         self.line_kp_zmin = None
         self.line_kp_sl = []
 
+        self._isometric_axis = False
+
+        self._auto_relim = False
+        self._auto_relim_update = False
+        self._autoscale_update = True
+
     @timer
     def draw(self):
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
+        self.init_axes()
 
         if self.data is None:
             return
@@ -60,24 +65,16 @@ class Plot(PamhyrPlot):
         if self.data.number_points == 0:
             return
 
-        self.canvas.axes.set_xlabel(
-            _translate("MainWindow_reach", "X (m)"),
-            color='black', fontsize=10
-        )
-        self.canvas.axes.set_ylabel(
-            _translate("MainWindow_reach", "Height (m)"),
-            color='black', fontsize=10
-        )
+        self.draw_data()
 
+        self.idle()
+        self._init = True
+
+    def draw_data(self):
         x = self.data.get_station()
         z = self.data.z()
         sl = self.data.get_sl()
 
-        self.canvas.axes.set_xlim(
-            left=min(x), right=max(x)
-        )
-
-        # Compute sediment layer in function to point z
         z_sl = reduce(
             lambda acc, v: acc + [
                 list(
@@ -97,21 +94,6 @@ class Plot(PamhyrPlot):
                 color='grey' if i == len(z_sl) - 1 else None
             )
 
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-        if self.toolbar is not None:
-            self.toolbar.update()
-
-        self._init = True
-
     @timer
-    def update(self, ind=None):
-        if not self._init:
-            self.draw()
-            return
-
-        if ind is None:
-            logger.info("TODO: Update")
-
-            self.canvas.axes.autoscale_view(True, True, True)
-            self.canvas.figure.canvas.draw_idle()
+    def update(self):
+        self.draw()
diff --git a/src/View/SedimentLayers/Reach/Profile/Window.py b/src/View/SedimentLayers/Reach/Profile/Window.py
index b7bdec22..63be359d 100644
--- a/src/View/SedimentLayers/Reach/Profile/Window.py
+++ b/src/View/SedimentLayers/Reach/Profile/Window.py
@@ -140,8 +140,8 @@ class ProfileSedimentLayersWindow(PamhyrWindow):
         self.plot = Plot(
             canvas=self.canvas,
             data=self._profile,
+            trad=self._trad,
             toolbar=self.toolbar,
-            display_current=False
         )
         self.plot.draw()
 
diff --git a/src/View/SedimentLayers/Reach/Profile/translate.py b/src/View/SedimentLayers/Reach/Profile/translate.py
index c811b188..ab1b177c 100644
--- a/src/View/SedimentLayers/Reach/Profile/translate.py
+++ b/src/View/SedimentLayers/Reach/Profile/translate.py
@@ -27,6 +27,14 @@ class SedimentProfileTranslate(SedimentReachTranslate):
     def __init__(self):
         super(SedimentProfileTranslate, self).__init__()
 
+        self._dict["x"] = _translate(
+            "SedimentLayers", "X (m)"
+        )
+        self._dict["height"] = _translate(
+            "SedimentLayers", "Height (m)"
+        )
+
+
         self._dict["Profile sediment layers"] = _translate(
             "SedimentLayers", "Profile sediment layers"
         )
diff --git a/src/View/SedimentLayers/Reach/Window.py b/src/View/SedimentLayers/Reach/Window.py
index 0d8fefdc..51d8dc41 100644
--- a/src/View/SedimentLayers/Reach/Window.py
+++ b/src/View/SedimentLayers/Reach/Window.py
@@ -134,7 +134,6 @@ class ReachSedimentLayersWindow(PamhyrWindow):
             data=self._reach,
             toolbar=self.toolbar,
             trad=self._trad,
-            display_current=False
         )
         self.plot.draw()
 
diff --git a/src/View/SedimentLayers/Window.py b/src/View/SedimentLayers/Window.py
index 758c6874..66188298 100644
--- a/src/View/SedimentLayers/Window.py
+++ b/src/View/SedimentLayers/Window.py
@@ -122,7 +122,6 @@ class SedimentLayersWindow(PamhyrWindow):
             data=self._sediment_layers.get(rows[0]),
             trad=self._trad,
             toolbar=None,
-            display_current=False
         )
         self.plot.draw()
 
-- 
GitLab