Changeset 2234


Ignore:
Timestamp:
May 1, 2016 10:46:45 PM (6 years ago)
Author:
toby
Message:

implement code to select appropriate tree time when a plot tab is selected

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r2215 r2234  
    384384
    385385        :param bool load2Tree: indicates if the file should be loaded
    386           into the data tree immediately (used for images only)
     386          into the data tree immediately (used for images only). True
     387          only when called from :meth:`OnImportImage`; causes return
     388          value to change to a list of True values rather than
     389          reader objects.
    387390
    388391        :returns: a list of reader objects (rd_list) that were able
     
    22722275        plotFrame = wx.Frame(None,-1,'GSASII Plots',size=wx.Size(700,600), \
    22732276            style=wx.DEFAULT_FRAME_STYLE ^ wx.CLOSE_BOX)
    2274         self.G2plotNB = G2plt.G2PlotNoteBook(plotFrame)
    2275         #self.G2plotNB = G2plt.G2PlotNoteBook(plotFrame,G2frame=self)
     2277        #self.G2plotNB = G2plt.G2PlotNoteBook(plotFrame)
     2278        self.G2plotNB = G2plt.G2PlotNoteBook(plotFrame,G2frame=self)
    22762279        plotFrame.Show()
    22772280       
     
    23492352        self.LastGPXdir = None    # directory where a GPX file was last read
    23502353        self.LastExportDir = None  # the last directory used for exports, if any.
     2354        self.dataDisplayPhaseText = ''
    23512355       
    23522356        arg = sys.argv
     
    36973701                if dlg2.ShowModal() == wx.ID_OK:
    36983702                    Id = 0
    3699                     self.G2plotNB.setReplotFlags() # mark all plots as old
    37003703                    self.PatternTree.DeleteChildren(self.root)
    37013704                    self.HKL = []
     
    37083711                    self.PickId = Id
    37093712                    self.PatternTree.SelectItem(Id)
    3710                     G2gd.MovePatternTreeToGrid(self,Id)
    3711                     self.G2plotNB.replotAll() # refresh any plots not yet updated
     3713                    G2gd.MovePatternTreeToGrid(self,Id) # reload current tree item, should update current plot
    37123714            finally:
    37133715                dlg2.Destroy()
  • trunk/GSASIIgrid.py

    r2231 r2234  
    38013801                         
    38023802def GetPatternTreeItemId(G2frame, parentId, itemText):
    3803     '''Needs a doc string
     3803    '''Find the tree item that matches the text in itemText starting with parentId
     3804
     3805    :param wx.Frame G2frame: Data tree frame object
     3806    :param wx.TreeItemId parentId: tree item to start search with
     3807    :param str itemText: text for tree item
    38043808    '''
    38053809    item, cookie = G2frame.PatternTree.GetFirstChild(parentId)
  • trunk/GSASIIphsGUI.py

    r2233 r2234  
    72797279    def ChangePage(page):
    72807280        text = G2frame.dataDisplay.GetPageText(page)
     7281        G2frame.dataDisplayPhaseText = text
    72817282        if text == 'General':
    72827283            G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.DataGeneral)
  • trunk/GSASIIplot.py

    r2216 r2234  
    7575        self.ReplotArgs = []
    7676        self.ReplotKwArgs = {}
    77         self.needsUpdate = True
    7877        wx.Panel.__init__(self,parent,id=id,**kwargs)
    7978       
    80     def AddRefresh(self,ReplotRoutine,ReplotArgs=[],ReplotKwArgs={}):
    81         '''Define a routine used to refresh the plot
    82        
    83         :param function ReplotRoutine: function to be called
    84         :param list ReplotArgs: list of positional parameters, if any
    85         :param dict ReplotKwArgs: dict of keyword parameters, if any
    86         '''
    87         self.ReplotRoutine = ReplotRoutine
    88         self.ReplotArgs = ReplotArgs
    89         self.ReplotKwArgs = ReplotKwArgs
    90 
    91     def Refresh(self):
    92         'Replots the selected tab'
    93         if self.ReplotRoutine:
    94             #print 'Refresh G2PlotMpl with',self.ReplotRoutine
    95             if not self.needsUpdate:
    96                 #print 'already updated\n'
    97                 return True
    98             #if self.ReplotArgs: print '...args',self.ReplotArgs
    99             #if self.ReplotKwArgs: print '...KWargs',self.ReplotKwArgs
    100             self.ReplotRoutine(*self.ReplotArgs,**self.ReplotKwArgs)
    101             return True
    102         else:
    103             return False
    10479class G2PlotMpl(_tabPlotWin):   
    10580    'Creates a Matplotlib 2-D plot in the GSAS-II graphics window'
     
    156131class G2PlotNoteBook(wx.Panel):
    157132    'create a tabbed window for GSAS-II graphics'
    158     def __init__(self,parent,id=-1):
     133    def __init__(self,parent,id=-1,G2frame=None):
    159134        wx.Panel.__init__(self,parent,id=id)
    160135        #so one can't delete a plot page from tab!!
     
    169144        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
    170145        self.nb.Bind(wx.EVT_KEY_UP,self.OnNotebookKey)
     146        self.G2frame = G2frame
    171147       
    172148        self.plotList = []   # contains the tab label for each plot
    173149        self.panelList = []   # contains the panel object for each plot
    174150        self.figList = []   # contains the figure object for each plot
    175         self.pageOnTop = None # keep track of top page during refresh all
    176         self.skipPageChange = False
     151        self.treeItem = {}  # contains the tree entry that generated a plot, indexed by the plot number
     152        self.skipPageChange = False # set to True when no plot update is needed
     153        self.allowZoomReset = True # this indicates plot should be updated not initialized
    177154       
    178155    def OnNotebookKey(self,event):
     
    202179        self.figList.append(page.figure)  # figure object for plot
    203180        self.skipPageChange = False
    204 
    205     def registerReplot(self,name,ReplotRoutine,ReplotArgs=[],ReplotKwArgs={}):
    206         'Define the routine and args needed to replot a figure'
    207         try:
    208             plotNum = self.plotList.index(name)
    209         except ValueError:
    210             print('No plot tab labeled '+name)
    211             GSASIIpath.IPyBreak()
    212         page = self.panelList[plotNum]
    213         page.AddRefresh(ReplotRoutine,ReplotArgs,ReplotKwArgs)
    214        
    215     def setReplotFlags(self):
    216         'Flag all plots as needing a redraw'
    217         for page in self.panelList:
    218             page.needsUpdate = True
    219         self.pageOnTop = self.nb.GetSelection()
    220        
    221     def clearReplotFlag(self,name):
    222         'Set when redrawing a plot so that it is not done twice'
    223         try:
    224             plotNum = self.plotList.index(name)
    225         except ValueError:
    226             print('clearReplotFlag: No plot tab labeled '+name)
    227         page = self.panelList[plotNum]
    228         page.needsUpdate = False
    229 
    230     def ResetOnTop(self):
    231         'Put the saved page back on top'
    232         self.SetSelectionNoRefresh(self.pageOnTop)
    233        
    234     def SetSelectionNoRefresh(self,plotNum):
    235         'Raises a plot tab without triggering a refresh via OnPageChanged'
    236         self.skipPageChange = True
    237         self.nb.SetSelection(plotNum) # raises plot tab
    238         self.skipPageChange = False
    239 
     181        self._registerTreeItem(name)
     182
     183    def _registerTreeItem(self,plotLabel):
     184        '''Save the name of the of the data tree item that has generated the plot
     185        and for phases, save the name of the generating tab (at present that is not used)
     186        '''
     187        tree = self.G2frame.PatternTree
     188        self.treeItem[plotLabel] = [tree._getTreeItemsList(tree.GetSelection()),'']
     189        if len(self.treeItem[plotLabel]) == 2 and self.treeItem[plotLabel][0][0] == 'Phases':
     190            self.treeItem[plotLabel][1] = self.G2frame.dataDisplayPhaseText
     191               
    240192    def RaisePageNoRefresh(self,Page):
    241193        'Raises a plot tab without triggering a refresh via OnPageChanged'
     
    243195        Page.SetFocus()
    244196        self.skipPageChange = False
    245                
    246     def replotAll(self):
    247         'refresh all current plots, if not already redrawn'
    248         for page,label in zip(self.panelList,self.plotList):
    249             if page.Refresh():
    250                 pass
    251             elif GSASIIpath.GetConfigValue('debug'):
    252                 print('No refresh for '+label)               
    253         if self.pageOnTop is not None:
    254             wx.CallLater(150,self.ResetOnTop)
    255        
     197       
     198    def SetSelectionNoRefresh(self,plotNum):
     199        'Raises a plot tab without triggering a refresh via OnPageChanged'
     200        self.skipPageChange = True
     201        self.nb.SetSelection(plotNum) # raises plot tab
     202        Page = self.G2frame.G2plotNB.nb.GetPage(plotNum)
     203        Page.SetFocus()
     204        self.skipPageChange = False
     205                                 
    256206    def addMpl(self,name=""):
    257207        'Add a tabbed page with a matplotlib plot'
     
    313263        self.status.SetStatusWidths([150,-1])
    314264        if self.skipPageChange:
    315 #            print 'skipping OnPageChanged'
    316265            self.skipPageChange = False
    317266            return
    318 #        print 'OnPageChanged'
    319267        page = self.panelList[self.nb.GetSelection()]   #GetCurrentPage() not in wx 2.7
    320         page.needsUpdate = True
    321         if page.Refresh():  # refresh plot, if possible
    322             pass
    323         elif self.plotList:
    324             self.status.SetStatusText('Better to select this from GSAS-II data tree',1)
    325        
     268        tabLabel = event.GetEventObject().GetPageText(event.GetSelection())
     269        if tabLabel in self.treeItem:
     270            treeItems, tabname = self.treeItem[tabLabel]
     271            id = self.G2frame.root
     272            for item in treeItems:
     273                id = G2gd.GetPatternTreeItemId(self.G2frame, id, item)
     274            wx.CallLater(100,self.InvokeTreeItem,id)
     275        else:
     276            print 'OnPageChanged: not found:',tabLabel
     277           
     278    def InvokeTreeItem(self,id):
     279        '''This is called to select an item from the tree using the self.allowZoomReset
     280        flag to prevent a reset to the zoom of the plot (where implemented)
     281        '''
     282        self.allowZoomReset = False
     283        self.G2frame.PatternTree.SelectItem(id)
     284        self.allowZoomReset = True
     285           
    326286class GSASIItoolbar(Toolbar):
    327287    'Override the matplotlib toolbar so we can add more icons'
     
    11611121################################################################################
    11621122           
    1163 def PlotPatterns(G2frame,newPlot=False,plotType='PWDR',TreeItemText=None):
     1123def PlotPatterns(G2frame,newPlot=False,plotType='PWDR'):
    11641124    '''Powder pattern plotting package - displays single or multiple powder patterns as intensity vs
    11651125    2-theta, q or TOF. Can display multiple patterns as "waterfall plots" or contour plots. Log I
    11661126    plotting available.
    1167 
    1168     The histogram to be plotted is found in G2frame.PatternId unless the histogram name is specified
    1169     as TreeItemText
    11701127    '''
    11711128    global exclLines
     
    11741131    global Pattern
    11751132    plottype = plotType
    1176     if TreeItemText:
    1177         G2frame.PatternId = G2gd.GetPatternTreeItemId(G2frame,G2frame.root,TreeItemText)
    1178     else:
    1179         TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId)
    11801133       
    11811134    if not G2frame.PatternId:
     
    16541607        Page.canvas.mpl_connect('button_release_event', OnRelease)
    16551608        Page.canvas.mpl_connect('button_press_event',OnPress)
    1656     G2frame.G2plotNB.registerReplot('Powder Patterns',
    1657         ReplotRoutine=PlotPatterns,
    1658         ReplotKwArgs={'G2frame':G2frame, 'plotType':plottype,
    1659                       'TreeItemText':TreeItemText, 'newPlot':True
    1660                       })
    1661     G2frame.G2plotNB.status.SetStatusText('histogram: '+TreeItemText,1)
    1662     G2frame.G2plotNB.clearReplotFlag('Powder Patterns')
    16631609#    if plottype == 'PWDR':  # avoids a very nasty clash with KILL_FOCUS in SASD TextCtrl?
    16641610#        Page.SetFocus()
     
    28492795################################################################################
    28502796           
    2851 def PlotPeakWidths(G2frame,TreeItemText=None):
     2797def PlotPeakWidths(G2frame):
    28522798    ''' Plotting of instrument broadening terms as function of 2-theta
    28532799    Seen when "Instrument Parameters" chosen from powder pattern data tree
     
    28572803#    gamFW = lambda s,g: np.exp(np.log(s**5+2.69269*s**4*g+2.42843*s**3*g**2+4.47163*s**2*g**3+0.07842*s*g**4+g**5)/5.)
    28582804#    gamFW2 = lambda s,g: math.sqrt(s**2+(0.4654996*g)**2)+.5345004*g  #Ubaldo Bafile - private communication
    2859     if TreeItemText:
    2860         G2frame.PatternId = G2gd.GetPatternTreeItemId(G2frame,G2frame.root,TreeItemText)
    2861     else:
    2862         TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId)
    28632805    PatternId = G2frame.PatternId
    28642806    limitID = G2gd.GetPatternTreeItemId(G2frame,PatternId, 'Limits')
     
    28852827        print "done"
    28862828        return
     2829    xylim = []
    28872830    try:
    28882831        plotNum = G2frame.G2plotNB.plotList.index('Peak Widths')
    28892832        Page = G2frame.G2plotNB.nb.GetPage(plotNum)
     2833        Plot = Page.figure.gca()
     2834        if not G2frame.G2plotNB.allowZoomReset: # save previous limits
     2835            xylim = Plot.get_xlim(),Plot.get_ylim()
    28902836        Page.figure.clf()
    28912837        Plot = Page.figure.gca()
     
    28952841        plotNum = G2frame.G2plotNB.plotList.index('Peak Widths')
    28962842        Page = G2frame.G2plotNB.nb.GetPage(plotNum)
    2897     G2frame.G2plotNB.registerReplot('Peak Widths',
    2898         ReplotRoutine=PlotPeakWidths,
    2899         ReplotKwArgs={'G2frame':G2frame, 'TreeItemText':TreeItemText})
     2843
     2844    TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId)
    29002845    G2frame.G2plotNB.status.SetStatusText('histogram: '+TreeItemText,1)
    2901     G2frame.G2plotNB.clearReplotFlag('Peak Widths')   
    29022846    Page.Choice = None
    29032847    G2frame.G2plotNB.RaisePageNoRefresh(Page)
     
    30192963        Plot.plot(Q,G,'+',color='m',label='Lorentzian peak')
    30202964        Plot.legend(loc='best')
     2965    if xylim and not G2frame.G2plotNB.allowZoomReset:
     2966        # this restores previous plot limits (but I'm not sure why there are two .push_current calls)
     2967        Page.toolbar.push_current()
     2968        Plot.set_xlim(xylim[0])
     2969        Plot.set_ylim(xylim[1])
     2970        Page.toolbar.push_current()
     2971        Page.toolbar.draw()
     2972    else:
    30212973        Page.canvas.draw()
    30222974   
Note: See TracChangeset for help on using the changeset viewer.