PlotKPC.py 4.91 KiB
# -*- coding: utf-8 -*-

from tools import timer
from View.Plot.APlot import APlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate

class PlotKPC(APlot):
    def __init__(self, canvas=None, data=None, toolbar=None):
        super(PlotKPC, self).__init__(
            canvas=canvas,
            data=data,
            toolbar=toolbar
        )

        self.line_kp_zgl = []
        self.line_kp_zmin = None
        self.line_kp_zmin_zmax = None

        self.before_plot_selected = None
        self.plot_selected = None
        self.after_plot_selected = None

    @timer
    def draw(self):
        self.canvas.axes.cla()
        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
        self.canvas.axes.set_xlabel(
            _translate("MainWindow_reach", "Kp (m)"),
            color='green', fontsize=12
        )
        self.canvas.axes.set_ylabel(
            _translate("MainWindow_reach", "Cote (m)"),
            color='green', fontsize=12
        )

        kp = self.data.get_kp()
        z_min = self.data.get_z_min()
        z_max = self.data.get_z_max()

        self.line_kp_zmin_zmax = self.canvas.axes.vlines(
            x=kp,
            ymin=z_min, ymax=z_max,
            color='r', lw=1.
        )

        self.plot_selected, = self.canvas.axes.plot(
            (kp[0], kp[0]),
            (z_min[0], z_max[0]),
            color='b', lw=1.8
        )
        self.plot_selected.set_visible(False)

        self.before_plot_selected, = self.canvas.axes.plot(
            (kp[0], kp[0]),
            (z_min[0], z_max[0]),
            color='k', lw=1.6, linestyle='--'
        )
        self.before_plot_selected.set_visible(False)

        self.after_plot_selected, = self.canvas.axes.plot(
            (kp[0], kp[0]),
            (z_min[0], z_max[0]),
            color='m', lw=1.6, linestyle='--'
        )
        self.after_plot_selected.set_visible(False)

        kp = self.data.get_kp()
        self.line_kp_zgl = []
        for z in self.data.get_guidelines_z():
            # Is incomplete guideline?
            if len(z) != len(kp):
                continue

            self.line_kp_zgl.append(
                self.canvas.axes.plot(
                    kp, z, lw=1.
                )
            )

        self.line_kp_zmin, = self.canvas.axes.plot(
            kp, z_min,
            linestyle=":", lw=1.8,
            color='lightgrey'
        )

        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
        self.toolbar.update()

    @timer
    def update(self, ind=None):
        if ind is not None:
            before = ind - 1
            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:
                kp_i = self.data.profile(before).kp
                z_min_i = self.data.profile(before).z_min()
                z_max_i = self.data.profile(before).z_max()

                self.before_plot_selected.set_data(
                    (kp_i, kp_i),
                    (z_min_i, z_max_i)
                )
                self.before_plot_selected.set_visible(True)

            if 0 <= ind < self.data.number_profiles:
                kp_i = self.data.profile(ind).kp
                z_min_i = self.data.profile(ind).z_min()
                z_max_i = self.data.profile(ind).z_max()

                self.plot_selected.set_data(
                    (kp_i, kp_i),
                    (z_min_i, z_max_i)
                )
                self.plot_selected.set_visible(True)

            if 0 <= after < self.data.number_profiles:
                kp_i = self.data.profile(after).kp
                z_min_i = self.data.profile(after).z_min()
                z_max_i = self.data.profile(after).z_max()

                self.after_plot_selected.set_data(
                    (kp_i, kp_i),
                    (z_min_i, z_max_i)
                )
                self.after_plot_selected.set_visible(True)

            self.canvas.figure.canvas.draw_idle()
        else:
            kp = self.data.get_kp()
            z_min = self.data.get_z_min()
            z_max = self.data.get_z_max()

            self.line_kp_zmin.set_data(kp, z_min)

            self.line_kp_zmin_zmax.remove()
            self.line_kp_zmin_zmax = self.canvas.axes.vlines(
                x=kp,
                ymin=z_min, ymax=z_max,
                color='r', lw=1.
            )

            z_complete = self.data.get_guidelines_z()
            try:
                for i in range(len(self.line_kp_zgl)):
                    self.line_kp_zgl[i][0].set_data(
                        kp, z_complete[i]
                    )
            except Exception as e:
                print(f"Failed to update graphic 2: {e}")

            self.canvas.axes.autoscale_view(True, True, True)
            self.canvas.figure.canvas.draw_idle()