Skip to content
Snippets Groups Projects
view.py 2.9 KiB
Newer Older
WHITFIELDRE email's avatar
WHITFIELDRE email committed
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
#     NScD Oak Ridge National Laboratory, European Spallation Source
#     & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
#  This file is part of the mantid workbench.
#
#
from __future__ import (absolute_import, division, print_function)
from qtpy.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from qtpy.QtCore import Qt
from mantidqt.MPLwidgets import FigureCanvas, NavigationToolbar2QT as NavigationToolbar
WHITFIELDRE email's avatar
WHITFIELDRE email committed
from matplotlib.figure import Figure
from .dimensionwidget import DimensionWidget
from mantidqt.widgets.colorbar.colorbar import ColorbarWidget
WHITFIELDRE email's avatar
WHITFIELDRE email committed


class SliceViewerView(QWidget):
    def __init__(self, presenter, dims_info, parent=None):
WHITFIELDRE email's avatar
WHITFIELDRE email committed
        super(SliceViewerView, self).__init__(parent)

        self.presenter = presenter

WHITFIELDRE email's avatar
WHITFIELDRE email committed
        self.setWindowTitle("SliceViewer")
        self.setWindowFlags(Qt.Window)
        self.setAttribute(Qt.WA_DeleteOnClose, True)

        # Dimension widget
        self.dimensions = DimensionWidget(dims_info, parent=self)
        self.dimensions.dimensionsChanged.connect(self.presenter.new_plot)
        self.dimensions.valueChanged.connect(self.presenter.update_plot_data)
        # MPL figure + colorbar
        self.mpl_layout = QHBoxLayout()
WHITFIELDRE email's avatar
WHITFIELDRE email committed
        self.fig = Figure()
        self.fig.set_facecolor(self.palette().window().color().getRgbF())
WHITFIELDRE email's avatar
WHITFIELDRE email committed
        self.fig.set_tight_layout(True)
        self.canvas = FigureCanvas(self.fig)
        self.ax = self.fig.add_subplot(111, projection='mantid')
        self.mpl_layout.addWidget(self.canvas)
        self.colorbar = ColorbarWidget(self)
        self.colorbar.colorbarChanged.connect(self.canvas.draw_idle)
        self.mpl_layout.addWidget(self.colorbar)
WHITFIELDRE email's avatar
WHITFIELDRE email committed

        # MPL toolbar
        self.mpl_toolbar = NavigationToolbar(self.canvas, self)

        # layout
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.dimensions)
        self.layout.addWidget(self.mpl_toolbar)
        self.layout.addLayout(self.mpl_layout, stretch=1)
WHITFIELDRE email's avatar
WHITFIELDRE email committed

        self.show()

    def plot(self, ws, **kwargs):
        """
        clears the plot and creates a new one using the workspace
        """
        self.ax.clear()
        self.im = self.ax.imshow(ws, origin='lower', aspect='auto',
                                 transpose=self.dimensions.transpose,
                                 norm=self.colorbar.get_norm(), **kwargs)
WHITFIELDRE email's avatar
WHITFIELDRE email committed
        self.ax.set_title('')
        self.colorbar.set_mappable(self.im)
WHITFIELDRE email's avatar
WHITFIELDRE email committed
        self.mpl_toolbar.update() # clear nav stack
        self.canvas.draw_idle()
WHITFIELDRE email's avatar
WHITFIELDRE email committed

    def update_plot_data(self, data):
        """
        This just updates the plot data without creating a new plot
        """
        self.im.set_data(data.T)
        self.colorbar.update_clim()
WHITFIELDRE email's avatar
WHITFIELDRE email committed

    def closeEvent(self, event):
        self.deleteLater()
        super(SliceViewerView, self).closeEvent(event)