source: sasview/src/sas/sasgui/plottools/toolbar.py @ 199b515e

Last change on this file since 199b515e was d7bb526, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Refactored plottools into sasgui

  • Property mode set to 100644
File size: 6.1 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    _NTB2_HOME = wx.NewId()
22    _NTB2_BACK = wx.NewId()
23    _NTB2_FORWARD = wx.NewId()
24    _NTB2_PAN = wx.NewId()
25    _NTB2_ZOOM = wx.NewId()
26    _NTB2_SAVE = wx.NewId()
27    _NTB2_PRINT = wx.NewId()
28    _NTB2_RESET = wx.NewId()
29    _NTB2_COPY = wx.NewId()
30    """
31    Overwrite matplotlib toolbar
32    """
33    def __init__(self, canvas, parent=None):
34        NavigationToolbar2WxAgg.__init__(self, canvas)
35
36    # CRUFT: mpl 1.1 uses save rather than save_figure
37    try: save_figure = NavigationToolbar2WxAgg.save
38    except AttributeError: pass
39   
40    def _init_toolbar(self):
41        self._parent = self.canvas.GetParent()
42
43        # for mpl 1.2+ compatibility
44        self.wx_ids = {}
45        self.wx_ids['Back'] = self._NTB2_BACK
46        self.wx_ids['Forward'] = self._NTB2_FORWARD
47        self.wx_ids['Pan'] = self._NTB2_PAN
48        self.wx_ids['Zoom'] = self._NTB2_ZOOM
49
50        self.SetToolBitmapSize(wx.Size(24, 24))
51
52        context_tip = 'Graph Menu: \n'
53        context_tip += '    For more menu options, \n'
54        context_tip += '    right-click the data symbols.'
55        context = wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR)
56        self.AddSimpleTool(self._NTB2_HOME, context, context_tip, context_tip)
57
58        self.InsertSeparator(1)
59
60        self.AddSimpleTool(self._NTB2_BACK, _load_bitmap('back.png'),
61                           'Back', 'Back navigation view')
62        self.AddSimpleTool(self._NTB2_FORWARD, _load_bitmap('forward.png'),
63                           'Forward', 'Forward navigation view')
64        # todo: get new bitmap
65        self.AddCheckTool(self._NTB2_PAN, _load_bitmap('move.png'),
66                          shortHelp='Pan',
67                          longHelp='Pan with left, zoom with right')
68        self.AddCheckTool(self._NTB2_ZOOM, _load_bitmap('zoom_to_rect.png'),
69                          shortHelp='Zoom', longHelp='Zoom to rectangle')
70
71        self.AddSeparator()
72        self.AddSimpleTool(self._NTB2_SAVE, _load_bitmap('filesave.png'),
73                           'Save', 'Save plot contents to file')
74
75        print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR)
76        self.AddSimpleTool(self._NTB2_PRINT, print_bmp, 'Print', 'Print plot')
77
78        reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR)
79        self.AddSimpleTool(self._NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range')
80
81        bind(self, wx.EVT_TOOL, self.context_menu, id=self._NTB2_HOME)
82        bind(self, wx.EVT_TOOL, self.forward, id=self._NTB2_FORWARD)
83        bind(self, wx.EVT_TOOL, self.back, id=self._NTB2_BACK)
84        bind(self, wx.EVT_TOOL, self.zoom, id=self._NTB2_ZOOM)
85        bind(self, wx.EVT_TOOL, self.pan, id=self._NTB2_PAN)
86        bind(self, wx.EVT_TOOL, self.save_figure, id=self._NTB2_SAVE)
87        bind(self, wx.EVT_TOOL, self.print_figure, id=self._NTB2_PRINT)
88        bind(self, wx.EVT_TOOL, self.home, id=self._NTB2_RESET)
89
90        self.Realize()
91
92    def on_menu(self, event):
93        try:
94            self._parent.onToolContextMenu(event=event)
95        except:
96            logging.error("Plot toolbar could not show menu")
97
98    def context_menu(self, event):
99        """
100        Default context menu for a plot panel
101
102        """
103        # Slicer plot popup menu
104        popup = wx.Menu()
105        popup.Append(self._NTB2_SAVE, '&Save image', 'Save image as PNG')
106        wx.EVT_MENU(self, self._NTB2_SAVE, self.save_figure)
107
108        popup.Append(self._NTB2_PRINT, '&Print image', 'Print image ')
109        wx.EVT_MENU(self, self._NTB2_PRINT, self.print_figure)
110
111        popup.Append(self._NTB2_COPY, '&Copy to Clipboard', 'Copy image to the clipboard')
112        wx.EVT_MENU(self, self._NTB2_COPY, self.copy_figure)
113
114        # Show the popup menu relative to the location of the toolbar
115        self.PopupMenu(popup, (0,0))
116
117
118    def print_figure(self, event):
119        try:
120            _printer = wx.Printer()
121            _printer.Print(self.canvas, PlotPrintout(self.canvas), True)
122        except:
123            import traceback
124            logging.error(traceback.format_exc())
125
126    def copy_figure(self, event):
127        copy_image_to_clipboard(self.canvas)
128
129class PlotPrintout(wx.Printout):
130    """
131    Create the wx.Printout object for matplotlib figure from the PlotPanel.
132    Provides the required OnPrintPage and HasPage overrides.  Other methods
133    may be added/overriden in the future.
134    :TODO: this needs LOTS of TLC .. but fixes immediate problem
135    """
136    def __init__(self, canvas):
137        """
138        Initialize wx.Printout and get passed figure object
139        """
140        wx.Printout.__init__(self)
141        self.canvas = canvas
142
143    def OnPrintPage(self, page):
144        """
145        Most rudimentry OnPrintPage overide.  instatiates a dc object, gets
146        its size, gets the size of the figure object, scales it to the dc
147        canvas size keeping the aspect ratio intact, then prints as bitmap
148        """
149        _dc = self.GetDC()
150        (_dcX, _dcY) = _dc.GetSizeTuple()
151        (_bmpX,_bmpY) = self.canvas.GetSize()
152        _scale = min(_dcX/_bmpX, _dcY/_bmpY)
153        _dc.SetUserScale(_scale, _scale)
154        _dc.DrawBitmap(self.canvas.bitmap, 0, 0, False,)
155        return True
156
157    def GetPageInfo(self):
158        """
159        just sets the page to 1 - no flexibility for now
160        """
161        return (1, 1, 1, 1)
162
163
164def copy_image_to_clipboard(canvas):
165    bmp = wx.BitmapDataObject()
166    bmp.SetBitmap(canvas.bitmap)
167
168    wx.TheClipboard.Open()
169    wx.TheClipboard.SetData(bmp)
170    wx.TheClipboard.Close()
171
172
Note: See TracBrowser for help on using the repository browser.