interactive selection in results

0.0.9
Theophile Terraz 2024-06-13 18:20:46 +02:00
parent 88dd9cf15a
commit 7b8191e2f0
2 changed files with 120 additions and 14 deletions

View File

@ -22,10 +22,14 @@ from functools import reduce
from tools import timer, trace
from View.Tools.PamhyrPlot import PamhyrPlot
import numpy as np
from matplotlib import collections
from PyQt5.QtCore import (
QCoreApplication
QCoreApplication, Qt, QItemSelectionModel,
QItemSelection, QItemSelectionRange
)
from PyQt5.QtWidgets import QApplication, QTableView
_translate = QCoreApplication.translate
@ -57,6 +61,74 @@ class PlotXY(PamhyrPlot):
self.label_y = _translate("Results", "Y (m)")
self._isometric_axis = True
self._tablemodel = parent._table["profile"]
self._table = parent.find(QTableView, f"tableView_profile")
def onpick(self, event):
print(event.mouseevent.button.value)
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
ind, point = self._closest_section(event)
if self._table is None:
return
self._select_in_table([ind])
self._table.blockSignals(False)
return
def _closest_section(self, event):
axes = self.canvas.axes
mx = event.mouseevent.xdata
my = event.mouseevent.ydata
bx, by = axes.get_xlim(), axes.get_ylim()
ratio = (bx[0] - bx[1]) / (by[0] - by[1])
segments = event.artist.get_segments()
ind = event.ind
points = []
for i in ind:
points = points + [[i, j] for j in segments[i]]
def dist_mouse(point):
x, y = point[1]
d2 = (((mx - x) / ratio) ** 2) + ((my - y) ** 2)
return d2
closest = min(
points, key=dist_mouse
)
return closest
def _select_in_table(self, ind):
if self._table is None:
return
self._table.setFocus()
selection = self._table.selectionModel()
index = QItemSelection()
if len(ind) == 0:
return
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))
@property
def results(self):
@ -90,18 +162,51 @@ class PlotXY(PamhyrPlot):
self._init = False
return
self.line_xy = [
self.canvas.axes.plot(
x, y,
color=self.color_plot_river_bottom,
**self.plot_default_kargs
)
for x, y, kp in zip(
reach.geometry.get_x(),
reach.geometry.get_y(),
reach.geometry.get_kp()
)
]
#self.line_xy = [
#self.canvas.axes.plot(
#x, y,
#color=self.color_plot_river_bottom,
#**self.plot_default_kargs
#)
#for x, y, kp in zip(
#reach.geometry.get_x(),
#reach.geometry.get_y(),
#reach.geometry.get_kp()
#)
#]
self.line_xy = []
for xy in zip(reach.geometry.get_x(), reach.geometry.get_y()):
self.line_xy.append(np.column_stack(xy))
self._colors, self._style = self.color_hightlight()
self.line_xy_collection = collections.LineCollection(
self.line_xy,
colors=self._colors,
linestyle=self._style,
picker=10
)
self.canvas.axes.add_collection(self.line_xy_collection)
def color_hightlight(self):
reach = self.results.river.reach(self._current_reach_id)
#rows = sorted(list(
#set(
#(i.row() for i in self._current_profile_id)
#)
#))
rows=[self._current_profile_id]
colors = [self.color_plot for row in range(reach.geometry.number_profiles)]
style = ["-" for row in range(reach.geometry.number_profiles)]
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] < reach.geometry.number_profiles-1:
colors[rows[-1]+1] = self.color_plot_next
style[rows[-1]+1] = "--"
return colors, style
def draw_guide_lines(self, reach):
x_complete = reach.geometry.get_guidelines_x()

View File

@ -176,7 +176,8 @@ class ResultsWindow(PamhyrWindow):
profile_id=0,
trad=self._trad,
toolbar=self.toolbar,
display_current=True
display_current=True,
parent=self
)
self.plot_xy.draw()