source: sasview/src/sas/plottools/toolbar.py @ d3d67f0

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since d3d67f0 was d3d67f0, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

fix toolbar on bumps/tools graphs

  • Property mode set to 100644
File size: 6.2 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
9# Event binding code changed after version 2.5
10if wx.VERSION_STRING >= '2.5':
11    def bind(actor, event, action, **kw):
12        actor.Bind(event, action, **kw)
13else:
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
20class 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        self._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        self._NTB2_SAVE = wx.NewId()
39        self._NTB2_PRINT = wx.NewId()
40        self._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(self._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(self._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(self._NTB2_PRINT, print_bmp, 'Print', 'Print plot')
76
77        reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR)
78        self.AddSimpleTool(self._NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range')
79
80        bind(self, wx.EVT_TOOL, self.context_menu, id=self._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=self._NTB2_SAVE)
86        bind(self, wx.EVT_TOOL, self.print_figure, id=self._NTB2_PRINT)
87        bind(self, wx.EVT_TOOL, self.home, id=self._NTB2_RESET)
88
89        self.Realize()
90
91    def on_menu(self, event):
92        try:
93            self._parent.onToolContextMenu(event=event)
94        except:
95            logging.error("Plot toolbar could not show menu")
96
97    def context_menu(self, event):
98        """
99        Default context menu for a plot panel
100
101        """
102        # Slicer plot popup menu
103        wx_id = wx.NewId()
104        popup = wx.Menu()
105        popup.Append(wx_id, '&Save image', 'Save image as PNG')
106        wx.EVT_MENU(self, wx_id, self.save_figure)
107
108        wx_id = wx.NewId()
109        popup.Append(wx_id, '&Print image', 'Print image ')
110        wx.EVT_MENU(self, wx_id, self.print_figure)
111
112        wx_id = wx.NewId()
113        popup.Append(wx_id, '&Copy to Clipboard', 'Copy image to the clipboard')
114        wx.EVT_MENU(self, wx_id, 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            logging.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 overide.  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.