Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/plottools/toolbar.py

    r3477478 rd3d67f0  
    3131    def _init_toolbar(self): 
    3232        self._parent = self.canvas.GetParent() 
    33         _NTB2_HOME = wx.NewId() 
     33        self._NTB2_HOME = wx.NewId() 
    3434        self._NTB2_BACK = wx.NewId() 
    3535        self._NTB2_FORWARD = wx.NewId() 
    3636        self._NTB2_PAN = wx.NewId() 
    3737        self._NTB2_ZOOM = wx.NewId() 
    38         _NTB2_SAVE = wx.NewId() 
    39         _NTB2_PRINT = wx.NewId() 
    40         _NTB2_RESET = wx.NewId() 
     38        self._NTB2_SAVE = wx.NewId() 
     39        self._NTB2_PRINT = wx.NewId() 
     40        self._NTB2_RESET = wx.NewId() 
    4141 
    4242        # for mpl 1.2+ compatibility 
     
    5353        context_tip += '    right-click the data symbols.' 
    5454        context = wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR) 
    55         self.AddSimpleTool(_NTB2_HOME, context, context_tip, context_tip) 
     55        self.AddSimpleTool(self._NTB2_HOME, context, context_tip, context_tip) 
    5656 
    5757        self.InsertSeparator(1) 
     
    6969 
    7070        self.AddSeparator() 
    71         self.AddSimpleTool(_NTB2_SAVE, _load_bitmap('filesave.png'), 
     71        self.AddSimpleTool(self._NTB2_SAVE, _load_bitmap('filesave.png'), 
    7272                           'Save', 'Save plot contents to file') 
    7373 
    7474        print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR) 
    75         self.AddSimpleTool(_NTB2_PRINT, print_bmp, 'Print', 'Print plot') 
     75        self.AddSimpleTool(self._NTB2_PRINT, print_bmp, 'Print', 'Print plot') 
    7676 
    7777        reset_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR) 
    78         self.AddSimpleTool(_NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range') 
     78        self.AddSimpleTool(self._NTB2_RESET, reset_bmp, 'Reset', 'Reset graph range') 
    7979 
    80         bind(self, wx.EVT_TOOL, self.on_menu, id=_NTB2_HOME) 
     80        bind(self, wx.EVT_TOOL, self.context_menu, id=self._NTB2_HOME) 
    8181        bind(self, wx.EVT_TOOL, self.forward, id=self._NTB2_FORWARD) 
    8282        bind(self, wx.EVT_TOOL, self.back, id=self._NTB2_BACK) 
    8383        bind(self, wx.EVT_TOOL, self.zoom, id=self._NTB2_ZOOM) 
    8484        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) 
     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) 
    8888 
    8989        self.Realize() 
    9090 
    9191    def on_menu(self, event): 
    92         """ 
    93             Plot menu 
    94         """ 
    9592        try: 
    9693            self._parent.onToolContextMenu(event=event) 
     
    9895            logging.error("Plot toolbar could not show menu") 
    9996 
    100     def on_reset(self, event): 
     97    def context_menu(self, event): 
    10198        """ 
    102             Reset plot 
     99        Default context menu for a plot panel 
     100 
    103101        """ 
     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): 
    104121        try: 
    105             self._parent.onResetGraph(event=event) 
     122            _printer = wx.Printer() 
     123            _printer.Print(self.canvas, PlotPrintout(self.canvas), True) 
    106124        except: 
    107             logging.error("Plot toolbar could not reset plot") 
     125            import traceback 
     126            logging.error(traceback.format_exc()) 
    108127 
    109     def on_print(self, event): 
     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): 
    110139        """ 
    111             Print 
     140        Initialize wx.Printout and get passed figure object 
    112141        """ 
    113         try: 
    114             self.canvas.Printer_Preview(event=event) 
    115         except: 
    116             logging.error("Plot toolbar could not print") 
     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 TracChangeset for help on using the changeset viewer.