Gnumed.wxpython.gmMultiColumnList

Package Gnumed :: Package wxpython :: Module gmMultiColumnList |
[frames] | no frames]

# Source Code for Module Gnumed.wxpython.gmMultiColumnList

      1  import wx
      2
      3  EXTRA_ROW_SPACE = 40
      4
      5 -class MultiColumnList( wx.Grid):
      6 -        def __init__(self, parent, id = -1):
      7                  wx.Grid.__init__(self, parent, id)
      8                  self.CreateGrid( 10, 1)
      9                  self.SetData( { 1: "2000 AMI",  2 : "2000 Stroke", 3 : "1987 Asthma", 4 : "2002 Thin bones", 5 : "2002 cough" } , 4)
     10
     11                  wx.EVT_SIZE( self, self._resizeData)
     12
     13                  self.EnableEditing(0)
     14                  wx.EVT_GRID_CELL_LEFT_DCLICK( self, self._cellDoubleClicked)
     15                  self.listeners = []
     16
     17
     18 -        def _cellDoubleClicked( self, event):
     19                  event.Skip()
     20                  x, y =  self.GetGridCursorRow(), self.GetGridCursorCol()
     21                  items = self.data.items()
     22                  ix = y * self.col_rows[1] + x
     23                  if ix < len(items):
     24                          self.selData = items[ix]
     25                          self._notifyObservers()
     26
     27 -        def addItemListener(self, listener):
     28                  if listener not in self.listeners:
     29                          self.listeners.append(listener)
     30
     31 -        def removeItemListener(self, listener):
     32                  if listener in self.listeners:
     33                          self.listeners.remove(listener)
     34
     35 -        def _notifyObservers(self):
     36                  for l in self.listeners:
     37                          l( { 'source':self, 'item': self.selData } )
     38
     39
     40
     41
     42 -        def _resizeData(self, event):
     43                  event.Skip()
     44                  self.SetData( self.GetData() , fitClientSize = 1)
     45
     46 -        def _getLongestItem(self, list):
     47                  l = 0
     48                  item = 'AAAAAAAAAAAA    '
     49                  for x in list:
     50                          if len(x) > l:
     51                                  l = len(x)
     52                                  item = x
     53                  return  item
     54
     55 -        def _ensureStringList(self, list):
     56                  alist = []
     57                  for x in list:
     58                          if type(x) == type(''):
     59                                  alist.append(x)
     60                          if type(x) in (type( () ), type( [] ) ):
     61                                  l = []
     62                                  for f in x:
     63                                          l.append(str(f))
     64
     65                                  alist.append("  ".join(l) )
     66                  return alist
     67
     68 -        def _getPredictedRows(self, items):
     69                  (w,h) = self.GetClientSize()
     70                  predictedRows = h/ (self.GetRowSize(0) + 1)
     71                  if predictedRows == 0:
     72                          predictedRows = 4
     73                  predictedCols = len(items) / predictedRows
     74                  list = []
     75                  for id , v in items:
     76                          list.append(v)
     77                  (x,y ) = self.GetTextExtent(self._getLongestItem(self._ensureStringList(list) ))
     78                  maxCols = w / (x + EXTRA_ROW_SPACE)
     79                  if maxCols == 0:
     80                          maxCols = 1
     81                  if predictedCols >= maxCols:
     82                          predictedCols = maxCols
     83                          predictedRows = len(items) / predictedCols + 1
     84
     85                  return predictedRows
     86
     87 -        def SetData(self, map, maxRows = 8, fitClientSize = 0):
     88                  self.SetDataItems( map.items(), maxRows, fitClientSize)
     89
     90
     91 -        def SetDataItems( self, items,  maxRows = 8 , fitClientSize = 0):
     92
     93                  self.GetTable().SetValue(0,0, 'AAAAAAAAAAAAAAAA')
     94                  if len(items) == 0:
     95                          items = [ [ 0, [""] ] ]
     96                  if fitClientSize :
     97                          predictedRows = self._getPredictedRows(items)
     98                  else:
     99                          predictedRows = maxRows
    100
    101                  self.ClearGrid()
    102                  rows = []
    103                  cols = []
    104
    105
    106                  seqTypes = ( type([]), type( () ) )
    107
    108
    109                  for k,v in items:
    110                          if type(v) in seqTypes:
    111                                  v = "   ".join(v)
    112
    113                          rows.append( (k,v) )
    114                          if len(rows) >= maxRows or ( fitClientSize and   (len(rows) ) >= predictedRows ):
    115                                  cols.append(rows)
    116                                  rows = []
    117
    118                  if rows != []:
    119                          cols.append(rows)
    120
    121                  table = self.GetTable()
    122
    123                  self.BeginBatch()
    124                  table.DeleteCols( 0, table.GetNumberCols())
    125                  #table.DeleteRows( 1, table.GetNumberRows() )
    126                  table.AppendCols( len(cols))
    127                  r = len(cols[0]) - table.GetNumberRows()
    128                  if r < 0:
    129                          table.DeleteRows(0, -r)
    130                  else:
    131                          table.AppendRows(r)
    132                  y , x = 0, 0
    133                  for c in cols:
    134                          y = 0
    135                          for r in c:
    136                                  table.SetValue(y, x,  str(r[1]) )
    137                                  y += 1
    138                          x += 1
    139                  self.AutoSizeColumns()
    140                  self.AutoSizeRows()
    141                  self.SetRowLabelSize( 1)
    142                  self.SetColLabelSize(1)
    143                  self.EndBatch()
    144                  self.col_rows = ( len(cols), predictedRows)
    145
    146                  map = {}
    147                  for id, m in items:
    148                          map[id] = m
    149                  self.data = map
    150
    151 -        def GetData(self):
    152                  return self.data
    153
    154
    155
    156
    157  if __name__ == "__main__":
    158           app = wxPyWidgetTester(size = (400, 100))
    159           app.SetWidget(MultiColumnList, -1)
    160           app.MainLoop()
    161