diff --git a/src/Model/Geometry/ProfileXYZ.py b/src/Model/Geometry/ProfileXYZ.py index a495a306f74665e243653f93709742b5350e72c3..043f43b5d7e98d0bb44670a615343bc719c9c0fd 100644 --- a/src/Model/Geometry/ProfileXYZ.py +++ b/src/Model/Geometry/ProfileXYZ.py @@ -3,6 +3,8 @@ import numpy as np from typing import List +from tools import timer + from Model.Geometry.Profile import Profile from Model.Geometry.PointXYZ import PointXYZ from Model.Geometry.Vector_1d import Vector1d @@ -143,6 +145,7 @@ class ProfileXYZ(Profile): return last_point + @timer def get_station(self) -> np.ndarray: """Projection of the points of the profile on a plane. @@ -153,7 +156,7 @@ class ProfileXYZ(Profile): Projection of the points of the profile on a plane. """ if self.nb_points < 3: - return np.array(np.nan) + return None else: first_named_point = None index_first_named_point = None @@ -168,7 +171,7 @@ class ProfileXYZ(Profile): first_named_point = point break - for point in self._points[::-1]: + for point in reversed(self._points): if point.point_is_named(): last_named_point = point break @@ -183,8 +186,7 @@ class ProfileXYZ(Profile): vector = Vector1d(first_named_point, last_named_point) normalized_direction_vec = vector.normalized_direction_vector() else: - vector = Vector1d(first_point_not_nan, - last_point_not_nan) + vector = Vector1d(first_point_not_nan, last_point_not_nan) normalized_direction_vec = vector.normalized_direction_vector() for point in self._points: @@ -194,11 +196,9 @@ class ProfileXYZ(Profile): normalized_direction_vec[1] * yi) station.append(station_i) - ret = np.array(station) - constant = ret[index_first_named_point] + constant = station[index_first_named_point] elif first_named_point is None: - vector = Vector1d(first_point_not_nan, - last_point_not_nan) + vector = Vector1d(first_point_not_nan, last_point_not_nan) normalized_direction_vec = vector.normalized_direction_vector() for point in self._points: @@ -208,8 +208,13 @@ class ProfileXYZ(Profile): normalized_direction_vec[1] * yi) station.append(station_i) - ret = np.array(station) - index_profile_z_min = np.where(np.array(self.z) == self.z_min)[0][0] - constant = ret[index_profile_z_min] + z_min = self.z_min() + index_profile_z_min = list( + filter( + lambda z: z[1] == z_min, + enumerate(self.z()) + ) + )[0] + constant = station[index_profile_z_min[0]] - return (ret - constant) + return list(map(lambda s: s - constant, station)) diff --git a/src/View/Geometry/GeometryWindow.py b/src/View/Geometry/GeometryWindow.py index aee955a0eac3ad131bda7bbccf724fd050f2ab15..8a6929b1cda11c49f61d5393b6dddc892aeee82f 100644 --- a/src/View/Geometry/GeometryWindow.py +++ b/src/View/Geometry/GeometryWindow.py @@ -80,10 +80,9 @@ class GeometryWindow(QMainWindow, WindowToolKit): self.tableView.setItemDelegate(Delegate()) def setup_plots(self): - if self._reach.number_profiles != 0: - self.plot_xy() - self.plot_kpc() - self.plot_ac() + self.plot_xy() + self.plot_kpc() + self.plot_ac() def setup_connections(self): self.ui.btn_open.clicked.connect(self.open_file_dialog) diff --git a/src/View/Geometry/PlotAC.py b/src/View/Geometry/PlotAC.py index 1dfb9bbc1bd7b92fc20996c1f9038b90c066fa45..56f96e391b1dd7f6f6ca03f78c3b4f6edda9e240 100644 --- a/src/View/Geometry/PlotAC.py +++ b/src/View/Geometry/PlotAC.py @@ -33,6 +33,12 @@ class PlotAC(APlot): @timer def draw(self): + self.canvas.axes.cla() + self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + + if self.data.number_profiles == 0: + return + selected_profile = 0 station = self.data.profile(selected_profile).get_station() station_plus_1 = self.data.profile(selected_profile + 1).get_station() @@ -40,8 +46,6 @@ class PlotAC(APlot): elevation_i_plus_1 = self.data.profile(selected_profile + 1).z() gl = self.data.profile(selected_profile).names() - self.canvas.axes.cla() - self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) self.canvas.axes.set_xlabel( _translate("MainWindow_reach", "Abscisse en travers (m)"), color='green', fontsize=12 @@ -130,6 +134,8 @@ class PlotAC(APlot): self.canvas.figure.canvas.draw_idle() self.toolbar.update() + self._init = True + def update_full(self): selected_profile = 0 station = self.data.profile(selected_profile).get_station() @@ -285,6 +291,10 @@ class PlotAC(APlot): @timer def update(self, ind=None): + if self._init == False: + self.draw() + return + if ind is not None: before = ind - 1 after = ind + 1 diff --git a/src/View/Geometry/PlotKPC.py b/src/View/Geometry/PlotKPC.py index 8a5d7364305087c0582e0fdc7456110d058cf805..374b963e2a4d0d9049802ce741f5d4390935a1f4 100644 --- a/src/View/Geometry/PlotKPC.py +++ b/src/View/Geometry/PlotKPC.py @@ -29,6 +29,10 @@ class PlotKPC(APlot): def draw(self): self.canvas.axes.cla() self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + + if self.data.number_profiles == 0: + return + self.canvas.axes.set_xlabel( _translate("MainWindow_reach", "Kp (m)"), color='green', fontsize=12 @@ -92,8 +96,14 @@ class PlotKPC(APlot): self.canvas.figure.canvas.draw_idle() self.toolbar.update() + self._init = True + @timer def update(self, ind=None): + if self._init == False: + self.draw() + return + if ind is not None: before = ind - 1 after = ind + 1 diff --git a/src/View/Geometry/PlotXY.py b/src/View/Geometry/PlotXY.py index fdb5fcc112807ecd7ef88d0143d5a675d2a6a05b..9393ae79d27f79232ac31ce71b170a0e891f9f4b 100644 --- a/src/View/Geometry/PlotXY.py +++ b/src/View/Geometry/PlotXY.py @@ -29,6 +29,10 @@ class PlotXY(APlot): self.canvas.axes.cla() self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + if self.data.number_profiles == 0: + self._init = False + return + # Axes self.canvas.axes.set_xlabel( _translate("MainWindow_reach", "X (m)"), @@ -91,8 +95,14 @@ class PlotXY(APlot): self.canvas.figure.canvas.draw_idle() self.toolbar.update() + self._init = True + @timer def update(self, ind=None): + if self._init == False: + self.draw() + return + if ind is not None: before = ind - 1 after = ind + 1 diff --git a/src/View/Geometry/Profile/Plot.py b/src/View/Geometry/Profile/Plot.py index 85bb19b5e82c5ce99ec141344ad9c8b0704fe0ed..100426f871206f3c4733d00ec174be424ee06f9b 100644 --- a/src/View/Geometry/Profile/Plot.py +++ b/src/View/Geometry/Profile/Plot.py @@ -29,94 +29,97 @@ class Plot(APlot): @timer def draw(self): + self.canvas.axes.cla() + self.canvas.axes.grid( + color='grey', linestyle='--', linewidth=0.5 + ) + x = self.data.get_station() y = self.data.z() - gl = self.data.name x_carto = self.data.x() y_carto = self.data.y() - self.canvas.axes.cla() - self.canvas.axes.grid( - color='grey', linestyle='--', linewidth=0.5 - ) + if (len(x_carto) < 3 or len(y_carto) < 3 or + len(x) < 3): + # Noting to do in this case + return - if (len(x_carto) >= 3 and - len(y_carto) >= 3 and - len(x) >= 3): - self.profile_line2D, = self.canvas.axes.plot( - x, y, color='r', lw=1.5, - markersize=7, marker='+', - picker=30 - ) - self.canvas.axes.set_xlabel( - _translate("MainWindowProfile", - "Abscisse en travers (m)"), - color='black', fontsize=10 - ) - self.canvas.axes.set_ylabel( - _translate("MainWindowProfile", "Cote (m)"), - color='black', fontsize=10 - ) + gl = map(lambda p: p.name, self.data.points) - # Add label on graph - self.annotation = [] - for i, txt in enumerate(list(gl)): - annotation = self.canvas.axes.annotate( - txt, (x[i], y[i]), - horizontalalignment='left', - verticalalignment='top', - annotation_clip=True, - fontsize=10, color='black' - ) - annotation.set_position((x[i], y[i])) - annotation.set_color("black") - self.annotation.append(annotation) - - al = 8. - arrowprops = dict( - clip_on=True, - headwidth=5., - facecolor='k' - ) - kwargs = dict( - xycoords='axes fraction', - textcoords='offset points', - arrowprops=arrowprops, + self.profile_line2D, = self.canvas.axes.plot( + x, y, color='r', lw=1.5, + markersize=7, marker='+', + picker=30 + ) + self.canvas.axes.set_xlabel( + _translate("MainWindowProfile", + "Abscisse en travers (m)"), + color='black', fontsize=10 + ) + self.canvas.axes.set_ylabel( + _translate("MainWindowProfile", "Cote (m)"), + color='black', fontsize=10 + ) + + # Add label on graph + self.annotation = [] + for i, name in enumerate(list(gl)): + annotation = self.canvas.axes.annotate( + name, (x[i], y[i]), + horizontalalignment='left', + verticalalignment='top', + annotation_clip=True, + fontsize=10, color='black' ) + annotation.set_position((x[i], y[i])) + annotation.set_color("black") + self.annotation.append(annotation) + + al = 8. + arrowprops = dict( + clip_on=True, + headwidth=5., + facecolor='k' + ) + kwargs = dict( + xycoords='axes fraction', + textcoords='offset points', + arrowprops=arrowprops, + ) - self.canvas.axes.annotate("", (1, 0), xytext=(-al, 0), **kwargs) - self.canvas.axes.annotate("", (0, 1), xytext=(0, -al), **kwargs) + self.canvas.axes.annotate("", (1, 0), xytext=(-al, 0), **kwargs) + self.canvas.axes.annotate("", (0, 1), xytext=(0, -al), **kwargs) - self.canvas.axes.spines[['top', 'right']].set_color('none') - self.canvas.axes.yaxis.tick_left() - self.canvas.axes.xaxis.tick_bottom() - self.canvas.axes.set_facecolor('#F9F9F9') - self.canvas.figure.patch.set_facecolor('white') + self.canvas.axes.spines[['top', 'right']].set_color('none') + self.canvas.axes.yaxis.tick_left() + self.canvas.axes.xaxis.tick_bottom() + self.canvas.axes.set_facecolor('#F9F9F9') + self.canvas.figure.patch.set_facecolor('white') - self.onpick_event = OnpickEvent( - self.canvas.axes, - x, y, x_carto, y_carto, - self._table - ) - self.canvas.figure.canvas\ - .mpl_connect( - 'pick_event', - self.onpick_event.onpick - ) - - self.onclick_event = OnpickEvent( - self.canvas.axes, - x, y, x_carto, y_carto, - self._table - ) - self.canvas.figure.canvas\ - .mpl_connect( - 'button_press_event', - self.onclick_event.onclick - ) - - self.canvas.figure.tight_layout() - self.canvas.figure.canvas.draw_idle() + self.onpick_event = OnpickEvent( + self.canvas.axes, + x, y, x_carto, y_carto, + self._table + ) + self.canvas.figure.canvas\ + .mpl_connect( + 'pick_event', + self.onpick_event.onpick + ) + + self.onclick_event = OnpickEvent( + self.canvas.axes, + x, y, x_carto, y_carto, + self._table + ) + self.canvas.figure.canvas\ + .mpl_connect( + 'button_press_event', + self.onclick_event.onclick + ) + + self.canvas.figure.tight_layout() + self.canvas.figure.canvas.draw_idle() @timer def update(self, ind=None): diff --git a/src/View/Geometry/Profile/ProfileWindow.py b/src/View/Geometry/Profile/ProfileWindow.py index 937b6e5f9c6f031da2487844c80fadb3d09273d3..e84751e0a61eaacff55ad6c9d5773a8a71d4d73d 100644 --- a/src/View/Geometry/Profile/ProfileWindow.py +++ b/src/View/Geometry/Profile/ProfileWindow.py @@ -131,6 +131,11 @@ class ProfileWindow(QMainWindow): self.ui.tableView.model().blockSignals(False) + def index_selected_row(self): + return self.ui.tableView\ + .selectionModel()\ + .selectedRows()[0]\ + .row() def insert_row(self): if len(self.ui.tableView.selectedIndexes()) == 0: diff --git a/src/View/Geometry/Profile/qtableview_profile.py b/src/View/Geometry/Profile/qtableview_profile.py index 0fa62ee9e0f45e1e795718e5dc3939c7ae02baee..d103e35b15b3a9c76ac0236f22108e408dcc07d1 100644 --- a/src/View/Geometry/Profile/qtableview_profile.py +++ b/src/View/Geometry/Profile/qtableview_profile.py @@ -55,14 +55,15 @@ class TableEditableModel(QAbstractTableModel): elif index.column() == 3: value = self._profile.point(index.row()).name elif index.column() == 4: - value = self._profile.get_station()[index.row()] + station = self._profile.get_station() + if station is None: + return "-" + else: + value = station[index.row()] + return f"{value:.3f}" if 0 <= index.column() < 3: return f"{value:.4f}" - elif index.column() == 4: - if np.isnan(value): - return "-" - return f"{value:.3f}" return f"{value}" diff --git a/src/View/Geometry/qtableview_reach.py b/src/View/Geometry/qtableview_reach.py index 3622ceb86680b6be66014c7b2485b7a398e70e29..56405933279e1c0102cc4a1094c48bd902cf4933 100644 --- a/src/View/Geometry/qtableview_reach.py +++ b/src/View/Geometry/qtableview_reach.py @@ -68,10 +68,10 @@ class TableEditableModel(QAbstractTableModel): .lower() if (name == "upstream" or name == "up" or name == _translate("Geometry", "upstream")): - return QtGui.QColor("Green") + return QColor("Green") elif (name == "downstream" or name == "down" or name == _translate("Geometry", "downstream")): - return QtGui.QColor("Red") + return QColor("Red") return QVariant() diff --git a/src/View/Plot/APlot.py b/src/View/Plot/APlot.py index baf544f333f25a4176df35294960575d033fdf72..cca27087aeb411db64e4e27de7f6e357ec613194 100644 --- a/src/View/Plot/APlot.py +++ b/src/View/Plot/APlot.py @@ -4,6 +4,8 @@ class APlot(object): def __init__(self, canvas=None, data=None, toolbar=None): super(APlot, self).__init__() + self._init = False + self._canvas = canvas self._data = data self._toolbar = toolbar diff --git a/src/tools.py b/src/tools.py index e4800f74e4cbce200fb2fa4682fe6bb3c1898d89..c26e9fe9385102b9c5e275b2e78aedc3764ea6da 100644 --- a/src/tools.py +++ b/src/tools.py @@ -56,7 +56,9 @@ def timer(func): try: value = func(*args, **kwargs) except Exception as e: - print(f"{e}") + print(f"[{Fore.RED}ERROR{Style.RESET_ALL}]" + + f"[{func.__module__}.{Fore.GREEN}{func.__qualname__}{Style.RESET_ALL}]: " + + f"{Fore.RED}{e}{Style.RESET_ALL}") end_time = time.perf_counter() run_time = end_time - start_time