PlotKPZ restructuration

0.0.9
Theophile Terraz 2024-06-10 15:59:02 +02:00
parent 052faed48d
commit 872ff0ef62
3 changed files with 146 additions and 125 deletions

View File

@ -21,7 +21,10 @@ import logging
from tools import timer from tools import timer
from View.Tools.PamhyrPlot import PamhyrPlot from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt from PyQt5.QtCore import(
Qt, QItemSelectionModel,
QItemSelection, QItemSelectionRange,
)
logger = logging.getLogger() logger = logging.getLogger()
@ -29,7 +32,7 @@ logger = logging.getLogger()
class PlotKPZ(PamhyrPlot): class PlotKPZ(PamhyrPlot):
def __init__(self, canvas=None, trad=None, def __init__(self, canvas=None, trad=None,
study=None, data=None, toolbar=None, study=None, data=None, toolbar=None,
parent=None): table=None, parent=None):
self._study = study self._study = study
super(PlotKPZ, self).__init__( super(PlotKPZ, self).__init__(
@ -37,6 +40,7 @@ class PlotKPZ(PamhyrPlot):
trad=trad, trad=trad,
data=data, data=data,
toolbar=toolbar, toolbar=toolbar,
table=table,
parent=parent parent=parent
) )
@ -55,6 +59,8 @@ class PlotKPZ(PamhyrPlot):
self.after_plot_selected = None self.after_plot_selected = None
self._rect_select = None self._rect_select = None
self.parent=parent self.parent=parent
self._table=table
self._colors = []
def onpick(self, event): def onpick(self, event):
if event.mouseevent.inaxes != self.canvas.axes: if event.mouseevent.inaxes != self.canvas.axes:
@ -69,7 +75,28 @@ class PlotKPZ(PamhyrPlot):
ind, point = self._closest_kp(event) ind, point = self._closest_kp(event)
if self.parent._table is not None: if self.parent._table is not None:
self.parent._table.blockSignals(True) self.parent._table.blockSignals(True)
self.parent.select_row_profile_slider(ind) if modifiers == Qt.ControlModifier:
rows = list(
set(
(i.row() for i in self.parent.tableView.selectedIndexes())
)
)
self._select_in_table(rows+[ind])
elif modifiers == Qt.ShiftModifier:
rows = list(
set(
(i.row() for i in self.parent.tableView.selectedIndexes())
)
)
if len(rows)>0:
i1 = min(rows[0], rows[-1], ind)
i2 = max(rows[0], rows[-1], ind)
else:
i1 = ind
i2 = ind
self._select_range_in_table(i1, i2)
else:
self.parent.select_row_profile_slider(ind)
self.parent._table.blockSignals(False) self.parent._table.blockSignals(False)
self.update() self.update()
@ -96,6 +123,42 @@ class PlotKPZ(PamhyrPlot):
return closest return closest
def _select_in_table(self, ind):
if self._table is not None:
self._table.blockSignals(True)
self._table.setFocus()
selection = self._table.selectionModel()
index = QItemSelection()
if len(ind) > 0:
for i in ind:
index.append(QItemSelectionRange(self._table.model().index(i, 0)))
selection.select(
index,
QItemSelectionModel.Rows |
QItemSelectionModel.ClearAndSelect |
QItemSelectionModel.Select
)
if len(ind) > 0:
self._table.scrollTo(self._table.model().index(ind[-1], 0))
self._table.blockSignals(False)
def _select_range_in_table(self, ind1, ind2):
if self._table is not None:
self._table.blockSignals(True)
self._table.setFocus()
selection = self._table.selectionModel()
index = QItemSelection(self._table.model().index(ind1, 0),
self._table.model().index(ind2, 0))
selection.select(
index,
QItemSelectionModel.Rows |
QItemSelectionModel.ClearAndSelect |
QItemSelectionModel.Select
)
self._table.scrollTo(self._table.model().index(ind2, 0))
self._table.blockSignals(False)
@timer @timer
def draw(self): def draw(self):
self.init_axes() self.init_axes()
@ -118,8 +181,7 @@ class PlotKPZ(PamhyrPlot):
return return
self.draw_z_line() self.draw_z_line()
self.draw_z_line_highlight() self.draw_lr()
self.draw_current()
self.draw_gl() self.draw_gl()
self.draw_bottom() self.draw_bottom()
self.draw_profiles_hs(self._data) self.draw_profiles_hs(self._data)
@ -132,77 +194,60 @@ class PlotKPZ(PamhyrPlot):
z_min = self.data.get_z_min() z_min = self.data.get_z_min()
z_max = self.data.get_z_max() z_max = self.data.get_z_max()
self._colors, self._style = self.color_hightlight()
self.line_kp_zmin_zmax = self.canvas.axes.vlines( self.line_kp_zmin_zmax = self.canvas.axes.vlines(
x=kp, ymin=z_min, ymax=z_max, x=kp, ymin=z_min, ymax=z_max,
color=self.color_plot, color=self._colors,
linestyle = self._style,
lw=1., lw=1.,
picker=10 picker=10,
) )
def draw_z_line_highlight(self): def color_hightlight(self):
if self._highlight_data is not None: rows = list(
kp = self.data.get_kp_complete_profiles() set(
z_min = self.data.get_z_min() (i.row() for i in self.parent.tableView.selectedIndexes())
z_max = self.data.get_z_max()
kp_min, kp_max = self._highlight_data
indexes = list(
map(
lambda x: x[0],
filter(
lambda x: kp_min <= x[1] <= kp_max,
enumerate(kp)
)
)
) )
)
colors = [self.color_plot for row in range(len(self._data))]
style = ["-" for row in range(len(self._data))]
if len(rows) >0:
for row in rows:
colors[row] = self.color_plot_current
if rows[0] > 0:
colors[rows[0]-1] = self.color_plot_previous
style[rows[0]-1] = "--"
if rows[-1] < len(self._data)-1:
colors[rows[-1]+1] = self.color_plot_next
style[rows[-1]+1] = "--"
return colors, style
def indexes_filter(data): return list( def draw_lr(self):
map(
lambda x: x[1],
filter(
lambda x: x[0] in indexes,
enumerate(data)
)
)
)
ikp = indexes_filter(kp)
imin = indexes_filter(z_min)
imax = indexes_filter(z_max)
self.line_kp_zmin_zmax_highlight = self.canvas.axes.vlines(
x=ikp,
ymin=imin, ymax=imax,
color=self.color_plot_highlight,
lw=1.
)
def draw_current(self):
kp = self.data.get_kp_complete_profiles() kp = self.data.get_kp_complete_profiles()
z_min = self.data.get_z_min() lz = []
z_max = self.data.get_z_max() rz = []
self.plot_selected, = self.canvas.axes.plot( self.line_lr = []
(kp[0], kp[0]), for z in self.data.get_z():
(z_min[0], z_max[0]), lz.append(z[0])
color=self.color_plot_current, lw=1.5 rz.append(z[-1])
)
self.plot_selected.set_visible(False)
self.before_plot_selected, = self.canvas.axes.plot( line = self.canvas.axes.plot(
(kp[0], kp[0]), kp, lz,
(z_min[0], z_max[0]), color=self.color_plot_river_bottom,
color=self.color_plot_previous, lw=1.5, linestyle='--' linestyle="dotted",
lw=1.,
) )
self.before_plot_selected.set_visible(False) self.line_lr.append(line)
self.after_plot_selected, = self.canvas.axes.plot( line = self.canvas.axes.plot(
(kp[0], kp[0]), kp, rz,
(z_min[0], z_max[0]), color=self.color_plot_river_bottom,
color=self.color_plot_next, lw=1.5, linestyle='--' linestyle="dotted",
lw=1.,
) )
self.after_plot_selected.set_visible(False) self.line_lr.append(line)
def draw_gl(self): def draw_gl(self):
kp = self.data.get_kp_complete_profiles() kp = self.data.get_kp_complete_profiles()
@ -270,6 +315,7 @@ class PlotKPZ(PamhyrPlot):
self.draw() self.draw()
return return
self.update_lr()
self.update_gl() self.update_gl()
self.update_current() self.update_current()
@ -277,52 +323,9 @@ class PlotKPZ(PamhyrPlot):
def update_current(self): def update_current(self):
if self._current_data_update: if self._current_data_update:
ind = self._current_data self._colors, self._style = self.color_hightlight()
before = ind - 1 self.line_kp_zmin_zmax.set_colors(self._colors)
after = ind + 1 self.line_kp_zmin_zmax.set_linestyle(self._style)
self.before_plot_selected.set_visible(False)
self.plot_selected.set_visible(False)
self.after_plot_selected.set_visible(False)
if 0 <= before < self.data.number_profiles:
profile = self.data.profile(before)
if len(profile) > 0:
kp_i = profile.kp
z_min_i = profile.z_min()
z_max_i = profile.z_max()
self.before_plot_selected.set_data(
(kp_i, kp_i),
(z_min_i, z_max_i)
)
self.before_plot_selected.set_visible(True)
if 0 <= ind < self.data.number_profiles:
profile = self.data.profile(ind)
if len(profile) > 0:
kp_i = profile.kp
z_min_i = profile.z_min()
z_max_i = profile.z_max()
self.plot_selected.set_data(
(kp_i, kp_i),
(z_min_i, z_max_i)
)
self.plot_selected.set_visible(True)
if 0 <= after < self.data.number_profiles:
profile = self.data.profile(after)
if len(profile) > 0:
kp_i = profile.kp
z_min_i = profile.z_min()
z_max_i = profile.z_max()
self.after_plot_selected.set_data(
(kp_i, kp_i),
(z_min_i, z_max_i)
)
self.after_plot_selected.set_visible(True)
def update_gl(self): def update_gl(self):
if self._current_data_update: if self._current_data_update:
@ -334,12 +337,19 @@ class PlotKPZ(PamhyrPlot):
self.line_kp_zmin.set_data(kp, z_min) self.line_kp_zmin.set_data(kp, z_min)
self.line_kp_zmin_zmax.remove() # TODO comprendre à quoi sert ce bout de code
self.line_kp_zmin_zmax = self.canvas.axes.vlines( # ========>
x=kp, #self.line_kp_zmin_zmax.remove()
ymin=z_min, ymax=z_max, #self._colors, self._style = self.color_hightlight()
color='r', lw=1. #self.line_kp_zmin_zmax = self.canvas.axes.vlines(
) #x=kp,
#ymin=z_min,
#ymax=z_max,
#color=self._colors,
#linestyle = self._style,
#lw=1.
#)
# <========
z_complete = self.data.get_guidelines_z() z_complete = self.data.get_guidelines_z()
try: try:
@ -349,3 +359,9 @@ class PlotKPZ(PamhyrPlot):
) )
except Exception as e: except Exception as e:
logger.warning(f"Failed to update graphic KPZ: {e}") logger.warning(f"Failed to update graphic KPZ: {e}")
def update_lr(self):
for line in self.line_lr:
line[0].remove()
self.draw_lr()

View File

@ -55,7 +55,8 @@ class PlotXY(PamhyrPlot):
self.parent=parent self.parent=parent
self.line_xy_collection = None self.line_xy_collection = None
self._table=table self._table=table
self.colors = [] self._colors = []
self._style = []
def onpick(self, event): def onpick(self, event):
if event.mouseevent.inaxes != self.canvas.axes: if event.mouseevent.inaxes != self.canvas.axes:
@ -175,7 +176,6 @@ class PlotXY(PamhyrPlot):
self.draw_xy() self.draw_xy()
self.draw_lr() self.draw_lr()
self.draw_gl() self.draw_gl()
#self.draw_current()
self.idle() self.idle()
self._init = True self._init = True
@ -186,9 +186,10 @@ class PlotXY(PamhyrPlot):
for xy in zip(self.data.get_x(), self.data.get_y()): for xy in zip(self.data.get_x(), self.data.get_y()):
self.line_xy.append(np.column_stack(xy)) self.line_xy.append(np.column_stack(xy))
self.colors = self.color_hightlight() self._colors, self._style = self.color_hightlight()
self.line_xy_collection = collections.LineCollection(self.line_xy, self.line_xy_collection = collections.LineCollection(self.line_xy,
colors = self.colors, colors = self._colors,
linestyle = self._style,
picker=10) picker=10)
self.canvas.axes.add_collection(self.line_xy_collection) self.canvas.axes.add_collection(self.line_xy_collection)
@ -198,16 +199,18 @@ class PlotXY(PamhyrPlot):
(i.row() for i in self.parent.tableView.selectedIndexes()) (i.row() for i in self.parent.tableView.selectedIndexes())
) )
) )
colors = [self.color_plot for row in range(len(self.line_xy))] colors = [self.color_plot for row in range(len(self._data))]
style = ["-" for row in range(len(self._data))]
if len(rows) >0: if len(rows) >0:
for row in rows: for row in rows:
colors[row] = self.color_plot_current colors[row] = self.color_plot_current
if rows[0] > 0: if rows[0] > 0:
colors[rows[0]-1] = self.color_plot_previous colors[rows[0]-1] = self.color_plot_previous
if rows[-1] < len(self.line_xy)-1: style[rows[0]-1] = "--"
if rows[-1] < len(self._data)-1:
colors[rows[-1]+1] = self.color_plot_next colors[rows[-1]+1] = self.color_plot_next
return colors style[rows[-1]+1] = "--"
return colors, style
def draw_lr(self): def draw_lr(self):
lx = [] lx = []
@ -309,8 +312,9 @@ class PlotXY(PamhyrPlot):
def update_current(self): def update_current(self):
if self._current_data_update: if self._current_data_update:
self.colors = self.color_hightlight() self._colors, self._style = self.color_hightlight()
self.line_xy_collection.set_colors(self.colors) self.line_xy_collection.set_colors(self._colors)
self.line_xy_collection.set_linestyle(self._style)
def update_lr(self): def update_lr(self):
for line in self.line_lr: for line in self.line_lr:

View File

@ -385,6 +385,7 @@ class GeometryWindow(PamhyrWindow):
data=self._reach, data=self._reach,
trad=self._trad, trad=self._trad,
toolbar=self._toolbar_kpc, toolbar=self._toolbar_kpc,
table=self.find(QTableView, "tableView"),
parent=self parent=self
) )
self._plot_kpc.draw() self._plot_kpc.draw()