Ignore:
Timestamp:
Jan 27, 2021 3:27:25 PM (12 months ago)
Author:
toby
Message:

new table for text w/sorting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrlGUI.py

    r4783 r4785  
    5757:class:`OrderBox`                  Creates a wx.Panel with scrollbars where items can be
    5858                                   ordered into columns.
     59:class:`SortableLstCtrl`           Creates a wx.Panel for a table of data that 
     60                                   can be sorted by clicking on a column label.
    5961:class:`ScrolledMultiEditor`       wx.Dialog for editing many dict- or list-contained items.
    6062                                   with validation. Results are placed in dict or list.
     
    27842786            hlp = HelpButton(self, self.hlp, wrap=450)
    27852787            btnsizer.Add((-1,-1),1, wx.EXPAND, 1)
    2786             btnsizer.Add(hlp,0,wx.ALIGN_RIGHT|wx.ALL)
     2788            #btnsizer.Add(hlp,0,wx.ALIGN_RIGHT|wx.ALL)
     2789            btnsizer.Add(hlp,0)
    27872790            mainSizer.Add(btnsizer,0,wx.EXPAND)
    27882791        if self.lbl:
    2789             mainSizer.Add(wx.StaticText(self,wx.ID_ANY,self.lbl),0,WACV,0)
     2792            mainSizer.Add(wx.StaticText(self,wx.ID_ANY,self.lbl))
    27902793            mainSizer.Add((-1,15))
    27912794        promptSizer = wx.FlexGridSizer(0,2,5,5)
     
    27932796        self.Indx = {}
    27942797        for prompt,value in zip(self.prompts,self.values):
    2795             promptSizer.Add(wx.StaticText(self,-1,prompt),0,WACV,0)
     2798            promptSizer.Add(wx.StaticText(self,-1,prompt))
    27962799            valItem = wx.TextCtrl(self,-1,value=value,style=wx.TE_PROCESS_ENTER,size=(self.size,-1))
    27972800            self.Indx[valItem.GetId()] = prompt
     
    28102813            btn = wx.Button(self, wx.ID_ANY,'+',style=wx.BU_EXACTFIT)
    28112814            btn.Bind(wx.EVT_BUTTON,self.onExpand)
    2812             btnsizer.Add(btn,0,wx.ALIGN_RIGHT)
     2815            btnsizer.Add(btn)
    28132816        mainSizer.Add(btnsizer,0,wx.EXPAND)
    28142817        self.SetSizer(mainSizer)
     
    59925995        'Get the version number in the dialog'
    59935996        return self.spin.GetValue()
     5997
     5998################################################################################
     5999################################################################################
     6000import wx.lib.mixins.listctrl  as  listmix
     6001class SortableLstCtrl(wx.Panel):
     6002    '''Creates a read-only table with sortable columns. Sorting is done by
     6003    clicking on a column label. A triangle facing up or down is added to
     6004    indicate the column is sorted.
     6005
     6006    To use, the header is labeled using
     6007    :meth:`PopulateHeader`, then :meth:`PopulateLine` is called for every
     6008    row in table and finally :meth:`SetColWidth` is called to set the column
     6009    widths.
     6010
     6011    :param wx.Frame parent: parent object for control
     6012    '''
     6013    def __init__(self, parent):
     6014        wx.Panel.__init__(self, parent, wx.ID_ANY)#, style=wx.WANTS_CHARS)
     6015        sizer = wx.BoxSizer(wx.VERTICAL)
     6016        self.list = G2LstCtrl(self, wx.ID_ANY, style=wx.LC_REPORT| wx.BORDER_SUNKEN)
     6017        sizer.Add(self.list, 1, wx.EXPAND)
     6018        self.SetSizer(sizer)
     6019        self.SetAutoLayout(True)
     6020        #self.SortListItems(0, True)
     6021
     6022    def PopulateHeader(self, header, justify):
     6023        '''Defines the column labels
     6024
     6025        :param list header: a list of strings with header labels
     6026        :param list justify: a list of int values where 0 causes left justification,
     6027          1 causes right justification, and -1 causes centering
     6028        '''
     6029        info = wx.ListItem()
     6030        info.Mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
     6031        info.Image = -1
     6032        for i,(h,j) in enumerate(zip(header, justify)):
     6033            info.Text = h
     6034            if j > 0:
     6035                info.Align =  wx.LIST_FORMAT_RIGHT
     6036            elif j < 0:
     6037                info.Align =  wx.LIST_FORMAT_CENTER
     6038            else:
     6039                info.Align = 0
     6040            self.list.InsertColumn(i, info)
     6041        listmix.ColumnSorterMixin.__init__(self.list, len(header))
     6042        self.list.itemDataMap = {}
     6043
     6044    def PopulateLine(self, key, data):
     6045        '''Enters each row into the table
     6046
     6047        :param int key: a unique int value for each line, probably should
     6048          be sequential
     6049        :param list data: a list of strings for each column in that row
     6050        '''
     6051        index = self.list.InsertItem(self.list.GetItemCount(), data[0])
     6052        for i,d in enumerate(data[1:]):
     6053            self.list.SetItem(index, i+1, d)
     6054        self.list.SetItemData(index, key)
     6055        self.list.itemDataMap[key] = data
     6056
     6057    def SetColWidth(self,col,width=None,auto=True,minwidth=0,maxwidth=None):
     6058        '''Sets the column width.
     6059
     6060        :param int width: the column width in pixels
     6061        :param bool auto: if True (default) and width is None (default) the
     6062          width is set by the maximum width entry in the column
     6063        :param int minwidth: used when auto is True, sets a minimum
     6064          column width
     6065        :param int maxwidth: used when auto is True, sets a maximum
     6066          column width. Do not use with minwidth
     6067        '''
     6068        if width:
     6069            self.list.SetColumnWidth(col, width)
     6070        elif auto:
     6071            self.list.SetColumnWidth(col, wx.LIST_AUTOSIZE)
     6072            if minwidth and self.list.GetColumnWidth(col) < minwidth:
     6073                self.list.SetColumnWidth(col, minwidth)
     6074            elif maxwidth and self.list.GetColumnWidth(col) > maxwidth:
     6075                self.list.SetColumnWidth(col, maxwidth)
     6076        else:
     6077            print('Error in SetColWidth: use either auto or width')
     6078           
     6079class G2LstCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
     6080    '''Creates a custom ListCtrl with support for images in column labels
     6081    '''
     6082    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
     6083                 size=wx.DefaultSize, style=0):
     6084        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
     6085        listmix.ListCtrlAutoWidthMixin.__init__(self)
     6086        from wx.lib.embeddedimage import PyEmbeddedImage
     6087        # from demo/images.py
     6088        SmallUpArrow = PyEmbeddedImage(
     6089            b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAADxJ"
     6090            b"REFUOI1jZGRiZqAEMFGke2gY8P/f3/9kGwDTjM8QnAaga8JlCG3CAJdt2MQxDCAUaOjyjKMp"
     6091            b"cRAYAABS2CPsss3BWQAAAABJRU5ErkJggg==")
     6092        SmallDnArrow = PyEmbeddedImage(
     6093            b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAEhJ"
     6094            b"REFUOI1jZGRiZqAEMFGke9QABgYGBgYWdIH///7+J6SJkYmZEacLkCUJacZqAD5DsInTLhDR"
     6095            b"bcPlKrwugGnCFy6Mo3mBAQChDgRlP4RC7wAAAABJRU5ErkJggg==")
     6096        self.il = wx.ImageList(16, 16)
     6097        self.UpArrow = self.il.Add(SmallUpArrow.GetBitmap())
     6098        self.DownArrow = self.il.Add(SmallDnArrow.GetBitmap())
     6099        self.parent=parent
     6100        self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
     6101       
     6102    def GetListCtrl(self): # needed for sorting
     6103        return self
     6104    def GetSortImages(self):
     6105        #return (self.parent.DownArrow, self.parent.UpArrow)
     6106        return (self.DownArrow, self.UpArrow)
    59946107
    59956108################################################################################
Note: See TracChangeset for help on using the changeset viewer.