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

# Source Code for Module Gnumed.wxpython.gmMultiColumnList

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