source: sasview/src/sas/sasgui/plottools/toolbar.py @ 2aba0a4

Last change on this file since 2aba0a4 was 20fa5fe, checked in by Stuart Prescott <stuart@…>, 7 years ago

Fix lots more typos in comments and docs

  • Property mode set to 100644
File size: 6.0 KB
Line 
1"""
2    This module overwrites matplotlib toolbar
3"""
4import wx
5from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
6from matplotlib.backends.backend_wx import _load_bitmap
7import logging
8
9logger = logging.getLogger(__name__)
10
11# Event binding code changed after version 2.5
12if wx.VERSION_STRING >= '2.5':
13    def bind(actor, event, action, **kw):
14        actor.Bind(event, action, **kw)
15else:
16    def bind(actor, event, action, id=None):
17        if id is not None:
18            event(actor, id, action)
19        else:
20            event(actor, action)
21
22class NavigationToolBar(NavigationToolbar2WxAgg):
23    _NTB2_HOME = wx.NewId()
24    _NTB2_BACK = wx.NewId()
25    _NTB2_FORWARD = wx.NewId()
26    _NTB2_PAN = wx.NewId()
27    _NTB2_ZOOM = wx.NewId()
28    _NTB2_SAVE = wx.NewId()
29    _NTB2_PRINT = wx.NewId()
30    _NTB2_RESET = wx.NewId()
31    _NTB2_COPY = wx.NewId()
32    """
33    Overwrite matplotlib toolbar
34    """
35    def __init__(self, canvas, parent=None):
36        NavigationToolbar2WxAgg.__init__(self, canvas)
37
38    # CRUFT: mpl 1.1 uses save rather than save_figure
39    try: save_figure = NavigationToolbar2WxAgg.save
40    except AttributeError: pass
41   
42    def _init_toolbar(self):
43        self._parent = self.canvas.GetParent()
44
45        # for mpl 1.2+ compatibility
46        self.wx_ids = {}
47        self.wx_ids['Back'] = self._NTB2_BACK
48        self.wx_ids['Forward'] = self._NTB2_FORWARD
49        self.wx_ids['Pan'] = self._NTB2_PAN
50        self.wx_ids['Zoom'] = self._NTB2_ZOOM
51
52        self.SetToolBitmapSize(wx.Size(24, 24))
53
54        context_tip = 'Graph Menu: \n'
55        context_tip += '    For more menu options, \n'
56        context_tip += '    right-click the data symbols.'
57        context = wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR)
58        self.AddSimpleTool(self._NTB2_HOME, context, context_tip, context_tip)
59
60        self.InsertSeparator(1)
61
62        self.AddSimpleTool(self._NTB2_BACK, _load_bitmap('back.png'),
63                           'Back', 'Back navigation view')
64        self.AddSimpleTool(self._NTB2_FORWARD, _load_bitmap('forward.png'),
65                           'Forward', 'Forward navigation view')
66        # todo: get new bitmap
67        self.AddCheckTool(self._NTB2_PAN, _load_bitmap('move.png'),
68                          shortHelp='Pan',
69                          longHelp='Pan with left, zoom with right')
70        self.AddCheckTool(self._NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'),
71                          shortHelp='Zoom', longHelp='Zoom to rectangle')
72
73        self.AddSeparator()
74        self.AddSimpleTool(self._NTB2_SAVE, _load_bitmap('filesave.png'),
75                           'Save', 'Save plot contents to file')
76
77        print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR)
78        self.AddSimpleTool(self._NTB2_PRINT, print_bmp, 'Print', 'Print plot')
79
80        reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR)
81        self.AddSimpleTool(self._NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range')
82
83        bind(self, wx.EVT_TOOL, self.context_menu, id=self._NTB2_HOME)
84        bind(self, wx.EVT_TOOL, self.forward, id=self._NTB2_FORWARD)
85        bind(self, wx.EVT_TOOL, self.back, id=self._NTB2_BACK)
86        bind(self, wx.EVT_TOOL, self.zoom, id=self._NTB2_ZOOM)
87        bind(self, wx.EVT_TOOL, self.pan, id=self._NTB2_PAN)
88        bind(self, wx.EVT_TOOL, self.save_figure, id=self._NTB2_SAVE)
89        bind(self, wx.EVT_TOOL, self.print_figure, id=self._NTB2_PRINT)
90        bind(self, wx.EVT_TOOL, self.home, id=self._NTB2_RESET)
91
92        self.Realize()
93
94    def on_menu(self, event):
95        try:
96            self._parent.onToolContextMenu(event=event)
97        except:
98            logger.error("Plot toolbar could not show menu")
99
100    def context_menu(self, event):
101        """
102        Default context menu for a plot panel
103
104        """
105        # Slicer plot popup menu
106        popup = wx.Menu()
107        popup.Append(self._NTB2_SAVE, '&Save image', 'Save image as PNG')
108        wx.EVT_MENU(self, self._NTB2_SAVE, self.save_figure)
109
110        popup.Append(self._NTB2_PRINT, '&Print image', 'Print image ')
111        wx.EVT_MENU(self, self._NTB2_PRINT, self.print_figure)
112
113        popup.Append(self._NTB2_COPY, '&Copy to Clipboard', 'Copy image to the clipboard')
114        wx.EVT_MENU(self, self._NTB2_COPY, 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):
121        try:
122            _printer = wx.Printer()
123            _printer.Print(self.canvas, PlotPrintout(self.canvas), True)
124        except:
125            import traceback
126            logger.error(traceback.format_exc())
127
128    def copy_figure(self, event):
129        copy_image_to_clipboard(self.canvas)
130
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):
139        """
140        Initialize wx.Printout and get passed figure object
141        """
142        wx.Printout.__init__(self)
143        self.canvas = canvas
144
145    def OnPrintPage(self, page):
146        """
147        Most rudimentry OnPrintPage override.  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.