source: sasview/src/sas/qtgui/Plotting/PlotterBase.py @ 42787fb

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 42787fb was 42787fb, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Fixing issues with DataOperationUtility? calculator

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[ef01be4]1import pylab
[9290b1a]2import numpy
[ef01be4]3
[4992ff2]4from PyQt5 import QtCore
5from PyQt5 import QtGui
[53c771e]6from PyQt5 import QtWidgets, QtPrintSupport
[ef01be4]7
[4992ff2]8from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
9from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
[ef01be4]10
11import matplotlib.pyplot as plt
12
13DEFAULT_CMAP = pylab.cm.jet
[dc5ef15]14from sas.qtgui.Plotting.Binder import BindArtist
15from sas.qtgui.Plotting.PlotterData import Data1D
16from sas.qtgui.Plotting.PlotterData import Data2D
[9290b1a]17
[83eb5208]18from sas.qtgui.Plotting.ScaleProperties import ScaleProperties
19from sas.qtgui.Plotting.WindowTitle import WindowTitle
[dc5ef15]20import sas.qtgui.Utilities.GuiUtils as GuiUtils
[83eb5208]21import sas.qtgui.Plotting.PlotHelper as PlotHelper
22import sas.qtgui.Plotting.PlotUtilities as PlotUtilities
[ef01be4]23
[4992ff2]24class PlotterBase(QtWidgets.QWidget):
[416fa8f]25    def __init__(self, parent=None, manager=None, quickplot=False):
[ef01be4]26        super(PlotterBase, self).__init__(parent)
27
28        # Required for the communicator
[416fa8f]29        self.manager = manager
[ef01be4]30        self.quickplot = quickplot
31
[fbfc488]32        #plt.style.use('ggplot')
[eb1a386]33        #plt.style.use('seaborn-darkgrid')
[fbfc488]34
[ef01be4]35        # a figure instance to plot on
36        self.figure = plt.figure()
37
[3b7b218]38        # Define canvas for the figure to be placed on
[ef01be4]39        self.canvas = FigureCanvas(self.figure)
40
[3b7b218]41        # ... and the toolbar with all the default MPL buttons
[ef01be4]42        self.toolbar = NavigationToolbar(self.canvas, self)
43
[092a3d9]44        # Simple window for data display
[4992ff2]45        self.txt_widget = QtWidgets.QTextEdit(None)
[092a3d9]46
[3b7b218]47        # Set the layout and place the canvas widget in it.
[4992ff2]48        layout = QtWidgets.QVBoxLayout()
49        # FIXME setMargin -> setContentsMargins in qt5 with 4 args
50        #layout.setContentsMargins(0)
[ef01be4]51        layout.addWidget(self.canvas)
52
[3b7b218]53        # 1D plotter defaults
[ef01be4]54        self.current_plot = 111
[6d05e1d]55        self._data = [] # Original 1D/2D object
56        self._xscale = 'log'
57        self._yscale = 'log'
[ef01be4]58        self.qx_data = []
59        self.qy_data = []
[b4b8589]60        self.color = 0
61        self.symbol = 0
[ef01be4]62        self.grid_on = False
63        self.scale = 'linear'
[6d05e1d]64        self.x_label = "log10(x)"
65        self.y_label = "log10(y)"
[ef01be4]66
[9290b1a]67        # Mouse click related
[b46f285]68        self._scale_xlo = None
69        self._scale_xhi = None
70        self._scale_ylo = None
[570a58f9]71        self._scale_yhi = None
[9290b1a]72        self.x_click = None
73        self.y_click = None
74        self.event_pos = None
75        self.leftdown = False
76        self.gotLegend = 0
77
[42787fb]78        self.show_legend = True
79
[9290b1a]80        # Annotations
81        self.selectedText = None
82        self.textList = []
83
[3b7b218]84        # Pre-define the Scale properties dialog
85        self.properties = ScaleProperties(self,
[570a58f9]86                                init_scale_x=self.x_label,
87                                init_scale_y=self.y_label)
[3b7b218]88
[ef01be4]89        # default color map
90        self.cmap = DEFAULT_CMAP
91
[3b7b218]92        # Add the axes object -> subplot
93        # TODO: self.ax will have to be tracked and exposed
94        # to enable subplot specific operations
[ef01be4]95        self.ax = self.figure.add_subplot(self.current_plot)
[3b7b218]96
[9290b1a]97        # Remove this, DAMMIT
98        self.axes = [self.ax]
99
[3b7b218]100        # Set the background color to white
[ef01be4]101        self.canvas.figure.set_facecolor('#FFFFFF')
102
[9290b1a]103        # Canvas event handlers
104        self.canvas.mpl_connect('button_release_event', self.onMplMouseUp)
105        self.canvas.mpl_connect('button_press_event', self.onMplMouseDown)
106        self.canvas.mpl_connect('motion_notify_event', self.onMplMouseMotion)
107        self.canvas.mpl_connect('pick_event', self.onMplPick)
[d3ca363]108        self.canvas.mpl_connect('scroll_event', self.onMplWheel)
[9290b1a]109
[4992ff2]110        self.contextMenu = QtWidgets.QMenu(self)
[aadf0af1]111
[ef01be4]112        if not quickplot:
[aadf0af1]113            # Add the toolbar
[ef01be4]114            layout.addWidget(self.toolbar)
[3b7b218]115            # Notify PlotHelper about the new plot
116            self.upatePlotHelper()
[ef01be4]117
118        self.setLayout(layout)
119
120    @property
121    def data(self):
[b4b8589]122        """ data getter """
[ef01be4]123        return self._data
124
125    @data.setter
126    def data(self, data=None):
[3b7b218]127        """ Pure virtual data setter """
128        raise NotImplementedError("Data setter must be implemented in derived class.")
[ef01be4]129
130    def title(self, title=""):
131        """ title setter """
[6d05e1d]132        self._title = title
[7d8bebf]133        # Set the object name to satisfy the Squish object picker
134        self.canvas.setObjectName(title)
[ef01be4]135
[6d05e1d]136    @property
137    def xLabel(self, xlabel=""):
138        """ x-label setter """
139        return self.x_label
140
141    @xLabel.setter
[ef01be4]142    def xLabel(self, xlabel=""):
143        """ x-label setter """
[8f83719f]144        self.x_label = r'$%s$'% xlabel if xlabel else ""
[ef01be4]145
[6d05e1d]146    @property
147    def yLabel(self, ylabel=""):
148        """ y-label setter """
149        return self.y_label
150
151    @yLabel.setter
[ef01be4]152    def yLabel(self, ylabel=""):
153        """ y-label setter """
[8f83719f]154        self.y_label = r'$%s$'% ylabel if ylabel else ""
[ef01be4]155
[6d05e1d]156    @property
157    def yscale(self):
158        """ Y-axis scale getter """
159        return self._yscale
160
[ef01be4]161    @yscale.setter
[6d05e1d]162    def yscale(self, scale='linear'):
163        """ Y-axis scale setter """
164        self.ax.set_yscale(scale, nonposy='clip')
165        self._yscale = scale
166
167    @property
168    def xscale(self):
169        """ X-axis scale getter """
170        return self._xscale
171
[ef01be4]172    @xscale.setter
[6d05e1d]173    def xscale(self, scale='linear'):
174        """ X-axis scale setter """
[eb1a386]175        self.ax.cla()
[6d05e1d]176        self.ax.set_xscale(scale)
177        self._xscale = scale
[ef01be4]178
[42787fb]179    @property
180    def showLegend(self):
181        """ Legend visibility getter """
182        return self.show_legend
183
184    @showLegend.setter
185    def showLegend(self, show=True):
186        """ Legend visibility setter """
187        self.show_legend = show
188
[3b7b218]189    def upatePlotHelper(self):
190        """
191        Notify the plot helper about the new plot
192        """
193        # Notify the helper
194        PlotHelper.addPlot(self)
195        # Notify the listeners about a new graph
196        self.manager.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots())
197
[c4e5400]198    def defaultContextMenu(self):
[ef01be4]199        """
[c4e5400]200        Content of the dialog-universal context menu:
201        Save, Print and Copy
[ef01be4]202        """
203        # Actions
[aadf0af1]204        self.contextMenu.clear()
[6d05e1d]205        self.actionSaveImage = self.contextMenu.addAction("Save Image")
206        self.actionPrintImage = self.contextMenu.addAction("Print Image")
207        self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard")
208        self.contextMenu.addSeparator()
209
210        # Define the callbacks
211        self.actionSaveImage.triggered.connect(self.onImageSave)
[ef01be4]212        self.actionPrintImage.triggered.connect(self.onImagePrint)
213        self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy)
[6d05e1d]214
[aadf0af1]215    def createContextMenu(self):
[c4e5400]216        """
217        Define common context menu and associated actions for the MPL widget
218        """
219        raise NotImplementedError("Context menu method must be implemented in derived class.")
220
[aadf0af1]221    def createContextMenuQuick(self):
[6d05e1d]222        """
223        Define context menu and associated actions for the quickplot MPL widget
224        """
[3b7b218]225        raise NotImplementedError("Context menu method must be implemented in derived class.")
[6d05e1d]226
227    def contextMenuEvent(self, event):
228        """
229        Display the context menu
230        """
[aadf0af1]231        if not self.quickplot:
232            self.createContextMenu()
233        else:
234            self.createContextMenuQuick()
235
[9290b1a]236        event_pos = event.pos()
237        self.contextMenu.exec_(self.canvas.mapToGlobal(event_pos))
238
[3bdbfcc]239    def onMplMouseUp(self, event):
[9290b1a]240        """
[3bdbfcc]241        Mouse button up callback
[9290b1a]242        """
[3bdbfcc]243        pass
[9290b1a]244
[3bdbfcc]245    def onMplMouseDown(self, event):
[9290b1a]246        """
[3bdbfcc]247        Mouse button down callback
[9290b1a]248        """
[3bdbfcc]249        pass
[9290b1a]250
251    def onMplMouseMotion(self, event):
252        """
[3bdbfcc]253        Mouse motion callback
[9290b1a]254        """
[3bdbfcc]255        pass
[9290b1a]256
257    def onMplPick(self, event):
258        """
[3bdbfcc]259        Mouse pick callback
[9290b1a]260        """
[3bdbfcc]261        pass
[d3ca363]262
263    def onMplWheel(self, event):
264        """
[3bdbfcc]265        Mouse wheel scroll callback
[d3ca363]266        """
[3bdbfcc]267        pass
[6d05e1d]268
[ef01be4]269    def clean(self):
270        """
271        Redraw the graph
272        """
273        self.figure.delaxes(self.ax)
274        self.ax = self.figure.add_subplot(self.current_plot)
275
276    def plot(self, marker=None, linestyle=None):
277        """
[3b7b218]278        PURE VIRTUAL
[ef01be4]279        Plot the content of self._data
280        """
[3b7b218]281        raise NotImplementedError("Plot method must be implemented in derived class.")
[ef01be4]282
283    def closeEvent(self, event):
284        """
285        Overwrite the close event adding helper notification
286        """
287        # Please remove me from your database.
288        PlotHelper.deletePlot(PlotHelper.idOfPlot(self))
[7d8bebf]289
[ef01be4]290        # Notify the listeners
[416fa8f]291        self.manager.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots())
[7d8bebf]292
[ef01be4]293        event.accept()
294
295    def onImageSave(self):
296        """
297        Use the internal MPL method for saving to file
298        """
299        self.toolbar.save_figure()
300
301    def onImagePrint(self):
302        """
303        Display printer dialog and print the MPL widget area
304        """
305        # Define the printer
[53c771e]306        printer = QtPrintSupport.QPrinter()
[6d05e1d]307
308        # Display the print dialog
[53c771e]309        dialog = QtPrintSupport.QPrintDialog(printer)
[6d05e1d]310        dialog.setModal(True)
311        dialog.setWindowTitle("Print")
[4992ff2]312        if dialog.exec_() != QtWidgets.QDialog.Accepted:
[ef01be4]313            return
314
315        painter = QtGui.QPainter(printer)
[b4b8589]316        # Grab the widget screenshot
[dd150ef]317        pmap = QtGui.QPixmap(self.size())
318        self.render(pmap)
[b4b8589]319        # Create a label with pixmap drawn
[4992ff2]320        printLabel = QtWidgets.QLabel()
[ef01be4]321        printLabel.setPixmap(pmap)
322
323        # Print the label
324        printLabel.render(painter)
325        painter.end()
326
327    def onClipboardCopy(self):
328        """
329        Copy MPL widget area to buffer
330        """
[7969b9c]331        bmp = QtWidgets.QApplication.clipboard()
[dd150ef]332        pixmap = QtGui.QPixmap(self.canvas.size())
333        self.canvas.render(pixmap)
[6d05e1d]334        bmp.setPixmap(pixmap)
[ef01be4]335
336    def onGridToggle(self):
337        """
338        Add/remove grid lines from MPL plot
339        """
340        self.grid_on = (not self.grid_on)
341        self.ax.grid(self.grid_on)
342        self.canvas.draw_idle()
[27313b7]343
344    def onWindowsTitle(self):
345        """
346        Show a dialog allowing chart title customisation
347        """
348        current_title = self.windowTitle()
349        titleWidget = WindowTitle(self, new_title=current_title)
350        result = titleWidget.exec_()
[4992ff2]351        if result != QtWidgets.QDialog.Accepted:
[27313b7]352            return
353
354        title = titleWidget.title()
355        self.setWindowTitle(title)
356        # Notify the listeners about a new graph title
357        self.manager.communicator.activeGraphName.emit((current_title, title))
[570a58f9]358
[b46f285]359    def offset_graph(self):
360        """
361        Zoom and offset the graph to the last known settings
362        """
363        for ax in self.axes:
364            if self._scale_xhi is not None and self._scale_xlo is not None:
365                ax.set_xlim(self._scale_xlo, self._scale_xhi)
366            if self._scale_yhi is not None and self._scale_ylo is not None:
367                ax.set_ylim(self._scale_ylo, self._scale_yhi)
[092a3d9]368
369    def onDataInfo(self, plot_data):
370        """
371        Displays data info text window for the selected plot
372        """
373        if isinstance(plot_data, Data1D):
374            text_to_show = GuiUtils.retrieveData1d(plot_data)
375        else:
376            text_to_show = GuiUtils.retrieveData2d(plot_data)
377        # Hardcoded sizes to enable full width rendering with default font
378        self.txt_widget.resize(420,600)
379
380        self.txt_widget.setReadOnly(True)
381        self.txt_widget.setWindowFlags(QtCore.Qt.Window)
382        self.txt_widget.setWindowIcon(QtGui.QIcon(":/res/ball.ico"))
383        self.txt_widget.setWindowTitle("Data Info: %s" % plot_data.filename)
384        self.txt_widget.insertPlainText(text_to_show)
385
386        self.txt_widget.show()
387        # Move the slider all the way up, if present
388        vertical_scroll_bar = self.txt_widget.verticalScrollBar()
[7969b9c]389        vertical_scroll_bar.triggerAction(QtWidgets.QScrollBar.SliderToMinimum)
[092a3d9]390
391    def onSavePoints(self, plot_data):
392        """
393        Saves plot data to a file
394        """
395        if isinstance(plot_data, Data1D):
396            GuiUtils.saveData1D(plot_data)
397        else:
398            GuiUtils.saveData2D(plot_data)
Note: See TracBrowser for help on using the repository browser.