From ea146cbe87e1743e900eb253ee74bb3c0e121c94 Mon Sep 17 00:00:00 2001 From: Theophile Terraz Date: Tue, 11 Jun 2024 14:17:38 +0200 Subject: [PATCH] interactive profile selection in structures + debug --- src/View/Geometry/PlotAC.py | 1 - src/View/Geometry/PlotKPZ.py | 36 ++++++++--------- src/View/Geometry/PlotXY.py | 1 - src/View/Geometry/Profile/Plot.py | 9 +++++ src/View/HydraulicStructures/PlotKPC.py | 53 +++++++++++++++++++++++-- src/View/HydraulicStructures/Window.py | 3 +- src/View/Tools/PamhyrPlot.py | 11 +---- 7 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/View/Geometry/PlotAC.py b/src/View/Geometry/PlotAC.py index 2bdee3d1..ad43fdf3 100644 --- a/src/View/Geometry/PlotAC.py +++ b/src/View/Geometry/PlotAC.py @@ -50,7 +50,6 @@ class PlotAC(PamhyrPlot): self.previous_plot_selected = None self.plot_selected = None self.next_plot_selected = None - self._rect_select = None @timer def draw(self): diff --git a/src/View/Geometry/PlotKPZ.py b/src/View/Geometry/PlotKPZ.py index 7d7921a5..36b9beba 100644 --- a/src/View/Geometry/PlotKPZ.py +++ b/src/View/Geometry/PlotKPZ.py @@ -57,7 +57,6 @@ class PlotKPZ(PamhyrPlot): self.before_plot_selected = None self.plot_selected = None self.after_plot_selected = None - self._rect_select = None self.parent=parent self._table=table self._colors = [] @@ -286,25 +285,26 @@ class PlotKPZ(PamhyrPlot): for hs in lhs: x = hs.input_kp - z_min = reach.get_z_min() - z_max = reach.get_z_max() + if x is not None: + z_min = reach.get_z_min() + z_max = reach.get_z_max() - self.canvas.axes.plot( - [x, x], - [min(z_min), max(z_max)], - linestyle="--", - lw=1., - color=self.color_plot_previous, - ) + self.canvas.axes.plot( + [x, x], + [min(z_min), max(z_max)], + linestyle="--", + lw=1., + color=self.color_plot_previous, + ) - self.canvas.axes.annotate( - " > " + hs.name, - (x, max(z_max)), - horizontalalignment='left', - verticalalignment='top', - annotation_clip=True, - fontsize=9, color=self.color_plot_previous, - ) + self.canvas.axes.annotate( + " > " + hs.name, + (x, max(z_max)), + horizontalalignment='left', + verticalalignment='top', + annotation_clip=True, + fontsize=9, color=self.color_plot_previous, + ) @timer def update(self): diff --git a/src/View/Geometry/PlotXY.py b/src/View/Geometry/PlotXY.py index a96fd45b..e163e538 100644 --- a/src/View/Geometry/PlotXY.py +++ b/src/View/Geometry/PlotXY.py @@ -51,7 +51,6 @@ class PlotXY(PamhyrPlot): self.before_plot_selected = None self.plot_selected = None self.after_plot_selected = None - self._rect_select = None self.parent=parent self.line_xy_collection = None self._table=table diff --git a/src/View/Geometry/Profile/Plot.py b/src/View/Geometry/Profile/Plot.py index f8ced64e..8ebe0aaa 100644 --- a/src/View/Geometry/Profile/Plot.py +++ b/src/View/Geometry/Profile/Plot.py @@ -29,6 +29,7 @@ from PyQt5.QtCore import ( ) from PyQt5.QtWidgets import QApplication +from matplotlib.widgets import RectangleSelector _translate = QCoreApplication.translate @@ -71,6 +72,14 @@ class Plot(PamhyrPlot): # wet_preimeter, water_width) ) self._onpickevent = None + self._rect_select = RectangleSelector(ax=self.canvas.axes, + onselect=self.rect_select_callback, + useblit=True, + button=[1], # don't use middle nor right button + minspanx=2.0, + minspany=2.0, + spancoords='pixels', + interactive=False) def onrelease(self, event): # we need to do that to prevent conflicst between onpick and rect_select_callback diff --git a/src/View/HydraulicStructures/PlotKPC.py b/src/View/HydraulicStructures/PlotKPC.py index 3327f6e6..209e68a1 100644 --- a/src/View/HydraulicStructures/PlotKPC.py +++ b/src/View/HydraulicStructures/PlotKPC.py @@ -20,8 +20,9 @@ from tools import timer from View.Tools.PamhyrPlot import PamhyrPlot from PyQt5.QtCore import ( - QCoreApplication + QCoreApplication, Qt, ) +from PyQt5.QtWidgets import QApplication from matplotlib.collections import LineCollection @@ -50,6 +51,7 @@ class PlotKPC(PamhyrPlot): self._auto_relim_update = True self._autoscale_update = True + self.parent = parent @property def river(self): @@ -91,7 +93,14 @@ class PlotKPC(PamhyrPlot): self.line_kp_zmin, = self.canvas.axes.plot( kp, z_min, color=self.color_plot_river_bottom, - lw=1. + lw=1., + ) + + self.line_kp_zmax, = self.canvas.axes.plot( + kp, z_max, + color=self.color_plot_river_bottom, + lw=1., + alpha=0.0 ) if len(kp) != 0: @@ -99,7 +108,8 @@ class PlotKPC(PamhyrPlot): x=kp, ymin=z_min, ymax=z_max, color=self.color_plot, - lw=1. + lw=1., + picker=10 ) def draw_current(self): @@ -163,3 +173,40 @@ class PlotKPC(PamhyrPlot): self.profile.set_data([], []) self.canvas.figure.canvas.draw_idle() + + def onpick(self, event): + if event.mouseevent.inaxes != self.canvas.axes: + return + if event.mouseevent.button.value != 1: + return + + modifiers = QApplication.keyboardModifiers() + if modifiers not in [Qt.ControlModifier, Qt.NoModifier, Qt.ShiftModifier]: + return + + closest = self._closest_profile(event) + index = self.parent.tableView.selectedIndexes() + print(index) + if self.parent._table is not None: + self.parent._table.setData(index[2], closest) + + + return + + def _closest_profile(self, event): + + s = event.artist.get_segments() + x = [i[0,0] for i in s] + mx = event.mouseevent.xdata + points = enumerate(x) + + def dist_mouse(point): + x = point[1] + d = abs(mx - x) + return d + + closest = min( + points, key=dist_mouse + ) + + return closest[1] diff --git a/src/View/HydraulicStructures/Window.py b/src/View/HydraulicStructures/Window.py index 1c6af72c..7dee787d 100644 --- a/src/View/HydraulicStructures/Window.py +++ b/src/View/HydraulicStructures/Window.py @@ -141,7 +141,8 @@ class HydraulicStructuresWindow(PamhyrWindow): reach=None, profile=None, trad=self._trad, - toolbar=self.toolbar + toolbar=self.toolbar, + parent=self ) self.plot_kpc.draw() diff --git a/src/View/Tools/PamhyrPlot.py b/src/View/Tools/PamhyrPlot.py index 41b0b87f..76159205 100644 --- a/src/View/Tools/PamhyrPlot.py +++ b/src/View/Tools/PamhyrPlot.py @@ -19,7 +19,6 @@ import logging import matplotlib.colors as mplcolors -from matplotlib.widgets import RectangleSelector from matplotlib import ticker from tools import timestamp_to_old_pamhyr_date @@ -43,7 +42,6 @@ class PamhyrPlot(APlot): color_plot_river_bottom = "grey" color_plot_river_water = "blue" color_plot_river_water_zone = "skyblue" - colors = list(mplcolors.TABLEAU_COLORS) linestyle = ['solid', 'dashed', 'dashdot', 'dotted'] @@ -106,14 +104,7 @@ class PamhyrPlot(APlot): self._highlight_data_update = False self._current_data = None #: Current data identifier self._current_data_update = False - self._rect_select = RectangleSelector(ax=self.canvas.axes, - onselect=self.rect_select_callback, - useblit=True, - button=[1], # don't use middle nor right button - minspanx=2.0, - minspany=2.0, - spancoords='pixels', - interactive=False) + self._rect_select = None @property def canvas(self):