Forked from HYCAR-Hydro / airGR
Source project has a limited visibility.
Plot.py 3.67 KiB
# -*- coding: utf-8 -*-

import logging

from tools import timer, trace
from View.Plot.APlot import APlot
from View.Plot.mpl_canvas_onpick_event import OnpickEvent

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate

logger = logging.getLogger()

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

        self._table = table

        self.line_xy = []
        self.line_gl = []

        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
        )

        x = self.data.get_station()
        y = self.data.z()
        x_carto = self.data.x()
        y_carto = self.data.y()

        if (len(x_carto) < 3 or len(y_carto) < 3 or
            len(x) < 3):
            # Noting to do in this case
            return

        gl = map(lambda p: p.name, self.data.points)

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

    @timer
    def update(self, ind=None):
        logger.info("TODO: implemente update")
        self.draw()