From 30d76da32fb5f195537c140224fc3105e772d06c Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Wed, 14 Feb 2024 16:23:48 +0100
Subject: [PATCH] Geometry: Renew Plot AC.

---
 src/View/Geometry/PlotAC.py | 375 +++++++++++-------------------------
 src/View/Geometry/Window.py |   6 +-
 2 files changed, 118 insertions(+), 263 deletions(-)

diff --git a/src/View/Geometry/PlotAC.py b/src/View/Geometry/PlotAC.py
index fecfb207..8184ba24 100644
--- a/src/View/Geometry/PlotAC.py
+++ b/src/View/Geometry/PlotAC.py
@@ -41,11 +41,31 @@ class PlotAC(PamhyrPlot):
             parent=parent
         )
 
+        self._isometric_axis = False
+
+        self._auto_relim_update = True
+        self._autoscale_update = True
+
         self.plot_xy = plot_xy
 
-        self.before_plot_selected = None
+        self.label_x = _translate(
+            "Geometry", "Transverse abscissa (m)"
+        )
+        self.label_y = _translate("Geometry", "Height (m)")
+
+        self.label_previous_plot_selected = _translate(
+            "Geometry", "Previous cross-section"
+        )
+        self.label_plot_selected = _translate(
+            "Geometry", "Cross-section"
+        )
+        self.label_next_plot_selected = _translate(
+            "Geometry", "Next cross-section"
+        )
+
+        self.previous_plot_selected = None
         self.plot_selected = None
-        self.after_plot_selected = None
+        self.next_plot_selected = None
 
     def get_line_gl_colors(self, line_2d):
         colors = []
@@ -67,153 +87,54 @@ class PlotAC(PamhyrPlot):
         if np < 2:
             return
 
-        selected_profile = 0
+        self.draw_current()
+        self.draw_gl()
 
-        station = self.data.profile(selected_profile).get_station()
-        elevation = self.data.profile(selected_profile).z()
-        gl = self.data.profile(selected_profile).names()
+        self.canvas.axes.legend(fancybox=True, shadow=True, fontsize=8)
 
-        station_plus_1 = self.data.profile(selected_profile + 1).get_station()
-        elevation_i_plus_1 = self.data.profile(selected_profile + 1).z()
+        self.idle()
+        self._init = True
 
-        self.canvas.axes.set_xlabel(
-            _translate("MainWindow_reach", "Transverse abscissa (m)"),
-            color='black', fontsize=10
-        )
-        self.canvas.axes.set_ylabel(
-            _translate("MainWindow_reach", "Height (m)"),
-            color='black', fontsize=10
-        )
-        self.canvas.figure.tight_layout()
-
-        label_before_plot_selected = _translate(
-            "MainWindow_reach", "Previous cross-section")
-        label_plot_selected = _translate("MainWindow_reach", "Cross-section")
-        label_after_plot_selected = _translate(
-            "MainWindow_reach", "Next cross-section")
-        color_before_plot_selected = "k"  # 'grey'
-        color_plot_selected = 'b'
-        color_after_plot_selected = 'm'
-
-        self.before_plot_selected, = self.canvas.axes.plot(
-            [], [], label=label_before_plot_selected, lw=1.8,
-            linestyle='--', color=color_before_plot_selected
-        )
-        self.plot_selected, = self.canvas.axes.plot(
-            station, elevation, label=label_plot_selected,
-            color=color_plot_selected, lw=1.8
+    def draw_current(self):
+        profile_id = 0
+        profile = self.data.profile(profile_id)
+        station = profile.get_station()
+        elevation = profile.z()
+        gl = profile.names()
+
+        self.previous_plot_selected, = self.canvas.axes.plot(
+            [], [],
+            label=self.label_previous_plot_selected,
+            lw=1.5, linestyle='--', color=self.color_plot_previous
         )
 
-        self.after_plot_selected, = self.canvas.axes.plot(
-            station_plus_1, elevation_i_plus_1,
-            label=label_after_plot_selected,
-            color=color_after_plot_selected, lw=1.6, linestyle='--'
+        self.plot_selected, = self.canvas.axes.plot(
+            station, elevation,
+            label=self.label_plot_selected,
+            color=self.color_plot_current, lw=1.5
         )
-        self.annotation = []
-        self.complete_gl, self.incomplete_gl = self.data.compute_guidelines()
-        lcomplete = list(self.complete_gl)
-        lincomplete = list(self.incomplete_gl)
-
-        line_2d = self.plot_xy.line_gl
-        self.color_complete_gl = self.get_line_gl_colors(line_2d)
-        self.color_incomplete_gl = 2 * ["#000000"]
-
-        x_gl_complete = []
-        y_gl_complete = []
-        color_scat_complete_gl = []
-        x_gl_incomplete = []
-        y_gl_incomplete = []
-        color_scat_incomplete_gl = []
-
-        for i, txt in enumerate(gl):
-            if txt.strip() in self.complete_gl:
-                annotation = self.canvas.axes.annotate(
-                    txt, (station[i], elevation[i]),
-                    horizontalalignment='left',
-                    verticalalignment='top', annotation_clip=True,
-                    fontsize=11,
-                    color=self.color_complete_gl[
-                        lcomplete.index(txt)
-                    ]
-                )
-
-                annotation.set_position((station[i] + 0., elevation[i] + 0.))
-                self.annotation.append(annotation)
-
-                x_gl_complete.append(station[i])
-                y_gl_complete.append(elevation[i])
-                color_scat_complete_gl.append(
-                    self.color_complete_gl[lcomplete.index(txt)])
-            elif txt.strip() in self.incomplete_gl:
-                annotate = self.canvas.axes.annotate(
-                    txt, (station[i], elevation[i]
-                          ), horizontalalignment='left',
-                    verticalalignment='top', annotation_clip=True, fontsize=11,
-                    color=self.color_incomplete_gl[
-                        lincomplete.index(txt)
-                    ],
-                )
-
-                self.annotation.append(annotate)
-
-                x_gl_incomplete.append(station[i])
-                y_gl_incomplete.append(elevation[i])
-                color_scat_incomplete_gl.append(
-                    self.color_incomplete_gl[lincomplete.index(txt)]
-                )
 
-        self.canvas.axes.legend(fancybox=True, shadow=True, fontsize=8)
-        self.canvas.figure.tight_layout()
+        next_id = profile_id + 1
+        station_next = self.data.profile(next_id).get_station()
+        elevation_next = self.data.profile(next_id).z()
 
-        self.canvas.figure.canvas.draw_idle()
-        self.toolbar.update()
+        self.next_plot_selected, = self.canvas.axes.plot(
+            station_next, elevation_next,
+            label=self.label_next_plot_selected,
+            color=self.color_plot_next, lw=1.6, linestyle='--'
+        )
 
-        self._init = True
+    def draw_gl(self):
+        if self._current_data_update:
+            profile_id = self._current_data
+        else:
+            profile_id = 0
 
-    def update_full(self):
-        selected_profile = 0
-        station = self.data.profile(selected_profile).get_station()
-        station_plus_1 = self.data.profile(selected_profile + 1).get_station()
-        elevation = self.data.profile(selected_profile).z()
-        elevation_i_plus_1 = self.data.profile(selected_profile + 1).z()
-        gl = self.data.profile(selected_profile).names()
+        profile = self.data.profile(profile_id)
+        station = profile.get_station()
+        elevation = profile.z()
+        gl = profile.names()
 
-        self.canvas.axes.cla()
-        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
-        self.canvas.axes.set_xlabel(
-            _translate("MainWindow_reach", "Transverse abscissa (m)"),
-            color='black', fontsize=10
-        )
-        self.canvas.axes.set_ylabel(
-            _translate("MainWindow_reach", "Height (m)"),
-            color='black', fontsize=10
-        )
-        self.canvas.figure.tight_layout()
-
-        label_before_plot_selected = _translate(
-            "MainWindow_reach", "Previous cross-section")
-        label_plot_selected = _translate(
-            "MainWindow_reach", "Cross-section")
-        label_after_plot_selected = _translate(
-            "MainWindow_reach", "Next cross-section")
-        color_before_plot_selected = "k"  # 'grey'
-        color_plot_selected = 'b'
-        color_after_plot_selected = 'm'
-
-        self.before_plot_selected, = self.canvas.axes.plot(
-            [], [], label=label_before_plot_selected, lw=1.8,
-            linestyle='--', color=color_before_plot_selected
-        )
-        self.plot_selected, = self.canvas.axes.plot(
-            station, elevation, label=label_plot_selected,
-            color=color_plot_selected, lw=1.8
-        )
-
-        self.after_plot_selected, = self.canvas.axes.plot(
-            station_plus_1, elevation_i_plus_1,
-            label=label_after_plot_selected,
-            color=color_after_plot_selected, lw=1.6, linestyle='--'
-        )
         self.annotation = []
         self.complete_gl, self.incomplete_gl = self.data.compute_guidelines()
         lcomplete = list(self.complete_gl)
@@ -221,157 +142,89 @@ class PlotAC(PamhyrPlot):
 
         line_2d = self.plot_xy.line_gl
         self.color_complete_gl = self.get_line_gl_colors(line_2d)
-        self.color_incomplete_gl = 2 * ["#000000"]
+        self.color_incomplete_gl = 2 * ["grey"]
 
         x_gl_complete = []
         y_gl_complete = []
-        color_scat_complete_gl = []
         x_gl_incomplete = []
         y_gl_incomplete = []
-        color_scat_incomplete_gl = []
 
         for i, txt in enumerate(gl):
-            if txt.strip() in self.complete_gl:
-                annotation = self.canvas.axes.annotate(
-                    txt, (station[i], elevation[i]),
-                    horizontalalignment='left',
-                    verticalalignment='top', annotation_clip=True,
-                    fontsize=11,
-                    color=self.color_complete_gl[
-                        lcomplete.index(txt)
-                    ]
-                )
+            if txt == "":
+                continue
 
-                annotation.set_position((station[i] + 0., elevation[i] + 0.))
-                self.annotation.append(annotation)
+            if txt.strip() in self.complete_gl:
+                color = self.color_complete_gl[
+                    lcomplete.index(txt)
+                ]
+            else:
+                color = self.color_incomplete_gl[
+                    lincomplete.index(txt)
+                ]
+
+            annotation = self.canvas.axes.annotate(
+                txt, (station[i], elevation[i]),
+                horizontalalignment='left',
+                verticalalignment='top',
+                annotation_clip=True,
+                fontsize=11, color=color
+            )
+
+            annotation.set_position((station[i] + 0., elevation[i] + 0.))
+            self.annotation.append(annotation)
 
+            if txt.strip() in self.complete_gl:
                 x_gl_complete.append(station[i])
                 y_gl_complete.append(elevation[i])
-                color_scat_complete_gl.append(
-                    self.color_complete_gl[lcomplete.index(txt)]
-                )
-            elif txt.strip() in self.incomplete_gl:
-                annotate = self.canvas.axes.annotate(
-                    txt,
-                    (station[i], elevation[i]),
-                    horizontalalignment='left',
-                    verticalalignment='top',
-                    annotation_clip=True, fontsize=11,
-                    color=self.color_incomplete_gl[
-                        lincomplete.index(txt)
-                    ],
-                )
-
-                self.annotation.append(annotate)
-
+            else:
                 x_gl_incomplete.append(station[i])
                 y_gl_incomplete.append(elevation[i])
-                color_scat_incomplete_gl.append(
-                    self.color_incomplete_gl[lincomplete.index(txt)]
-                )
-
-        self.canvas.axes.legend(fancybox=True, shadow=True, fontsize=8)
-        self.canvas.figure.tight_layout()
-
-        self.canvas.figure.canvas.draw_idle()
-        self.toolbar.update()
-
-    def update_annotate_full(self, ind):
-        for a in self.annotation:
-            a.remove()
-
-        self.annotation[:] = []
-
-        x = self.data.profile(ind).get_station()
-        y = self.data.profile(ind).z()
-        gl = self.data.profile(ind).names()
-        complete, incomplete = self.data.compute_guidelines()
-
-        lcomplete = list(complete)
-        lincomplete = list(incomplete)
-
-        self.x_complete = []
-        color_scat_complete = []
-        self.x_incomplete = []
-        color_scat_incomplete = []
-
-        try:
-            for i, txt in enumerate(gl):
-                if txt in complete:
-                    annotate = self.canvas.axes.annotate(
-                        txt, (x[i], y[i]), horizontalalignment='left',
-                        verticalalignment='top', annotation_clip=True,
-                        fontsize=11,
-                        color=self.color_complete_gl[
-                            lcomplete.index(txt)
-                        ],
-                    )
-                    self.annotation.append(annotate)
-                    self.x_complete.append([x[i], y[i]])
-                    color_scat_complete.append(
-                        self.color_complete_gl[lcomplete.index(txt)]
-                    )
-                elif txt in incomplete:
-                    annotate = self.canvas.axes.annotate(
-                        txt, (x[i], y[i]), horizontalalignment='left',
-                        verticalalignment='top', annotation_clip=True,
-                        fontsize=11,
-                        color=self.color_incomplete_gl[
-                            lincomplete.index(txt)
-                        ],
-                    )
-                    self.annotation.append(annotate)
-                    self.x_incomplete.append([x[i], y[i]])
-                    color_scat_incomplete.append(
-                        self.color_incomplete_gl[lincomplete.index(txt)]
-                    )
-        except Exception as e:
-            logger.warning(f"{e}")
-
-        self.canvas.figure.canvas.draw_idle()
 
     @timer
-    def update(self, ind=None):
+    def update(self):
         if not self._init:
             self.draw()
             return
 
-        line_2d = self.plot_xy.line_gl
-        self.color_complete_gl = self.get_line_gl_colors(line_2d)
-        self.color_incomplete_gl = 2 * ["#000000"]
+        self.update_current()
+        self.update_gl()
 
-        if ind is not None:
-            before = ind - 1
-            after = ind + 1
+        self.update_idle()
 
-            self.before_plot_selected.set_data([], [])
+    def update_current(self):
+        if self._current_data_update:
+            profile_id = self._current_data
+            previous_id = profile_id - 1
+            next_id = profile_id + 1
+
+            self.previous_plot_selected.set_data([], [])
             self.plot_selected.set_data([], [])
-            self.after_plot_selected.set_data([], [])
+            self.next_plot_selected.set_data([], [])
 
-            if 0 <= before < self.data.number_profiles:
-                self.before_plot_selected.set_data(
-                    self.data.profile(before).get_station(),
-                    self.data.profile(before).z()
+            if 0 <= previous_id < self.data.number_profiles:
+                self.previous_plot_selected.set_data(
+                    self.data.profile(previous_id).get_station(),
+                    self.data.profile(previous_id).z()
                 )
 
-            if 0 <= ind < self.data.number_profiles:
+            if 0 <= profile_id < self.data.number_profiles:
                 self.plot_selected.set_data(
-                    self.data.profile(ind).get_station(),
-                    self.data.profile(ind).z()
+                    self.data.profile(profile_id).get_station(),
+                    self.data.profile(profile_id).z()
                 )
 
-            if 0 <= after < self.data.number_profiles:
-                self.after_plot_selected.set_data(
-                    self.data.profile(after).get_station(),
-                    self.data.profile(after).z()
+            if 0 <= next_id < self.data.number_profiles:
+                self.next_plot_selected.set_data(
+                    self.data.profile(next_id).get_station(),
+                    self.data.profile(next_id).z()
                 )
 
-            self.update_annotate_full(ind)
-        else:
-            self.update_full()
-            self.update_annotate_full(0)
+    def update_full(self):
+        self.draw()
 
-        self.canvas.axes.relim()
-        self.canvas.axes.autoscale()
-        self.canvas.axes.autoscale_view()
-        self.canvas.figure.canvas.draw_idle()
+    def update_gl(self):
+        for a in self.annotation:
+            a.remove()
+
+        self.annotation[:] = []
+        self.draw_gl()
diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py
index 35a94bce..19f0a724 100644
--- a/src/View/Geometry/Window.py
+++ b/src/View/Geometry/Window.py
@@ -371,7 +371,8 @@ class GeometryWindow(PamhyrWindow):
 
     def update_plot_ac(self, ind: int):
         self.tableView.model().blockSignals(True)
-        self._plot_ac.update(ind=ind)
+        self._plot_ac.current = ind
+        self._plot_ac.update()
         self.tableView.model().blockSignals(False)
 
     def get_station(self, ind: int):
@@ -394,7 +395,8 @@ class GeometryWindow(PamhyrWindow):
 
     def select_plot_ac(self, ind: int):
         self.tableView.model().blockSignals(True)
-        self._plot_ac.update(ind=ind)
+        self._plot_ac.current = ind
+        self._plot_ac.update()
         self.tableView.model().blockSignals(False)
 
     def select_row_profile_slider(self, ind: int = 0):
-- 
GitLab