1 | """ |
---|
2 | This module overwrites matplotlib toolbar |
---|
3 | """ |
---|
4 | import wx |
---|
5 | from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg |
---|
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) |
---|
19 | |
---|
20 | class NavigationToolBar(NavigationToolbar2WxAgg): |
---|
21 | """ |
---|
22 | Overwrite matplotlib toolbar |
---|
23 | """ |
---|
24 | def __init__(self, canvas, parent=None): |
---|
25 | NavigationToolbar2WxAgg.__init__(self, canvas) |
---|
26 | |
---|
27 | # CRUFT: mpl 1.1 uses save rather than save_figure |
---|
28 | try: save_figure = NavigationToolbar2WxAgg.save |
---|
29 | except AttributeError: pass |
---|
30 | |
---|
31 | def _init_toolbar(self): |
---|
32 | self._parent = self.canvas.GetParent() |
---|
33 | _NTB2_HOME = wx.NewId() |
---|
34 | self._NTB2_BACK = wx.NewId() |
---|
35 | self._NTB2_FORWARD = wx.NewId() |
---|
36 | self._NTB2_PAN = wx.NewId() |
---|
37 | self._NTB2_ZOOM = wx.NewId() |
---|
38 | _NTB2_SAVE = wx.NewId() |
---|
39 | _NTB2_PRINT = wx.NewId() |
---|
40 | _NTB2_RESET = wx.NewId() |
---|
41 | |
---|
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 | |
---|
49 | self.SetToolBitmapSize(wx.Size(24, 24)) |
---|
50 | |
---|
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) |
---|
55 | self.AddSimpleTool(_NTB2_HOME, context, context_tip, context_tip) |
---|
56 | |
---|
57 | self.InsertSeparator(1) |
---|
58 | |
---|
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'), |
---|
65 | shortHelp='Pan', |
---|
66 | longHelp='Pan with left, zoom with right') |
---|
67 | self.AddCheckTool(self._NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'), |
---|
68 | shortHelp='Zoom', longHelp='Zoom to rectangle') |
---|
69 | |
---|
70 | self.AddSeparator() |
---|
71 | self.AddSimpleTool(_NTB2_SAVE, _load_bitmap('filesave.png'), |
---|
72 | 'Save', 'Save plot contents to file') |
---|
73 | |
---|
74 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR) |
---|
75 | self.AddSimpleTool(_NTB2_PRINT, print_bmp, 'Print', 'Print plot') |
---|
76 | |
---|
77 | reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR) |
---|
78 | self.AddSimpleTool(_NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range') |
---|
79 | |
---|
80 | bind(self, wx.EVT_TOOL, self.on_menu, id=_NTB2_HOME) |
---|
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) |
---|
85 | bind(self, wx.EVT_TOOL, self.save_figure, id=_NTB2_SAVE) |
---|
86 | bind(self, wx.EVT_TOOL, self.on_print, id=_NTB2_PRINT) |
---|
87 | bind(self, wx.EVT_TOOL, self.on_reset, id=_NTB2_RESET) |
---|
88 | |
---|
89 | self.Realize() |
---|
90 | |
---|
91 | def on_menu(self, event): |
---|
92 | """ |
---|
93 | Plot menu |
---|
94 | """ |
---|
95 | try: |
---|
96 | self._parent.onToolContextMenu(event=event) |
---|
97 | except: |
---|
98 | logging.error("Plot toolbar could not show menu") |
---|
99 | |
---|
100 | def on_reset(self, event): |
---|
101 | """ |
---|
102 | Reset plot |
---|
103 | """ |
---|
104 | try: |
---|
105 | self._parent.onResetGraph(event=event) |
---|
106 | except: |
---|
107 | logging.error("Plot toolbar could not reset plot") |
---|
108 | |
---|
109 | def on_print(self, event): |
---|
110 | """ |
---|
111 | Print |
---|
112 | """ |
---|
113 | try: |
---|
114 | self.canvas.Printer_Preview(event=event) |
---|
115 | except: |
---|
116 | logging.error("Plot toolbar could not print") |
---|