controls/CustomTable.py
changeset 1735 c02818d7e29f
parent 1571 486f94a8032c
child 1736 7e61baa047f0
equal deleted inserted replaced
1734:750eeb7230a1 1735:c02818d7e29f
    29     ROW_HEIGHT = 20
    29     ROW_HEIGHT = 20
    30 else:
    30 else:
    31     ROW_HEIGHT = 28
    31     ROW_HEIGHT = 28
    32 
    32 
    33 class CustomTable(wx.grid.PyGridTableBase):
    33 class CustomTable(wx.grid.PyGridTableBase):
    34     
    34 
    35     """
    35     """
    36     A custom wx.grid.Grid Table using user supplied data
    36     A custom wx.grid.Grid Table using user supplied data
    37     """
    37     """
    38     def __init__(self, parent, data, colnames):
    38     def __init__(self, parent, data, colnames):
    39         # The base class must be initialized *first*
    39         # The base class must be initialized *first*
    45         # XXX
    45         # XXX
    46         # we need to store the row length and collength to
    46         # we need to store the row length and collength to
    47         # see if the table has changed size
    47         # see if the table has changed size
    48         self._rows = self.GetNumberRows()
    48         self._rows = self.GetNumberRows()
    49         self._cols = self.GetNumberCols()
    49         self._cols = self.GetNumberCols()
    50     
    50 
    51     def GetNumberCols(self):
    51     def GetNumberCols(self):
    52         return len(self.colnames)
    52         return len(self.colnames)
    53         
    53 
    54     def GetNumberRows(self):
    54     def GetNumberRows(self):
    55         return len(self.data)
    55         return len(self.data)
    56 
    56 
    57     def GetColLabelValue(self, col, translate=True):
    57     def GetColLabelValue(self, col, translate=True):
    58         if col < len(self.colnames):
    58         if col < len(self.colnames):
    64         return row
    64         return row
    65 
    65 
    66     def GetValue(self, row, col):
    66     def GetValue(self, row, col):
    67         if row < self.GetNumberRows():
    67         if row < self.GetNumberRows():
    68             return self.data[row].get(self.GetColLabelValue(col, False), "")
    68             return self.data[row].get(self.GetColLabelValue(col, False), "")
    69     
    69 
    70     def SetValue(self, row, col, value):
    70     def SetValue(self, row, col, value):
    71         if col < len(self.colnames):
    71         if col < len(self.colnames):
    72             self.data[row][self.GetColLabelValue(col, False)] = value
    72             self.data[row][self.GetColLabelValue(col, False)] = value
    73     
    73 
    74     def GetValueByName(self, row, colname):
    74     def GetValueByName(self, row, colname):
    75         if row < self.GetNumberRows():
    75         if row < self.GetNumberRows():
    76             return self.data[row].get(colname)
    76             return self.data[row].get(colname)
    77 
    77 
    78     def SetValueByName(self, row, colname, value):
    78     def SetValueByName(self, row, colname, value):
   123         """
   123         """
   124         for row in range(self.GetNumberRows()):
   124         for row in range(self.GetNumberRows()):
   125             row_highlights = self.Highlights.get(row, {})
   125             row_highlights = self.Highlights.get(row, {})
   126             for col in range(self.GetNumberCols()):
   126             for col in range(self.GetNumberCols()):
   127                 colname = self.GetColLabelValue(col, False)
   127                 colname = self.GetColLabelValue(col, False)
   128                 
   128 
   129                 grid.SetReadOnly(row, col, True)
   129                 grid.SetReadOnly(row, col, True)
   130                 grid.SetCellEditor(row, col, None)
   130                 grid.SetCellEditor(row, col, None)
   131                 grid.SetCellRenderer(row, col, None)
   131                 grid.SetCellRenderer(row, col, None)
   132                 
   132 
   133                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   133                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   134                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   134                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   135                 grid.SetCellTextColour(row, col, highlight_colours[1])
   135                 grid.SetCellTextColour(row, col, highlight_colours[1])
   136             self.ResizeRow(grid, row)
   136             self.ResizeRow(grid, row)
   137     
   137 
   138     def ResizeRow(self, grid, row):
   138     def ResizeRow(self, grid, row):
   139         if grid.GetRowSize(row) < ROW_HEIGHT:
   139         if grid.GetRowSize(row) < ROW_HEIGHT:
   140             grid.SetRowMinimalHeight(row, ROW_HEIGHT)
   140             grid.SetRowMinimalHeight(row, ROW_HEIGHT)
   141             grid.AutoSizeRow(row, False)
   141             grid.AutoSizeRow(row, False)
   142     
   142 
   143     def SetData(self, data):
   143     def SetData(self, data):
   144         self.data = data
   144         self.data = data
   145     
   145 
   146     def GetData(self):
   146     def GetData(self):
   147         return self.data
   147         return self.data
   148     
   148 
   149     def GetCurrentIndex(self):
   149     def GetCurrentIndex(self):
   150         return self.CurrentIndex
   150         return self.CurrentIndex
   151     
   151 
   152     def SetCurrentIndex(self, index):
   152     def SetCurrentIndex(self, index):
   153         self.CurrentIndex = index
   153         self.CurrentIndex = index
   154     
   154 
   155     def AppendRow(self, row_content):
   155     def AppendRow(self, row_content):
   156         self.data.append(row_content)
   156         self.data.append(row_content)
   157 
   157 
   158     def InsertRow(self, index, row_content):
   158     def InsertRow(self, index, row_content):
   159         self.data.insert(index, row_content)
   159         self.data.insert(index, row_content)