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 | 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 | |
---|
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) |
---|
44 | self.AddSimpleTool(_NTB2_HOME, context, context_tip, context_tip) |
---|
45 | |
---|
46 | self.InsertSeparator(1) |
---|
47 | |
---|
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 | |
---|
63 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR) |
---|
64 | self.AddSimpleTool(_NTB2_PRINT, print_bmp, 'Print', 'Print plot') |
---|
65 | |
---|
66 | reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR) |
---|
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() |
---|
79 | |
---|
80 | def on_menu(self, event): |
---|
81 | """ |
---|
82 | Plot menu |
---|
83 | """ |
---|
84 | try: |
---|
85 | self._parent.onToolContextMenu(event=event) |
---|
86 | except: |
---|
87 | logging.error("Plot toolbar could not show menu") |
---|
88 | |
---|
89 | def on_reset(self, event): |
---|
90 | """ |
---|
91 | Reset plot |
---|
92 | """ |
---|
93 | try: |
---|
94 | self._parent.onResetGraph(event=event) |
---|
95 | except: |
---|
96 | logging.error("Plot toolbar could not reset plot") |
---|
97 | |
---|
98 | def on_print(self, event): |
---|
99 | """ |
---|
100 | Print |
---|
101 | """ |
---|
102 | try: |
---|
103 | self.canvas.Printer_Preview(event=event) |
---|
104 | except: |
---|
105 | logging.error("Plot toolbar could not print") |
---|
106 | |
---|