diff --git a/src/View/Geometry/PlotXY.py b/src/View/Geometry/PlotXY.py index a60d9a967b1c522732361d1788d6517317cf89ae..b4d106e2411cb3fbd6d66dc7f61f8ad281e742d4 100644 --- a/src/View/Geometry/PlotXY.py +++ b/src/View/Geometry/PlotXY.py @@ -18,22 +18,27 @@ from tools import timer, trace from View.Tools.PamhyrPlot import PamhyrPlot +from matplotlib import collections +import numpy as np from PyQt5.QtCore import ( - QCoreApplication + QCoreApplication, Qt, QItemSelectionModel, + QItemSelection, QItemSelectionRange, ) +from PyQt5.QtWidgets import QApplication _translate = QCoreApplication.translate class PlotXY(PamhyrPlot): def __init__(self, canvas=None, trad=None, data=None, toolbar=None, - parent=None): + table=None, parent=None): super(PlotXY, self).__init__( canvas=canvas, trad=trad, data=data, toolbar=toolbar, + table=table, parent=parent ) @@ -47,6 +52,112 @@ class PlotXY(PamhyrPlot): self.plot_selected = None self.after_plot_selected = None self._rect_select = None + self.parent=parent + self.line_xy_collection = None + self._table=table + self.colors = [] + + 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 + + ind, point = self._closest_section(event) + print(ind) + if self._table is not None: + self._table.blockSignals(True) + 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._table.blockSignals(False) + + #self.update() + 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 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 def draw(self): @@ -64,33 +175,39 @@ class PlotXY(PamhyrPlot): self.draw_xy() self.draw_lr() self.draw_gl() - self.draw_current() + #self.draw_current() self.idle() self._init = True def draw_xy(self): - kp = self.data.get_kp_complete_profiles() - - kp_min, kp_max = (-1, -1) - if self._highlight_data is not None: - kp_min, kp_max = self._highlight_data - - def color_hightlight(kp): - if kp_min <= kp <= kp_max: - return self.color_plot_highlight - return self.color_plot self.line_xy = [] - for x, y, kp in zip(self.data.get_x(), - self.data.get_y(), - kp): - line = self.canvas.axes.plot( - x, y, - color=color_hightlight(kp), - **self.plot_default_kargs + for xy in zip(self.data.get_x(), self.data.get_y()): + self.line_xy.append(np.column_stack(xy)) + + self.colors = self.color_hightlight() + self.line_xy_collection = collections.LineCollection(self.line_xy, + colors = self.colors, + picker=10) + self.canvas.axes.add_collection(self.line_xy_collection) + + def color_hightlight(self): + rows = list( + set( + (i.row() for i in self.parent.tableView.selectedIndexes()) ) - self.line_xy.append(line) + ) + colors = [self.color_plot for row in range(len(self.line_xy))] + 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 + if rows[-1] < len(self.line_xy)-1: + colors[rows[-1]+1] = self.color_plot_next + return colors + def draw_lr(self): lx = [] @@ -137,33 +254,6 @@ class PlotXY(PamhyrPlot): self.line_gl.append(line) ind += 1 - def draw_current(self): - # Previous profile - self.before_plot_selected, = self.canvas.axes.plot( - self.data.profile(0).x(), - self.data.profile(0).y(), - color=self.color_plot_previous, linestyle="--", - **self.plot_default_kargs - ) - self.before_plot_selected.set_visible(False) - - # Current profile - self.plot_selected, = self.canvas.axes.plot( - self.data.profile(0).x(), - self.data.profile(0).y(), - color=self.color_plot_current, **self.plot_default_kargs - ) - self.plot_selected.set_visible(False) - - # Next profile - self.after_plot_selected, = self.canvas.axes.plot( - self.data.profile(0).x(), - self.data.profile(0).y(), - color=self.color_plot_next, linestyle='--', - **self.plot_default_kargs - ) - self.after_plot_selected.set_visible(False) - @timer def update(self): if not self._init: @@ -184,21 +274,24 @@ class PlotXY(PamhyrPlot): x_complete = list(self.data.get_guidelines_x()) y_complete = list(self.data.get_guidelines_y()) - for i in range(self.data.number_profiles): - if i < len(self.line_xy): - self.line_xy[i][0].set_data( - self.data.profile(i).x(), - self.data.profile(i).y() - ) - else: - self.line_xy.append( - self.canvas.axes.plot( - self.data.profile(i).x(), - self.data.profile(i).y(), - color='r', - **self.plot_default_kargs - ) - ) + # TODO comprendre à quoi sert ce bout de code + # ========> + #for i in range(self.data.number_profiles): + #if i < len(self.line_xy): + #self.line_xy[i][0].set_data( + #self.data.profile(i).x(), + #self.data.profile(i).y() + #) + #else: + #self.line_xy.append( + #self.canvas.axes.plot( + #self.data.profile(i).x(), + #self.data.profile(i).y(), + #color='r', + #**self.plot_default_kargs + #) + #) + # <======== for i in range(len(x_complete)): if i < len(self.line_gl): @@ -216,32 +309,8 @@ class PlotXY(PamhyrPlot): def update_current(self): if self._current_data_update: - ind = self._current_data - before = ind - 1 - after = ind + 1 - - 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: - self.before_plot_selected.set_data( - self.data.profile(before).x(), - self.data.profile(before).y() - ) - self.before_plot_selected.set_visible(True) - - if 0 <= ind < self.data.number_profiles: - self.plot_selected.set_data(self.data.profile(ind).x(), - self.data.profile(ind).y()) - self.plot_selected.set_visible(True) - - if 0 <= after < self.data.number_profiles: - self.after_plot_selected.set_data( - self.data.profile(after).x(), - self.data.profile(after).y() - ) - self.after_plot_selected.set_visible(True) + self.colors = self.color_hightlight() + self.line_xy_collection.set_colors(self.colors) def update_lr(self): for line in self.line_lr: diff --git a/src/View/Geometry/Profile/Plot.py b/src/View/Geometry/Profile/Plot.py index fdb153035c3b7cf8d6c0445e1c236a6a4694554e..7b7815add81b0d232a6fcd3d1fdfc9ab6e741686 100644 --- a/src/View/Geometry/Profile/Plot.py +++ b/src/View/Geometry/Profile/Plot.py @@ -158,7 +158,6 @@ class Plot(PamhyrPlot): if self._table is not None: self._table.blockSignals(True) self._table.setFocus() - selection = self._table.selectionModel() index = QItemSelection() if len(ind) > 0: @@ -211,11 +210,8 @@ class Plot(PamhyrPlot): def dist_mouse(point): x, y = point[1] - d = sqrt( - (((mx - x) / ratio) ** 2) + - ((my - y) ** 2) - ) - return d + d2 = ((mx - x) / ratio) ** 2 + ((my - y) ** 2) + return d2 closest = min( points, key=dist_mouse diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py index 020c29b4bebea0d378e4cc4cf23ea4e305bc1e89..830f1ded262b56cd03ee7b8f9de668b9b45fe0ca 100644 --- a/src/View/Geometry/Window.py +++ b/src/View/Geometry/Window.py @@ -363,7 +363,9 @@ class GeometryWindow(PamhyrWindow): canvas=self._canvas_xy, data=self._reach, trad=self._trad, - toolbar=self._toolbar_xy + toolbar=self._toolbar_xy, + table=self.find(QTableView, "tableView"), + parent=self ) self._plot_xy.draw()