Package diffpy :: Package pdfgui :: Package gui :: Package wxExtensions :: Module listctrls
[hide private]
[frames] | no frames]

Source Code for Module diffpy.pdfgui.gui.wxExtensions.listctrls

  1  ############################################################################## 
  2  # 
  3  # wxExtensions      by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2006 trustees of the Michigan State University. 
  6  #                   All rights reserved. 
  7  # 
  8  # File coded by:    Chris Farrow 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 12  # 
 13  ############################################################################## 
 14   
 15  """This module contains custom wxListCtrl subclasses. 
 16  AutoWidthListCtrl  - A wxListCtrl object that automatically adjusts the width of 
 17  its columns.   
 18  ColumnSortListCtrl - An AutoWidthListCtrl that sorts its entries when the column 
 19  header is clicked. 
 20  KeyEventsListCtrl  - A ColumnSortListCtrl that selects and item as you type its 
 21  name. 
 22  """ 
 23   
 24  __id__ = "$Id: listctrls.py 2980 2009-04-02 00:14:33Z juhas $" 
 25   
 26  import wx 
 27  import wx.lib.mixins.listctrl as listmix 
 28   
29 -class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
30 """wxListCtrl subclass that automatically adjusts its column width."""
31 - def __init__(self, parent, ID, pos=wx.DefaultPosition, 32 size=wx.DefaultSize, style=0, *args, **kwargs):
33 wx.ListCtrl.__init__(self, parent, ID, pos, size, style, *args, **kwargs) 34 listmix.ListCtrlAutoWidthMixin.__init__(self)
35
36 - def clearSelections(self):
37 """Clear all selections in the list.""" 38 for item in range( self.GetItemCount() ): 39 self.Select(item, on=0) 40 return
41
42 - def setSelection(self, itemtext = None):
43 """Convenience function for simple selection of a list item by label. 44 45 itemtext -- The label of the item to select. If itemtext is None 46 (default) then all items will be deselected. 47 """ 48 # Clear all selections 49 self.clearSelections() 50 51 # Set the selected item 52 item = 0 53 if itemtext: 54 item = self.FindItem(-1, itemtext) 55 self.Select(item) 56 self.Focus(item) 57 return item
58 # end AutoWidthListCtrl 59
60 -class ColumnSortListCtrl(AutoWidthListCtrl, listmix.ColumnSorterMixin):
61 """AutoWidthListCtrl subclass that sorts its columns when the column header 62 is pressed. 63 64 This ListCtrl requires an itemDataMap member dictionary to be initialized 65 before the sorting capabilites can be realized. This dictionary simply references 66 the ListCtrl's entries by a unique number. This number must be stored as the 67 ItemData (with SetItemData) of the entry. The member data must be in the 68 form of a tuple, where the tuple has a number of entries as the ListCtrl has 69 columns. The sorting routine sorts the items in the ListCtrl by the entries 70 in this tuple. 71 """
72 - def __init__(self, parent, ID, pos=wx.DefaultPosition, 73 size=wx.DefaultSize, style=0, *args, **kwargs):
74 AutoWidthListCtrl.__init__(self, parent, ID, pos, size, style, 75 *args, **kwargs) 76 listmix.ListCtrlAutoWidthMixin.__init__(self)
77
78 - def GetListCtrl(self):
79 """This method is required by the sorter mixin.""" 80 return self
81
82 - def initializeSorter(self):
83 """Initialize the column sorter mixin after the ListCtrl is filled. 84 85 This method must be called whenever the itemDataMap is altered. 86 """ 87 numcol = self.GetColumnCount() 88 listmix.ColumnSorterMixin.__init__(self, numcol) 89 return
90
91 - def makeIDM(self):
92 """This method automatically sets up the itemDataMap. 93 94 The itemDataMap gets filled with the current ListCtrl entries. The 95 itemDataMap does not update automatically when the list is changed. To 96 update the itemDataMap this method must be called again. 97 initializeSorter should be called after a call to this method. 98 """ 99 numcol = self.GetColumnCount() 100 numrow = self.GetItemCount() 101 self.itemDataMap = {} 102 for i in range(numrow): 103 infolist = [] 104 for j in range(numcol): 105 infolist.append( self.GetItem(i,j).GetText() ) 106 self.itemDataMap[i+1] = tuple(infolist) 107 self.SetItemData(i, i+1) 108 return
109 110 # end ColumnSortListCtrl 111
112 -class KeyEventsListCtrl(ColumnSortListCtrl):
113 """ColumnSortListCtrl that catches key events and selects the item that 114 matches. 115 116 It only searches for items in the first column. 117 """
118 - def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize, 119 style=0, *args, **kwargs):
120 ColumnSortListCtrl.__init__(self, parent, id, pos, size, style, *args, 121 **kwargs) 122 self.typedText = '' 123 self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
124
125 - def findPrefix(self, prefix):
126 if prefix: 127 prefix = prefix.lower() 128 length = len(prefix) 129 130 for x in range(self.GetItemCount()): 131 text = self.GetItemText(x) 132 text = text.lower() 133 134 if text[:length] == prefix: 135 return x 136 137 return -1
138 139
140 - def OnKey(self, evt):
141 key = evt.GetKeyCode() 142 143 # Select All - Ctrl+A 144 if evt.ControlDown() and key == 65: 145 for item in range(self.GetItemCount()): 146 self.Select(item) 147 return 148 149 # Search for name 150 if key >= 32 and key <= 127: 151 self.typedText = self.typedText + chr(key) 152 item = self.findPrefix(self.typedText) 153 154 if item != -1: 155 itemtext = self.GetItemText(item) 156 self.setSelection(itemtext) 157 158 elif key == wx.WXK_BACK: 159 self.typedText = self.typedText[:-1] 160 161 if not self.typedText: 162 itemtext = self.GetItemText(0) 163 self.setSelection(itemtext) 164 else: 165 item = self.findPrefix(self.typedText) 166 167 if item != -1: 168 itemtext = self.GetItemText(item) 169 self.setSelection(itemtext) 170 171 else: 172 self.typedText = '' 173 evt.Skip()
174
175 - def OnKeyDown(self, evt):
176 pass
177 178 # End of class KeyEventsListCtrl 179