Pamhyr2/src/View/HydraulicStructures/PlotAC.py

131 lines
3.5 KiB
Python

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