diff --git a/src/View/HydraulicStructures/PlotAC.py b/src/View/HydraulicStructures/PlotAC.py new file mode 100644 index 0000000000000000000000000000000000000000..355842031e32a018bc1ddf7707c1d0b675a383ce --- /dev/null +++ b/src/View/HydraulicStructures/PlotAC.py @@ -0,0 +1,130 @@ +# PlotAC.py -- Pamhyr +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +from tools import timer +from View.Tools.PamhyrPlot import PamhyrPlot +from matplotlib import pyplot as plt + +from PyQt5.QtCore import ( + QCoreApplication +) + +_translate = QCoreApplication.translate + + +class PlotAC(PamhyrPlot): + def __init__(self, canvas=None, trad=None, toolbar=None, + river=None, reach=None, profile=None, + parent=None): + super(PlotAC, self).__init__( + canvas=canvas, + trad=trad, + data=river, + toolbar=toolbar, + parent=parent + ) + + self._current_reach = reach + self._current_profile = profile + + @property + def river(self): + return self.data + + @river.setter + def river(self, river): + self.data = river + + @timer + def draw(self, highlight=None): + self.canvas.axes.cla() + self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + + if self.data is None: + self.line_kp = None + return + + if self._current_reach is None: + self.line_kp = None + return + + reach = self._current_reach + + self.canvas.axes.set_xlabel( + _translate("MainWindow_reach", "X (m)"), + color='black', fontsize=11 + ) + self.canvas.axes.set_ylabel( + _translate("MainWindow_reach", "Elevation (m)"), + color='black', fontsize=11 + ) + + if self._current_profile is None: + self.line_kp = None + else: + + profile = self._current_profile + x = profile.get_station() + z = profile.z() + + self.canvas.axes.set_xlim( + left=min(x), right=max(x) + ) + self.line_kp, = self.canvas.axes.plot( + x, z, + linestyle="solid", + lw=1.8, + color='grey', + ) + + self.canvas.figure.tight_layout() + self.canvas.figure.canvas.draw_idle() + if self.toolbar is not None: + self.toolbar.update() + + def set_reach(self, reach): + self._current_reach = reach + self.update() + + def set_profile(self, profile): + self._current_profile = profile + self.update() + + def update(self): + + if self.line_kp is None: + self.draw() + else: + if self._current_reach is None or self._current_profile is None: + self.clear() + return + profile = self._current_profile + x = profile.get_station() + z = profile.z() + + self.line_kp.set_data(x, z) + + self.canvas.axes.set_xlim( + left=min(x), right=max(x) + ) + self.canvas.figure.canvas.draw_idle() + + def clear(self): + if self.line_kp is not None: + self.line_kp.set_data([],[]) + self.canvas.figure.canvas.draw_idle() diff --git a/src/View/HydraulicStructures/PlotKPC.py b/src/View/HydraulicStructures/PlotKPC.py new file mode 100644 index 0000000000000000000000000000000000000000..15a5a1a40f03569710bc3c8d63697912082b9051 --- /dev/null +++ b/src/View/HydraulicStructures/PlotKPC.py @@ -0,0 +1,159 @@ +# PlotKPC.py -- Pamhyr +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +from tools import timer +from View.Tools.PamhyrPlot import PamhyrPlot + +from PyQt5.QtCore import ( + QCoreApplication +) + +from matplotlib.collections import LineCollection + +_translate = QCoreApplication.translate + + +class PlotKPC(PamhyrPlot): + def __init__(self, canvas=None, trad=None, toolbar=None, + river=None, reach=None, profile=None, + parent=None): + super(PlotKPC, self).__init__( + canvas=canvas, + trad=trad, + data=river, + toolbar=toolbar, + parent=parent + ) + + self._current_reach = reach + self._current_profile = profile + + @property + def river(self): + return self.data + + @river.setter + def river(self, river): + self.data = river + + @timer + def draw(self, highlight=None): + self.canvas.axes.cla() + self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5) + + if self.data is None: + self.profile = None + self.line_kp_zmin_zmax = None + self.line_kp_zmin = None + return + + if self._current_reach is None: + self.profile = None + self.line_kp_zmin_zmax = None + self.line_kp_zmin = None + return + + reach = self._current_reach + + self.canvas.axes.set_ylabel( + _translate("MainWindow_reach", "Elevation (m)"), + color='black', fontsize=11 + ) + self.canvas.axes.set_xlabel( + _translate("MainWindow_reach", "KP (m)"), + color='black', fontsize=11 + ) + + kp = reach.reach.get_kp() + z_min = reach.reach.get_z_min() + z_max = reach.reach.get_z_max() + + self.canvas.axes.set_xlim( + left=min(kp), right=max(kp) + ) + + self.line_kp_zmin, = self.canvas.axes.plot( + kp, z_min, + color='grey', lw=1. + ) + + if len(kp) != 0: + + self.line_kp_zmin_zmax = self.canvas.axes.vlines( + x=kp, + ymin=z_min, ymax=z_max, + color='b', + lw=1. + ) + + if self._current_profile is None: + self.profile = None + else: + self.profile, = self.canvas.axes.plot( + [self._current_profile.kp, self._current_profile.kp], + [self._current_profile.z_min(), self._current_profile.z_max()], + color='red', lw=1. + ) + + + self.canvas.figure.tight_layout() + self.canvas.figure.canvas.draw_idle() + if self.toolbar is not None: + self.toolbar.update() + + def set_reach(self, reach): + self._current_reach = reach + self._current_profile = None + self.update() + + def set_profile(self, profile): + self._current_profile = profile + self.update_profil() + + def update(self): + self.draw() + + def update_profil(self): + reach = self._current_reach + kp = reach.reach.get_kp() + z_min = reach.reach.get_z_min() + z_max = reach.reach.get_z_max() + if self.profile is None: + self.draw() + else: + self.profile.set_data( + [self._current_profile.kp, self._current_profile.kp], + [self._current_profile.z_min(), self._current_profile.z_max()], + ) + self.canvas.figure.canvas.draw_idle() + + def clear(self): + if self.profile is not None: + self.profile.set_data([],[]) + if self.line_kp_zmin_zmax is not None: + self.line_kp_zmin_zmax.remove() + self.line_kp_zmin_zmax = None + if self.line_kp_zmin is not None: + self.line_kp_zmin.set_data([],[]) + self.canvas.figure.canvas.draw_idle() + + def clear_profile(self): + if self.profile is not None: + self.profile.set_data([],[]) + self.canvas.figure.canvas.draw_idle() + diff --git a/src/View/HydraulicStructures/Window.py b/src/View/HydraulicStructures/Window.py index 0620d4a4715c415cd14e7b045e560b40b66e07d2..2b0d1f8d911c3bbf493024a0fe69f43482cc1a2a 100644 --- a/src/View/HydraulicStructures/Window.py +++ b/src/View/HydraulicStructures/Window.py @@ -35,6 +35,12 @@ from PyQt5.QtWidgets import ( QHeaderView, QDoubleSpinBox, QVBoxLayout, QCheckBox ) +from View.Tools.Plot.PamhyrCanvas import MplCanvas +from View.Tools.Plot.PamhyrToolbar import PamhyrPlotToolbar + +from View.HydraulicStructures.PlotAC import PlotAC +from View.HydraulicStructures.PlotKPC import PlotKPC + from View.HydraulicStructures.Table import ( TableModel, ComboBoxDelegate ) @@ -112,21 +118,71 @@ class HydraulicStructuresWindow(PamhyrWindow): def setup_checkbox(self): self._checkbox = self.find(QCheckBox, f"checkBox") - self._set_checkbox_state + self._set_checkbox_state() def setup_plots(self): - return + self.canvas = MplCanvas(width=5, height=4, dpi=100) + self.canvas.setObjectName("canvas") + self.toolbar = PamhyrPlotToolbar( + self.canvas, self + ) + self.plot_layout = self.find(QVBoxLayout, "verticalLayout") + self.plot_layout.addWidget(self.toolbar) + self.plot_layout.addWidget(self.canvas) + + self.plot_kpc = PlotKPC( + canvas=self.canvas, + river=self._study.river, + reach=None, + profile=None, + toolbar=self.toolbar + ) + self.plot_kpc.draw() + + self.canvas_2 = MplCanvas(width=5, height=4, dpi=100) + self.canvas_2.setObjectName("canvas_2") + self.toolbar_2 = PamhyrPlotToolbar( + self.canvas_2, self + ) + self.plot_layout_2 = self.find(QVBoxLayout, "verticalLayout_2") + self.plot_layout_2.addWidget(self.toolbar_2) + self.plot_layout_2.addWidget(self.canvas_2) + + self.plot_ac = PlotAC( + canvas=self.canvas_2, + river=self._study.river, + reach=None, + profile=None, + toolbar=self.toolbar_2 + ) + self.plot_ac.draw() def setup_connections(self): self.find(QAction, "action_add").triggered.connect(self.add) self.find(QAction, "action_delete").triggered.connect(self.delete) - self.find(QAction, "action_edit").triggered.connect(self.edit) + #self.find(QAction, "action_edit").triggered.connect(self.edit) self._checkbox.stateChanged.connect(self._set_structure_state) table = self.find(QTableView, "tableView") table.selectionModel()\ .selectionChanged\ .connect(self._set_checkbox_state) + table.selectionModel()\ + .selectionChanged\ + .connect(self.update) + #self._delegate_kp.currentItemChanged.connect(lambda:x, print("toto")) + #self._delegate_reach.currentItemChanged.connect(lambda:x, print("titi")) + self._table.dataChanged.connect(self.update) + self._table.layoutChanged.connect(self.update) + + def index_selected(self): + table = self.find(QTableView, "tableView") + r = table.selectionModel()\ + .selectedRows() + if len(r)>0: + return r[0] + else: + return None def index_selected_row(self): table = self.find(QTableView, "tableView") @@ -204,5 +260,42 @@ class HydraulicStructuresWindow(PamhyrWindow): def _set_structure_state(self): row = self.index_selected_row() - #self._checkbox.setChecked(self._hs_lst.get(row).enabled) - self._hs_lst.get(row).enabled = self._checkbox.isChecked() + if row is None: + self._checkbox.setEnabled(False) + else: + self._hs_lst.get(row).enabled = self._checkbox.isChecked() + + def update(self): + rows = self.index_selected_rows() + + if len(rows) > 0 and len(self._hs_lst) > 0: + reach = self._hs_lst.get(rows[0]).input_reach + else: + reach=None + self.plot_kpc.clear() + self.plot_ac.clear() + return + + profile_kp = self._hs_lst.get(rows[0]).input_kp + if profile_kp is None or profile_kp == "Not associated": + profile = None + self.plot_ac.clear() + self.plot_kpc.clear_profile() + else: + profile = reach.reach.get_profiles_from_kp(float(profile_kp)) + + if reach is not None and reach != "Not associated": + self.plot_kpc.set_reach(reach) + self.plot_ac.set_reach(reach) + else: + self.plot_kpc.clear() + self.plot_ac.clear() + return + + + if profile is not None: + self.plot_kpc.set_profile(profile[0]) + self.plot_ac.set_profile(profile[0]) + else: + self.plot_ac.clear() + self.plot_kpc.clear_profile() diff --git a/src/View/Results/Window.py b/src/View/Results/Window.py index 75682ed557915fd4945b304e4e4e0e4b639b470a..9933f18989582b3ce73db51c0c27decface8d42a 100644 --- a/src/View/Results/Window.py +++ b/src/View/Results/Window.py @@ -97,7 +97,7 @@ class ResultsWindow(PamhyrWindow): self._additional_plot = {} self.setup_table() - self.setup_plot() + self.setup_plots() self.setup_slider() self.setup_statusbar() self.setup_connections() @@ -136,7 +136,7 @@ class ResultsWindow(PamhyrWindow): self._button_last = self.find(QPushButton, f"lastButton") self._timer = QTimer(self) - def setup_plot(self): + def setup_plots(self): self.canvas = MplCanvas(width=5, height=4, dpi=100) self.canvas.setObjectName("canvas") self.toolbar = PamhyrPlotToolbar(