From 4f747f8fa7b174ddcde7648a196899a0208c9635 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 16 Feb 2024 10:49:59 +0100
Subject: [PATCH] HS: Apply new PamhyrPlot features.

---
 src/View/HydraulicStructures/PlotAC.py  | 60 +++++++++++--------------
 src/View/HydraulicStructures/PlotKPC.py | 60 +++++++++++++------------
 src/View/Tools/PamhyrPlot.py            |  1 +
 3 files changed, 58 insertions(+), 63 deletions(-)

diff --git a/src/View/HydraulicStructures/PlotAC.py b/src/View/HydraulicStructures/PlotAC.py
index 03c384c9..d8b81974 100644
--- a/src/View/HydraulicStructures/PlotAC.py
+++ b/src/View/HydraulicStructures/PlotAC.py
@@ -42,6 +42,14 @@ class PlotAC(PamhyrPlot):
         self._current_reach = reach
         self._current_profile = profile
 
+        self.label_x = _translate("MainWindow_reach", "X (m)")
+        self.label_y = _translate("MainWindow_reach", "Elevation (m)")
+
+        self._isometric_axis = False
+
+        self._auto_relim_update = True
+        self._autoscale_update = True
+
     @property
     def river(self):
         return self.data
@@ -51,9 +59,8 @@ class PlotAC(PamhyrPlot):
         self.data = river
 
     @timer
-    def draw(self, highlight=None):
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
+    def draw(self):
+        self.init_axes()
 
         if self.data is None:
             self.line_kp = None
@@ -63,16 +70,13 @@ class PlotAC(PamhyrPlot):
             self.line_kp = None
             return
 
-        reach = self._current_reach
+        self.draw_data()
 
-        self.canvas.axes.set_xlabel(
-            _translate("MainWindow_reach", "X (m)"),
-            color='black', fontsize=11
-        )
-        self.canvas.axes.set_ylabel(
-            _translate("MainWindow_reach", "Elevation (m)"),
-            color='black', fontsize=11
-        )
+        self.idle()
+        self._init = True
+
+    def draw_data(self):
+        reach = self._current_reach
 
         if self._current_profile is None:
             self.line_kp = None
@@ -83,19 +87,10 @@ class PlotAC(PamhyrPlot):
 
             self.line_kp, = self.canvas.axes.plot(
                 x, z,
-                linestyle="solid",
-                lw=1.8,
-                color='grey',
+                color=self.color_plot_river_bottom,
+                **self.plot_default_kargs
             )
 
-        self.canvas.axes.relim()
-        self.canvas.axes.autoscale_view()
-
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-        if self.toolbar is not None:
-            self.toolbar.update()
-
     def set_reach(self, reach):
         self._current_reach = reach
         self.update()
@@ -110,24 +105,19 @@ class PlotAC(PamhyrPlot):
             return
 
         if self._current_reach is None or self._current_profile is None:
-            self.clear()
-            return
+            self.update_clear()
+        else:
+            self.update_data()
 
+        self.update_idle()
+
+    def update_data(self):
         profile = self._current_profile
         x = profile.get_station()
         z = profile.z()
 
         self.line_kp.set_data(x, z)
 
-        self.canvas.axes.relim()
-        self.canvas.axes.autoscale_view()
-
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-
-    def clear(self):
+    def update_clear(self):
         if self.line_kp is not None:
             self.line_kp.set_data([], [])
-
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
diff --git a/src/View/HydraulicStructures/PlotKPC.py b/src/View/HydraulicStructures/PlotKPC.py
index b4bc6fe9..961f0a81 100644
--- a/src/View/HydraulicStructures/PlotKPC.py
+++ b/src/View/HydraulicStructures/PlotKPC.py
@@ -43,6 +43,14 @@ class PlotKPC(PamhyrPlot):
         self._current_reach = reach
         self._current_profile = profile
 
+        self.label_x = _translate("MainWindow_reach", "KP (m)")
+        self.label_y = _translate("MainWindow_reach", "Elevation (m)")
+
+        self._isometric_axis = False
+
+        self._auto_relim_update = True
+        self._autoscale_update = True
+
     @property
     def river(self):
         return self.data
@@ -53,8 +61,7 @@ class PlotKPC(PamhyrPlot):
 
     @timer
     def draw(self, highlight=None):
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
+        self.init_axes()
 
         if self.data is None:
             self.profile = None
@@ -68,16 +75,14 @@ class PlotKPC(PamhyrPlot):
             self.line_kp_zmin = None
             return
 
-        reach = self._current_reach
+        self.draw_data()
+        self.draw_current()
 
-        self.canvas.axes.set_ylabel(
-            _translate("MainWindow_reach", "Elevation (m)"),
-            color='black', fontsize=11
-        )
-        self.canvas.axes.set_xlabel(
-            _translate("MainWindow_reach", "KP (m)"),
-            color='black', fontsize=11
-        )
+        self.idle()
+        self._init = True
+
+    def draw_data(self):
+        reach = self._current_reach
 
         kp = reach.reach.get_kp()
         z_min = reach.reach.get_z_min()
@@ -85,32 +90,33 @@ class PlotKPC(PamhyrPlot):
 
         self.line_kp_zmin, = self.canvas.axes.plot(
             kp, z_min,
-            color='grey', lw=1.
+            color=self.color_plot_river_bottom,
+            lw=1.
         )
 
         if len(kp) != 0:
             self.line_kp_zmin_zmax = self.canvas.axes.vlines(
                 x=kp,
                 ymin=z_min, ymax=z_max,
-                color='b',
+                color=self.color_plot,
                 lw=1.
             )
 
+    def draw_current(self):
         if self._current_profile is None:
             self.profile = None
         else:
-            self.profile, = self.canvas.axes.plot(
-                [self._current_profile.kp, self._current_profile.kp],
-                [self._current_profile.z_min(), self._current_profile.z_max()],
-                color='red', lw=1.
+            kp = [self._current_profile.kp,
+                  self._current_profile.kp]
+            min_max = [self._current_profile.z_min(),
+                       self._current_profile.z_max()]
+
+            self.profile = self.canvas.axes.plot(
+                kp, min_max,
+                color=self.color_plot_current,
+                lw=1.
             )
 
-        self.canvas.axes.relim()
-        self.canvas.figure.tight_layout()
-        self.canvas.figure.canvas.draw_idle()
-        if self.toolbar is not None:
-            self.toolbar.update()
-
     def set_reach(self, reach):
         self._current_reach = reach
         self._current_profile = None
@@ -118,12 +124,12 @@ class PlotKPC(PamhyrPlot):
 
     def set_profile(self, profile):
         self._current_profile = profile
-        self.update_profil()
+        self.update_current_profile()
 
     def update(self):
         self.draw()
 
-    def update_profil(self):
+    def update_current_profile(self):
         reach = self._current_reach
         kp = reach.reach.get_kp()
         z_min = reach.reach.get_z_min()
@@ -137,9 +143,7 @@ class PlotKPC(PamhyrPlot):
                 [self._current_profile.z_min(), self._current_profile.z_max()],
             )
 
-            self.canvas.axes.relim()
-            self.canvas.axes.autoscale_view()
-            self.canvas.figure.canvas.draw_idle()
+            self.update_idle()
 
     def clear(self):
         if self.profile is not None:
diff --git a/src/View/Tools/PamhyrPlot.py b/src/View/Tools/PamhyrPlot.py
index f3b97631..c0c060cc 100644
--- a/src/View/Tools/PamhyrPlot.py
+++ b/src/View/Tools/PamhyrPlot.py
@@ -35,6 +35,7 @@ class PamhyrPlot(APlot):
     color_plot_previous = "black"
     color_plot_current = "blue"
     color_plot_next = "purple"
+    color_plot_river_bottom = "grey"
 
     colors = list(mplcolors.TABLEAU_COLORS)
 
-- 
GitLab