source: sasview/src/sas/plottools/toolbar.py @ 600bea1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 600bea1 was d3d67f0, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

fix toolbar on bumps/tools graphs

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[a9d5684]1"""
2    This module overwrites matplotlib toolbar
3"""
4import wx
5from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
[eea3ffa]6from matplotlib.backends.backend_wx import _load_bitmap
7import logging
8
9# Event binding code changed after version 2.5
10if wx.VERSION_STRING >= '2.5':
[3477478]11    def bind(actor, event, action, **kw):
12        actor.Bind(event, action, **kw)
[eea3ffa]13else:
[3477478]14    def bind(actor, event, action, id=None):
[eea3ffa]15        if id is not None:
16            event(actor, id, action)
17        else:
[3477478]18            event(actor, action)
[a9d5684]19
20class NavigationToolBar(NavigationToolbar2WxAgg):
21    """
22    Overwrite matplotlib toolbar
23    """
24    def __init__(self, canvas, parent=None):
25        NavigationToolbar2WxAgg.__init__(self, canvas)
[da6c9847]26
27    # CRUFT: mpl 1.1 uses save rather than save_figure
28    try: save_figure = NavigationToolbar2WxAgg.save
29    except AttributeError: pass
[3477478]30   
[eea3ffa]31    def _init_toolbar(self):
32        self._parent = self.canvas.GetParent()
[d3d67f0]33        self._NTB2_HOME = wx.NewId()
[3477478]34        self._NTB2_BACK = wx.NewId()
[eea3ffa]35        self._NTB2_FORWARD = wx.NewId()
[3477478]36        self._NTB2_PAN = wx.NewId()
37        self._NTB2_ZOOM = wx.NewId()
[d3d67f0]38        self._NTB2_SAVE = wx.NewId()
39        self._NTB2_PRINT = wx.NewId()
40        self._NTB2_RESET = wx.NewId()
[eea3ffa]41
[da6c9847]42        # for mpl 1.2+ compatibility
43        self.wx_ids = {}
44        self.wx_ids['Back'] = self._NTB2_BACK
45        self.wx_ids['Forward'] = self._NTB2_FORWARD
46        self.wx_ids['Pan'] = self._NTB2_PAN
47        self.wx_ids['Zoom'] = self._NTB2_ZOOM
48
[3477478]49        self.SetToolBitmapSize(wx.Size(24, 24))
[eea3ffa]50
[a9d5684]51        context_tip = 'Graph Menu: \n'
52        context_tip += '    For more menu options, \n'
53        context_tip += '    right-click the data symbols.'
54        context = wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR)
[d3d67f0]55        self.AddSimpleTool(self._NTB2_HOME, context, context_tip, context_tip)
[eea3ffa]56
[a9d5684]57        self.InsertSeparator(1)
[3477478]58
[eea3ffa]59        self.AddSimpleTool(self._NTB2_BACK, _load_bitmap('back.png'),
60                           'Back', 'Back navigation view')
61        self.AddSimpleTool(self._NTB2_FORWARD, _load_bitmap('forward.png'),
62                           'Forward', 'Forward navigation view')
63        # todo: get new bitmap
64        self.AddCheckTool(self._NTB2_PAN, _load_bitmap('move.png'),
[3477478]65                          shortHelp='Pan',
66                          longHelp='Pan with left, zoom with right')
[eea3ffa]67        self.AddCheckTool(self._NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'),
[3477478]68                          shortHelp='Zoom', longHelp='Zoom to rectangle')
[eea3ffa]69
70        self.AddSeparator()
[d3d67f0]71        self.AddSimpleTool(self._NTB2_SAVE, _load_bitmap('filesave.png'),
[eea3ffa]72                           'Save', 'Save plot contents to file')
[3477478]73
[a9d5684]74        print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR)
[d3d67f0]75        self.AddSimpleTool(self._NTB2_PRINT, print_bmp, 'Print', 'Print plot')
[3477478]76
[a9d5684]77        reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR)
[d3d67f0]78        self.AddSimpleTool(self._NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range')
[eea3ffa]79
[d3d67f0]80        bind(self, wx.EVT_TOOL, self.context_menu, id=self._NTB2_HOME)
[eea3ffa]81        bind(self, wx.EVT_TOOL, self.forward, id=self._NTB2_FORWARD)
82        bind(self, wx.EVT_TOOL, self.back, id=self._NTB2_BACK)
83        bind(self, wx.EVT_TOOL, self.zoom, id=self._NTB2_ZOOM)
84        bind(self, wx.EVT_TOOL, self.pan, id=self._NTB2_PAN)
[d3d67f0]85        bind(self, wx.EVT_TOOL, self.save_figure, id=self._NTB2_SAVE)
86        bind(self, wx.EVT_TOOL, self.print_figure, id=self._NTB2_PRINT)
87        bind(self, wx.EVT_TOOL, self.home, id=self._NTB2_RESET)
[eea3ffa]88
89        self.Realize()
[da6c9847]90
[a9d5684]91    def on_menu(self, event):
92        try:
[eea3ffa]93            self._parent.onToolContextMenu(event=event)
[a9d5684]94        except:
[eea3ffa]95            logging.error("Plot toolbar could not show menu")
[da6c9847]96
[d3d67f0]97    def context_menu(self, event):
[a9d5684]98        """
[d3d67f0]99        Default context menu for a plot panel
100
[a9d5684]101        """
[d3d67f0]102        # Slicer plot popup menu
103        wx_id = wx.NewId()
104        popup = wx.Menu()
105        popup.Append(wx_id, '&Save image', 'Save image as PNG')
106        wx.EVT_MENU(self, wx_id, self.save_figure)
107
108        wx_id = wx.NewId()
109        popup.Append(wx_id, '&Print image', 'Print image ')
110        wx.EVT_MENU(self, wx_id, self.print_figure)
111
112        wx_id = wx.NewId()
113        popup.Append(wx_id, '&Copy to Clipboard', 'Copy image to the clipboard')
114        wx.EVT_MENU(self, wx_id, self.copy_figure)
115
116        # Show the popup menu relative to the location of the toolbar
117        self.PopupMenu(popup, (0,0))
118
119
120    def print_figure(self, event):
[a9d5684]121        try:
[d3d67f0]122            _printer = wx.Printer()
123            _printer.Print(self.canvas, PlotPrintout(self.canvas), True)
[a9d5684]124        except:
[d3d67f0]125            import traceback
126            logging.error(traceback.format_exc())
127
128    def copy_figure(self, event):
129        copy_image_to_clipboard(self.canvas)
[da6c9847]130
[d3d67f0]131class PlotPrintout(wx.Printout):
132    """
133    Create the wx.Printout object for matplotlib figure from the PlotPanel.
134    Provides the required OnPrintPage and HasPage overrides.  Other methods
135    may be added/overriden in the future.
136    :TODO: this needs LOTS of TLC .. but fixes immediate problem
137    """
138    def __init__(self, canvas):
[a9d5684]139        """
[d3d67f0]140        Initialize wx.Printout and get passed figure object
[a9d5684]141        """
[d3d67f0]142        wx.Printout.__init__(self)
143        self.canvas = canvas
144
145    def OnPrintPage(self, page):
146        """
147        Most rudimentry OnPrintPage overide.  instatiates a dc object, gets
148        its size, gets the size of the figure object, scales it to the dc
149        canvas size keeping the aspect ratio intact, then prints as bitmap
150        """
151        _dc = self.GetDC()
152        (_dcX, _dcY) = _dc.GetSizeTuple()
153        (_bmpX,_bmpY) = self.canvas.GetSize()
154        _scale = min(_dcX/_bmpX, _dcY/_bmpY)
155        _dc.SetUserScale(_scale, _scale)
156        _dc.DrawBitmap(self.canvas.bitmap, 0, 0, False,)
157        return True
158
159    def GetPageInfo(self):
160        """
161        just sets the page to 1 - no flexibility for now
162        """
163        return (1, 1, 1, 1)
164
165
166def copy_image_to_clipboard(canvas):
167    bmp = wx.BitmapDataObject()
168    bmp.SetBitmap(canvas.bitmap)
169
170    wx.TheClipboard.Open()
171    wx.TheClipboard.SetData(bmp)
172    wx.TheClipboard.Close()
173
174
Note: See TracBrowser for help on using the repository browser.