Commit 052faed4 authored by Theophile Terraz's avatar Theophile Terraz
Browse files

PlotXY restructuration

Showing with 163 additions and 96 deletions
+163 -96
...@@ -18,22 +18,27 @@ ...@@ -18,22 +18,27 @@
from tools import timer, trace from tools import timer, trace
from View.Tools.PamhyrPlot import PamhyrPlot from View.Tools.PamhyrPlot import PamhyrPlot
from matplotlib import collections
import numpy as np
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QCoreApplication QCoreApplication, Qt, QItemSelectionModel,
QItemSelection, QItemSelectionRange,
) )
from PyQt5.QtWidgets import QApplication
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
class PlotXY(PamhyrPlot): class PlotXY(PamhyrPlot):
def __init__(self, canvas=None, trad=None, data=None, toolbar=None, def __init__(self, canvas=None, trad=None, data=None, toolbar=None,
parent=None): table=None, parent=None):
super(PlotXY, self).__init__( super(PlotXY, self).__init__(
canvas=canvas, canvas=canvas,
trad=trad, trad=trad,
data=data, data=data,
toolbar=toolbar, toolbar=toolbar,
table=table,
parent=parent parent=parent
) )
...@@ -47,6 +52,112 @@ class PlotXY(PamhyrPlot): ...@@ -47,6 +52,112 @@ class PlotXY(PamhyrPlot):
self.plot_selected = None self.plot_selected = None
self.after_plot_selected = None self.after_plot_selected = None
self._rect_select = 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 @timer
def draw(self): def draw(self):
...@@ -64,33 +175,39 @@ class PlotXY(PamhyrPlot): ...@@ -64,33 +175,39 @@ 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.draw_current()
self.idle() self.idle()
self._init = True self._init = True
def draw_xy(self): 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 = [] self.line_xy = []
for x, y, kp in zip(self.data.get_x(), for xy in zip(self.data.get_x(), self.data.get_y()):
self.data.get_y(), self.line_xy.append(np.column_stack(xy))
kp):
line = self.canvas.axes.plot( self.colors = self.color_hightlight()
x, y, self.line_xy_collection = collections.LineCollection(self.line_xy,
color=color_hightlight(kp), colors = self.colors,
**self.plot_default_kargs 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): def draw_lr(self):
lx = [] lx = []
...@@ -137,33 +254,6 @@ class PlotXY(PamhyrPlot): ...@@ -137,33 +254,6 @@ class PlotXY(PamhyrPlot):
self.line_gl.append(line) self.line_gl.append(line)
ind += 1 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 @timer
def update(self): def update(self):
if not self._init: if not self._init:
...@@ -184,21 +274,24 @@ class PlotXY(PamhyrPlot): ...@@ -184,21 +274,24 @@ class PlotXY(PamhyrPlot):
x_complete = list(self.data.get_guidelines_x()) x_complete = list(self.data.get_guidelines_x())
y_complete = list(self.data.get_guidelines_y()) y_complete = list(self.data.get_guidelines_y())
for i in range(self.data.number_profiles): # TODO comprendre à quoi sert ce bout de code
if i < len(self.line_xy): # ========>
self.line_xy[i][0].set_data( #for i in range(self.data.number_profiles):
self.data.profile(i).x(), #if i < len(self.line_xy):
self.data.profile(i).y() #self.line_xy[i][0].set_data(
) #self.data.profile(i).x(),
else: #self.data.profile(i).y()
self.line_xy.append( #)
self.canvas.axes.plot( #else:
self.data.profile(i).x(), #self.line_xy.append(
self.data.profile(i).y(), #self.canvas.axes.plot(
color='r', #self.data.profile(i).x(),
**self.plot_default_kargs #self.data.profile(i).y(),
) #color='r',
) #**self.plot_default_kargs
#)
#)
# <========
for i in range(len(x_complete)): for i in range(len(x_complete)):
if i < len(self.line_gl): if i < len(self.line_gl):
...@@ -216,32 +309,8 @@ class PlotXY(PamhyrPlot): ...@@ -216,32 +309,8 @@ class PlotXY(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.color_hightlight()
before = ind - 1 self.line_xy_collection.set_colors(self.colors)
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)
def update_lr(self): def update_lr(self):
for line in self.line_lr: for line in self.line_lr:
......
...@@ -158,7 +158,6 @@ class Plot(PamhyrPlot): ...@@ -158,7 +158,6 @@ class Plot(PamhyrPlot):
if self._table is not None: if self._table is not None:
self._table.blockSignals(True) self._table.blockSignals(True)
self._table.setFocus() self._table.setFocus()
selection = self._table.selectionModel() selection = self._table.selectionModel()
index = QItemSelection() index = QItemSelection()
if len(ind) > 0: if len(ind) > 0:
...@@ -211,11 +210,8 @@ class Plot(PamhyrPlot): ...@@ -211,11 +210,8 @@ class Plot(PamhyrPlot):
def dist_mouse(point): def dist_mouse(point):
x, y = point[1] x, y = point[1]
d = sqrt( d2 = ((mx - x) / ratio) ** 2 + ((my - y) ** 2)
(((mx - x) / ratio) ** 2) + return d2
((my - y) ** 2)
)
return d
closest = min( closest = min(
points, key=dist_mouse points, key=dist_mouse
......
...@@ -363,7 +363,9 @@ class GeometryWindow(PamhyrWindow): ...@@ -363,7 +363,9 @@ class GeometryWindow(PamhyrWindow):
canvas=self._canvas_xy, canvas=self._canvas_xy,
data=self._reach, data=self._reach,
trad=self._trad, trad=self._trad,
toolbar=self._toolbar_xy toolbar=self._toolbar_xy,
table=self.find(QTableView, "tableView"),
parent=self
) )
self._plot_xy.draw() self._plot_xy.draw()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment