Pamhyr2/src/View/HydraulicStructures/PlotKPC.py

158 lines
4.5 KiB
Python

# 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()