Changeset 3316 for trunk/GSASIIplot.py


Ignore:
Timestamp:
Mar 16, 2018 9:39:58 AM (5 years ago)
Author:
toby
Message:

simplify plot toolbar code; continue on missing icon

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIplot.py

    r3315 r3316  
    144144import gltext
    145145import matplotlib.colors as mpcls
    146 from matplotlib.backends.backend_wx import _load_bitmap
    147146try:
    148147    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
     
    570569class GSASIItoolbar(Toolbar):
    571570    'Override the matplotlib toolbar so we can add more icons'
    572     ON_MPL_HELP = wx.NewId()
    573     ON_MPL_KEY = wx.NewId()
    574571    arrows = {}
    575     for direc in ('left','right','up','down','Expand X',
    576                   'Shrink X','Expand Y','Shrink Y'):
    577         arrows[direc] = wx.NewId()
    578572       
    579573    def __init__(self,plotCanvas,publish=None):
     
    593587        if not deleted: self.DeleteToolByPos(POS_CONFIG_SPLTS_BTN) #doesn't work in some wxpython versions
    594588        self.parent = self.GetParent()
    595         key = os.path.join(G2path,'key.ico')
    596         if 'phoenix' in wx.version():
    597             button = self.AddTool(self.ON_MPL_KEY,'Key press',_load_bitmap(key),'Select key press')
    598         else:
    599             button = self.AddSimpleTool(self.ON_MPL_KEY,_load_bitmap(key),'Key press','Select key press')
    600         wx.EVT_TOOL.Bind(self,self.ON_MPL_KEY,button.GetId(),self.OnKey)
    601         help = os.path.join(G2path,'help.ico')
    602         if 'phoenix' in wx.version():
    603             button = self.AddTool(self.ON_MPL_HELP,'Help on',_load_bitmap(help),'Show help on')
    604         else:
    605             button = self.AddSimpleTool(self.ON_MPL_HELP,_load_bitmap(help),'Help on','Show help on')
    606         wx.EVT_TOOL.Bind(self,self.ON_MPL_HELP,button.GetId(),self.OnHelp)
     589        self.AddToolBarTool('Key press','Select key press','key.ico',self.OnKey)
     590        self.AddToolBarTool('Help on','Show help on this plot','help.ico',self.OnHelp)
    607591        # add arrow keys to control zooming
    608         for direc in ('left','right','up','down'):
    609             icon =  os.path.join(G2path,direc[0]+'arrow.ico')
    610             if 'phoenix' in wx.version():
    611                 button = self.AddTool(self.arrows[direc],'Shift '+direc,_load_bitmap(icon),'Shift plot '+direc)
     592        for direc in ('left','right','up','down', 'Expand X','Shrink X','Expand Y','Shrink Y'):
     593            if ' ' in direc:
     594                sprfx = ''
     595                prfx = 'Zoom: '
    612596            else:
    613                 button = self.AddSimpleTool(self.arrows[direc],_load_bitmap(icon),'Shift '+direc,'Shift plot '+direc)
    614             wx.EVT_TOOL.Bind(self,self.arrows[direc],button.GetId(),self.OnArrow)
    615         for direc in ('Expand X','Shrink X','Expand Y','Shrink Y'):
     597                sprfx = 'Shift '
     598                prfx = 'Shift plot '
    616599            fil = ''.join([i[0].lower() for i in direc.split()]+['arrow.ico'])
    617             icon =  os.path.join(G2path,fil)
    618             if 'phoenix' in wx.version():
    619                 button = self.AddTool(self.arrows[direc],direc,_load_bitmap(icon),'Zoom: '+direc)
    620             else:
    621                 button = self.AddSimpleTool(self.arrows[direc],_load_bitmap(icon),direc,'Zoom: '+direc)
    622             wx.EVT_TOOL.Bind(self,self.arrows[direc],button.GetId(),self.OnArrow)
    623         id = wx.NewId()
     600            self.arrows[direc] = self.AddToolBarTool(sprfx+direc,prfx+direc,fil,self.OnArrow)
    624601        G2path = os.path.split(os.path.abspath(__file__))[0]
    625602        if publish:
    626             self.addPublish(publish)
    627 
    628     def addPublish(self,callBack):
    629         id = wx.NewId()
     603            self.AddToolBarTool('Publish plot','Create publishable version of plot','publish.ico',publish)
     604           
     605    def AddToolBarTool(self,label,title,filename,callback):
    630606        G2path = os.path.split(os.path.abspath(__file__))[0]
     607   
     608        bmpFilename = os.path.normpath(os.path.join(G2path, filename))
     609        if not os.path.exists(bmpFilename):
     610            print('Could not find bitmap file "%s"; skipping' % bmpFilename)
     611            bmp = wx.EmptyBitmap(32,32)
     612        else:
     613            bmp = wx.Bitmap(bmpFilename)
    631614        if 'phoenix' in wx.version():
    632             button = self.AddTool(id,'Publish plot',_load_bitmap(os.path.join(G2path,'publish.ico')),
    633                                   'Create publishable version of plot')
     615            button = self.AddTool(wx.ID_ANY, label, bmp, title)
    634616        else:
    635             button = self.AddSimpleTool(id,_load_bitmap(os.path.join(G2path,'publish.ico')),
    636                                         'Publish plot','Create publishable version of plot')
    637         wx.EVT_TOOL.Bind(self,id,button.GetId(),callBack)
    638            
     617            button = self.AddSimpleTool(wx.ID_ANY, bmp, label, title)
     618        wx.EVT_TOOL.Bind(self, button.GetId(), button.GetId(), callback)
     619        return button.GetId()
     620
    639621    def _update_view(self):
    640622        '''Overrides the post-buttonbar update action to invoke a redraw; needed for plot magnification
Note: See TracChangeset for help on using the changeset viewer.