Source code for tangram.mpl_figure

# Copyright 2017 The Tangram Developers. See the AUTHORS file at the
# top-level directory of this distribution and at
# https://github.com/renatoGarcia/tangram/blob/master/AUTHORS.
#
# This file is part of Tangram.
#
# Tangram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tangram 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Tangram in the COPYING and COPYING.LESSER files.
# If not, see <http://www.gnu.org/licenses/>.

from gi.repository import GLib

from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas

from .widget import InputState, MouseButton
from ._base import Base
from ._utility import Signal


[docs]class MPLFigure(Base):
[docs] def __init__(self, *, name=None): super().__init__(name) self.__figure = Figure() self._gtk_obj = FigureCanvas(self.__figure) self._gtk_obj.props.expand = True self._gtk_obj.mpl_connect("button_press_event", self.__on_button_press_event) self.mouse_click = Signal()
def __enter__(self): return self.__figure def __exit__(self, exc_type, exc_value, traceback): self._gtk_obj.queue_draw() def __on_button_press_event(self, event): pos = (event.xdata, event.ydata) button = MouseButton(button=event.button, n_clicks=2 if event.dblclick else 1) input_state = InputState(Base._pressed_keys, event.guiEvent.state) self.mouse_click.emit(pos, button, input_state) return GLib.SOURCE_CONTINUE