[a9d5684] | 1 | """ |
---|
| 2 | This module overwrites matplotlib toolbar |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
| 5 | from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg |
---|
[eea3ffa] | 6 | from matplotlib.backends.backend_wx import _load_bitmap |
---|
| 7 | import logging |
---|
| 8 | |
---|
| 9 | # Event binding code changed after version 2.5 |
---|
| 10 | if wx.VERSION_STRING >= '2.5': |
---|
| 11 | def bind(actor,event,action,**kw): |
---|
| 12 | actor.Bind(event,action,**kw) |
---|
| 13 | else: |
---|
| 14 | def bind(actor,event,action,id=None): |
---|
| 15 | if id is not None: |
---|
| 16 | event(actor, id, action) |
---|
| 17 | else: |
---|
| 18 | event(actor,action) |
---|
[a9d5684] | 19 | |
---|
| 20 | class NavigationToolBar(NavigationToolbar2WxAgg): |
---|
| 21 | """ |
---|
| 22 | Overwrite matplotlib toolbar |
---|
| 23 | """ |
---|
| 24 | def __init__(self, canvas, parent=None): |
---|
| 25 | NavigationToolbar2WxAgg.__init__(self, canvas) |
---|
| 26 | |
---|
[eea3ffa] | 27 | def _init_toolbar(self): |
---|
| 28 | self._parent = self.canvas.GetParent() |
---|
| 29 | _NTB2_HOME = wx.NewId() |
---|
| 30 | self._NTB2_BACK = wx.NewId() |
---|
| 31 | self._NTB2_FORWARD = wx.NewId() |
---|
| 32 | self._NTB2_PAN = wx.NewId() |
---|
| 33 | self._NTB2_ZOOM = wx.NewId() |
---|
| 34 | _NTB2_SAVE = wx.NewId() |
---|
| 35 | _NTB2_PRINT = wx.NewId() |
---|
| 36 | _NTB2_RESET = wx.NewId() |
---|
| 37 | |
---|
| 38 | self.SetToolBitmapSize(wx.Size(24,24)) |
---|
| 39 | |
---|
[a9d5684] | 40 | context_tip = 'Graph Menu: \n' |
---|
| 41 | context_tip += ' For more menu options, \n' |
---|
| 42 | context_tip += ' right-click the data symbols.' |
---|
| 43 | context = wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR) |
---|
[eea3ffa] | 44 | self.AddSimpleTool(_NTB2_HOME, context, context_tip, context_tip) |
---|
| 45 | |
---|
[a9d5684] | 46 | self.InsertSeparator(1) |
---|
| 47 | |
---|
[eea3ffa] | 48 | self.AddSimpleTool(self._NTB2_BACK, _load_bitmap('back.png'), |
---|
| 49 | 'Back', 'Back navigation view') |
---|
| 50 | self.AddSimpleTool(self._NTB2_FORWARD, _load_bitmap('forward.png'), |
---|
| 51 | 'Forward', 'Forward navigation view') |
---|
| 52 | # todo: get new bitmap |
---|
| 53 | self.AddCheckTool(self._NTB2_PAN, _load_bitmap('move.png'), |
---|
| 54 | shortHelp='Pan', |
---|
| 55 | longHelp='Pan with left, zoom with right') |
---|
| 56 | self.AddCheckTool(self._NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'), |
---|
| 57 | shortHelp='Zoom', longHelp='Zoom to rectangle') |
---|
| 58 | |
---|
| 59 | self.AddSeparator() |
---|
| 60 | self.AddSimpleTool(_NTB2_SAVE, _load_bitmap('filesave.png'), |
---|
| 61 | 'Save', 'Save plot contents to file') |
---|
| 62 | |
---|
[a9d5684] | 63 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR) |
---|
[eea3ffa] | 64 | self.AddSimpleTool(_NTB2_PRINT, print_bmp, 'Print', 'Print plot') |
---|
| 65 | |
---|
[a9d5684] | 66 | reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR) |
---|
[eea3ffa] | 67 | self.AddSimpleTool(_NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range') |
---|
| 68 | |
---|
| 69 | bind(self, wx.EVT_TOOL, self.on_menu, id=_NTB2_HOME) |
---|
| 70 | bind(self, wx.EVT_TOOL, self.forward, id=self._NTB2_FORWARD) |
---|
| 71 | bind(self, wx.EVT_TOOL, self.back, id=self._NTB2_BACK) |
---|
| 72 | bind(self, wx.EVT_TOOL, self.zoom, id=self._NTB2_ZOOM) |
---|
| 73 | bind(self, wx.EVT_TOOL, self.pan, id=self._NTB2_PAN) |
---|
| 74 | bind(self, wx.EVT_TOOL, self.save, id=_NTB2_SAVE) |
---|
| 75 | bind(self, wx.EVT_TOOL, self.on_print, id=_NTB2_PRINT) |
---|
| 76 | bind(self, wx.EVT_TOOL, self.on_reset, id=_NTB2_RESET) |
---|
| 77 | |
---|
| 78 | self.Realize() |
---|
[a9d5684] | 79 | |
---|
| 80 | def on_menu(self, event): |
---|
| 81 | """ |
---|
[eea3ffa] | 82 | Plot menu |
---|
[a9d5684] | 83 | """ |
---|
| 84 | try: |
---|
[eea3ffa] | 85 | self._parent.onToolContextMenu(event=event) |
---|
[a9d5684] | 86 | except: |
---|
[eea3ffa] | 87 | logging.error("Plot toolbar could not show menu") |
---|
[a9d5684] | 88 | |
---|
| 89 | def on_reset(self, event): |
---|
| 90 | """ |
---|
[eea3ffa] | 91 | Reset plot |
---|
[a9d5684] | 92 | """ |
---|
| 93 | try: |
---|
[eea3ffa] | 94 | self._parent.onResetGraph(event=event) |
---|
[a9d5684] | 95 | except: |
---|
[eea3ffa] | 96 | logging.error("Plot toolbar could not reset plot") |
---|
[a9d5684] | 97 | |
---|
| 98 | def on_print(self, event): |
---|
| 99 | """ |
---|
[eea3ffa] | 100 | Print |
---|
[a9d5684] | 101 | """ |
---|
| 102 | try: |
---|
[eea3ffa] | 103 | self.canvas.Printer_Preview(event=event) |
---|
[a9d5684] | 104 | except: |
---|
[eea3ffa] | 105 | logging.error("Plot toolbar could not print") |
---|
[a9d5684] | 106 | |
---|