class MatplotlibPlotting(PlottingFrame, QWidget): def __init__(self, controller, parent=None, plotting_info_viewer=None): super(MatplotlibPlotting, self).__init__(controller, plotting_info_viewer=plotting_info_viewer) self._controller.new_data.connect(self.set_new_data) self._controller.new_config.connect(self.set_new_config) self._auto_render = True self.figure = Figure() self.visualizer = MapsVisualizer(self._controller.get_data(), self.figure) self._axes_data = self.visualizer.render(self._controller.get_config()) self.canvas = FigureCanvas(self.figure) self.canvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) self.canvas.updateGeometry() layout = QVBoxLayout() layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.canvas) self.setLayout(layout) self.setParent(parent) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setFocus() self._redraw_timer = QTimer() self._redraw_timer.timeout.connect(self._timer_event) self._redraw_timer.timeout.connect(self._redraw_timer.stop) self._mouse_interaction = _MouseInteraction(self.figure, self._plotting_info_viewer) self._mouse_interaction.update_axes_data(self._axes_data) self._previous_config = None self.setMinimumWidth(100) def export_image(self, filename, width, height, dpi=100): width_inch = width / dpi height_inch = height / dpi figure = Figure(figsize=(width_inch, height_inch), dpi=dpi) visualizer = MapsVisualizer(self._controller.get_data(), figure) FigureCanvas(figure) visualizer.to_file(filename, self._controller.get_config(), dpi=dpi) def set_auto_rendering(self, auto_render): self._auto_render = auto_render def redraw(self): self._redraw() @pyqtSlot() def _timer_event(self): self._redraw() @pyqtSlot(DataInfo) def set_new_data(self, data_info): self.visualizer = MapsVisualizer(data_info, self.figure) self._redraw_timer.start(300) @pyqtSlot(MapPlotConfig) def set_new_config(self, configuration): if not self._previous_config or configuration.visible_changes( self._previous_config): self._previous_config = configuration if self._auto_render: self._redraw_timer.start(300) def _redraw(self): self.figure.clf() self._axes_data = self.visualizer.render(self._controller.get_config()) self._mouse_interaction.update_axes_data(self._axes_data) self.figure.canvas.draw()
class MatplotlibPlotting(PlottingFrame, QWidget): def __init__(self, controller, parent=None, plotting_info_viewer=None): super().__init__(controller, plotting_info_viewer=plotting_info_viewer) self._controller.model_updated.connect(self.update_model) self._auto_render = True current_model = self._controller.get_model() self.figure = Figure(facecolor='#bfbfbf') self.visualizer = MapsVisualizer(current_model.get_data(), self.figure) self._axes_data = self.visualizer.render(current_model.get_config()) self.canvas = FigureCanvas(self.figure) self.canvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus) self.canvas.setFocus() self.canvas.updateGeometry() layout = QVBoxLayout() layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.canvas) self.setLayout(layout) self.setParent(parent) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setFocus() self._redraw_timer = QTimer() self._redraw_timer.timeout.connect(self._timer_event) self._redraw_timer.timeout.connect(self._redraw_timer.stop) self._mouse_interaction = _MouseInteraction(self.figure, self._plotting_info_viewer, self._controller) self._mouse_interaction.update_axes_data(self._axes_data) self._previous_model = None self.setMinimumWidth(100) def export_image(self, filename, width, height, dpi=100): current_model = self._controller.get_model() width_inch = width / dpi height_inch = height / dpi figure = Figure(figsize=(width_inch, height_inch), dpi=dpi) visualizer = MapsVisualizer(current_model.get_data(), figure) FigureCanvas(figure) visualizer.to_file(filename, current_model.get_config(), dpi=dpi) def set_auto_rendering(self, auto_render): self._auto_render = auto_render def redraw(self): self._redraw() @pyqtSlot() def _timer_event(self): self._redraw() @pyqtSlot(DataConfigModel) def update_model(self, model): def update(): self._previous_model = model self.visualizer = MapsVisualizer(model.get_data(), self.figure) if self._auto_render: self._redraw_timer.start(300) if not self._previous_model: update() elif model.get_config().visible_changes( self._previous_model.get_config()): update() elif model.get_data() != self._previous_model.get_data(): update() def _redraw(self): current_model = self._controller.get_model() self.figure.clf() self._axes_data = self.visualizer.render(current_model.get_config()) self._mouse_interaction.update_axes_data(self._axes_data) try: self.figure.canvas.draw() except ValueError as exception: logger = logging.getLogger(__name__) logger.error(exception)