source: sasview/src/sas/sasgui/plottools/toolbar.py @ 9a26428

Last change on this file since 9a26428 was d7bb526, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Refactored plottools into sasgui

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