Changeset c4e5400 in sasview


Ignore:
Timestamp:
Dec 12, 2016 3:21:03 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
27313b7
Parents:
3b7b218
Message:

Refactored context menu setup for plots

Location:
src/sas/qtgui
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Plotter.py

    r3b7b218 rc4e5400  
    7373        self.canvas.draw() 
    7474 
     75    def contextMenu(self): 
     76        """ 
     77        Define common context menu and associated actions for the MPL widget 
     78        """ 
     79        self.defaultContextMenu() 
     80 
     81 
    7582    def contextMenuQuickPlot(self): 
    7683        """ 
    7784        Define context menu and associated actions for the quickplot MPL widget 
    7885        """ 
    79         # Actions 
    80         self.contextMenu = QtGui.QMenu(self) 
    81         self.actionSaveImage = self.contextMenu.addAction("Save Image") 
    82         self.actionPrintImage = self.contextMenu.addAction("Print Image") 
    83         self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard") 
    84         self.contextMenu.addSeparator() 
     86        # Default actions 
     87        self.defaultContextMenu() 
     88 
     89        # Additional actions 
    8590        self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") 
    8691        self.contextMenu.addSeparator() 
     
    8893 
    8994        # Define the callbacks 
    90         self.actionSaveImage.triggered.connect(self.onImageSave) 
    91         self.actionPrintImage.triggered.connect(self.onImagePrint) 
    92         self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy) 
    9395        self.actionToggleGrid.triggered.connect(self.onGridToggle) 
    9496        self.actionChangeScale.triggered.connect(self.onScaleChange) 
  • src/sas/qtgui/Plotter2D.py

    rfecfe28 rc4e5400  
    7878                      zmax=zmax_2D_temp) 
    7979 
     80    def contextMenu(self): 
     81        """ 
     82        Define common context menu and associated actions for the MPL widget 
     83        """ 
     84        self.defaultContextMenu() 
     85 
    8086    def contextMenuQuickPlot(self): 
    8187        """ 
    8288        Define context menu and associated actions for the quickplot MPL widget 
    8389        """ 
    84         # Actions 
    85         self.contextMenu = QtGui.QMenu(self) 
    86         self.actionSaveImage = self.contextMenu.addAction("Save Image") 
    87         self.actionPrintImage = self.contextMenu.addAction("Print Image") 
    88         self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard") 
    89         self.contextMenu.addSeparator() 
     90        self.defaultContextMenu() 
     91 
    9092        if self.dimension == 2: 
    9193            self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") 
     
    9496 
    9597        # Define the callbacks 
    96         self.actionSaveImage.triggered.connect(self.onImageSave) 
    97         self.actionPrintImage.triggered.connect(self.onImagePrint) 
    98         self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy) 
     98        self.actionChangeScale.triggered.connect(self.onToggleScale) 
    9999        if self.dimension == 2: 
    100100            self.actionToggleGrid.triggered.connect(self.onGridToggle) 
    101         self.actionChangeScale.triggered.connect(self.onToggleScale) 
    102101 
    103102    def onToggleScale(self, event): 
  • src/sas/qtgui/PlotterBase.py

    r3b7b218 rc4e5400  
    145145        self.manager.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) 
    146146 
    147     def contextMenu(self): 
    148         """ 
    149         Define common context menu and associated actions for the MPL widget 
    150         TODO: move to plotter1d/plotter2d 
     147    def defaultContextMenu(self): 
     148        """ 
     149        Content of the dialog-universal context menu: 
     150        Save, Print and Copy 
    151151        """ 
    152152        # Actions 
     
    162162        self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy) 
    163163 
     164    def contextMenu(self): 
     165        """ 
     166        Define common context menu and associated actions for the MPL widget 
     167        TODO: move to plotter1d/plotter2d 
     168        """ 
     169        raise NotImplementedError("Context menu method must be implemented in derived class.") 
     170 
    164171    def contextMenuQuickPlot(self): 
    165172        """ 
  • src/sas/qtgui/UnitTesting/PlotterBaseTest.py

    r3b7b218 rc4e5400  
    11import sys 
    22import unittest 
    3 from mock import MagicMock 
     3from mock import patch, MagicMock 
    44 
    55from PyQt4 import QtGui 
     
    1515#import sas.qtgui.GuiUtils as GuiUtils 
    1616from sas.qtgui.GuiUtils import * 
     17import sas.qtgui.PlotHelper as PlotHelper 
    1718 
    1819# Tested module 
     
    3031            def perspective(self): 
    3132                return MyPerspective() 
    32  
    33         #PlotterBase.PlotterBase.updatePlotHelper = MagicMock() 
    34         #self.plotter = PlotterBase.PlotterBase(None, manager=dummy_manager()) 
    3533 
    3634        PlotterBase.PlotterBase.contextMenuQuickPlot = MagicMock() 
     
    6462    def testContextMenu(self): 
    6563        ''' Test the default context menu ''' 
    66         pass 
    67  
    68     def testContextMenuQuickPlot(self): 
    69         ''' Test the default quick context menu ''' 
    70         pass 
     64        with self.assertRaises(NotImplementedError): 
     65            self.plotter.contextMenu() 
    7166 
    7267    def testClean(self): 
    7368        ''' test the graph cleanup ''' 
    74         pass 
     69        self.plotter.figure.delaxes = MagicMock() 
     70        self.plotter.clean() 
     71        self.assertTrue(self.plotter.figure.delaxes.called) 
    7572 
    7673    def testPlot(self): 
     
    7976            self.plotter.plot() 
    8077 
    81     def testOnCloseEvent(self): 
     78    def notestOnCloseEvent(self): 
    8279        ''' test the plotter close behaviour ''' 
    83         pass 
     80        PlotHelper.deletePlot = MagicMock() 
     81        self.plotter.closeEvent(None) 
     82        self.assertTrue(PlotHelper.deletePlot.called) 
    8483 
    8584    def testOnImageSave(self): 
    8685        ''' test the workspace save ''' 
    87         pass 
     86        self.plotter.toolbar.save_figure = MagicMock() 
     87        self.plotter.onImageSave() 
     88        self.assertTrue(self.plotter.toolbar.save_figure.called) 
    8889 
    8990    def testOnImagePrint(self): 
    9091        ''' test the workspace print ''' 
    91         pass 
     92        QtGui.QPainter.end = MagicMock() 
     93        QtGui.QLabel.render = MagicMock() 
    9294 
    93     def tesOonClipboardCopy(self): 
    94         ''' test the workspace copy ''' 
    95         pass 
     95        # First, let's cancel printing 
     96        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) 
     97        self.plotter.onImagePrint() 
     98        self.assertFalse(QtGui.QPainter.end.called) 
     99        self.assertFalse(QtGui.QLabel.render.called) 
     100 
     101        # Let's print now 
     102        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) 
     103        self.plotter.onImagePrint() 
     104        self.assertTrue(QtGui.QPainter.end.called) 
     105        self.assertTrue(QtGui.QLabel.render.called) 
     106 
     107    def testOnClipboardCopy(self): 
     108        ''' test the workspace screen copy ''' 
     109        QtGui.QClipboard.setPixmap = MagicMock() 
     110        self.plotter.onClipboardCopy() 
     111        self.assertTrue(QtGui.QClipboard.setPixmap.called) 
    96112 
    97113    def testOnGridToggle(self): 
    98114        ''' test toggling the grid lines ''' 
    99         pass 
     115        # Check the toggle 
     116        orig_toggle = self.plotter.grid_on 
     117         
     118        FigureCanvas.draw_idle = MagicMock() 
     119        self.plotter.onGridToggle() 
     120 
     121        self.assertTrue(FigureCanvas.draw_idle.called) 
     122        self.assertTrue(self.plotter.grid_on != orig_toggle) 
     123 
     124    def testDefaultContextMenu(self): 
     125        """ Test the right click default menu """ 
     126 
     127        self.plotter.defaultContextMenu() 
     128 
     129        actions = self.plotter.contextMenu.actions() 
     130        self.assertEqual(len(actions), 4) 
     131 
     132        # Trigger Save Image and make sure the method is called 
     133        self.assertEqual(actions[0].text(), "Save Image") 
     134        self.plotter.toolbar.save_figure = MagicMock() 
     135        actions[0].trigger() 
     136        self.assertTrue(self.plotter.toolbar.save_figure.called) 
     137 
     138        # Trigger Print Image and make sure the method is called 
     139        self.assertEqual(actions[1].text(), "Print Image") 
     140        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) 
     141        actions[1].trigger() 
     142        self.assertTrue(QtGui.QPrintDialog.exec_.called) 
     143 
     144        # Trigger Copy to Clipboard and make sure the method is called 
     145        self.assertEqual(actions[2].text(), "Copy to Clipboard") 
     146 
     147        # Spy on cliboard's dataChanged() signal 
     148        self.clipboard_called = False 
     149        def done(): 
     150            self.clipboard_called = True 
     151        QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) 
     152        actions[2].trigger() 
     153        QtGui.qApp.processEvents() 
     154        # Make sure clipboard got updated. 
     155        self.assertTrue(self.clipboard_called) 
    100156 
    101157 
Note: See TracChangeset for help on using the changeset viewer.